From 9ea96d0c1ded794f0c6bec5401cc0a33c99438fa Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Mon, 26 Sep 2022 00:16:08 -0600 Subject: [PATCH] Implement single-file packages Fixes #5 --- src/python/cram/__main__.py | 22 ++++++++-------------- src/python/cram/common.py | 3 +-- src/python/cram/v0.py | 6 +++--- src/python/cram/v1.py | 24 +++++++++++++++++++++--- src/python/vfs/impl.py | 1 - test/integration/packages.d/p8.toml | 5 +++++ tools/autoflake/__main__.py | 1 - tools/black/__main__.py | 1 - tools/flake8/__main__.py | 1 - tools/isort/__main__.py | 1 - tools/python/bzl_pytest_shim.py | 1 - tools/python/test_licenses.py | 6 +----- tools/unify/__main__.py | 1 - 13 files changed, 39 insertions(+), 34 deletions(-) create mode 100644 test/integration/packages.d/p8.toml diff --git a/src/python/cram/__main__.py b/src/python/cram/__main__.py index 400e63b..d3ae3fa 100644 --- a/src/python/cram/__main__.py +++ b/src/python/cram/__main__.py @@ -2,28 +2,22 @@ import logging import os -from pathlib import Path import pickle -from typing import List, Optional, Union -import re import platform -from pprint import pformat +import re from collections import defaultdict - -from . import ( - __author__, - __copyright__, - __license__, - __version__, -) -from .v0 import PackageV0, ProfileV0 -from .v1 import PackageV1, ProfileV1 +from pathlib import Path +from pprint import pformat +from typing import List, Optional, Union import click import toml from toposort import toposort_flatten from vfs import Vfs +from . import __author__, __copyright__, __license__, __version__ +from .v0 import PackageV0, ProfileV0 +from .v1 import LightPackageV1, PackageV1, ProfileV1 log = logging.getLogger(__name__) @@ -114,7 +108,7 @@ def load(root: Path, name: str, clss): def load_package(root, name): log.debug(f"Attempting to load package {name} from {root}") - return load(root, name, [PackageV1, PackageV0]) + return load(root, name, [LightPackageV1, PackageV1, PackageV0]) def load_profile(root, name): diff --git a/src/python/cram/common.py b/src/python/cram/common.py index 35948bd..9f40e1e 100644 --- a/src/python/cram/common.py +++ b/src/python/cram/common.py @@ -1,14 +1,13 @@ #!/usr/bin/env python3 import os +import sys from pathlib import Path from shlex import quote as sh_quote -import sys from typing import List, Optional from vfs import Vfs - # FIXME: This should be a config somewhere SHELL = "/bin/sh" diff --git a/src/python/cram/v0.py b/src/python/cram/v0.py index ab8c5db..61eadf7 100644 --- a/src/python/cram/v0.py +++ b/src/python/cram/v0.py @@ -3,13 +3,13 @@ An ill-considered pseudo-format. """ -from pathlib import Path import re - -from .common import Package, sh, stow +from pathlib import Path from vfs import Vfs +from .common import Package, sh, stow + class PackageV0(Package): """The original package format from install.sh.""" diff --git a/src/python/cram/v1.py b/src/python/cram/v1.py index 72462b3..8f654b0 100644 --- a/src/python/cram/v1.py +++ b/src/python/cram/v1.py @@ -9,11 +9,11 @@ from hashlib import sha256 from pathlib import Path from typing import List, Optional, Union -from .common import Package, sh, stow - import toml from vfs import Vfs +from .common import Package, sh, stow + def tempf(name): root = Path("/tmp/stow") @@ -94,11 +94,29 @@ class PackageV1(Package): def post_install(self, fs: Vfs, dest: Path): self.do_sh_or_script(self.config().get("package", {}).get("post_install"), fs, dest) - def json(self): return self.config() +class LightPackageV1(PackageV1): + """A package with no file content, defined in a single file.""" + + SPECIAL_FILES = [] + _config = None + + def config(self): + if not self._config: + with open(self.root, "r") as fp: + self._config = toml.load(fp) + return self._config + + def test(self): + return self.root.exists() \ + and self.root.is_file() \ + and self.root.name.endswith(".toml") \ + and self.config().get("cram", {}).get("version") == 1 + + class ProfileV1(PackageV1): """Unline packages, profiles don't support recursive stow of contents.""" diff --git a/src/python/vfs/impl.py b/src/python/vfs/impl.py index f6943e6..bbe916d 100644 --- a/src/python/vfs/impl.py +++ b/src/python/vfs/impl.py @@ -6,7 +6,6 @@ import logging from shutil import rmtree from subprocess import run - _log = logging.getLogger(__name__) diff --git a/test/integration/packages.d/p8.toml b/test/integration/packages.d/p8.toml new file mode 100644 index 0000000..bacb9cf --- /dev/null +++ b/test/integration/packages.d/p8.toml @@ -0,0 +1,5 @@ +[cram] +version = 1 + +[[package.install]] +run = "echo 'ok'" diff --git a/tools/autoflake/__main__.py b/tools/autoflake/__main__.py index 2108d2e..1295fe5 100644 --- a/tools/autoflake/__main__.py +++ b/tools/autoflake/__main__.py @@ -9,7 +9,6 @@ import sys from autoflake import main - if __name__ == "__main__": sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) sys.exit(main()) diff --git a/tools/black/__main__.py b/tools/black/__main__.py index 238de21..c451b5d 100644 --- a/tools/black/__main__.py +++ b/tools/black/__main__.py @@ -5,7 +5,6 @@ import sys from black import nullcontext, patched_main - parser = argparse.ArgumentParser() parser.add_argument("--output-file", default=None) diff --git a/tools/flake8/__main__.py b/tools/flake8/__main__.py index dfc8ba6..c9c7d7b 100644 --- a/tools/flake8/__main__.py +++ b/tools/flake8/__main__.py @@ -1,5 +1,4 @@ from flake8.main import cli - if __name__ == "__main__": cli.main() diff --git a/tools/isort/__main__.py b/tools/isort/__main__.py index 89ac8b5..dae495d 100644 --- a/tools/isort/__main__.py +++ b/tools/isort/__main__.py @@ -9,7 +9,6 @@ import sys from isort.main import main - if __name__ == "__main__": sys.argv[0] = re.sub(r"(-script\.pyw?|\.exe)?$", "", sys.argv[0]) sys.exit(main()) diff --git a/tools/python/bzl_pytest_shim.py b/tools/python/bzl_pytest_shim.py index 5a65b98..f331e33 100644 --- a/tools/python/bzl_pytest_shim.py +++ b/tools/python/bzl_pytest_shim.py @@ -4,7 +4,6 @@ import sys import pytest - if __name__ == "__main__": cmdline = ["--ignore=external"] + sys.argv[1:] print(cmdline, file=sys.stderr) diff --git a/tools/python/test_licenses.py b/tools/python/test_licenses.py index 00f3c58..dc21743 100644 --- a/tools/python/test_licenses.py +++ b/tools/python/test_licenses.py @@ -4,12 +4,8 @@ Validate 3rdparty library licenses as approved. import re -from pkg_resources import ( - DistInfoDistribution, - working_set, -) import pytest - +from pkg_resources import DistInfoDistribution, working_set # Licenses approved as representing non-copyleft and not precluding commercial usage. # This is all easy, there's a good schema here. diff --git a/tools/unify/__main__.py b/tools/unify/__main__.py index a749480..6b2a084 100644 --- a/tools/unify/__main__.py +++ b/tools/unify/__main__.py @@ -7,6 +7,5 @@ Shim for executing isort. from unify import main - if __name__ == "__main__": exit(main())