forked from fluidattacks/makes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(back): fluidattacks#1146 make python pyproject
- add make-python-pyproject buiding from nixpkgs functions - add python-override-utils - add docs and example Signed-off-by: Daniel F. Murcia Rivera <danmur97@outlook.com>
- Loading branch information
Showing
11 changed files
with
279 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
buildEnv, | ||
buildPythonPackage, | ||
pkgDeps, | ||
src, | ||
}: | ||
import ./generic_builder { | ||
inherit buildEnv buildPythonPackage pkgDeps src; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/args/make-python-pyproject-package/generic_builder/check.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{pkg}: let | ||
build_check = check: | ||
pkg.overridePythonAttrs ( | ||
old: { | ||
checkPhase = [old."${check}"]; | ||
} | ||
); | ||
in { | ||
tests = build_check "test_check"; | ||
types = build_check "type_check"; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/args/make-python-pyproject-package/generic_builder/default.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
buildEnv, | ||
buildPythonPackage, | ||
pkgDeps, | ||
src, | ||
}: let | ||
metadata = import ./metadata.nix src; | ||
pkg = import ./pkg { | ||
inherit buildPythonPackage metadata pkgDeps src; | ||
}; | ||
env = import ./env.nix { | ||
inherit buildEnv pkgDeps pkg; | ||
}; | ||
checks = import ./check.nix {inherit pkg;}; | ||
in { | ||
inherit pkg env; | ||
check = checks; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/args/make-python-pyproject-package/generic_builder/env.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
buildEnv, | ||
pkgDeps, | ||
pkg, | ||
}: let | ||
build_env = extraLibs: | ||
buildEnv { | ||
inherit extraLibs; | ||
ignoreCollisions = false; | ||
}; | ||
in { | ||
runtime = build_env [pkg]; | ||
dev = build_env (pkgDeps.runtime_deps ++ pkgDeps.test_deps); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/args/make-python-pyproject-package/generic_builder/metadata.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
src: let | ||
_metadata = (builtins.fromTOML (builtins.readFile "${src}/pyproject.toml")).project; | ||
file_str = builtins.readFile "${src}/${_metadata.name}/__init__.py"; | ||
match = builtins.match ".*__version__ *= *\"(.+)\"\n.*" file_str; | ||
version = builtins.elemAt match 0; | ||
in | ||
_metadata // {inherit version;} |
6 changes: 6 additions & 0 deletions
6
src/args/make-python-pyproject-package/generic_builder/pkg/check/tests.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# shellcheck shell=bash | ||
|
||
echo "Executing test phase" \ | ||
&& pytest --version \ | ||
&& pytest ./tests \ | ||
&& echo "Finished test phase" |
6 changes: 6 additions & 0 deletions
6
src/args/make-python-pyproject-package/generic_builder/pkg/check/types.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# shellcheck shell=bash | ||
|
||
echo "Executing type check phase" \ | ||
&& mypy --version \ | ||
&& mypy . --config-file ./mypy.ini \ | ||
&& echo "Finished type check phase" |
26 changes: 26 additions & 0 deletions
26
src/args/make-python-pyproject-package/generic_builder/pkg/default.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
buildPythonPackage, | ||
metadata, | ||
pkgDeps, | ||
src, | ||
}: let | ||
type_check = ./check/types.sh; | ||
test_check = ./check/tests.sh; | ||
in | ||
buildPythonPackage { | ||
inherit src type_check test_check; | ||
inherit (metadata) version; | ||
pname = metadata.name; | ||
format = "pyproject"; | ||
checkPhase = [ | ||
'' | ||
source ${type_check} \ | ||
&& source ${test_check} \ | ||
'' | ||
]; | ||
doCheck = true; | ||
pythonImportsCheck = [metadata.name]; | ||
nativeBuildInputs = pkgDeps.build_deps; | ||
propagatedBuildInputs = pkgDeps.runtime_deps; | ||
nativeCheckInputs = pkgDeps.test_deps; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# python override utils, | ||
# useful when overriding pkgs from an environment to ensure no collisions | ||
let | ||
recursive_python_pkg_override = is_pkg: override: let | ||
# is_pkg: Derivation -> Bool | ||
# override: Derivation -> Derivation | ||
self = recursive_python_pkg_override is_pkg override; | ||
in | ||
pkg: | ||
if is_pkg pkg | ||
then override pkg | ||
else if pkg ? overridePythonAttrs && pkg ? pname | ||
then | ||
pkg.overridePythonAttrs ( | ||
builtins.mapAttrs (_: value: | ||
if builtins.isList value | ||
then map self value | ||
else self value) | ||
) | ||
else pkg; | ||
|
||
# no_check_override: Derivation -> Derivation | ||
no_check_override = recursive_python_pkg_override (pkg: pkg ? overridePythonAttrs && pkg ? pname) ( | ||
pkg: | ||
pkg.overridePythonAttrs ( | ||
old: | ||
( | ||
builtins.mapAttrs (_: value: | ||
if builtins.isList value | ||
then map no_check_override value | ||
else no_check_override value) | ||
old | ||
) | ||
// { | ||
doCheck = false; | ||
} | ||
) | ||
); | ||
|
||
# replace_pkg: List[str] -> Derivation -> Derivation | ||
replace_pkg = names: new_pkg: | ||
recursive_python_pkg_override ( | ||
x: x ? overridePythonAttrs && x ? pname && builtins.elem x.pname names | ||
) (_: new_pkg); | ||
in { | ||
inherit recursive_python_pkg_override no_check_override replace_pkg; | ||
compose = functions: val: builtins.foldl' (x: f: f x) val functions; | ||
} |