eval: Use cargo lock class to distinguish different cases instead of tristate bool|Optional|str

This commit is contained in:
Jörg Thalheim 2023-04-03 09:26:16 +02:00
parent 927f47a36e
commit 01408b8b13
3 changed files with 49 additions and 18 deletions

View File

@ -4,7 +4,7 @@ import sys
import tempfile
from typing import NoReturn, Optional
from .eval import Package, eval_attr
from .eval import Package, eval_attr, CargoLockInSource
from .options import Options
from .update import update
from .utils import run
@ -157,8 +157,8 @@ def git_commit(git_dir: str, package: Package) -> None:
msg = format_commit_message(package)
new_version = package.new_version
cmd = ["git", "-C", git_dir, "add", package.filename]
if package.cargo_lock:
cmd.append(package.cargo_lock)
if isinstance(package.cargo_lock, CargoLockInSource):
cmd.append(package.cargo_lock.path)
run(cmd, stdout=None)
if new_version and package.old_version != new_version.number:
run(

View File

@ -2,7 +2,7 @@ import json
import os
from dataclasses import InitVar, dataclass, field
from textwrap import dedent, indent
from typing import Any, Dict, List, Literal, Optional
from typing import Any, Dict, List, Literal, Optional, Union
from urllib.parse import ParseResult, urlparse
from .errors import UpdateError
@ -18,9 +18,27 @@ class Position:
column: int
class CargoLock:
pass
class NoCargoLock(CargoLock):
pass
class CargoLockInSource(CargoLock):
def __init__(self, path: str) -> None:
self.path = path
class CargoLockInStore(CargoLock):
pass
@dataclass
class Package:
attribute: str
import_path: InitVar[str]
name: str
old_version: str
filename: str
@ -34,19 +52,25 @@ class Package:
vendor_hash: Optional[str]
vendor_sha256: Optional[str]
cargo_deps: Optional[str]
cargo_lock: Optional[str | Literal[False]]
npm_deps: Optional[str]
tests: List[str]
has_update_script: bool
raw_version_position: InitVar[Optional[Dict[str, Any]]]
raw_cargo_lock: InitVar[Union[Literal[False], str, None]]
parsed_url: Optional[ParseResult] = None
new_version: Optional[Version] = None
version_position: Optional[Position] = field(init=False)
cargo_lock: CargoLock = field(init=False)
diff_url: Optional[str] = None
def __post_init__(self, raw_version_position: Optional[Dict[str, Any]]) -> None:
def __post_init__(
self,
import_path: str,
raw_version_position: Optional[Dict[str, Any]],
raw_cargo_lock: Union[Literal[False], str, None],
) -> None:
url = self.url or (self.urls[0] if self.urls else None)
if url:
self.parsed_url = urlparse(url)
@ -54,6 +78,15 @@ class Package:
self.version_position = None
else:
self.version_position = Position(**raw_version_position)
raw_cargo_lock
if raw_cargo_lock is None:
self.cargo_lock = NoCargoLock()
elif raw_cargo_lock is False:
self.cargo_lock = CargoLockInStore()
elif not os.path.realpath(raw_cargo_lock).startswith(import_path):
self.cargo_lock = CargoLockInStore()
else:
self.cargo_lock = CargoLockInSource(raw_cargo_lock)
def eval_expression(
@ -111,7 +144,7 @@ in {{
vendor_hash = pkg.vendorHash or null;
vendor_sha256 = pkg.vendorSha256 or null;
cargo_deps = pkg.cargoDeps.outputHash or null;
cargo_lock =
raw_cargo_lock =
if pkg ? cargoDeps.lockFile then
let
inherit (pkg.cargoDeps) lockFile;
@ -142,7 +175,7 @@ def eval_attr(opts: Options) -> Package:
] + opts.extra_flags
res = run(cmd)
out = json.loads(res.stdout)
package = Package(attribute=opts.attribute, **out)
package = Package(attribute=opts.attribute, import_path=opts.import_path, **out)
if opts.override_filename is not None:
package.filename = opts.override_filename
if opts.url is not None:
@ -151,9 +184,5 @@ def eval_attr(opts: Options) -> Package:
raise UpdateError(
f"Nix's builtins.parseDrvName could not parse the version from {package.name}"
)
if package.cargo_lock and not os.path.realpath(package.cargo_lock).startswith(
opts.import_path
):
package.cargo_lock = False
return package

View File

@ -9,10 +9,10 @@ import tomllib
from concurrent.futures import ThreadPoolExecutor
from os import path
from pathlib import Path
from typing import Dict, Literal, Optional, Tuple
from typing import Dict, Literal, Optional, Tuple, Union
from .errors import UpdateError
from .eval import Package, eval_attr
from .eval import CargoLockInSource, CargoLockInStore, NoCargoLock, Package, eval_attr
from .git import old_version_from_git
from .options import Options
from .utils import info, run
@ -152,7 +152,7 @@ def update_cargo_deps_hash(opts: Options, filename: str, current_hash: str) -> N
replace_hash(filename, current_hash, target_hash)
def update_cargo_lock(opts: Options, filename: str, dst: str | Literal[False]) -> None:
def update_cargo_lock(opts: Options, filename: str, dst: Union[CargoLockInSource, CargoLockInStore]) -> None:
res = run(
[
"nix",
@ -178,8 +178,8 @@ def update_cargo_lock(opts: Options, filename: str, dst: str | Literal[False]) -
return
with open(src, "rb") as f:
if dst:
with open(dst, "wb") as fdst:
if isinstance(dst, CargoLockInSource):
with open(dst.path, "wb") as fdst:
shutil.copyfileobj(f, fdst)
f.seek(0)
@ -339,7 +339,9 @@ def update(opts: Options) -> Package:
if package.cargo_deps:
update_cargo_deps_hash(opts, package.filename, package.cargo_deps)
if package.cargo_lock is not None:
if isinstance(package.cargo_lock, CargoLockInSource) or isinstance(
package.cargo_lock, CargoLockInStore
):
update_cargo_lock(opts, package.filename, package.cargo_lock)
if package.npm_deps: