mirror of
https://github.com/vale981/poetry2nix
synced 2025-03-04 08:41:42 -05:00
support url source types
This commit is contained in:
parent
689373cef7
commit
38704165d4
2 changed files with 105 additions and 55 deletions
139
bin/poetry2nix
139
bin/poetry2nix
|
@ -7,35 +7,84 @@ import toml
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from typing import Dict, Any, Tuple
|
from typing import Dict, Any, Tuple, List
|
||||||
|
|
||||||
|
|
||||||
def fetch_git(pkg: Dict[str, Any]) -> Tuple[str, subprocess.CompletedProcess]:
|
class Package:
|
||||||
try:
|
def __init__(self, attrs: Dict[str, Any]) -> None:
|
||||||
reference = pkg["source"]["resolved_reference"]
|
self.attrs = attrs
|
||||||
except KeyError:
|
self.name = attrs["name"]
|
||||||
reference = pkg["source"]["reference"]
|
self.source = self.attrs["source"]
|
||||||
|
|
||||||
return (
|
def fetch(self) -> Tuple["Package", subprocess.CompletedProcess]:
|
||||||
pkg["name"],
|
raise NotImplementedError()
|
||||||
subprocess.run(
|
|
||||||
[
|
def expression(self, output: str) -> str:
|
||||||
"nix-prefetch-git",
|
raise NotImplementedError()
|
||||||
"--fetch-submodules",
|
|
||||||
"--url",
|
|
||||||
pkg["source"]["url"],
|
|
||||||
"--rev",
|
|
||||||
reference,
|
|
||||||
],
|
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def indent(expr: str, spaces: int = 2) -> str:
|
class UrlPackage(Package):
|
||||||
i = " " * spaces
|
def fetch(self) -> Tuple[Package, subprocess.CompletedProcess]:
|
||||||
return "\n".join([(i if l != "" else "") + l for l in expr.split("\n")])
|
return (
|
||||||
|
self,
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
"nix-prefetch-url",
|
||||||
|
"--unpack",
|
||||||
|
self.source["url"],
|
||||||
|
],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def expression(self, output: str) -> str:
|
||||||
|
sha256 = output.rstrip()
|
||||||
|
return textwrap.dedent("""
|
||||||
|
%s = super.%s.overridePythonAttrs (
|
||||||
|
_: {
|
||||||
|
src = pkgs.fetchzip {
|
||||||
|
url = "%s";
|
||||||
|
sha256 = "%s";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);""" % (self.name, self.name, self.source["url"], sha256))
|
||||||
|
|
||||||
|
|
||||||
|
class GitPackage(Package):
|
||||||
|
def fetch(self) -> Tuple[Package, subprocess.CompletedProcess]:
|
||||||
|
reference = self.source.get("resolved_reference", self.source["reference"])
|
||||||
|
|
||||||
|
return (
|
||||||
|
self,
|
||||||
|
subprocess.run(
|
||||||
|
[
|
||||||
|
"nix-prefetch-git",
|
||||||
|
"--fetch-submodules",
|
||||||
|
"--url",
|
||||||
|
self.source["url"],
|
||||||
|
"--rev",
|
||||||
|
reference,
|
||||||
|
],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
def expression(self, output: str) -> str:
|
||||||
|
meta = json.loads(output)
|
||||||
|
return textwrap.dedent("""
|
||||||
|
%s = super.%s.overridePythonAttrs (
|
||||||
|
_: {
|
||||||
|
src = pkgs.fetchgit {
|
||||||
|
url = "%s";
|
||||||
|
rev = "%s";
|
||||||
|
sha256 = "%s";
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);""" % (self.name, self.name, meta["url"], meta["rev"], meta["sha256"]))
|
||||||
|
|
||||||
|
|
||||||
def parse_args() -> argparse.Namespace:
|
def parse_args() -> argparse.Namespace:
|
||||||
|
@ -54,22 +103,31 @@ def parse_args() -> argparse.Namespace:
|
||||||
return argparser.parse_args()
|
return argparser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
def indent(expr: str, spaces: int = 2) -> str:
|
||||||
|
i = " " * spaces
|
||||||
|
return "\n".join([(i if l != "" else "") + l for l in expr.split("\n")])
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
|
||||||
with open(args.lock) as lockf:
|
with open(args.lock) as lockf:
|
||||||
lock = toml.load(lockf)
|
lock = toml.load(lockf)
|
||||||
|
|
||||||
pkgs = []
|
pkgs: List[Package] = []
|
||||||
for pkg in lock["package"]:
|
for pkg in lock["package"]:
|
||||||
if "source" in pkg:
|
if "source" in pkg:
|
||||||
pkgs.append(pkg)
|
source_type = pkg["source"]["type"]
|
||||||
|
if source_type == "git":
|
||||||
|
pkgs.append(GitPackage(pkg))
|
||||||
|
elif source_type == "url":
|
||||||
|
pkgs.append(UrlPackage(pkg))
|
||||||
|
|
||||||
with ThreadPoolExecutor() as e:
|
with ThreadPoolExecutor() as e:
|
||||||
futures = []
|
futures = []
|
||||||
|
|
||||||
for pkg in pkgs:
|
for pkg in pkgs:
|
||||||
futures.append(e.submit(fetch_git, pkg))
|
futures.append(e.submit(pkg.fetch))
|
||||||
|
|
||||||
lines = [
|
lines = [
|
||||||
"{ pkgs }:",
|
"{ pkgs }:",
|
||||||
|
@ -77,30 +135,13 @@ def main() -> None:
|
||||||
]
|
]
|
||||||
|
|
||||||
for f in futures:
|
for f in futures:
|
||||||
drv_name, p = f.result()
|
package, p = f.result()
|
||||||
if p.returncode != 0:
|
if p.returncode != 0:
|
||||||
sys.stderr.buffer.write(p.stderr)
|
sys.stderr.write(p.stderr)
|
||||||
sys.stderr.buffer.flush()
|
sys.stderr.flush()
|
||||||
exit(p.returncode)
|
exit(p.returncode)
|
||||||
|
expr = package.expression(p.stdout)
|
||||||
meta = json.loads(p.stdout.decode())
|
lines.append(indent(expr))
|
||||||
lines.append(
|
|
||||||
indent(
|
|
||||||
textwrap.dedent(
|
|
||||||
"""
|
|
||||||
%s = super.%s.overridePythonAttrs (
|
|
||||||
_: {
|
|
||||||
src = pkgs.fetchgit {
|
|
||||||
url = "%s";
|
|
||||||
rev = "%s";
|
|
||||||
sha256 = "%s";
|
|
||||||
};
|
|
||||||
}
|
|
||||||
);"""
|
|
||||||
% (drv_name, drv_name, meta["url"], meta["rev"], meta["sha256"])
|
|
||||||
)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
lines.extend(["", "}", ""])
|
lines.extend(["", "}", ""])
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,7 @@ pythonPackages.callPackage
|
||||||
toPath = s: pwd + "/${s}";
|
toPath = s: pwd + "/${s}";
|
||||||
isSource = source != null;
|
isSource = source != null;
|
||||||
isGit = isSource && source.type == "git";
|
isGit = isSource && source.type == "git";
|
||||||
|
isUrl = isSource && source.type == "url";
|
||||||
isLocal = isSource && source.type == "directory";
|
isLocal = isSource && source.type == "directory";
|
||||||
localDepPath = toPath source.url;
|
localDepPath = toPath source.url;
|
||||||
|
|
||||||
|
@ -95,7 +96,7 @@ pythonPackages.callPackage
|
||||||
lib.optional (! lib.elem name skipSetupToolsSCM) pythonPackages.setuptools-scm
|
lib.optional (! lib.elem name skipSetupToolsSCM) pythonPackages.setuptools-scm
|
||||||
++ lib.optional (! lib.elem name [ "pip" "setuptools" "wheel" ]) pythonPackages.wheel
|
++ lib.optional (! lib.elem name [ "pip" "setuptools" "wheel" ]) pythonPackages.wheel
|
||||||
;
|
;
|
||||||
format = if isLocal then "pyproject" else if isGit then "pyproject" else fileInfo.format;
|
format = if isLocal || isGit || isUrl then "pyproject" else fileInfo.format;
|
||||||
in
|
in
|
||||||
buildPythonPackage {
|
buildPythonPackage {
|
||||||
pname = moduleName name;
|
pname = moduleName name;
|
||||||
|
@ -165,11 +166,19 @@ pythonPackages.callPackage
|
||||||
rev = source.resolved_reference or source.reference;
|
rev = source.resolved_reference or source.reference;
|
||||||
ref = sourceSpec.branch or sourceSpec.rev or sourceSpec.tag or "HEAD";
|
ref = sourceSpec.branch or sourceSpec.rev or sourceSpec.tag or "HEAD";
|
||||||
}
|
}
|
||||||
) else if isLocal then (poetryLib.cleanPythonSources { src = localDepPath; }) else
|
)
|
||||||
fetchFromPypi {
|
else if isUrl then
|
||||||
pname = name;
|
builtins.fetchTarball
|
||||||
inherit (fileInfo) file hash kind;
|
{
|
||||||
};
|
inherit (source) url;
|
||||||
|
}
|
||||||
|
else if isLocal then
|
||||||
|
(poetryLib.cleanPythonSources { src = localDepPath; })
|
||||||
|
else
|
||||||
|
fetchFromPypi {
|
||||||
|
pname = name;
|
||||||
|
inherit (fileInfo) file hash kind;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
{ }
|
{ }
|
||||||
|
|
Loading…
Add table
Reference in a new issue