Skip to content

Commit

Permalink
vendor: Upgrade pyproject.nix
Browse files Browse the repository at this point in the history
This includes a full rewrite of the PEP-508 parser which performs ~2x as fast with ~0.4x the RAM.
  • Loading branch information
adisbladis committed Sep 3, 2024
1 parent 7bd341a commit 184203e
Show file tree
Hide file tree
Showing 17 changed files with 1,982 additions and 1,536 deletions.
3 changes: 3 additions & 0 deletions dev/treefmt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
};

black.excludes = [ "vendor/**.py" ];

deadnix.excludes = [ "vendor/**.nix" ];
statix.excludes = [ "vendor/**.nix" ];
};

programs.deadnix.enable = true;
Expand Down
37 changes: 21 additions & 16 deletions vendor/pyproject.nix/lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ let
inherit (lib) fix;
in

fix (self: mapAttrs (_: path: import path ({ inherit lib; } // self)) {
pip = ./pip.nix;
pypa = ./pypa.nix;
project = ./project.nix;
renderers = ./renderers.nix;
validators = ./validators.nix;
poetry = ./poetry.nix;
eggs = ./eggs.nix;
pep440 = ./pep440.nix;
pep508 = ./pep508.nix;
pep518 = ./pep518.nix;
pep599 = ./pep599.nix;
pep600 = ./pep600.nix;
pep621 = ./pep621.nix;
pep656 = ./pep656.nix;
})
fix (
self:
mapAttrs (_: path: import path ({ inherit lib; } // self)) {
pip = ./pip.nix;
pypa = ./pypa.nix;
project = ./project.nix;
renderers = ./renderers.nix;
validators = ./validators.nix;
scripts = ./scripts.nix;
poetry = ./poetry.nix;
eggs = ./eggs.nix;
pep440 = ./pep440.nix;
pep508 = ./pep508.nix;
pep518 = ./pep518.nix;
pep599 = ./pep599.nix;
pep600 = ./pep600.nix;
pep621 = ./pep621.nix;
pep656 = ./pep656.nix;
pep723 = ./pep723.nix;
}
)
73 changes: 43 additions & 30 deletions vendor/pyproject.nix/lib/eggs.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{ lib, ... }:
let
inherit (builtins) filter match elemAt compareVersions sort;
inherit (builtins)
filter
match
elemAt
compareVersions
sort
;
inherit (lib) isString;

# Tag normalization documented in
Expand All @@ -16,55 +22,61 @@ let

in
lib.fix (self: {
/* Regex match an egg file name, returning a list of match groups. Returns null if no match.
/*
Regex match an egg file name, returning a list of match groups. Returns null if no match.
Type: matchEggFileName :: string -> [ string ]
Type: matchEggFileName :: string -> [ string ]
*/
matchEggFileName = name:
matchEggFileName =
name:
let
m = match "([^-]+)-([^-]+)-(.+)\\.egg" name;
in
if m != null then filter isString m else null;

/* Check whether string is an egg file or not.
/*
Check whether string is an egg file or not.
Type: isEggFileName :: string -> bool
Type: isEggFileName :: string -> bool
Example:
# isEggFileName "cryptography-41.0.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl"
false
Example:
# isEggFileName "cryptography-41.0.1-cp37-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl"
false
*/
isEggFileName =
# The filename string
name: self.matchEggFileName name != null;

/* Parse an egg file name.
/*
Parse an egg file name.
Type: parsehEggFileName :: string -> AttrSet
Type: parsehEggFileName :: string -> AttrSet
Example:
# parseEggFileName
Example:
# parseEggFileName
*/
parseEggFileName = name:
parseEggFileName =
name:
let
m = self.matchEggFileName name;
mAt = elemAt m;
langM = match "([^0-9]*)(.+)" (mAt 2);
langAt = elemAt langM;
langM = match "([^0-9]*)(.+)" (elemAt m 2);
in
assert m != null; {
assert m != null;
assert langM != null;
{
filename = name;
distribution = mAt 0;
version = mAt 1;
distribution = elemAt m 0;
version = elemAt m 1;
languageTag = {
implementation = normalizeImpl (langAt 0);
version = langAt 1;
implementation = normalizeImpl (elemAt langM 0);
version = elemAt langM 1;
};
};

/* Select compatible eggs from a list and return them in priority order.
/*
Select compatible eggs from a list and return them in priority order.
Type: selectEggs :: derivation -> [ AttrSet ] -> [ AttrSet ]
Type: selectEggs :: derivation -> [ AttrSet ] -> [ AttrSet ]
*/
selectEggs =
# Python interpreter derivation
Expand All @@ -74,13 +86,14 @@ lib.fix (self: {
let
inherit (python.passthru) pythonVersion implementation;

langCompatible = filter
(file: file.languageTag.implementation == "python" || file.languageTag.implementation == implementation)
files;
langCompatible = filter (
file:
file.languageTag.implementation == "python" || file.languageTag.implementation == implementation
) files;

versionCompatible = filter
(file: compareVersions pythonVersion file.languageTag.version >= 0)
langCompatible;
versionCompatible = filter (
file: compareVersions pythonVersion file.languageTag.version >= 0
) langCompatible;

in
sort (a: b: compareVersions a.languageTag.version b.languageTag.version > 0) versionCompatible;
Expand Down
Loading

0 comments on commit 184203e

Please sign in to comment.