From a3c705cfae3f04ee17aa1d07977f12a72b60c61c Mon Sep 17 00:00:00 2001 From: Tom de Geus Date: Fri, 14 Jul 2023 16:57:01 +0200 Subject: [PATCH] Adding `location` to `shelephant info` (#179) --- .pre-commit-config.yaml | 10 +++---- shelephant/dataset.py | 62 ++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dfd8dad..3ff6da9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: check-toml - id: debug-statements - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks - rev: v2.9.0 + rev: v2.10.0 hooks: - id: pretty-format-yaml args: [--preserve-quotes, --autofix, --indent, '2'] @@ -25,7 +25,7 @@ repos: - id: rst-directive-colons - id: rst-inline-touching-normal - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 23.7.0 hooks: - id: black args: [--safe, --quiet, --line-length=100] @@ -35,11 +35,11 @@ repos: - id: autoflake args: [--in-place, --remove-unused-variable] - repo: https://github.com/asottile/reorder_python_imports - rev: v3.9.0 + rev: v3.10.0 hooks: - id: reorder-python-imports - repo: https://github.com/asottile/pyupgrade - rev: v3.4.0 + rev: v3.9.0 hooks: - id: pyupgrade args: [--py36-plus] @@ -49,7 +49,7 @@ repos: - id: flake8 args: ["--max-line-length=100", "--per-file-ignores=shelephant/__init__.py:F401", "--ignore=E203,W503"] - repo: https://github.com/asottile/setup-cfg-fmt - rev: v2.3.0 + rev: v2.4.0 hooks: - id: setup-cfg-fmt - repo: https://github.com/tdegeus/conda_envfile diff --git a/shelephant/dataset.py b/shelephant/dataset.py index eddfeb3..34f6af3 100644 --- a/shelephant/dataset.py +++ b/shelephant/dataset.py @@ -33,8 +33,8 @@ def _force_absolute_path(root: pathlib.Path, path: pathlib.Path) -> pathlib.Path """ Force a path to be absolute. - :param root: The root directory. - :param path: The path that may be absolute or relative. + :param root: A base directory. + :param path: The path that may be absolute or relative to ``root``. :return: The absolute ``path``. """ if path.is_absolute(): @@ -48,7 +48,7 @@ class Location: Attributes: - * :py:attr:`Location.root`: The root directory. + * :py:attr:`Location.root`: The base directory. * :py:attr:`Location.ssh` (optional): ``[user@]host`` * :py:attr:`Location.prefix` (optional): Prefix to add to all paths. * :py:attr:`Location.python` (optional): The python executable on the ``ssh`` host. @@ -882,7 +882,7 @@ def lock(args: list[str]): parser = _lock_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert args.name.lower() != "here", "cannot lock 'here'" assert (sdir / "storage" / f"{args.name}.yaml").is_file(), "storage location not found" (sdir / "lock.txt").write_text(args.name) @@ -954,7 +954,7 @@ def add(args: list[str]): parser = _add_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "cannot remove location from storage location" assert args.name != "all", "all is a reserved name" assert args.name != "any", "any is a reserved name" @@ -1049,7 +1049,7 @@ def remove(args: list[str]): parser = _remove_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "cannot remove location from storage location" storage = yaml.read(sdir / "storage.yaml") @@ -1112,7 +1112,7 @@ def update(args: list[str]): parser = _update_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" base = sdir.parent paths = [os.path.relpath(path, base) for path in args.path] paths = np.unique(paths) if len(paths) > 0 else None @@ -1359,7 +1359,7 @@ def cp(args: list[str]): parser = _cp_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "cannot remove location from storage location" storage = yaml.read(sdir / "storage.yaml") assert args.source in storage, f"Unknown storage location {args.source}" @@ -1432,7 +1432,7 @@ def mv(args: list[str]): parser = _mv_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "cannot remove location from storage location" assert args.destination != "here", "Cannot copy to here." storage = yaml.read(sdir / "storage.yaml") @@ -1501,7 +1501,7 @@ def rm(args: list[str]): parser = _rm_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "cannot remove location from storage location" storage = yaml.read(sdir / "storage.yaml") assert args.source in storage, f"Unknown storage location {args.source}" @@ -1559,7 +1559,7 @@ def pwd(args: list[str]): parser = _pwd_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" assert not (sdir / "lock.txt").exists(), "not available from storage location" storage = yaml.read(sdir / "storage.yaml") assert args.source in storage, f"Unknown storage location {args.source}" @@ -1672,7 +1672,7 @@ def status(args: list[str]): parser = _status_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" base = sdir.parent cwd = os.path.relpath(pathlib.Path.cwd(), base) paths = [os.path.relpath(path, base) for path in args.path] @@ -1825,10 +1825,11 @@ class MyFmt( parser = argparse.ArgumentParser(formatter_class=MyFmt, description=desc) parser.add_argument("--version", action="version", version=version) - parser.add_argument("--cachedir", action="store_true", help="Print cachedir.") + parser.add_argument("--cachedir", action="store_true", help="Print cache-dir and quit.") parser.add_argument( - "--basedir", action="store_true", help="Print basedir (that contain '.shelephant')." + "--basedir", action="store_true", help="Print basedir (containing '.shelephant') and quit." ) + parser.add_argument("location", type=str, nargs="*", help="Name of the storage location(s).") return parser @@ -1843,13 +1844,40 @@ def info(args: list[str]): args = parser.parse_args(args) if args.cachedir: - print(user_cache_dir("shelephant", "tdegeus")) + print(user_cache_dir("shelephant", os.getlogin())) return + sdir = _search_upwards_dir(".shelephant") + assert sdir is not None, "Not in a shelephant dataset" + if args.basedir: - print(_search_upwards_dir(".shelephant").parent) + print(sdir.parent) return + locations = yaml.read(sdir / "storage.yaml") + if len(args.location) == 0: + args.location = locations + else: + assert all([i in locations for i in args.location]), "Unknown storage location(s)" + + ret = "" + for i, location in enumerate(args.location): + loc = Location.from_yaml(sdir / "storage" / f"{location}.yaml") + out = prettytable.PrettyTable() + out.set_style(prettytable.SINGLE_BORDER) + out.field_names = ["name", location] + out.align = "l" + out.add_row(["root", loc.root]) + if loc.prefix is not None: + out.add_row(["prefix", loc.prefix]) + if loc.ssh is not None: + out.add_row(["ssh", loc.ssh]) + ret += out.get_string() + if i < len(args.location) - 1: + ret += "\n" + + output.autoprint(ret) + def _find_matching( text: str, @@ -1957,7 +1985,7 @@ def gitignore(args: list[str]): parser = _gitignore_parser() args = parser.parse_args(args) sdir = _search_upwards_dir(".shelephant") - assert sdir is not None, "Not a shelephant dataset" + assert sdir is not None, "Not in a shelephant dataset" path = sdir / ".." / ".gitignore" if path.exists():