Skip to content

Commit

Permalink
vendor: Upgrade pyproject.nix
Browse files Browse the repository at this point in the history
- Upgrade compatibility fixes

- Use fetchPypiLegacy from nixpkgs
  This was moved from pyproject.nix to nixpkgs

- Move fetchFromPypi fetcher to poetry2nix
  Pyproject.nix has stoppied carrying fetchers
  • Loading branch information
adisbladis committed Jul 15, 2024
1 parent 54083f9 commit 7f304a8
Show file tree
Hide file tree
Showing 16 changed files with 453 additions and 239 deletions.
4 changes: 3 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
}:
let

pyproject-nix = import ./vendor/pyproject.nix { inherit pkgs lib; };
pyproject-nix = import ./vendor/pyproject.nix { inherit lib; };

poetryLib = import ./lib.nix { inherit lib pkgs pyproject-nix; inherit (pkgs) stdenv; };
inherit (poetryLib) readTOML;
Expand Down Expand Up @@ -253,6 +253,8 @@ lib.makeScope pkgs.newScope (self: {
inherit pyproject-nix;
};

inherit (final.callPackage ./fetchers { inherit pyproject-nix; }) fetchFromPypi;

__toPluginAble = toPluginAble final;
}
)
Expand Down
58 changes: 9 additions & 49 deletions vendor/pyproject.nix/fetchers/default.nix → fetchers/default.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
{ curl
, jq
{ pkgs
, lib
, python3
, runCommand
, stdenvNoCC
, pyproject-nix
}:
let
inherit (builtins) substring filter head nixPath elemAt;
inherit (builtins) substring elemAt;
inherit (lib) toLower;

pyproject = import ../lib { inherit lib; };
inherit (pyproject-nix.lib.pypa) matchWheelFileName;
inherit (pyproject-nix.lib.eggs) matchEggFileName;

# Predict URL from the PyPI index.
# Args:
Expand All @@ -25,8 +24,8 @@ let
file
}:
let
matchedWheel = pyproject.pypa.matchWheelFileName file;
matchedEgg = pyproject.pypa.matchEggFileName file;
matchedWheel = matchWheelFileName file;
matchedEgg = matchEggFileName file;
kind =
if matchedWheel != null then "wheel"
else if matchedEgg != null then elemAt matchedEgg 2
Expand Down Expand Up @@ -63,8 +62,8 @@ lib.mapAttrs (_: func: lib.makeOverridable func) {
stdenvNoCC.mkDerivation {
name = file;
nativeBuildInputs = [
curl
jq
pkgs.curl
pkgs.jq
];
isWheel = lib.strings.hasSuffix "whl" file;
system = "builtin";
Expand All @@ -88,43 +87,4 @@ lib.mapAttrs (_: func: lib.makeOverridable func) {
urls = [ predictedURL ]; # retain compatibility with nixpkgs' fetchurl
};
};

/*
Fetch from the PyPI legacy API.
Some repositories (such as Devpi) expose the Pypi legacy API (https://warehouse.pypa.io/api-reference/legacy.html).
Type: fetchFromLegacy :: AttrSet -> derivation
*/
fetchFromLegacy =
{
# package name
pname
, # URL to package index
url
, # filename including extension
file
, # SRI hash
hash
,
}:
let
pathParts = filter ({ prefix, path }: "NETRC" == prefix) nixPath; # deadnix: skip
netrc_file =
if (pathParts != [ ])
then (head pathParts).path
else "";
in
runCommand file
({
nativeBuildInputs = [ python3 ];
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ (lib.optional lib.inPureEvalMode "NETRC");
outputHashMode = "flat";
outputHashAlgo = "sha256";
outputHash = hash;
passthru.isWheel = lib.strings.hasSuffix "whl" file;
} // lib.optionalAttrs (!lib.inPureEvalMode) { NETRC = netrc_file; }) ''
python ${./fetch-from-legacy.py} ${url} ${pname} ${file}
mv ${file} $out
'';
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,19 @@ def handle_endtag(self, tag: str) -> None:
if username and password:
import base64

password_b64 = base64.b64encode(":".join((username, password)).encode()).decode("utf-8")
password_b64 = base64.b64encode(":".join((username, password)).encode()).decode(
"utf-8"
)
req.add_header("Authorization", "Basic {}".format(password_b64))
response = urllib.request.urlopen(req, context=context)
index = response.read()

parser = Pep503()
parser.feed(str(index, "utf-8"))
if package_filename not in parser.sources:
print("The file %s has not be found in the index %s" % (package_filename, index_url))
print(
"The file %s has not be found in the index %s" % (package_filename, index_url)
)
exit(1)

package_file = open(package_filename, "wb")
Expand Down
File renamed without changes.
6 changes: 4 additions & 2 deletions mk-poetry-dep.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
, pep508Env
, pyVersion
, pyproject-nix
, fetchPypiLegacy
, fetchFromPypi
}:
{ name
, version
Expand Down Expand Up @@ -224,14 +226,14 @@ pythonPackages.callPackage
else if isFile then
localDepPath
else if isLegacy then
pyproject-nix.fetchers.fetchFromLegacy
fetchPypiLegacy
{
pname = name;
inherit (fileInfo) file hash;
inherit (source) url;
}
else
pyproject-nix.fetchers.fetchFromPypi {
fetchFromPypi {
pname = name;
inherit (fileInfo) file hash;
inherit version;
Expand Down
3 changes: 1 addition & 2 deletions vendor/pyproject.nix/default.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{ pkgs, lib }:
{ lib }:
{
lib = import ./lib { inherit lib; };
fetchers = pkgs.callPackage ./fetchers { };
}
2 changes: 1 addition & 1 deletion vendor/pyproject.nix/lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fix (self: mapAttrs (_: path: import path ({ inherit lib; } // self)) {
renderers = ./renderers.nix;
validators = ./validators.nix;
poetry = ./poetry.nix;

eggs = ./eggs.nix;
pep440 = ./pep440.nix;
pep508 = ./pep508.nix;
pep518 = ./pep518.nix;
Expand Down
87 changes: 87 additions & 0 deletions vendor/pyproject.nix/lib/eggs.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{ lib, ... }:
let
inherit (builtins) filter match elemAt compareVersions sort;
inherit (lib) isString;

# Tag normalization documented in
# https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/#details
normalizedImpls = {
py = "python";
cp = "cpython";
ip = "ironpython";
pp = "pypy";
jy = "jython";
};
normalizeImpl = t: normalizedImpls.${t} or t;

in
lib.fix (self: {
/* Regex match an egg file name, returning a list of match groups. Returns null if no match.
Type: matchEggFileName :: string -> [ string ]
*/
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.
Type: isEggFileName :: string -> bool
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.
Type: parsehEggFileName :: string -> AttrSet
Example:
# parseEggFileName
*/
parseEggFileName = name:
let
m = self.matchEggFileName name;
mAt = elemAt m;
langM = match "([^0-9]*)(.+)" (mAt 2);
langAt = elemAt langM;
in
assert m != null; {
filename = name;
distribution = mAt 0;
version = mAt 1;
languageTag = {
implementation = normalizeImpl (langAt 0);
version = langAt 1;
};
};

/* Select compatible eggs from a list and return them in priority order.
Type: selectEggs :: derivation -> [ AttrSet ] -> [ AttrSet ]
*/
selectEggs =
# Python interpreter derivation
python:
# List of files parsed by parseEggFileName
files:
let
inherit (python.passthru) pythonVersion implementation;

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

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

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

0 comments on commit 7f304a8

Please sign in to comment.