Don't change format of pyproject.toml when removing special dependencies

This is needed because Scipy parses pyproject.toml by hand, under
the assumption of one dependency per line.
This commit is contained in:
Sem Mulder 2022-05-08 19:01:23 +02:00 committed by adisbladis
parent bee08d0554
commit 59a79de540
3 changed files with 22 additions and 28 deletions

View file

@ -3,15 +3,18 @@
, makeSetupHook
, wheel
, pip
, pkgs
}:
let
callPackage = python.pythonForBuild.pkgs.callPackage;
pythonInterpreter = python.pythonForBuild.interpreter;
pythonSitePackages = python.sitePackages;
nonOverlayedPython = pkgs.python3.pythonForBuild.withPackages (ps: [ ps.tomlkit ]);
in
{
removePathDependenciesHook = callPackage
# NOTE: We have to use a non-overlayed Python here because otherwise we run into an infinite recursion
# because building of tomlkit and its dependencies also use these hooks.
removePathDependenciesHook = nonOverlayedPython.pkgs.callPackage
(
{}:
makeSetupHook
@ -19,8 +22,7 @@ in
name = "remove-path-dependencies.sh";
deps = [ ];
substitutions = {
inherit pythonInterpreter;
yj = "${buildPackages.yj}/bin/yj";
pythonInterpreter = nonOverlayedPython.interpreter;
pyprojectPatchScript = "${./pyproject-without-special-deps.py}";
fields = [ "path" ];
kind = "path";
@ -29,15 +31,15 @@ in
)
{ };
removeGitDependenciesHook = callPackage
({}:
removeGitDependenciesHook = nonOverlayedPython.pkgs.callPackage
(
{}:
makeSetupHook
{
name = "remove-git-dependencies.sh";
deps = [ ];
substitutions = {
inherit pythonInterpreter;
yj = "${buildPackages.yj}/bin/yj";
pythonInterpreter = nonOverlayedPython.interpreter;
pyprojectPatchScript = "${./pyproject-without-special-deps.py}";
fields = [ "git" "branch" "rev" "tag" ];
kind = "git";
@ -89,6 +91,4 @@ in
} ./wheel-unpack-hook.sh
)
{ };
}

View file

@ -1,13 +1,14 @@
#!/usr/bin/env python
# Patch out special dependencies (git and path) from a pyproject.json file
# Patch out special dependencies (git and path) from a pyproject.toml file
import argparse
import json
import sys
import tomlkit
def main(input, output, fields_to_remove):
data = json.load(input)
data = tomlkit.loads(input.read())
try:
deps = data["tool"]["poetry"]["dependencies"]
@ -22,12 +23,7 @@ def main(input, output, fields_to_remove):
if any_removed:
dep["version"] = "*"
# Set ensure_ascii to False because TOML is valid UTF-8 so text that can't
# be represented in ASCII is perfectly legitimate
# HACK: Setting ensure_asscii to False breaks Python2 for some dependencies (like cachy==0.3.0)
json.dump(
data, output, separators=(",", ":"), ensure_ascii=sys.version_info.major < 3
)
output.write(tomlkit.dumps(data))
if __name__ == "__main__":
@ -37,20 +33,20 @@ if __name__ == "__main__":
"--input",
type=argparse.FileType("r"),
default=sys.stdin,
help="Location from which to read input JSON",
help="Location from which to read input TOML",
)
p.add_argument(
"-o",
"--output",
type=argparse.FileType("w"),
default=sys.stdout,
help="Location to write output JSON",
help="Location to write output TOML",
)
p.add_argument(
"-f",
"--fields-to-remove",
nargs="+",
help="The fields to remove from the dependency's JSON",
help="The fields to remove from the dependency's TOML",
)
args = p.parse_args()

View file

@ -6,13 +6,11 @@ remove-@kind@-dependencies-hook() {
echo "Removing @kind@ dependencies"
# Tell poetry not to resolve special dependencies. Any version is fine!
@yj@ -tj < pyproject.toml | \
@pythonInterpreter@ \
@pyprojectPatchScript@ \
--fields-to-remove @fields@ > pyproject.json
@yj@ -jt < pyproject.json > pyproject.toml
--fields-to-remove @fields@ < pyproject.toml > pyproject.formatted.toml
rm pyproject.json
mv pyproject.formatted.toml pyproject.toml
echo "Finished removing @kind@ dependencies"
}