2019-12-13 16:56:51 +01:00
|
|
|
{ lib, pkgs }:
|
|
|
|
let
|
2020-01-09 12:42:56 +00:00
|
|
|
inherit (import ./semver.nix { inherit lib ireplace; }) satisfiesSemver;
|
|
|
|
inherit (builtins) genList length;
|
|
|
|
|
|
|
|
# Replace a list entry at defined index with set value
|
|
|
|
ireplace = idx: value: list: (
|
|
|
|
genList (i: if i == idx then value else (builtins.elemAt list i)) (length list)
|
|
|
|
);
|
2019-12-13 16:56:51 +01:00
|
|
|
|
2020-04-15 10:22:26 +01:00
|
|
|
# Do some canonicalisation of module names
|
|
|
|
moduleName = name: lib.toLower (lib.replaceStrings [ "_" "." ] [ "-" "-" ] name);
|
|
|
|
|
2020-02-23 17:16:47 +00:00
|
|
|
# Compare a semver expression with a version
|
2020-03-14 23:13:51 +00:00
|
|
|
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;
|
2019-12-13 16:56:51 +01:00
|
|
|
|
2020-01-12 22:18:49 +00:00
|
|
|
fromTOML = builtins.fromTOML or
|
2020-01-13 00:24:05 +00:00
|
|
|
(
|
|
|
|
toml: builtins.fromJSON (
|
|
|
|
builtins.readFile (
|
|
|
|
pkgs.runCommand "from-toml"
|
|
|
|
{
|
|
|
|
inherit toml;
|
|
|
|
allowSubstitutes = false;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
}
|
|
|
|
''
|
|
|
|
${pkgs.remarshal}/bin/remarshal \
|
|
|
|
-if toml \
|
|
|
|
-i <(echo "$toml") \
|
|
|
|
-of json \
|
|
|
|
-o $out
|
|
|
|
''
|
|
|
|
)
|
2020-01-11 23:33:40 +00:00
|
|
|
)
|
2020-01-13 00:24:05 +00:00
|
|
|
);
|
2020-01-11 23:33:40 +00:00
|
|
|
readTOML = path: fromTOML (builtins.readFile path);
|
2019-12-13 16:56:51 +01:00
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the appropriate manylinux dependencies and string representation for the file specified
|
|
|
|
#
|
|
|
|
getManyLinuxDeps = f:
|
|
|
|
let
|
|
|
|
ml = pkgs.pythonManylinuxPackages;
|
|
|
|
in
|
|
|
|
if lib.strings.hasInfix "manylinux1" f then { pkg = [ ml.manylinux1 ]; str = "1"; }
|
|
|
|
else if lib.strings.hasInfix "manylinux2010" f then { pkg = [ ml.manylinux2010 ]; str = "2010"; }
|
|
|
|
else if lib.strings.hasInfix "manylinux2014" f then { pkg = [ ml.manylinux2014 ]; str = "2014"; }
|
|
|
|
else { pkg = []; str = null; };
|
|
|
|
|
|
|
|
# Fetch the artifacts from the PyPI index. Since we get all
|
|
|
|
# info we need from the lock file we don't use nixpkgs' fetchPyPi
|
|
|
|
# as it modifies casing while not providing anything we don't already
|
|
|
|
# have.
|
|
|
|
#
|
|
|
|
# Args:
|
|
|
|
# pname: package name
|
|
|
|
# file: filename including extension
|
|
|
|
# hash: SRI hash
|
|
|
|
# kind: Language implementation and version tag https://www.python.org/dev/peps/pep-0427/#file-name-convention
|
|
|
|
fetchFromPypi = lib.makeOverridable (
|
|
|
|
{ pname, file, hash, kind }:
|
|
|
|
pkgs.fetchurl {
|
|
|
|
url = "https://files.pythonhosted.org/packages/${kind}/${lib.toLower (builtins.substring 0 1 file)}/${pname}/${file}";
|
|
|
|
inherit hash;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-29 12:51:22 +00:00
|
|
|
getBuildSystemPkgs =
|
|
|
|
{ pythonPackages
|
|
|
|
, pyProject
|
2020-03-14 23:13:51 +00:00
|
|
|
}:
|
2020-04-14 13:07:15 +01:00
|
|
|
let
|
2020-04-14 11:42:16 +01:00
|
|
|
buildSystem = lib.attrByPath [ "build-system" "build-backend" ] "" pyProject;
|
2020-04-15 10:22:26 +01:00
|
|
|
drvAttr = moduleName (builtins.elemAt (builtins.split "\\.|:" buildSystem) 0);
|
2020-03-14 23:13:51 +00:00
|
|
|
in
|
|
|
|
if buildSystem == "" then [] else (
|
|
|
|
[ pythonPackages.${drvAttr} or (throw "unsupported build system ${buildSystem}") ]
|
|
|
|
);
|
2019-12-29 12:51:22 +00:00
|
|
|
|
2020-02-29 21:19:09 +00:00
|
|
|
# Find gitignore files recursively in parent directory stopping with .git
|
2020-03-14 23:13:51 +00:00
|
|
|
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;
|
2020-02-29 21:19:09 +00:00
|
|
|
|
2020-02-26 18:18:56 +00:00
|
|
|
/*
|
|
|
|
Provides a source filtering mechanism that:
|
|
|
|
|
2020-02-29 21:19:09 +00:00
|
|
|
- Filters gitignore's
|
2020-02-26 18:18:56 +00:00
|
|
|
- Filters pycache/pyc files
|
|
|
|
- Uses cleanSourceFilter to filter out .git/.hg, .o/.so, editor backup files & nix result symlinks
|
|
|
|
*/
|
2020-03-14 23:13:51 +00:00
|
|
|
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;
|
|
|
|
};
|
2020-02-26 18:18:56 +00:00
|
|
|
};
|
2019-12-13 16:56:51 +01:00
|
|
|
in
|
|
|
|
{
|
|
|
|
inherit
|
|
|
|
fetchFromPypi
|
|
|
|
getManyLinuxDeps
|
|
|
|
isCompatible
|
|
|
|
readTOML
|
2019-12-29 12:51:22 +00:00
|
|
|
getBuildSystemPkgs
|
2020-02-23 17:16:47 +00:00
|
|
|
satisfiesSemver
|
2020-02-26 18:18:56 +00:00
|
|
|
cleanPythonSources
|
2020-04-15 10:22:26 +01:00
|
|
|
moduleName
|
2019-12-16 14:51:45 +01:00
|
|
|
;
|
2019-12-13 16:56:51 +01:00
|
|
|
}
|