Skip to content

Commit

Permalink
vendor: Update pyproject.nix
Browse files Browse the repository at this point in the history
  • Loading branch information
adisbladis committed Nov 9, 2024
1 parent be34b41 commit 5a0a462
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 559 deletions.
13 changes: 11 additions & 2 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

let
flake = builtins.getFlake "${toString ../.}";
in
Expand All @@ -10,8 +11,16 @@ in
}
}:
let
poetry2nix = import ./.. { inherit pkgs; };
callTest = test: attrs: pkgs.callPackage test ({ inherit poetry2nix; } // attrs);
pkgs' = pkgs // {
inherit poetry2nix;

# At the time of writing 3.12 is causing issues.
python3 = pkgs.python311;
python = pkgs.python311;
};

poetry2nix = import ./.. { pkgs = pkgs'; };
callTest = lib.callPackageWith pkgs';

inherit (pkgs) lib stdenv;

Expand Down
4 changes: 2 additions & 2 deletions tests/textual-textarea/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ poetry2nix, python3, runCommand }:
{ poetry2nix, python311, runCommand }:
let
env = poetry2nix.mkPoetryEnv {
python = python3;
python = python311;
pyproject = ./pyproject.toml;
poetrylock = ./poetry.lock;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/use-url-wheel/default.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ lib, poetry2nix, python3, stdenv }:
{ lib, poetry2nix, python311, stdenv }:
let
args = {
python = python3;
python = python311;
projectDir = ./.;
preferWheels = true;
};
Expand Down
8 changes: 6 additions & 2 deletions vendor/pyproject.nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{ lib }:
{
lib.fix (self: {
lib = import ./lib { inherit lib; };
}
build = import ./build {
pyproject-nix = self;
inherit lib;
};
})
19 changes: 14 additions & 5 deletions vendor/pyproject.nix/lib/pep440.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ let
post = null;
pre = null;
release = [ ];
str = "";
};

# We consider some words to be alternate spellings of other words and
Expand Down Expand Up @@ -164,11 +165,14 @@ fix (self: {
# Split post345 into ["post" "345"]
m = match "-?([^0-9]+)([0-9]+)" mod;
in
assert m != null;
{
type = normalizedReleaseType (elemAt m 0);
value = toIntRelease (elemAt m 1);
}
if m == null then
throw "PEP-440 modifier segment invalid: ${mod}"
# assert m != null;
else
{
type = normalizedReleaseType (elemAt m 0);
value = toIntRelease (elemAt m 1);
}
) (filter (s: isString s && s != "") (split "\\." modifiersSegment));

in
Expand All @@ -190,6 +194,11 @@ fix (self: {
# Local releases needs to be treated specially.
# The value isn't just a straight up number, but an arbitrary string.
local = if local != "" then local else null;

# Return the input verbatim for lexicographical comparisons such as those
# done for platform_release which is parsed as PEP-440 when compliant,
# but are compare lexicographically when the version string is non-compliant.
str = version;
};

/*
Expand Down
115 changes: 104 additions & 11 deletions vendor/pyproject.nix/lib/pep508.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ let
length
isList
any
isAttrs
tryEval
deepSeq
splitVersion
concatStringsSep
tail
;
inherit (lib)
stringToCharacters
sublist
hasInfix
take
toInt
;
inherit (import ./util.nix { inherit lib; }) splitComma stripStr;

Expand All @@ -42,16 +50,35 @@ let
type = "version";
value = pep440.parseVersion value;
};

emptyPlatformRelease = {
type = "platform_release";
value = "";
};

platform_release =
value:
if value == "" then
emptyPlatformRelease
else
let
parsed' = pep440.parseVersion value;
eval' = tryEval (deepSeq parsed' parsed');
in
{
type = "platform_release";
value = if eval'.success then eval'.value else value;
};
in
{
"implementation_name" = default;
"implementation_version" = version;
"os_name" = default;
"platform_machine" = default;
"platform_python_implementation" = default;
"platform_release" = default;
"platform_release" = platform_release;
"platform_system" = default;
"platform_version" = version;
"platform_version" = default;
"python_full_version" = version;
"python_version" = version;
"sys_platform" = default;
Expand Down Expand Up @@ -83,6 +110,31 @@ let
"!=" = extras: extra: if typeOf extras == "list" then !(elem extra extras) else extras != extra;
};

# Platform_release parsing is more complicated than other fields.
#
# If both lhs and rhs were correctly parsed as PEP-440 versions run regular version comparison,
# otherwise compare lexicographically
#
# See:
# - https://github.com/pypa/packaging/issues/774
# - https://github.com/astral-sh/uv/issues/3917#issuecomment-2141754917
platformReleaseComparators = {
"==" =
a: b: if isAttrs a && isAttrs b then pep440.comparators."==" a b else (a.str or a) == (b.str or b);
"!=" =
a: b: if isAttrs a && isAttrs b then pep440.comparators."!=" a b else (a.str or a) != (b.str or b);
"<=" =
a: b: if isAttrs a && isAttrs b then pep440.comparators."<=" a b else (a.str or a) <= (b.str or b);
">=" =
a: b: if isAttrs a && isAttrs b then pep440.comparators.">=" a b else (a.str or a) >= (b.str or b);
"<" =
a: b: if isAttrs a && isAttrs b then pep440.comparators."<" a b else (a.str or a) < (b.str or b);

">" =
a: b: if isAttrs a && isAttrs b then pep440.comparators.">" a b else (a.str or a) > (b.str or b);
"===" = a: b: a == b;
};

boolOps = {
"and" = x: y: x && y;
"or" = x: y: x || y;
Expand Down Expand Up @@ -614,35 +666,73 @@ in
let
inherit (python) stdenv;
inherit (stdenv) targetPlatform;
inherit (targetPlatform)
isLinux
isDarwin
isFreeBSD
isAarch64
isx86_64
;
impl = python.passthru.implementation;
in
mapAttrs (name: markerFields.${name}) {
os_name = if python.pname == "jython" then "java" else "posix";
sys_platform =
if stdenv.isLinux then
if isLinux then
"linux"
else if stdenv.isDarwin then
else if isDarwin then
"darwin"
else if isFreeBSD then
"freebsd${head (splitVersion stdenv.cc.libc.version)}"
else
throw "Unsupported platform";
platform_machine =
if targetPlatform.isDarwin then
if isDarwin then
targetPlatform.darwinArch
else if isLinux then
pep599.manyLinuxTargetMachines.${targetPlatform.parsed.cpu.name} or targetPlatform.parsed.cpu.name
else if isFreeBSD then
(
if isx86_64 then
"amd64"
else if isAarch64 then
"arm64"
else
throw "Unhandled FreeBSD architecture"
)
else
pep599.manyLinuxTargetMachines.${targetPlatform.parsed.cpu.name} or targetPlatform.parsed.cpu.name;
throw "Unsupported platform";
platform_python_implementation =
if impl == "cpython" then
"CPython"
else if impl == "pypy" then
"PyPy"
else
throw "Unsupported implementation ${impl}";
platform_release = ""; # Field not reproducible
# We have no reliable value to set platform_release to.
# In theory this could be set to linuxHeaders.version on Linux, but that's
# not correct
platform_release =
if isLinux then
stdenv.cc.libc.linuxHeaders.version
else if isDarwin then
(
let
tokens = splitVersion targetPlatform.darwinSdkVersion;
in
concatStringsSep "." ([ (toString ((toInt (head tokens)) + 9)) ] ++ tail tokens)
)
else if isFreeBSD then
"${concatStringsSep "." (take 2 (splitVersion stdenv.cc.libc.version))}-RELEASE"
else
throw "Unsupported platform";
platform_system =
if stdenv.isLinux then
if isLinux then
"Linux"
else if stdenv.isDarwin then
else if isDarwin then
"Darwin"
else if isFreeBSD then
"FreeBSD"
else
throw "Unsupported platform";
platform_version = ""; # Field not reproducible
Expand Down Expand Up @@ -675,8 +765,11 @@ in
if value.type == "compare" then
(
(
# platform_release is being compared as foo
if value.lhs.type == "variable" && value.lhs.value == "platform_release" then
platformReleaseComparators.${value.op}
# Version comparison
if value.lhs.type == "version" || value.rhs.type == "version" then
else if value.lhs.type == "version" || value.rhs.type == "version" then
pep440.comparators.${value.op}
# `Extra` environment marker comparison requires special casing because it's equality checks can
# == can be considered a `"key" in set` comparison when multiple extras are activated for a dependency.
Expand All @@ -694,7 +787,7 @@ in
boolOps.${value.op} (evalMarkers environ value.lhs) (evalMarkers environ value.rhs)
else if value.type == "variable" then
(evalMarkers environ environ.${value.value})
else if value.type == "version" || value.type == "extra" then
else if value.type == "version" || value.type == "extra" || value.type == "platform_release" then
value.value
else if elem value.type primitives then
value.value
Expand Down
35 changes: 33 additions & 2 deletions vendor/pyproject.nix/lib/pep621.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ let
foldl'
split
filter
concatMap
isAttrs
;
inherit (lib)
isString
Expand Down Expand Up @@ -51,21 +53,49 @@ fix (_self: {
pyproject,
extrasAttrPaths ? [ ],
extrasListPaths ? { },
groupsAttrPaths ? [ ],
groupsListPaths ? { },
}:
let
# Fold extras from all considered attributes into one set
extras' =
foldl' (acc: attr: acc // getAttrPath attr { } pyproject) (pyproject.project.optional-dependencies
(foldl' (acc: attr: acc // getAttrPath attr { } pyproject) (pyproject.project.optional-dependencies
or { }
) extrasAttrPaths
) extrasAttrPaths)
// filterAttrs (_: deps: length deps > 0) (
mapAttrs' (path: attr: nameValuePair attr (getAttrPath path [ ] pyproject)) extrasListPaths
);

depGroups' =
(foldl' (acc: attr: acc // getAttrPath attr { } pyproject) (pyproject.dependency-groups or { }
) groupsAttrPaths)
// filterAttrs (_: deps: length deps > 0) (
mapAttrs' (path: attr: nameValuePair attr (getAttrPath path [ ] pyproject)) groupsListPaths
);

in
{
dependencies = map pep508.parseString (pyproject.project.dependencies or [ ]);
extras = mapAttrs (_: map pep508.parseString) extras';
build-systems = pep518.parseBuildSystems pyproject;

# PEP-735 dependency groups
groups =
let
groups' = mapAttrs (
_group:
concatMap (
x:
if isString x then
[ (pep508.parseString x) ]
else if isAttrs x then
groups'.${x.include-group}
else
throw "Unsupported dependency group: ${x}"
)
) depGroups';
in
groups';
};

/*
Expand Down Expand Up @@ -115,6 +145,7 @@ fix (_self: {
dependencies = filterList dependencies.dependencies;
extras = mapAttrs (_: filterList) dependencies.extras;
build-systems = filterList dependencies.build-systems;
groups = mapAttrs (_: filterList) dependencies.groups;
}
);
})
Loading

0 comments on commit 5a0a462

Please sign in to comment.