Merge pull request #28 from Mic92/to_sri

This commit is contained in:
Jörg Thalheim 2020-11-30 16:41:02 +00:00 committed by GitHub
commit d9d4c02ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 11 deletions

View File

@ -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",

View File

@ -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="")