add fetchCrate support

This commit is contained in:
figsoda 2022-11-14 09:34:46 -05:00
parent dbb398b08f
commit 725c510332
6 changed files with 63 additions and 0 deletions

View File

@ -6,6 +6,7 @@ designed to work with nixpkgs but also other package sets.
## Features
- automatically figure out the latest version of packages from:
- crates.io
- github.com
- gitlab.com or other instances that uses fetchFromGitLab
- pypi

View File

@ -3,6 +3,7 @@ from typing import List, Callable, Optional
import re
from ..errors import VersionError
from .crate import fetch_crate_versions
from .github import fetch_github_versions
from .gitlab import fetch_gitlab_versions
from .pypi import fetch_pypi_versions
@ -21,6 +22,7 @@ from .version import VersionPreference, Version
# return None
fetchers: List[Callable[[ParseResult], List[Version]]] = [
fetch_crate_versions,
fetch_pypi_versions,
fetch_github_versions,
fetch_gitlab_versions,

View File

@ -0,0 +1,19 @@
import json
import urllib.request
from typing import List
from urllib.parse import ParseResult
from ..utils import info
from .version import Version
def fetch_crate_versions(url: ParseResult) -> List[Version]:
if url.netloc != "crates.io":
return []
parts = url.path.split("/")
package = parts[4]
crate_url = f"https://crates.io/api/v1/crates/{package}/versions"
info(f"fetch {crate_url}")
resp = urllib.request.urlopen(crate_url)
data = json.loads(resp.read())
return [Version(version["num"]) for version in data["versions"]]

27
tests/test_crate.py Normal file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
from nix_update.options import Options
from nix_update.update import update
import subprocess
import conftest
def test_update(helpers: conftest.Helpers) -> None:
with helpers.testpkgs() as path:
opts = Options(attribute="crate", import_path=str(path))
update(opts)
version = subprocess.run(
[
"nix",
"eval",
"--raw",
"--extra-experimental-features",
"nix-command",
"-f",
path,
"crate.version",
],
text=True,
stdout=subprocess.PIPE,
)
assert version.stdout.strip() >= "8.5.2"

13
tests/testpkgs/crate.nix Normal file
View File

@ -0,0 +1,13 @@
{ rustPlatform, fetchCrate }:
rustPlatform.buildRustPackage rec {
pname = "fd-find";
version = "8.0.0";
src = fetchCrate {
inherit pname version;
sha256 = "";
};
cargoSha256 = "";
}

View File

@ -1,5 +1,6 @@
{ pkgs ? import <nixpkgs> {} }:
{
crate = pkgs.callPackage ./crate.nix {};
pypi = pkgs.python3.pkgs.callPackage ./pypi.nix {};
sourcehut = pkgs.python3.pkgs.callPackage ./sourcehut.nix {};
savanna = pkgs.python3.pkgs.callPackage ./savanna.nix {};