automatically detect name and version from source

This commit is contained in:
DavHau 2020-08-25 10:11:08 +07:00
parent b230865660
commit 76238f2825
4 changed files with 62 additions and 24 deletions

View file

@ -4,6 +4,35 @@ let
python_deps = (builtins.attrValues (import ./mach_nix/nix/python-deps.nix { inherit python; fetchurl = pkgs.fetchurl; }));
mergeOverrides = with pkgs.lib; foldr composeExtensions (self: super: { });
autoPatchelfHook = import ./mach_nix/nix/auto_patchelf_hook.nix {inherit (pkgs) fetchurl makeSetupHook writeText;};
extractMeta = python: src: extras:
with builtins;
let
file = (builtins.readFile "${(import ./lib/extractor).extract_from_src {
py = python;
src = src;
}}/python.json");
data = fromJSON file;
setup_requires = if hasAttr "setup_requires" data then data.setup_requires else [];
install_requires = if hasAttr "install_requires" data then data.install_requires else [];
name = if hasAttr "name" data then data.name else null;
version = if hasAttr "version" data then data.version else null;
extras_require =
if hasAttr "install_requires" data then
pkgs.lib.flatten (map (extra: data.extras_require."${extra}") extras)
else [];
concat = s1: s2: s1 + "\n" + s2;
all_reqs = builtins.foldl' concat "" (setup_requires ++ install_requires ++ extras_require);
in
{
reqs = trace "${all_reqs}" all_reqs;
inherit name version;
};
is_http_url = url:
with builtins;
if (substring 0 8 url) == "https://" || (substring 0 7 url) == "http://" then true else false;
get_src = src:
with builtins;
if isString src && is_http_url src then (fetchTarball src) else src;
in
rec {
# the mach-nix cmdline tool derivation
@ -42,10 +71,13 @@ rec {
mkPythonShell = args: (mkPython args).env;
# equivalent to buildPythonPackage of nixpkgs
buildPythonPackage = _buildPython "buildPythonPackage";
buildPythonPackage = __buildPython "buildPythonPackage";
# equivalent to buildPythonApplication of nixpkgs
buildPythonApplication = _buildPython "buildPythonApplication";
buildPythonApplication = __buildPython "buildPythonApplication";
__buildPython = func: args:
if builtins.isString args then _buildPython func { src = args; } else _buildPython func args;
_buildPython = func: args@{
requirements ? null, # content from a requirements.txt file
@ -62,33 +94,29 @@ rec {
_provider_defaults ? with builtins; fromTOML (readFile ./mach_nix/provider_defaults.toml),
...
}:
with builtins;
let
# Extract dependencies automatically if requirements is unset
src = get_src pass_args.src;
# Extract dependencies automatically if 'requirements' is unset
meta = extractMeta python src extras;
reqs =
with builtins;
if requirements == null then
if builtins.hasAttr "format" args && args.format != "setuptools" then
throw "Automatic dependency extraction is only available for 'setuptools' format."
" Please specify requirements="
" Please specify 'requirements' if setuptools is not used."
else
let
file = (builtins.readFile "${(import ./lib/extractor).extract_from_src {
py = python;
src = args.src;
}}/python.json");
data = fromJSON file;
setup_requires = if hasAttr "setup_requires" data then data.setup_requires else [];
install_requires = if hasAttr "install_requires" data then data.install_requires else [];
extras_require =
if hasAttr "install_requires" data then
nixpkgs.lib.flatten (map (extra: data.extras_require."${extra}") extras)
else [];
concat = s1: s2: s1 + "\n" + s2;
all_reqs = builtins.foldl' concat "" (setup_requires ++ install_requires ++ extras_require);
in
trace "${all_reqs}\n${if doCheck then "check: true" else "check: false"}" all_reqs
meta.reqs
else
requirements;
pname =
if hasAttr "name" args then null
else if hasAttr "pname" args then args.pname
else meta.name;
version =
if hasAttr "name" args then null
else if hasAttr "version" args then args.version
else meta.version;
py = python.override { packageOverrides = mergeOverrides overrides_pre; };
result = machNix {
inherit disable_checks providers pypi_deps_db_commit pypi_deps_db_sha256 _provider_defaults;
@ -106,7 +134,8 @@ rec {
in
py_final.pkgs."${func}" ( pass_args // {
propagatedBuildInputs = result.select_pkgs py_final.pkgs;
inherit doCheck;
src = src;
inherit doCheck pname version;
});

View file

@ -2,7 +2,7 @@ diff --git a/Lib/distutils/core.py b/Lib/distutils/core.py
index d603d4a45a..a589477b8e 100644
--- a/Lib/distutils/core.py
+++ b/Lib/distutils/core.py
@@ -120,6 +120,50 @@ def setup (**attrs):
@@ -120,6 +120,59 @@ def setup (**attrs):
# the setup script, but be overridden by the command line.
dist.parse_config_files()
@ -31,12 +31,21 @@ index d603d4a45a..a589477b8e 100644
+ 'tests_require',
+ 'python_requires'
+ )
+ meta_keys = (
+ 'name',
+ 'version'
+ )
+ data = {}
+ for key in keys:
+ val = getattr(dist, key, None)
+ if not val:
+ continue
+ data[key] = jsonify(val)
+ for key in meta_keys:
+ val = getattr(dist.metadata, key, None)
+ if not val:
+ continue
+ data[key] = jsonify(val)
+ return data
+ if os.environ.get("dump_setup_attrs", None):
+ import json

View file

@ -1 +1 @@
a92c11b68cba9d33f6c08d9a79f1bc7ca3956720
c6c31fb218f67930f3d9200e67a2bff441e38788

View file

@ -1 +1 @@
037lbxyr8ynm6n7lgk77j2x2v2jfhhy55wvg4amjpw9dsf14jzfi
1vfmmxy1c5vy8vym29zfx4xyg79xshrw4svi5kaz8ji0093pjgqg