mirror of
https://github.com/Mic92/nix-update.git
synced 2024-11-05 02:16:07 +03:00
Merge pull request #28 from Mic92/to_sri
This commit is contained in:
commit
d9d4c02ae7
17
flake.lock
17
flake.lock
@ -2,11 +2,11 @@
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"locked": {
|
||||
"lastModified": 1597053966,
|
||||
"narHash": "sha256-f9lbPS/GJ1His8fsDqM6gfa8kSqREU4eKiMCS5hrKg4=",
|
||||
"lastModified": 1605370193,
|
||||
"narHash": "sha256-YyMTf3URDL/otKdKgtoMChu4vfVL3vCMkRqpGifhUn0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "ec20f52e2ff61e9c36c2b894b62fc1b4bd04c71b",
|
||||
"rev": "5021eac20303a61fafe17224c087f5519baed54d",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@ -17,12 +17,11 @@
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1597648381,
|
||||
"narHash": "sha256-0VkeNHjgm4CVBVAmv9+REAWIeI+xRjrtwG7aQt3Z43M=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "cb02aa394f471e67ba0505910c1b91148c1ee319",
|
||||
"type": "github"
|
||||
"lastModified": 1605991784,
|
||||
"narHash": "sha256-xKiqqUKr00PxPVYlwMsenThw0Ba/rXDGuae6XB9XqGE=",
|
||||
"path": "/nix/store/550xkq18mh8a6xgpnard55rjqxa9zmlc-source",
|
||||
"rev": "6159c7006e48f57a47964ba890276f36124258d6",
|
||||
"type": "path"
|
||||
},
|
||||
"original": {
|
||||
"id": "nixpkgs",
|
||||
|
@ -1,6 +1,7 @@
|
||||
import fileinput
|
||||
import re
|
||||
from typing import List
|
||||
import subprocess
|
||||
|
||||
from .errors import UpdateError
|
||||
from .eval import Package, eval_attr
|
||||
@ -25,11 +26,39 @@ def update_version(package: Package) -> None:
|
||||
info(f"Not updating version, already {old_version}")
|
||||
|
||||
|
||||
def to_sri(hashstr: str) -> str:
|
||||
if "-" in hashstr:
|
||||
return hashstr
|
||||
l = len(hashstr)
|
||||
if l == 32:
|
||||
prefix = "md5:"
|
||||
elif l == 40:
|
||||
# could be also base32 == 32, but we ignore this case and hope no one is using it
|
||||
prefix = "sha1:"
|
||||
elif l == 64 or l == 52:
|
||||
prefix = "sha256:"
|
||||
elif l == 103 or l == 128:
|
||||
prefix = "sha512:"
|
||||
else:
|
||||
return hashstr
|
||||
|
||||
cmd = [
|
||||
"nix",
|
||||
"--experimental-features",
|
||||
"nix-command",
|
||||
"to-sri",
|
||||
f"{prefix}{hashstr}",
|
||||
]
|
||||
proc = subprocess.run(cmd, stdout=subprocess.PIPE, check=True, text=True)
|
||||
return proc.stdout.rstrip("\n")
|
||||
|
||||
|
||||
def replace_hash(filename: str, current: str, target: str) -> None:
|
||||
if current != target:
|
||||
normalized_hash = to_sri(target)
|
||||
if to_sri(current) != normalized_hash:
|
||||
with fileinput.FileInput(filename, inplace=True) as f:
|
||||
for line in f:
|
||||
line = re.sub(current, target, line)
|
||||
line = re.sub(current, normalized_hash, line)
|
||||
print(line, end="")
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user