X-Git-Url: http://git.scottworley.com/pinch/blobdiff_plain/9e4ad8905fad1963badf6e7ff46d299e2128058f..42dbaf9df4f644bf053e4a37847803283860ec18:/pinch.py diff --git a/pinch.py b/pinch.py index a66a03a..40ebf1b 100644 --- a/pinch.py +++ b/pinch.py @@ -1,3 +1,10 @@ +# pinch: PIN CHannels - a replacement for `nix-channel --update` +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, version 3. + + import argparse import configparser import filecmp @@ -141,7 +148,6 @@ def symlink_archive(v: Verification, path: str) -> str: class AliasSearchPath(NamedTuple): alias_of: str - # pylint: disable=no-self-use def pin(self, _: Verification, __: Optional[Pin]) -> AliasPin: return AliasPin() @@ -149,7 +155,6 @@ class AliasSearchPath(NamedTuple): class SymlinkSearchPath(NamedTuple): path: str - # pylint: disable=no-self-use def pin(self, _: Verification, __: Optional[Pin]) -> SymlinkPin: return SymlinkPin() @@ -202,7 +207,6 @@ class ChannelSearchPath(NamedTuple): tarball_sha256=table['nixexprs.tar.xz'].digest, git_revision=new_gitpin.git_revision) - # pylint: disable=no-self-use def fetch(self, v: Verification, pin: Pin) -> str: assert isinstance(pin, ChannelPin) @@ -262,15 +266,29 @@ def parse_channel(v: Verification, channel_html: str) \ d = xml.dom.minidom.parseString(channel_html) v.ok() - v.status('Extracting release name:') - title_name = d.getElementsByTagName( - 'title')[0].firstChild.nodeValue.split()[2] - h1_name = d.getElementsByTagName('h1')[0].firstChild.nodeValue.split()[2] - v.status(title_name) - v.result(title_name == h1_name) - - v.status('Extracting git commit:') + v.status('Finding release name (1)') + title = d.getElementsByTagName('title')[0].firstChild + v.result(isinstance(title, xml.dom.minidom.CharacterData)) + assert isinstance(title, xml.dom.minidom.CharacterData) + release_name = title.nodeValue.split()[2] + v.status('Finding release name (2)') + h1 = d.getElementsByTagName('h1')[0].firstChild + v.result(isinstance(h1, xml.dom.minidom.CharacterData)) + assert isinstance(h1, xml.dom.minidom.CharacterData) + v.status('Verifying release name:') + v.status(release_name) + v.result(release_name == h1.nodeValue.split()[2]) + + v.status('Finding git commit') git_commit_node = d.getElementsByTagName('tt')[0] + v.result( + isinstance( + git_commit_node.firstChild, + xml.dom.minidom.CharacterData)) + assert isinstance( + git_commit_node.firstChild, + xml.dom.minidom.CharacterData) + v.status('Extracting git commit:') git_revision = git_commit_node.firstChild.nodeValue v.status(git_revision) v.ok() @@ -286,7 +304,7 @@ def parse_channel(v: Verification, channel_html: str) \ digest = Digest16(row.childNodes[2].firstChild.firstChild.nodeValue) table[name] = ChannelTableEntry(url=url, digest=digest, size=size) v.ok() - return table, GitPin(release_name=title_name, git_revision=git_revision) + return table, GitPin(release_name=release_name, git_revision=git_revision) def digest_string(s: bytes) -> Digest16: @@ -302,22 +320,12 @@ def digest_file(filename: str) -> Digest16: return Digest16(hasher.hexdigest()) -@functools.lru_cache -def _experimental_flag_needed(v: Verification) -> bool: - v.status('Checking Nix version') - process = subprocess.run(['nix', '--help'], stdout=subprocess.PIPE) - v.result(process.returncode == 0) - return b'--experimental-features' in process.stdout - - -def _nix_command(v: Verification) -> List[str]: - return ['nix', '--experimental-features', - 'nix-command'] if _experimental_flag_needed(v) else ['nix'] +_NIX_COMMAND = ['nix', '--experimental-features', 'nix-command'] def to_Digest16(v: Verification, digest32: Digest32) -> Digest16: v.status('Converting digest to base16') - process = subprocess.run(_nix_command(v) + [ + process = subprocess.run(_NIX_COMMAND + [ 'to-base16', '--type', 'sha256', @@ -329,7 +337,7 @@ def to_Digest16(v: Verification, digest32: Digest32) -> Digest16: def to_Digest32(v: Verification, digest16: Digest16) -> Digest32: v.status('Converting digest to base32') - process = subprocess.run(_nix_command(v) + [ + process = subprocess.run(_NIX_COMMAND + [ 'to-base32', '--type', 'sha256', @@ -396,14 +404,24 @@ def verify_git_ancestry( v.result(process.returncode == 0) +def broken_symlinks_are_identical(root1: str, root2: str, path: str) -> bool: + a = os.path.join(root1, path) + b = os.path.join(root2, path) + return (os.path.islink(a) + and os.path.islink(b) + and not os.path.exists(a) + and not os.path.exists(b) + and os.readlink(a) == os.readlink(b)) + + def compare_tarball_and_git( v: Verification, pin: GitPin, channel_contents: str, git_contents: str) -> None: v.status('Comparing channel tarball with git checkout') - match, mismatch, errors = compare(os.path.join( - channel_contents, pin.release_name), git_contents) + tarball_contents = os.path.join(channel_contents, pin.release_name) + match, mismatch, errors = compare(tarball_contents, git_contents) v.ok() v.check(f'{len(match)} files match', len(match) > 0) v.check(f'{len(mismatch)} files differ', len(mismatch) == 0) @@ -413,15 +431,21 @@ def compare_tarball_and_git( 'nixpkgs', 'programs.sqlite', 'svn-revision'] - benign_errors = [] + benign_expected_errors = [] for ee in expected_errors: if ee in errors: errors.remove(ee) - benign_errors.append(ee) - v.check(f'{len(errors)} unexpected incomparable files', len(errors) == 0) + benign_expected_errors.append(ee) + errors = [ + e for e in errors + if not broken_symlinks_are_identical(tarball_contents, git_contents, e) + ] + v.check( + f'{len(errors)} unexpected incomparable files: {errors}', + len(errors) == 0) v.check( - f'({len(benign_errors)} of {len(expected_errors)} expected incomparable files)', - len(benign_errors) == len(expected_errors)) + f'({len(benign_expected_errors)} of {len(expected_errors)} expected incomparable files)', + len(benign_expected_errors) == len(expected_errors)) def extract_tarball( @@ -536,7 +560,9 @@ def git_revision_name( git_revision], stdout=subprocess.PIPE) v.result(process.returncode == 0 and process.stdout != b'') - return f'{os.path.basename(channel.git_repo)}-{process.stdout.decode().strip()}' + return f'{ + os.path.basename(channel.git_repo)}-{ + process.stdout.decode().strip()}' K = TypeVar('K') @@ -581,7 +607,7 @@ def read_pinned_config_section( section: str, conf: configparser.SectionProxy) -> Tuple[SearchPath, Pin]: sp, pin = read_config_section(conf) if pin is None: - raise Exception( + raise RuntimeError( f'Cannot update unpinned channel "{section}" (Run "pin" before "update")') return sp, pin @@ -600,7 +626,7 @@ def read_config_files( config = read_config(file) for section in config.sections(): if section in merged_config: - raise Exception('Duplicate channel "{section}"') + raise RuntimeError('Duplicate channel "{section}"') merged_config[section] = config[section] return merged_config @@ -649,23 +675,35 @@ def updateCommand(args: argparse.Namespace) -> None: assert isinstance(sp, AliasSearchPath) # For mypy exprs[section] = exprs[sp.alias_of] - command = [ - 'nix-env', - '--profile', - args.profile, - '--show-trace', - '--file', - '', - '--install', - '--remove-all', - ] + search_paths + ['--from-expression'] + [ - exprs[name] % name for name in sorted(exprs.keys())] - if args.dry_run: - print(' '.join(map(shlex.quote, command))) - else: - v.status('Installing channels with nix-env') - process = subprocess.run(command) - v.result(process.returncode == 0) + with tempfile.NamedTemporaryFile() as unpack_channel_nix: + unpack_channel_nix.write(b''' + { name, channelName, src, }: + derivation { + inherit name channelName src; + builder = "builtin:unpack-channel"; + system = "builtin"; + preferLocalBuild = true; + } + ''') + unpack_channel_nix.flush() + + command = [ + 'nix-env', + '--profile', + args.profile, + '--show-trace', + '--file', + unpack_channel_nix.name, + '--install', + '--remove-all', + ] + search_paths + ['--from-expression'] + [ + exprs[name] % name for name in sorted(exprs.keys())] + if args.dry_run: + print(' '.join(map(shlex.quote, command))) + else: + v.status('Installing channels with nix-env') + process = subprocess.run(command) + v.result(process.returncode == 0) def main() -> None: