From 2c2fa1fbc9a6ed3152481809c1f66651a2e2bab9 Mon Sep 17 00:00:00 2001 From: Bryan Gardiner Date: Mon, 3 May 2021 18:33:54 -0700 Subject: [PATCH] Use nix-store to query direct deps of command line arguments (closes #4). --- CHANGELOG.md | 13 +++++++++++++ default.nix | 2 +- src/nvd | 23 ++++++++++------------- 3 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..843c398 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +# nvd changelog + +## 0.1.0 + +- Optimized first level dependency calculation to read depenencies from + `nix-store` rather than walking a directory tree manually (issue #4). This + also fixes nvd's support for things other than simple `buildEnv`s, e.g. file + entries at the top level of the store, derivations, and references in files + other than symlinks. + +## 0.0.1 + +Initial release. diff --git a/default.nix b/default.nix index 857085c..ff00224 100644 --- a/default.nix +++ b/default.nix @@ -4,7 +4,7 @@ let in stdenv.mkDerivation { pname = "nvd"; - version = "0.0.1"; + version = "0.1.0"; src = nix-gitignore.gitignoreSourcePure [ ./.gitignore ] ./src; diff --git a/src/nvd b/src/nvd index 5801bcd..32a2726 100755 --- a/src/nvd +++ b/src/nvd @@ -237,22 +237,19 @@ class PackageManifest: @staticmethod def parse_tree(root: Path) -> "PackageManifest": - assert isinstance(root, Path), f"Not a Path: {root!r}" - - store_paths = set() - for curdir, dirs, files in os.walk(root, onerror=raise_arg): - for filename in files: - path_str = os.path.join(curdir, filename) - if os.path.islink(path_str): - store_paths.add(StorePath(os.readlink(path_str)).to_base_path()) + direct_deps: List[str] = subprocess.run( + ["nix-store", "--query", "--references", str(root)], + stdout=PIPE, + text=True + ).stdout.rstrip("\n").split("\n") packages = [] - for store_path in store_paths: - pname, version = parse_pname_version(str(store_path.path())) + for dep_path in direct_deps: + pname, version = parse_pname_version(dep_path) packages.append(Package( pname=pname, version=Version(version), - store_path=store_path, + store_path=StorePath(dep_path), )) return PackageManifest(packages) @@ -455,8 +452,8 @@ def main(): right_manifest: Optional[PackageManifest] = None if (left_resolved / "sw").is_dir() and (right_resolved / "sw").is_dir(): - left_manifest = PackageManifest.parse_tree(left_resolved / "sw") - right_manifest = PackageManifest.parse_tree(right_resolved / "sw") + left_manifest = PackageManifest.parse_tree((left_resolved / "sw").resolve()) + right_manifest = PackageManifest.parse_tree((right_resolved / "sw").resolve()) else: left_manifest = PackageManifest.parse_tree(left_resolved) right_manifest = PackageManifest.parse_tree(right_resolved)