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

View file

@ -1,13 +1,14 @@
#!/usr/bin/env python #!/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 argparse
import json
import sys import sys
import tomlkit
def main(input, output, fields_to_remove): def main(input, output, fields_to_remove):
data = json.load(input) data = tomlkit.loads(input.read())
try: try:
deps = data["tool"]["poetry"]["dependencies"] deps = data["tool"]["poetry"]["dependencies"]
@ -22,12 +23,7 @@ def main(input, output, fields_to_remove):
if any_removed: if any_removed:
dep["version"] = "*" dep["version"] = "*"
# Set ensure_ascii to False because TOML is valid UTF-8 so text that can't output.write(tomlkit.dumps(data))
# 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
)
if __name__ == "__main__": if __name__ == "__main__":
@ -37,20 +33,20 @@ if __name__ == "__main__":
"--input", "--input",
type=argparse.FileType("r"), type=argparse.FileType("r"),
default=sys.stdin, default=sys.stdin,
help="Location from which to read input JSON", help="Location from which to read input TOML",
) )
p.add_argument( p.add_argument(
"-o", "-o",
"--output", "--output",
type=argparse.FileType("w"), type=argparse.FileType("w"),
default=sys.stdout, default=sys.stdout,
help="Location to write output JSON", help="Location to write output TOML",
) )
p.add_argument( p.add_argument(
"-f", "-f",
"--fields-to-remove", "--fields-to-remove",
nargs="+", 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() args = p.parse_args()

View file

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