Skip to content

Commit

Permalink
Merge pull request NixOS#99327 from adisbladis/poetry2nix-1_13_0
Browse files Browse the repository at this point in the history
poetry2nix: 1.12.0 -> 1.13.0
  • Loading branch information
adisbladis committed Oct 1, 2020
2 parents 8aa575d + 0eeedc7 commit c25f55e
Show file tree
Hide file tree
Showing 12 changed files with 961 additions and 1,777 deletions.
108 changes: 69 additions & 39 deletions pkgs/development/tools/poetry2nix/poetry2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,58 @@ let

# Experimental withPlugins functionality
toPluginAble = (import ./plugins.nix { inherit pkgs lib; }).toPluginAble;

mkInputAttrs =
{ py
, pyProject
, attrs
, includeBuildSystem ? true
}:
let
getInputs = attr: attrs.${attr} or [ ];

# Get dependencies and filter out depending on interpreter version
getDeps = depAttr:
let
compat = isCompatible (poetryLib.getPythonVersion py);
deps = pyProject.tool.poetry.${depAttr} or { };
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames deps);
in
(
builtins.map
(
dep:
let
pkg = py.pkgs."${moduleName dep}";
constraints = deps.${dep}.python or "";
isCompat = compat constraints;
in
if isCompat then pkg else null
)
depAttrs
);

buildSystemPkgs = poetryLib.getBuildSystemPkgs {
inherit pyProject;
pythonPackages = py.pkgs;
};

mkInput = attr: extraInputs: getInputs attr ++ extraInputs;

in
{
buildInputs = mkInput "buildInputs" (if includeBuildSystem then buildSystemPkgs else [ ]);
propagatedBuildInputs = mkInput "propagatedBuildInputs" (getDeps "dependencies") ++ ([ py.pkgs.setuptools ]);
nativeBuildInputs = mkInput "nativeBuildInputs" [ ];
checkInputs = mkInput "checkInputs" (getDeps "dev-dependencies");
};


in
lib.makeScope pkgs.newScope (self: {

# Poetry2nix version
version = "1.12.0";
version = "1.13.0";

/*
Returns an attrset { python, poetryPackages, pyProject, poetryLock } for the given pyproject/lockfile.
Expand Down Expand Up @@ -61,7 +108,7 @@ lib.makeScope pkgs.newScope (self: {
# Filter packages by their PEP508 markers & pyproject interpreter version
partitions =
let
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true;
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true && isCompatible (poetryLib.getPythonVersion python) pkgMeta.python-versions;
in
lib.partition supportsPythonVersion poetryLock.package;
compatible = partitions.right;
Expand Down Expand Up @@ -91,7 +138,7 @@ lib.makeScope pkgs.newScope (self: {
);
}
)
compatible
(lib.reverseList compatible)
);
in
lockPkgs;
Expand All @@ -106,9 +153,13 @@ lib.makeScope pkgs.newScope (self: {
in
{
mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
inherit pkgs lib python poetryLib;
inherit pkgs lib python poetryLib evalPep508;
};
poetry = poetryPkg;

# Use poetry-core from the poetry build (pep517/518 build-system)
poetry-core = if __isBootstrap then null else poetryPkg.passthru.python.pkgs.poetry-core;
poetry = if __isBootstrap then null else poetryPkg;

# The canonical name is setuptools-scm
setuptools-scm = super.setuptools_scm;

Expand All @@ -126,10 +177,13 @@ lib.makeScope pkgs.newScope (self: {
);
packageOverrides = lib.foldr lib.composeExtensions (self: super: { }) overlays;
py = python.override { inherit packageOverrides; self = py; };

inputAttrs = mkInputAttrs { inherit py pyProject; attrs = { }; includeBuildSystem = false; };

in
{
python = py;
poetryPackages = map (pkg: py.pkgs.${moduleName pkg.name}) compatible;
poetryPackages = builtins.foldl' (acc: v: acc ++ v) [ ] (lib.attrValues inputAttrs);
poetryLock = poetryLock;
inherit pyProject;
};
Expand Down Expand Up @@ -219,32 +273,12 @@ lib.makeScope pkgs.newScope (self: {
];
passedAttrs = builtins.removeAttrs attrs specialAttrs;

# Get dependencies and filter out depending on interpreter version
getDeps = depAttr:
let
compat = isCompatible (poetryLib.getPythonVersion py);
deps = pyProject.tool.poetry.${depAttr} or { };
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames deps);
in
builtins.map
(
dep:
let
pkg = py.pkgs."${moduleName dep}";
constraints = deps.${dep}.python or "";
isCompat = compat constraints;
in
if isCompat then pkg else null
)
depAttrs;
getInputs = attr: attrs.${attr} or [ ];
mkInput = attr: extraInputs: getInputs attr ++ extraInputs;
buildSystemPkgs = poetryLib.getBuildSystemPkgs {
inherit pyProject;
pythonPackages = py.pkgs;
};
inputAttrs = mkInputAttrs { inherit py pyProject attrs; };

app = py.pkgs.buildPythonPackage (
passedAttrs // {
passedAttrs // inputAttrs // {
nativeBuildInputs = inputAttrs.nativeBuildInputs ++ [ py.pkgs.removePathDependenciesHook ];
} // {
pname = moduleName pyProject.tool.poetry.name;
version = pyProject.tool.poetry.version;

Expand All @@ -256,11 +290,6 @@ lib.makeScope pkgs.newScope (self: {
# provides python modules
namePrefix = "";

buildInputs = mkInput "buildInputs" buildSystemPkgs;
propagatedBuildInputs = mkInput "propagatedBuildInputs" (getDeps "dependencies") ++ ([ py.pkgs.setuptools ]);
nativeBuildInputs = mkInput "nativeBuildInputs" [ pkgs.yj py.pkgs.removePathDependenciesHook ];
checkInputs = mkInput "checkInputs" (getDeps "dev-dependencies");

passthru = {
python = py;
dependencyEnv = (
Expand All @@ -274,9 +303,10 @@ lib.makeScope pkgs.newScope (self: {
) { inherit app; };
};

meta = lib.optionalAttrs (lib.hasAttr "description" pyProject.tool.poetry) {
inherit (pyProject.tool.poetry) description;
} // lib.optionalAttrs (lib.hasAttr "homepage" pyProject.tool.poetry) {
meta = lib.optionalAttrs (lib.hasAttr "description" pyProject.tool.poetry)
{
inherit (pyProject.tool.poetry) description;
} // lib.optionalAttrs (lib.hasAttr "homepage" pyProject.tool.poetry) {
inherit (pyProject.tool.poetry) homepage;
} // {
inherit (py.meta) platforms;
Expand Down
35 changes: 18 additions & 17 deletions pkgs/development/tools/poetry2nix/poetry2nix/editable.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ let
# A python package that contains simple .egg-info and .pth files for an editable installation
editablePackage = python.pkgs.toPythonModule (pkgs.runCommandNoCC "${name}-editable"
{ } ''
mkdir -p "$out/${python.sitePackages}"
cd "$out/${python.sitePackages}"
mkdir -p "$out/${python.sitePackages}"
cd "$out/${python.sitePackages}"
# See https://docs.python.org/3.8/library/site.html for info on such .pth files
# These add another site package path for each line
touch poetry2nix-editable.pth
${lib.concatMapStringsSep "\n" (src: ''
echo "${toString src}" >> poetry2nix-editable.pth
'')
(lib.attrValues editablePackageSources)}
# See https://docs.python.org/3.8/library/site.html for info on such .pth files
# These add another site package path for each line
touch poetry2nix-editable.pth
${lib.concatMapStringsSep "\n"
(src: ''
echo "${toString src}" >> poetry2nix-editable.pth
'')
(lib.attrValues editablePackageSources)}
# Create a very simple egg so pkg_resources can find this package
# See https://setuptools.readthedocs.io/en/latest/formats.html for more info on the egg format
mkdir "${name}.egg-info"
cd "${name}.egg-info"
ln -s ${pkgInfoFile} PKG-INFO
${lib.optionalString (pyProject.tool.poetry ? plugins) ''
ln -s ${entryPointsFile} entry_points.txt
''}
# Create a very simple egg so pkg_resources can find this package
# See https://setuptools.readthedocs.io/en/latest/formats.html for more info on the egg format
mkdir "${name}.egg-info"
cd "${name}.egg-info"
ln -s ${pkgInfoFile} PKG-INFO
${lib.optionalString (pyProject.tool.poetry ? plugins) ''
ln -s ${entryPointsFile} entry_points.txt
''}
''
);
in
Expand Down
12 changes: 8 additions & 4 deletions pkgs/development/tools/poetry2nix/poetry2nix/hooks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ in
pyprojectPatchScript = "${./pyproject-without-path.py}";
};
} ./remove-path-dependencies.sh
) { };
)
{ };

pipBuildHook = callPackage
(
Expand All @@ -37,7 +38,8 @@ in
inherit pythonInterpreter pythonSitePackages;
};
} ./pip-build-hook.sh
) { };
)
{ };

poetry2nixFixupHook = callPackage
(
Expand All @@ -47,7 +49,8 @@ in
name = "fixup-hook.sh";
deps = [ ];
} ./fixup-hook.sh
) { };
)
{ };

# When the "wheel" package itself is a wheel the nixpkgs hook (which pulls in "wheel") leads to infinite recursion
# It doesn't _really_ depend on wheel though, it just copies the wheel.
Expand All @@ -58,7 +61,8 @@ in
name = "wheel-unpack-hook.sh";
deps = [ ];
} ./wheel-unpack-hook.sh
) { };
)
{ };


}
8 changes: 3 additions & 5 deletions pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ let
let
missingBuildBackendError = "No build-system.build-backend section in pyproject.toml. "
+ "Add such a section as described in https://python-poetry.org/docs/pyproject/#poetry-and-pep-517";
buildSystem = lib.attrByPath [ "build-system" "build-backend" ] (throw missingBuildBackendError) pyProject;
drvAttr = moduleName (builtins.elemAt (builtins.split "\\.|:" buildSystem) 0);
requires = lib.attrByPath [ "build-system" "requires" ] (throw missingBuildBackendError) pyProject;
requiredPkgs = builtins.map (n: lib.elemAt (builtins.match "([^!=<>~\[]+).*" n) 0) requires;
in
if buildSystem == "" then [ ] else (
[ pythonPackages.${drvAttr} or (throw "unsupported build system ${buildSystem}") ]
);
builtins.map (drvAttr: pythonPackages.${drvAttr} or (throw "unsupported build system requirement ${drvAttr}")) requiredPkgs;

# Find gitignore files recursively in parent directory stopping with .git
findGitIgnores = path:
Expand Down
32 changes: 19 additions & 13 deletions pkgs/development/tools/poetry2nix/poetry2nix/mk-poetry-dep.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
, lib
, python
, buildPythonPackage
, pythonPackages
, poetryLib
, evalPep508
}:
{ name
, version
Expand Down Expand Up @@ -53,9 +53,11 @@ pythonPackages.callPackage
pyProjectPath = localDepPath + "/pyproject.toml";
pyProject = poetryLib.readTOML pyProjectPath;
in
if builtins.pathExists pyProjectPath then poetryLib.getBuildSystemPkgs {
inherit pythonPackages pyProject;
} else [ ];
if builtins.pathExists pyProjectPath then
poetryLib.getBuildSystemPkgs
{
inherit pythonPackages pyProject;
} else [ ];

fileInfo =
let
Expand Down Expand Up @@ -127,8 +129,9 @@ pythonPackages.callPackage
n: v:
let
constraints = v.python or "";
pep508Markers = v.markers or "";
in
compat constraints
compat constraints && evalPep508 pep508Markers
)
dependencies
);
Expand All @@ -150,15 +153,18 @@ pythonPackages.callPackage
# Interpreters should declare what wheel types they're compatible with (python type + ABI)
# Here we can then choose a file based on that info.
src =
if isGit then (
builtins.fetchGit {
inherit (source) url;
rev = source.reference;
ref = sourceSpec.branch or sourceSpec.rev or sourceSpec.tag or "HEAD";
}
) else if isLocal then (poetryLib.cleanPythonSources { src = localDepPath; }) else fetchFromPypi {
if isGit then
(
builtins.fetchGit {
inherit (source) url;
rev = source.reference;
ref = sourceSpec.branch or sourceSpec.rev or sourceSpec.tag or "HEAD";
}
) else if isLocal then (poetryLib.cleanPythonSources { src = localDepPath; }) else
fetchFromPypi {
pname = name;
inherit (fileInfo) file hash kind;
};
}
) { }
)
{ }

0 comments on commit c25f55e

Please sign in to comment.