forked from nix-community/NUR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
427 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# mypy | ||
.mypy_cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env nix-shell | ||
#!nix-shell -p python3 -p nix-prefetch-git -p nix -i python3 | ||
import sys | ||
import os | ||
|
||
sys.path.insert(0, os.path.join( | ||
os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) | ||
|
||
from nur import main | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import argparse | ||
import sys | ||
from typing import List | ||
|
||
from .format_manifest import format_manifest_command | ||
from .index import index_command | ||
from .update import update_command | ||
|
||
# from .build import build_channel_command | ||
|
||
|
||
def parse_arguments(argv: List[str]) -> argparse.Namespace: | ||
parser = argparse.ArgumentParser( | ||
prog=argv[0], description="nur management commands" | ||
) | ||
|
||
subparsers = parser.add_subparsers(description="subcommands") | ||
|
||
# build_channel = subparsers.add_parser("build-channel") | ||
# build_channel.set_defaults(func=build_channel_command) | ||
|
||
format_manifest = subparsers.add_parser("format-manifest") | ||
format_manifest.set_defaults(func=format_manifest_command) | ||
|
||
update = subparsers.add_parser("update") | ||
update.set_defaults(func=update_command) | ||
|
||
index = subparsers.add_parser("index") | ||
index.set_defaults(func=index_command) | ||
|
||
return parser.parse_args(argv[1:]) | ||
|
||
|
||
def main() -> None: | ||
args = parse_arguments(sys.argv) | ||
args.func(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pathlib import Path | ||
|
||
from .path import LOCK_PATH, MANIFEST_PATH | ||
|
||
|
||
def build_channel_command(_path: str): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class NurError(Exception): | ||
pass |
13 changes: 3 additions & 10 deletions
13
nur/format-manifest.py → nur/format_manifest.py
100755 → 100644
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
#!/usr/bin/env nix-shell | ||
#!nix-shell -p python3 -i python3 | ||
|
||
import json | ||
import shutil | ||
from pathlib import Path | ||
from argparse import Namespace | ||
|
||
ROOT = Path(__file__).parent.parent | ||
from .path import ROOT | ||
|
||
|
||
def main() -> None: | ||
def format_manifest_command(args: Namespace) -> None: | ||
path = ROOT.joinpath("repos.json") | ||
manifest = json.load(open(path)) | ||
tmp_path = str(path) + ".tmp" | ||
with open(tmp_path, "w+") as tmp: | ||
json.dump(manifest, tmp, indent=4, sort_keys=True) | ||
tmp.write("\n") | ||
shutil.move(tmp_path, path) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
import subprocess | ||
from argparse import Namespace | ||
from pathlib import Path | ||
from tempfile import NamedTemporaryFile | ||
from typing import Any, Dict | ||
|
||
from .path import ROOT | ||
|
||
|
||
def index_repo(repo: str, expression_file: str) -> Dict[str, Any]: | ||
fetch_source_cmd = [ | ||
"nix-build", | ||
str(ROOT), | ||
"-A", | ||
f"repo-sources.{repo}", | ||
"--no-out-link", | ||
] | ||
|
||
repo_path = subprocess.check_output(fetch_source_cmd).strip() | ||
|
||
expression_path = Path(repo_path.decode("utf-8")).joinpath(expression_file) | ||
|
||
with NamedTemporaryFile(mode="w") as f: | ||
expr = f"with import <nixpkgs> {{}}; callPackage {expression_path} {{}}" | ||
f.write(expr) | ||
f.flush() | ||
query_cmd = ["nix-env", "-qa", "*", "--json", "-f", str(f.name)] | ||
out = subprocess.check_output(query_cmd).strip() | ||
raw_pkgs = json.loads(out) | ||
pkgs = {} | ||
for name, pkg in raw_pkgs.items(): | ||
pkg["_attr"] = name | ||
pkgs[f"nur.repos.{repo}.{name}"] = pkg | ||
return pkgs | ||
|
||
|
||
def index_command(args: Namespace) -> None: | ||
manifest_path = ROOT.joinpath("repos.json") | ||
with open(manifest_path) as f: | ||
manifest = json.load(f) | ||
repos = manifest.get("repos", []) | ||
pkgs: Dict[str, Any] = {} | ||
|
||
for (repo, data) in repos.items(): | ||
pkgs.update(index_repo(repo, data.get("file", "default.nix"))) | ||
|
||
with open(ROOT.joinpath("packages.json"), "w") as f: | ||
json.dump(pkgs, f, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import json | ||
from enum import Enum, auto | ||
from pathlib import Path | ||
from typing import Dict, List, Optional, Union | ||
from urllib.parse import ParseResult, urlparse | ||
|
||
Url = ParseResult | ||
|
||
|
||
class LockedVersion: | ||
def __init__( | ||
self, url: Url, rev: str, sha256: str, submodules: bool = False | ||
) -> None: | ||
self.url = url | ||
self.rev = rev | ||
self.sha256 = sha256 | ||
self.submodules = submodules | ||
|
||
def as_json(self) -> Dict[str, Union[bool, str]]: | ||
d = dict( | ||
url=self.url.geturl(), | ||
rev=self.rev, | ||
sha256=self.sha256, | ||
) | ||
if self.submodules: | ||
d["submodules"] = self.submodules | ||
return d | ||
|
||
|
||
class RepoType(Enum): | ||
GITHUB = auto() | ||
GITLAB = auto() | ||
GIT = auto() | ||
|
||
@staticmethod | ||
def from_repo(repo: "Repo", type_: str) -> "RepoType": | ||
if repo.submodules: | ||
return RepoType.GIT | ||
if repo.url.hostname == "github.com": | ||
return RepoType.GITHUB | ||
if repo.url.hostname == "gitlab.com" or type_ == "gitlab": | ||
return RepoType.GITLAB | ||
else: | ||
return RepoType.GIT | ||
|
||
|
||
class Repo: | ||
def __init__( | ||
self, | ||
name: str, | ||
url: Url, | ||
submodules: bool, | ||
type_: str, | ||
file_: Optional[str], | ||
locked_version: Optional[LockedVersion], | ||
) -> None: | ||
self.name = name | ||
self.url = url | ||
self.submodules = submodules | ||
if file_ is None: | ||
self.file = "default.nix" | ||
else: | ||
self.file = file_ | ||
self.locked_version = None | ||
|
||
if ( | ||
locked_version is not None | ||
and locked_version.url != url.geturl() | ||
and locked_version.submodules == submodules | ||
): | ||
self.locked_version = locked_version | ||
|
||
self.type = RepoType.from_repo(self, type_) | ||
|
||
|
||
class Manifest: | ||
def __init__(self, repos: List[Repo]) -> None: | ||
self.repos = repos | ||
|
||
|
||
def _load_locked_versions(path: Path) -> Dict[str, LockedVersion]: | ||
with open(path) as f: | ||
data = json.load(f) | ||
|
||
locked_versions = {} | ||
|
||
for name, repo in data["repos"].items(): | ||
url = urlparse(repo["url"]) | ||
rev = repo["rev"] | ||
sha256 = repo["sha256"] | ||
locked_versions[name] = LockedVersion(url, rev, sha256) | ||
|
||
return locked_versions | ||
|
||
|
||
def load_locked_versions(path: Path) -> Dict[str, LockedVersion]: | ||
if path.exists(): | ||
return _load_locked_versions(path) | ||
else: | ||
return {} | ||
|
||
|
||
def load_manifest(manifest_path: Union[str, Path], lock_path) -> Manifest: | ||
locked_versions = load_locked_versions(lock_path) | ||
|
||
with open(manifest_path) as f: | ||
data = json.load(f) | ||
|
||
repos = [] | ||
for name, repo in data["repos"].items(): | ||
url = urlparse(repo["url"]) | ||
submodules = repo.get("submodules", False) | ||
file_ = repo.get("file", "default.nix") | ||
type_ = repo.get("type", None) | ||
locked_version = locked_versions.get(name) | ||
repos.append(Repo(name, url, submodules, type_, file_, locked_version)) | ||
|
||
return Manifest(repos) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import subprocess | ||
from pathlib import Path | ||
|
||
ROOT = Path(__file__).parent.parent.resolve() | ||
LOCK_PATH = ROOT.joinpath("repos.json.lock") | ||
MANIFEST_PATH = ROOT.joinpath("repos.json") | ||
EVALREPO_PATH = ROOT.joinpath("lib/evalRepo.nix") | ||
|
||
_NIXPKGS_PATH = None | ||
|
||
|
||
def nixpkgs_path() -> str: | ||
global _NIXPKGS_PATH | ||
if _NIXPKGS_PATH is not None: | ||
return _NIXPKGS_PATH | ||
cmd = ["nix-instantiate", "--find-file", "nixpkgs"] | ||
path = subprocess.check_output(cmd).decode("utf-8").strip() | ||
_NIXPKGS_PATH = str(Path(path).resolve()) | ||
|
||
return _NIXPKGS_PATH |
Oops, something went wrong.