Merge pull request #313 from crazazy/master

This commit is contained in:
Jörg Thalheim 2021-01-13 14:07:04 +00:00 committed by GitHub
commit a2fdc91ff1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,56 @@ from tempfile import NamedTemporaryFile
from typing import Any, Dict
def index_repo(directory: Path, repo: str, expression_file: str) -> Dict[str, Any]:
def resolve_source(pkg: Dict, repo: str, url: str) -> str:
# TODO commit hash
prefix = f"https://github.com/nix-community/nur-combined/tree/master/repos/{repo}"
position = pkg["meta"].get("position", None)
if position is not None and position.startswith("/nix/store"):
path_str, line = position.rsplit(":", 1)
path = Path(path_str)
# I've decided to just take these 2 repositories,
# update this whenever someone decided to use a recipe source other than
# NUR or nixpkgs to override packages on. right now this is about as accurate as
# `nix edit` is
# TODO find commit hash
prefixes = {
"nixpkgs": "https://github.com/nixos/nixpkgs/tree/master/",
"nur": "https://github.com/nix-community/nur-combined/tree/master/",
}
stripped = path.parts[4:]
if path.parts[3].endswith("source"):
canonical_url = url
# if we want to add the option of specifying branches, we have to update this
if "github" in url:
canonical_url += "/blob/HEAD/"
elif "gitlab" in url:
canonical_url += "/-/blob/HEAD/"
attr_path = "/".join(stripped)
location = f"{canonical_url}{attr_path}"
return f"{location}#L{line}"
elif stripped[0] not in prefixes:
print(path, file=sys.stderr)
print(
f"we could not find {stripped} , you can file an issue at https://github.com/nix-community/NUR/issues to the indexing file if you think this is a mistake",
file=sys.stderr,
)
return prefix
else:
attr_path = "/".join(stripped[1:])
location = f"{prefixes[stripped[0]]}{attr_path}"
return f"{location}#L{line}"
elif position is not None and "nur-combined" in position:
path_str, line = position.rsplit(":", 1)
stripped = path_str.partition(f"nur-combined/repos/{repo}")[2]
return f"{prefix}{stripped}#L{line}"
else:
return prefix
def index_repo(
directory: Path, repo: str, expression_file: str, url: str
) -> Dict[str, Any]:
default_nix = directory.joinpath("default.nix").resolve()
expr = """
with import <nixpkgs> {};
@ -36,17 +85,9 @@ callPackage (nur.repo-sources."%s" + "/%s") {}
for name, pkg in raw_pkgs.items():
pkg["_attr"] = name
pkg["_repo"] = repo
position = pkg["meta"].get("position", None)
# TODO commit hash
prefix = f"https://github.com/nix-community/nur-combined/tree/master/repos/{repo}"
if position is not None and position.startswith("/nix/store"):
path_str, line = position.rsplit(":", 1)
path = Path(path_str)
stripped = path.parts[4:]
pkg["meta"]["position"] = f"{prefix}{stripped}#L{line}"
else:
pkg["meta"]["position"] = prefix
pkg["meta"]["position"] = resolve_source(pkg, repo, url)
pkgs[f"nur.repos.{repo}.{name}"] = pkg
return pkgs
@ -59,7 +100,12 @@ def index_command(args: Namespace) -> None:
pkgs: Dict[str, Any] = {}
for (repo, data) in repos.items():
repo_pkgs = index_repo(directory, repo, data.get("file", "default.nix"))
repo_pkgs = index_repo(
directory,
repo,
data.get("file", "default.nix"),
data.get("url", "https://github.com/nixos/nixpkgs"),
)
pkgs.update(repo_pkgs)
json.dump(pkgs, sys.stdout, indent=4)