diff --git a/hooks/default.nix b/hooks/default.nix index 5d56201..20f1756 100644 --- a/hooks/default.nix +++ b/hooks/default.nix @@ -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 ) { }; - - } diff --git a/hooks/pyproject-without-special-deps.py b/hooks/pyproject-without-special-deps.py index 9f79f9a..b74bfeb 100644 --- a/hooks/pyproject-without-special-deps.py +++ b/hooks/pyproject-without-special-deps.py @@ -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() diff --git a/hooks/remove-special-dependencies.sh b/hooks/remove-special-dependencies.sh index 2d37667..a20b204 100644 --- a/hooks/remove-special-dependencies.sh +++ b/hooks/remove-special-dependencies.sh @@ -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 + @pythonInterpreter@ \ + @pyprojectPatchScript@ \ + --fields-to-remove @fields@ < pyproject.toml > pyproject.formatted.toml - rm pyproject.json + mv pyproject.formatted.toml pyproject.toml echo "Finished removing @kind@ dependencies" }