poetry2nix: 1.6.0 -> 1.6.1

wip/yesman
adisbladis 4 years ago
parent c669d2991b
commit 7401afc428
No known key found for this signature in database
GPG Key ID: 110BFAD44C6249B7
  1. 2
      pkgs/development/tools/poetry2nix/poetry2nix/cli.nix
  2. 296
      pkgs/development/tools/poetry2nix/poetry2nix/default.nix
  3. 1
      pkgs/development/tools/poetry2nix/poetry2nix/hooks/default.nix
  4. 107
      pkgs/development/tools/poetry2nix/poetry2nix/lib.nix
  5. 13
      pkgs/development/tools/poetry2nix/poetry2nix/mk-poetry-dep.nix
  6. 174
      pkgs/development/tools/poetry2nix/poetry2nix/overrides.nix
  7. 3
      pkgs/development/tools/poetry2nix/poetry2nix/pep425.nix
  8. 359
      pkgs/development/tools/poetry2nix/poetry2nix/pep508.nix
  9. 109
      pkgs/development/tools/poetry2nix/poetry2nix/semver.nix

@ -2,10 +2,8 @@
, lib ? pkgs.lib
, version
}:
let
inherit (pkgs) python3;
in
pkgs.stdenv.mkDerivation {
pname = "poetry2nix";

@ -7,7 +7,7 @@ let
inherit (poetryLib) isCompatible readTOML;
# Poetry2nix version
version = "1.6.0";
version = "1.6.1";
/* The default list of poetry2nix override overlays */
defaultPoetryOverrides = (import ./overrides.nix { inherit pkgs lib; });
@ -34,91 +34,94 @@ let
, overrides ? [ defaultPoetryOverrides ]
, python ? pkgs.python3
, pwd ? projectDir
}@attrs: let
poetryPkg = poetry.override { inherit python; };
pyProject = readTOML pyproject;
poetryLock = readTOML poetrylock;
lockFiles = lib.getAttrFromPath [ "metadata" "files" ] poetryLock;
}@attrs:
let
poetryPkg = poetry.override { inherit python; };
specialAttrs = [
"overrides"
"poetrylock"
"pwd"
];
passedAttrs = builtins.removeAttrs attrs specialAttrs;
pyProject = readTOML pyproject;
poetryLock = readTOML poetrylock;
lockFiles = lib.getAttrFromPath [ "metadata" "files" ] poetryLock;
evalPep508 = mkEvalPep508 python;
specialAttrs = [
"overrides"
"poetrylock"
"projectDir"
"pwd"
];
passedAttrs = builtins.removeAttrs attrs specialAttrs;
# Filter packages by their PEP508 markers & pyproject interpreter version
partitions = let
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true;
in
lib.partition supportsPythonVersion poetryLock.package;
evalPep508 = mkEvalPep508 python;
compatible = partitions.right;
incompatible = partitions.wrong;
# Filter packages by their PEP508 markers & pyproject interpreter version
partitions = let
supportsPythonVersion = pkgMeta: if pkgMeta ? marker then (evalPep508 pkgMeta.marker) else true;
in
lib.partition supportsPythonVersion poetryLock.package;
compatible = partitions.right;
incompatible = partitions.wrong;
# Create an overriden version of pythonPackages
#
# We need to avoid mixing multiple versions of pythonPackages in the same
# closure as python can only ever have one version of a dependency
baseOverlay = self: super:
let
getDep = depName: self.${depName};
lockPkgs = builtins.listToAttrs (
builtins.map (
pkgMeta: rec {
name = pkgMeta.name;
value = self.mkPoetryDep (
pkgMeta // {
inherit pwd;
source = pkgMeta.source or null;
files = lockFiles.${name};
pythonPackages = self;
}
);
}
) compatible
);
in
lockPkgs;
overlays = builtins.map getFunctorFn (
[
(
self: super:
let
hooks = self.callPackage ./hooks {};
in
{
mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
inherit pkgs lib python poetryLib;
};
poetry = poetryPkg;
# The canonical name is setuptools-scm
setuptools-scm = super.setuptools_scm;
inherit (hooks) removePathDependenciesHook poetry2nixFixupHook;
}
)
# Null out any filtered packages, we don't want python.pkgs from nixpkgs
(self: super: builtins.listToAttrs (builtins.map (x: { name = x.name; value = null; }) incompatible))
# Create poetry2nix layer
baseOverlay
] ++ # User provided overrides
overrides
);
# Create an overriden version of pythonPackages
#
# We need to avoid mixing multiple versions of pythonPackages in the same
# closure as python can only ever have one version of a dependency
baseOverlay = self: super:
let
getDep = depName: self.${depName};
packageOverrides = lib.foldr lib.composeExtensions (self: super: {}) overlays;
lockPkgs = builtins.listToAttrs (
builtins.map (
pkgMeta: rec {
name = pkgMeta.name;
value = self.mkPoetryDep (
pkgMeta // {
inherit pwd;
source = pkgMeta.source or null;
files = lockFiles.${name};
pythonPackages = self;
}
);
}
) compatible
);
in
lockPkgs;
overlays = builtins.map getFunctorFn (
[
(
self: super: let
hooks = self.callPackage ./hooks {};
in
{
mkPoetryDep = self.callPackage ./mk-poetry-dep.nix {
inherit pkgs lib python poetryLib;
};
poetry = poetryPkg;
# The canonical name is setuptools-scm
setuptools-scm = super.setuptools_scm;
inherit (hooks) removePathDependenciesHook poetry2nixFixupHook;
}
)
# Null out any filtered packages, we don't want python.pkgs from nixpkgs
(self: super: builtins.listToAttrs (builtins.map (x: { name = x.name; value = null; }) incompatible))
# Create poetry2nix layer
baseOverlay
] ++ # User provided overrides
overrides
);
packageOverrides = lib.foldr lib.composeExtensions (self: super: {}) overlays;
py = python.override { inherit packageOverrides; self = py; };
in
{
python = py;
poetryPackages = map (pkg: py.pkgs.${pkg.name}) compatible;
poetryLock = poetryLock;
inherit pyProject;
};
py = python.override { inherit packageOverrides; self = py; };
in
{
python = py;
poetryPackages = map (pkg: py.pkgs.${pkg.name}) compatible;
poetryLock = poetryLock;
inherit pyProject;
};
/* Returns a package with a python interpreter and all packages specified in the poetry.lock lock file.
@ -153,76 +156,78 @@ let
, python ? pkgs.python3
, pwd ? projectDir
, ...
}@attrs: let
poetryPython = mkPoetryPackages {
inherit pyproject poetrylock overrides python pwd;
};
py = poetryPython.python;
inherit (poetryPython) pyProject;
specialAttrs = [
"overrides"
"poetrylock"
"pwd"
"pyproject"
];
passedAttrs = builtins.removeAttrs attrs specialAttrs;
# Get dependencies and filter out depending on interpreter version
getDeps = depAttr: let
compat = isCompatible py.pythonVersion;
deps = pyProject.tool.poetry.${depAttr} or {};
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames deps);
in
builtins.map (
dep: let
pkg = py.pkgs."${dep}";
constraints = deps.${dep}.python or "";
isCompat = compat constraints;
}@attrs:
let
poetryPython = mkPoetryPackages {
inherit pyproject poetrylock overrides python pwd;
};
py = poetryPython.python;
inherit (poetryPython) pyProject;
specialAttrs = [
"overrides"
"poetrylock"
"projectDir"
"pwd"
"pyproject"
];
passedAttrs = builtins.removeAttrs attrs specialAttrs;
# Get dependencies and filter out depending on interpreter version
getDeps = depAttr:
let
compat = isCompatible py.pythonVersion;
deps = pyProject.tool.poetry.${depAttr} or {};
depAttrs = builtins.map (d: lib.toLower d) (builtins.attrNames deps);
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;
};
in
py.pkgs.buildPythonApplication (
passedAttrs // {
pname = pyProject.tool.poetry.name;
version = pyProject.tool.poetry.version;
builtins.map (
dep:
let
pkg = py.pkgs."${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;
};
in
py.pkgs.buildPythonApplication (
passedAttrs // {
pname = pyProject.tool.poetry.name;
version = pyProject.tool.poetry.version;
inherit src;
inherit src;
format = "pyproject";
format = "pyproject";
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");
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;
};
passthru = {
python = py;
};
meta = meta // {
inherit (pyProject.tool.poetry) description homepage;
inherit (py.meta) platforms;
license = getLicenseBySpdxId (pyProject.tool.poetry.license or "unknown");
};
meta = meta // {
inherit (pyProject.tool.poetry) description homepage;
inherit (py.meta) platforms;
license = getLicenseBySpdxId (pyProject.tool.poetry.license or "unknown");
};
}
);
}
);
/* Poetry2nix CLI used to supplement SHA-256 hashes for git dependencies */
cli = import ./cli.nix { inherit pkgs lib version; };
in
{
inherit mkPoetryEnv mkPoetryApplication mkPoetryPackages cli version;
@ -236,11 +241,12 @@ in
*/
defaultPoetryOverrides = {
__functor = defaultPoetryOverrides;
overrideOverlay = fn: self: super: let
defaultSet = defaultPoetryOverrides self super;
customSet = fn self super;
in
defaultSet // customSet;
overrideOverlay = fn: self: super:
let
defaultSet = defaultPoetryOverrides self super;
customSet = fn self super;
in
defaultSet // customSet;
};
/*

@ -3,7 +3,6 @@
, makeSetupHook
, yj
}:
let
pythonInterpreter = python.pythonForBuild.interpreter;
in

@ -9,29 +9,30 @@ let
);
# Compare a semver expression with a version
isCompatible = version: let
operators = {
"||" = cond1: cond2: cond1 || cond2;
"," = cond1: cond2: cond1 && cond2; # , means &&
"&&" = cond1: cond2: cond1 && cond2;
};
splitRe = "(" + (builtins.concatStringsSep "|" (builtins.map (x: lib.replaceStrings [ "|" ] [ "\\|" ] x) (lib.attrNames operators))) + ")";
in
expr:
let
tokens = builtins.filter (x: x != "") (builtins.split splitRe expr);
combine = acc: v:
let
isOperator = builtins.typeOf v == "list";
operator = if isOperator then (builtins.elemAt v 0) else acc.operator;
in
if isOperator then (acc // { inherit operator; }) else {
inherit operator;
state = operators."${operator}" acc.state (satisfiesSemver version v);
};
initial = { operator = "&&"; state = true; };
in
if expr == "" then true else (builtins.foldl' combine initial tokens).state;
isCompatible = version:
let
operators = {
"||" = cond1: cond2: cond1 || cond2;
"," = cond1: cond2: cond1 && cond2; # , means &&
"&&" = cond1: cond2: cond1 && cond2;
};
splitRe = "(" + (builtins.concatStringsSep "|" (builtins.map (x: lib.replaceStrings [ "|" ] [ "\\|" ] x) (lib.attrNames operators))) + ")";
in
expr:
let
tokens = builtins.filter (x: x != "") (builtins.split splitRe expr);
combine = acc: v:
let
isOperator = builtins.typeOf v == "list";
operator = if isOperator then (builtins.elemAt v 0) else acc.operator;
in
if isOperator then (acc // { inherit operator; }) else {
inherit operator;
state = operators."${operator}" acc.state (satisfiesSemver version v);
};
initial = { operator = "&&"; state = true; };
in
if expr == "" then true else (builtins.foldl' combine initial tokens).state;
fromTOML = builtins.fromTOML or
(
@ -88,23 +89,25 @@ let
getBuildSystemPkgs =
{ pythonPackages
, pyProject
}: let
buildSystem = lib.getAttrFromPath [ "build-system" "build-backend" ] pyProject;
drvAttr = builtins.elemAt (builtins.split "\\.|:" buildSystem) 0;
in
if buildSystem == "" then [] else (
[ pythonPackages.${drvAttr} or (throw "unsupported build system ${buildSystem}") ]
);
}:
let
buildSystem = lib.getAttrFromPath [ "build-system" "build-backend" ] pyProject;
drvAttr = builtins.elemAt (builtins.split "\\.|:" buildSystem) 0;
in
if buildSystem == "" then [] else (
[ pythonPackages.${drvAttr} or (throw "unsupported build system ${buildSystem}") ]
);
# Find gitignore files recursively in parent directory stopping with .git
findGitIgnores = path: let
parent = path + "/..";
gitIgnore = path + "/.gitignore";
isGitRoot = builtins.pathExists (path + "/.git");
hasGitIgnore = builtins.pathExists gitIgnore;
gitIgnores = if hasGitIgnore then [ gitIgnore ] else [];
in
lib.optionals (builtins.toString path != "/" && ! isGitRoot) (findGitIgnores parent) ++ gitIgnores;
findGitIgnores = path:
let
parent = path + "/..";
gitIgnore = path + "/.gitignore";
isGitRoot = builtins.pathExists (path + "/.git");
hasGitIgnore = builtins.pathExists gitIgnore;
gitIgnores = if hasGitIgnore then [ gitIgnore ] else [];
in
lib.optionals (builtins.toString path != "/" && ! isGitRoot) (findGitIgnores parent) ++ gitIgnores;
/*
Provides a source filtering mechanism that:
@ -113,21 +116,21 @@ let
- Filters pycache/pyc files
- Uses cleanSourceFilter to filter out .git/.hg, .o/.so, editor backup files & nix result symlinks
*/
cleanPythonSources = { src }: let
gitIgnores = findGitIgnores src;
pycacheFilter = name: type:
(type == "directory" && ! lib.strings.hasInfix "__pycache__" name)
|| (type == "regular" && ! lib.strings.hasSuffix ".pyc" name)
;
in
lib.cleanSourceWith {
filter = lib.cleanSourceFilter;
src = lib.cleanSourceWith {
filter = pkgs.nix-gitignore.gitignoreFilterPure pycacheFilter gitIgnores src;
inherit src;
cleanPythonSources = { src }:
let
gitIgnores = findGitIgnores src;
pycacheFilter = name: type:
(type == "directory" && ! lib.strings.hasInfix "__pycache__" name)
|| (type == "regular" && ! lib.strings.hasSuffix ".pyc" name)
;
in
lib.cleanSourceWith {
filter = lib.cleanSourceFilter;
src = lib.cleanSourceWith {
filter = pkgs.nix-gitignore.gitignoreFilterPure pycacheFilter gitIgnores src;
inherit src;
};
};
};
in
{
inherit

@ -22,9 +22,7 @@ pythonPackages.callPackage (
{ preferWheel ? false
, ...
}@args:
let
inherit (poetryLib) isCompatible getManyLinuxDeps fetchFromPypi;
inherit (import ./pep425.nix {
@ -68,7 +66,6 @@ pythonPackages.callPackage (
lockFileEntry = builtins.head entries;
_isEgg = isEgg lockFileEntry;
in
rec {
inherit (lockFileEntry) file hash;
@ -92,7 +89,6 @@ pythonPackages.callPackage (
baseBuildInputs = lib.optional (! lib.elem name skipSetupToolsSCM) pythonPackages.setuptools-scm;
format = if isLocal then "pyproject" else if isGit then "setuptools" else fileInfo.format;
in
buildPythonPackage {
@ -123,10 +119,11 @@ pythonPackages.callPackage (
compat = isCompatible python.pythonVersion;
deps = lib.filterAttrs (n: v: v) (
lib.mapAttrs (
n: v: let
constraints = v.python or "";
in
compat constraints
n: v:
let
constraints = v.python or "";
in
compat constraints
) dependencies
);
depAttrs = lib.attrNames deps;

@ -195,39 +195,39 @@ self: super:
);
matplotlib = super.matplotlib.overrideAttrs (
old: let
enableGhostscript = old.passthru.enableGhostscript or false;
enableGtk3 = old.passthru.enableTk or false;
enableQt = old.passthru.enableQt or false;
enableTk = old.passthru.enableTk or false;
inherit (pkgs.darwin.apple_sdk.frameworks) Cocoa;
in
{
NIX_CFLAGS_COMPILE = stdenv.lib.optionalString stdenv.isDarwin "-I${pkgs.libcxx}/include/c++/v1";
XDG_RUNTIME_DIR = "/tmp";
buildInputs = old.buildInputs
++ lib.optional enableGhostscript pkgs.ghostscript
++ lib.optional stdenv.isDarwin [ Cocoa ];
nativeBuildInputs = old.nativeBuildInputs ++ [
pkgs.pkgconfig
];
propagatedBuildInputs = old.propagatedBuildInputs ++ [
pkgs.libpng
pkgs.freetype
]
++ stdenv.lib.optionals enableGtk3 [ pkgs.cairo self.pycairo pkgs.gtk3 pkgs.gobject-introspection self.pygobject3 ]
++ stdenv.lib.optionals enableTk [ pkgs.tcl pkgs.tk self.tkinter pkgs.libX11 ]
++ stdenv.lib.optionals enableQt [ self.pyqt5 ]
;
old:
let
enableGhostscript = old.passthru.enableGhostscript or false;
enableGtk3 = old.passthru.enableTk or false;
enableQt = old.passthru.enableQt or false;
enableTk = old.passthru.enableTk or false;
inherit (pkgs.darwin.apple_sdk.frameworks) Cocoa;
in
{
NIX_CFLAGS_COMPILE = stdenv.lib.optionalString stdenv.isDarwin "-I${pkgs.libcxx}/include/c++/v1";
XDG_RUNTIME_DIR = "/tmp";
buildInputs = old.buildInputs
++ lib.optional enableGhostscript pkgs.ghostscript
++ lib.optional stdenv.isDarwin [ Cocoa ];
nativeBuildInputs = old.nativeBuildInputs ++ [
pkgs.pkgconfig
];
propagatedBuildInputs = old.propagatedBuildInputs ++ [
pkgs.libpng
pkgs.freetype
]
++ stdenv.lib.optionals enableGtk3 [ pkgs.cairo self.pycairo pkgs.gtk3 pkgs.gobject-introspection self.pygobject3 ]
++ stdenv.lib.optionals enableTk [ pkgs.tcl pkgs.tk self.tkinter pkgs.libX11 ]
++ stdenv.lib.optionals enableQt [ self.pyqt5 ]
;
inherit (super.matplotlib) patches;
}
inherit (super.matplotlib) patches;
}
);
# Calls Cargo at build time for source builds and is really tricky to package
@ -266,36 +266,37 @@ self: super:
);
numpy = super.numpy.overrideAttrs (
old: let
blas = old.passthru.args.blas or pkgs.openblasCompat;
blasImplementation = lib.nameFromURL blas.name "-";
cfg = pkgs.writeTextFile {
name = "site.cfg";
text = (
lib.generators.toINI {} {
${blasImplementation} = {
include_dirs = "${blas}/include";
library_dirs = "${blas}/lib";
} // lib.optionalAttrs (blasImplementation == "mkl") {
mkl_libs = "mkl_rt";
lapack_libs = "";
};
}
);
};
in
{
nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.gfortran ];
buildInputs = old.buildInputs ++ [ blas self.cython ];
enableParallelBuilding = true;
preBuild = ''
ln -s ${cfg} site.cfg
'';
passthru = old.passthru // {
blas = blas;
inherit blasImplementation cfg;
old:
let
blas = old.passthru.args.blas or pkgs.openblasCompat;
blasImplementation = lib.nameFromURL blas.name "-";
cfg = pkgs.writeTextFile {
name = "site.cfg";
text = (
lib.generators.toINI {} {
${blasImplementation} = {
include_dirs = "${blas}/include";
library_dirs = "${blas}/lib";
} // lib.optionalAttrs (blasImplementation == "mkl") {
mkl_libs = "mkl_rt";
lapack_libs = "";
};
}
);
};
}
in
{
nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.gfortran ];
buildInputs = old.buildInputs ++ [ blas self.cython ];
enableParallelBuilding = true;
preBuild = ''
ln -s ${cfg} site.cfg
'';
passthru = old.passthru // {
blas = blas;
inherit blasImplementation cfg;
};
}
);
openexr = super.openexr.overrideAttrs (
@ -306,16 +307,17 @@ self: super:
);
peewee = super.peewee.overridePythonAttrs (
old: let
withPostgres = old.passthru.withPostgres or false;
withMysql = old.passthru.withMysql or false;
in
{
buildInputs = old.buildInputs ++ [ self.cython pkgs.sqlite ];
propagatedBuildInputs = old.propagatedBuildInputs
++ lib.optional withPostgres self.psycopg2
++ lib.optional withMysql self.mysql-connector;
}
old:
let
withPostgres = old.passthru.withPostgres or false;
withMysql = old.passthru.withMysql or false;
in
{
buildInputs = old.buildInputs ++ [ self.cython pkgs.sqlite ];
propagatedBuildInputs = old.propagatedBuildInputs
++ lib.optional withPostgres self.psycopg2
++ lib.optional withMysql self.mysql-connector;
}
);
pillow = super.pillow.overrideAttrs (
@ -667,16 +669,24 @@ self: super:
);
zipp =
if lib.versionAtLeast super.zipp.version "2.0.0" then (
super.zipp.overridePythonAttrs (
old: {
prePatch = ''
substituteInPlace setup.py --replace \
'setuptools.setup()' \
'setuptools.setup(version="${super.zipp.version}")'
'';
}
)
) else super.zipp;
(
if lib.versionAtLeast super.zipp.version "2.0.0" then (
super.zipp.overridePythonAttrs (
old: {
prePatch = ''
substituteInPlace setup.py --replace \
'setuptools.setup()' \
'setuptools.setup(version="${super.zipp.version}")'
'';
}
)
) else super.zipp
).overridePythonAttrs (
old: {
propagatedBuildInputs = old.propagatedBuildInputs ++ [
self.toml
];
}
);
}

@ -1,5 +1,4 @@
{ lib, stdenv, python, isLinux ? stdenv.isLinux }:
let
inherit (lib.strings) hasSuffix hasInfix splitString removeSuffix;
@ -94,12 +93,10 @@ let
if isLinux
then chooseLinux files
else chooseOSX files;
in
if (builtins.length filtered == 0)
then []
else choose (filtered);
in
{
inherit selectWheel toWheelAttrs isPyVersionCompatible;

@ -1,5 +1,4 @@
{ lib, stdenv, poetryLib }: python:
let
inherit (poetryLib) ireplace;
@ -37,195 +36,199 @@ let
);
# Make a tree out of expression groups (parens)
findSubExpressions = expr: let
acc = builtins.foldl' findSubExpressionsFun {
exprs = [];
expr = expr;
pos = 0;
openP = 0;
exprPos = 0;
startPos = 0;
} (lib.stringToCharacters expr);
tailExpr = (substr acc.exprPos acc.pos expr);
tailExprs = if tailExpr != "" then [ tailExpr ] else [];
in
acc.exprs ++ tailExprs;
parseExpressions = exprs: let
splitCond = (
s: builtins.map
(x: stripStr (if builtins.typeOf x == "list" then (builtins.elemAt x 0) else x))
(builtins.split " (and|or) " (s + " "))
);
mapfn = expr: (
if (builtins.match "^ ?$" expr != null) then null # Filter empty
else if (builtins.elem expr [ "and" "or" ]) then {
type = "bool";
value = expr;
}
else {
type = "expr";
value = expr;
}
);
findSubExpressions = expr:
let
acc = builtins.foldl' findSubExpressionsFun {
exprs = [];
expr = expr;
pos = 0;
openP = 0;
exprPos = 0;
startPos = 0;
} (lib.stringToCharacters expr);
tailExpr = (substr acc.exprPos acc.pos expr);
tailExprs = if tailExpr != "" then [ tailExpr ] else [];
in
acc.exprs ++ tailExprs;
parseExpressions = exprs:
let
splitCond = (
s: builtins.map
(x: stripStr (if builtins.typeOf x == "list" then (builtins.elemAt x 0) else x))
(builtins.split " (and|or) " (s + " "))
);
parse = expr: builtins.filter (x: x != null) (builtins.map mapfn (splitCond expr));
mapfn = expr: (
if (builtins.match "^ ?$" expr != null) then null # Filter empty
else if (builtins.elem expr [ "and" "or" ]) then {
type = "bool";
value = expr;
}
else {
type = "expr";
value = expr;
}
);
in
builtins.foldl' (
acc: v: acc ++ (
if builtins.typeOf v == "string" then parse v else [ (parseExpressions v) ]
)
) [] exprs;
parse = expr: builtins.filter (x: x != null) (builtins.map mapfn (splitCond expr));
in
builtins.foldl' (
acc: v: acc ++ (
if builtins.typeOf v == "string" then parse v else [ (parseExpressions v) ]
)
) [] exprs;
# Transform individual expressions to structured expressions
# This function also performs variable substitution, replacing environment markers with their explicit values
transformExpressions = exprs: let
variables = {
os_name = (
if python.pname == "jython" then "java"
else "posix"
);
sys_platform = (
if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "darwin"
else throw "Unsupported platform"
);
platform_machine = stdenv.platform.kernelArch;
platform_python_implementation = let
impl = python.passthru.implementation;
in
(
if impl == "cpython" then "CPython"
else if impl == "pypy" then "PyPy"
else throw "Unsupported implementation ${impl}"
transformExpressions = exprs:
let
variables = {
os_name = (
if python.pname == "jython" then "java"
else "posix"
);
platform_release = ""; # Field not reproducible
platform_system = (
if stdenv.isLinux then "Linux"
else if stdenv.isDarwin then "Darwin"
else throw "Unsupported platform"
);
platform_version = ""; # Field not reproducible
python_version = python.passthru.pythonVersion;
python_full_version = python.version;
implementation_name = python.implementation;
implementation_version = python.version;
extra = "";
};
substituteVar = value: if builtins.hasAttr value variables then (builtins.toJSON variables."${value}") else value;
processVar = value: builtins.foldl' (acc: v: v acc) value [
stripStr
substituteVar
];
in
if builtins.typeOf exprs == "set" then (
if exprs.type == "expr" then (
let
mVal = ''[a-zA-Z0-9\'"_\. ]+'';
mOp = "in|[!=<>]+";
e = stripStr exprs.value;
m = builtins.map stripStr (builtins.match ''^(${mVal}) *(${mOp}) *(${mVal})$'' e);
sys_platform = (
if stdenv.isLinux then "linux"
else if stdenv.isDarwin then "darwin"
else throw "Unsupported platform"
);
platform_machine = stdenv.platform.kernelArch;
platform_python_implementation = let
impl = python.passthru.implementation;
in
{
type = "expr";
value = {
op = builtins.elemAt m 1;
values = [
(processVar (builtins.elemAt m 0))
(processVar (builtins.elemAt m 2))
];
};
}
) else exprs
) else builtins.map transformExpressions exprs;
(
if impl == "cpython" then "CPython"
else if impl == "pypy" then "PyPy"
else throw "Unsupported implementation ${impl}"
);
platform_release = ""; # Field not reproducible
platform_system = (
if stdenv.isLinux then "Linux"
else if stdenv.isDarwin then "Darwin"
else throw "Unsupported platform"
);
platform_version = ""; # Field not reproducible
python_version = python.passthru.pythonVersion;
python_full_version = python.version;
implementation_name = python.implementation;
implementation_version = python.version;
extra = "";
};
substituteVar = value: if builtins.hasAttr value variables then (builtins.toJSON variables."${value}") else value;
processVar = value: builtins.foldl' (acc: v: v acc) value [
stripStr
substituteVar
];
in
if builtins.typeOf exprs == "set" then (
if exprs.type == "expr" then (
let
mVal = ''[a-zA-Z0-9\'"_\. ]+'';
mOp = "in|[!=<>]+";
e = stripStr exprs.value;
m = builtins.map stripStr (builtins.match ''^(${mVal}) *(${mOp}) *(${mVal})$'' e);
in
{
type = "expr";
value = {
op = builtins.elemAt m 1;
values = [
(processVar (builtins.elemAt m 0))
(processVar (builtins.elemAt m 2))
];
};
}
) else exprs
) else builtins.map transformExpressions exprs;
# Recursively eval all expressions
evalExpressions = exprs: let
unmarshal = v: (
# TODO: Handle single quoted values
if v == "True" then true
else if v == "False" then false
else builtins.fromJSON v
);
hasElem = needle: haystack: builtins.elem needle (builtins.filter (x: builtins.typeOf x == "string") (builtins.split " " haystack));
op = {
"<=" = x: y: (unmarshal x) <= (unmarshal y);
"<" = x: y: (unmarshal x) < (unmarshal y);
"!=" = x: y: x != y;
"==" = x: y: x == y;
">=" = x: y: (unmarshal x) >= (unmarshal y);
">" = x: y: (unmarshal x) > (unmarshal y);
"~=" = v: c: let
parts = builtins.splitVersion c;
pruned = lib.take ((builtins.length parts) - 1) parts;
upper = builtins.toString (
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
);
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
in
op.">=" v c && op."<" v upperConstraint;
"===" = x: y: x == y;
"in" = x: y: let
values = builtins.filter (x: builtins.typeOf x == "string") (builtins.split " " (unmarshal y));
in
builtins.elem (unmarshal x) values;
};
in
if builtins.typeOf exprs == "set" then (
if exprs.type == "expr" then (
let
expr = exprs;
result = (op."${expr.value.op}") (builtins.elemAt expr.value.values 0) (builtins.elemAt expr.value.values 1);
in
{
type = "value";
value = result;
}
) else exprs
) else builtins.map evalExpressions exprs;
evalExpressions = exprs:
let
unmarshal = v: (
# TODO: Handle single quoted values
if v == "True" then true
else if v == "False" then false
else builtins.fromJSON v
);
hasElem = needle: haystack: builtins.elem needle (builtins.filter (x: builtins.typeOf x == "string") (builtins.split " " haystack));
op = {
"<=" = x: y: (unmarshal x) <= (unmarshal y);
"<" = x: y: (unmarshal x) < (unmarshal y);
"!=" = x: y: x != y;
"==" = x: y: x == y;
">=" = x: y: (unmarshal x) >= (unmarshal y);
">" = x: y: (unmarshal x) > (unmarshal y);
"~=" = v: c:
let
parts = builtins.splitVersion c;
pruned = lib.take ((builtins.length parts) - 1) parts;
upper = builtins.toString (
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
);
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
in
op.">=" v c && op."<" v upperConstraint;
"===" = x: y: x == y;
"in" = x: y:
let
values = builtins.filter (x: builtins.typeOf x == "string") (builtins.split " " (unmarshal y));
in
builtins.elem (unmarshal x) values;
};
in
if builtins.typeOf exprs == "set" then (
if exprs.type == "expr" then (
let
expr = exprs;
result = (op."${expr.value.op}") (builtins.elemAt expr.value.values 0) (builtins.elemAt expr.value.values 1);
in
{
type = "value";
value = result;
}
) else exprs
) else builtins.map evalExpressions exprs;
# Now that we have performed an eval all that's left to do is to concat the graph into a single bool
reduceExpressions = exprs: let
cond = {
"and" = x: y: x && y;
"or" = x: y: x || y;
};
reduceExpressionsFun = acc: v: (
if builtins.typeOf v == "set" then (
if v.type == "value" then (
acc // {
value = cond."${acc.cond}" acc.value v.value;
}
) else if v.type == "bool" then (
acc // {
cond = v.value;
}
reduceExpressions = exprs:
let
cond = {
"and" = x: y: x && y;
"or" = x: y: x || y;
};
reduceExpressionsFun = acc: v: (
if builtins.typeOf v == "set" then (
if v.type == "value" then (
acc // {
value = cond."${acc.cond}" acc.value v.value;
}
) else if v.type == "bool" then (
acc // {
cond = v.value;
}
) else throw "Unsupported type"
) else if builtins.typeOf v == "list" then (
let
ret = builtins.foldl' reduceExpressionsFun {
value = true;
cond = "and";
} v;
in
acc // {
value = cond."${acc.cond}" acc.value ret.value;
}
) else throw "Unsupported type"
) else if builtins.typeOf v == "list" then (
let
ret = builtins.foldl' reduceExpressionsFun {
value = true;
cond = "and";
} v;
in
acc // {
value = cond."${acc.cond}" acc.value ret.value;
}
) else throw "Unsupported type"
);
in
(
builtins.foldl' reduceExpressionsFun {
value = true;
cond = "and";
} exprs
).value;
);
in
(
builtins.foldl' reduceExpressionsFun {
value = true;
cond = "and";
} exprs
).value;
in
e: builtins.foldl' (acc: v: v acc) e [
findSubExpressions

@ -1,26 +1,27 @@
{ lib, ireplace }:
let
inherit (builtins) elemAt match;
operators = let
matchWildCard = s: match "([^\*])(\.[\*])" s;
mkComparison = ret: version: v: builtins.compareVersions version v == ret;
mkIdxComparison = idx: version: v: let
ver = builtins.splitVersion v;
minor = builtins.toString (lib.toInt (elemAt ver idx) + 1);
upper = builtins.concatStringsSep "." (ireplace idx minor ver);
in
operators.">=" version v && operators."<" version upper;
dropWildcardPrecision = f: version: constraint: let
m = matchWildCard constraint;
hasWildcard = m != null;
c = if hasWildcard then (elemAt m 0) else constraint;
v =
if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version)
else version;
in
f v c;
mkIdxComparison = idx: version: v:
let
ver = builtins.splitVersion v;
minor = builtins.toString (lib.toInt (elemAt ver idx) + 1);
upper = builtins.concatStringsSep "." (ireplace idx minor ver);
in
operators.">=" version v && operators."<" version upper;
dropWildcardPrecision = f: version: constraint:
let
m = matchWildCard constraint;
hasWildcard = m != null;
c = if hasWildcard then (elemAt m 0) else constraint;
v =
if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version)
else version;
in
f v c;
in
{
# Prefix operators
@ -33,16 +34,17 @@ let
# Semver specific operators
"~" = mkIdxComparison 1;
"^" = mkIdxComparison 0;
"~=" = v: c: let
# Prune constraint
parts = builtins.splitVersion c;
pruned = lib.take ((builtins.length parts) - 1) parts;
upper = builtins.toString (
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
);
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
in
operators.">=" v c && operators."<" v upperConstraint;
"~=" = v: c:
let
# Prune constraint
parts = builtins.splitVersion c;
pruned = lib.take ((builtins.length parts) - 1) parts;
upper = builtins.toString (
(lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1
);
upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned);
in
operators.">=" v c && operators."<" v upperConstraint;
# Infix operators
"-" = version: v: operators.">=" version v.vl && operators."<=" version v.vu;
# Arbitrary equality clause, just run simple comparison
@ -55,33 +57,34 @@ let
version = "([0-9\.\*x]+)";
};
parseConstraint = constraint: let
constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint;
# The common prefix operators
mPre = match "${re.operators} *${re.version}" constraintStr;
# There is also an infix operator to match ranges
mIn = match "${re.version} *(-) *${re.version}" constraintStr;
in
(
if mPre != null then {
op = elemAt mPre 0;
v = elemAt mPre 1;
}
# Infix operators are range matches
else if mIn != null then {
op = elemAt mIn 1;
v = {
vl = (elemAt mIn 0);
vu = (elemAt mIn 2);
};
}
else throw "Constraint \"${constraintStr}\" could not be parsed"
);
satisfiesSemver = version: constraint: let
inherit (parseConstraint constraint) op v;
in
if constraint == "*" then true else operators."${op}" version v;
parseConstraint = constraint:
let
constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint;
# The common prefix operators
mPre = match "${re.operators} *${re.version}" constraintStr;
# There is also an infix operator to match ranges
mIn = match "${re.version} *(-) *${re.version}" constraintStr;
in
(
if mPre != null then {
op = elemAt mPre 0;
v = elemAt mPre 1;
}
# Infix operators are range matches
else if mIn != null then {
op = elemAt mIn 1;
v = {
vl = (elemAt mIn 0);
vu = (elemAt mIn 2);
};
}
else throw "Constraint \"${constraintStr}\" could not be parsed"
);
satisfiesSemver = version: constraint:
let
inherit (parseConstraint constraint) op v;
in
if constraint == "*" then true else operators."${op}" version v;
in
{ inherit satisfiesSemver; }

Loading…
Cancel
Save