19 import xml
.dom
.minidom
37 # Use xdg module when it's less painful to have as a dependency
40 class XDG(NamedTuple
):
45 XDG_CACHE_HOME
=os
.getenv(
47 os
.path
.expanduser('~/.cache')))
50 class VerificationError(Exception):
56 def __init__(self
) -> None:
59 def status(self
, s
: str) -> None:
60 print(s
, end
=' ', file=sys
.stderr
, flush
=True)
61 self
.line_length
+= 1 + len(s
) # Unicode??
64 def _color(s
: str, c
: int) -> str:
65 return '\033[%2dm%s\033[00m' % (c
, s
)
67 def result(self
, r
: bool) -> None:
68 message
, color
= {True: ('OK ', 92), False: ('FAIL', 91)}
[r
]
70 cols
= shutil
.get_terminal_size().columns
or 80
71 pad
= (cols
- (self
.line_length
+ length
)) % cols
72 print(' ' * pad
+ self
._color
(message
, color
), file=sys
.stderr
)
75 raise VerificationError()
77 def check(self
, s
: str, r
: bool) -> None:
85 Digest16
= NewType('Digest16', str)
86 Digest32
= NewType('Digest32', str)
89 class ChannelTableEntry(types
.SimpleNamespace
):
97 class AliasPin(NamedTuple
):
101 class SymlinkPin(NamedTuple
):
103 def release_name(self
) -> str:
107 class GitPin(NamedTuple
):
112 class ChannelPin(NamedTuple
):
119 Pin
= Union
[AliasPin
, SymlinkPin
, GitPin
, ChannelPin
]
122 def copy_to_nix_store(v
: Verification
, filename
: str) -> str:
123 v
.status('Putting tarball in Nix store')
124 process
= subprocess
.run(
125 ['nix-store', '--add', filename
], stdout
=subprocess
.PIPE
)
126 v
.result(process
.returncode
== 0)
127 return process
.stdout
.decode().strip()
130 class AliasSearchPath(NamedTuple
):
133 # pylint: disable=no-self-use
134 def pin(self
, _
: Verification
, __
: Optional
[Pin
]) -> AliasPin
:
138 class SymlinkSearchPath(NamedTuple
):
141 # pylint: disable=no-self-use
142 def pin(self
, _
: Verification
, __
: Optional
[Pin
]) -> SymlinkPin
:
145 def fetch(self
, v
: Verification
, _
: Pin
) -> str:
146 with tempfile
.TemporaryDirectory() as td
:
147 archive_filename
= os
.path
.join(td
, 'link.tar.gz')
148 os
.symlink(self
.path
, os
.path
.join(td
, 'link'))
149 with tarfile
.open(archive_filename
, mode
='x:gz') as t
:
150 t
.add(os
.path
.join(td
, 'link'), arcname
='link')
151 return copy_to_nix_store(v
, archive_filename
)
154 class GitSearchPath(NamedTuple
):
158 def pin(self
, v
: Verification
, old_pin
: Optional
[Pin
]) -> GitPin
:
159 if old_pin
is not None:
160 assert isinstance(old_pin
, GitPin
)
161 old_revision
= old_pin
.git_revision
if old_pin
is not None else None
163 new_revision
= git_fetch(v
, self
, None, old_revision
)
164 return GitPin(release_name
=git_revision_name(v
, self
, new_revision
),
165 git_revision
=new_revision
)
167 def fetch(self
, v
: Verification
, pin
: Pin
) -> str:
168 assert isinstance(pin
, GitPin
)
169 ensure_git_rev_available(v
, self
, pin
, None)
170 return git_get_tarball(v
, self
, pin
)
173 class ChannelSearchPath(NamedTuple
):
178 def pin(self
, v
: Verification
, old_pin
: Optional
[Pin
]) -> ChannelPin
:
179 if old_pin
is not None:
180 assert isinstance(old_pin
, ChannelPin
)
181 old_revision
= old_pin
.git_revision
if old_pin
is not None else None
183 channel_html
, forwarded_url
= fetch_channel(v
, self
)
184 table
, new_gitpin
= parse_channel(v
, channel_html
)
185 fetch_resources(v
, new_gitpin
, forwarded_url
, table
)
186 ensure_git_rev_available(v
, self
, new_gitpin
, old_revision
)
187 check_channel_contents(v
, self
, table
, new_gitpin
)
189 release_name
=new_gitpin
.release_name
,
190 tarball_url
=table
['nixexprs.tar.xz'].absolute_url
,
191 tarball_sha256
=table
['nixexprs.tar.xz'].digest
,
192 git_revision
=new_gitpin
.git_revision
)
194 # pylint: disable=no-self-use
195 def fetch(self
, v
: Verification
, pin
: Pin
) -> str:
196 assert isinstance(pin
, ChannelPin
)
198 return fetch_with_nix_prefetch_url(
199 v
, pin
.tarball_url
, Digest16(pin
.tarball_sha256
))
202 SearchPath
= Union
[AliasSearchPath
,
206 TarrableSearchPath
= Union
[GitSearchPath
, ChannelSearchPath
]
209 def compare(a
: str, b
: str) -> Tuple
[List
[str], List
[str], List
[str]]:
211 def throw(error
: OSError) -> None:
214 def join(x
: str, y
: str) -> str:
215 return y
if x
== '.' else os
.path
.join(x
, y
)
217 def recursive_files(d
: str) -> Iterable
[str]:
218 all_files
: List
[str] = []
219 for path
, dirs
, files
in os
.walk(d
, onerror
=throw
):
220 rel
= os
.path
.relpath(path
, start
=d
)
221 all_files
.extend(join(rel
, f
) for f
in files
)
222 for dir_or_link
in dirs
:
223 if os
.path
.islink(join(path
, dir_or_link
)):
224 all_files
.append(join(rel
, dir_or_link
))
227 def exclude_dot_git(files
: Iterable
[str]) -> Iterable
[str]:
228 return (f
for f
in files
if not f
.startswith('.git/'))
230 files
= functools
.reduce(
233 recursive_files(x
))) for x
in [a
, b
]))
234 return filecmp
.cmpfiles(a
, b
, files
, shallow
=False)
238 v
: Verification
, channel
: ChannelSearchPath
) -> Tuple
[str, str]:
239 v
.status('Fetching channel')
240 request
= urllib
.request
.urlopen(channel
.channel_url
, timeout
=10)
241 channel_html
= request
.read()
242 forwarded_url
= request
.geturl()
243 v
.result(request
.status
== 200) # type: ignore # (for old mypy)
244 v
.check('Got forwarded', channel
.channel_url
!= forwarded_url
)
245 return channel_html
, forwarded_url
248 def parse_channel(v
: Verification
, channel_html
: str) \
249 -> Tuple
[Dict
[str, ChannelTableEntry
], GitPin
]:
250 v
.status('Parsing channel description as XML')
251 d
= xml
.dom
.minidom
.parseString(channel_html
)
254 v
.status('Extracting release name:')
255 title_name
= d
.getElementsByTagName(
256 'title')[0].firstChild
.nodeValue
.split()[2]
257 h1_name
= d
.getElementsByTagName('h1')[0].firstChild
.nodeValue
.split()[2]
259 v
.result(title_name
== h1_name
)
261 v
.status('Extracting git commit:')
262 git_commit_node
= d
.getElementsByTagName('tt')[0]
263 git_revision
= git_commit_node
.firstChild
.nodeValue
264 v
.status(git_revision
)
266 v
.status('Verifying git commit label')
267 v
.result(git_commit_node
.previousSibling
.nodeValue
== 'Git commit ')
269 v
.status('Parsing table')
270 table
: Dict
[str, ChannelTableEntry
] = {}
271 for row
in d
.getElementsByTagName('tr')[1:]:
272 name
= row
.childNodes
[0].firstChild
.firstChild
.nodeValue
273 url
= row
.childNodes
[0].firstChild
.getAttribute('href')
274 size
= int(row
.childNodes
[1].firstChild
.nodeValue
)
275 digest
= Digest16(row
.childNodes
[2].firstChild
.firstChild
.nodeValue
)
276 table
[name
] = ChannelTableEntry(url
=url
, digest
=digest
, size
=size
)
278 return table
, GitPin(release_name
=title_name
, git_revision
=git_revision
)
281 def digest_string(s
: bytes) -> Digest16
:
282 return Digest16(hashlib
.sha256(s
).hexdigest())
285 def digest_file(filename
: str) -> Digest16
:
286 hasher
= hashlib
.sha256()
287 with open(filename
, 'rb') as f
:
288 # pylint: disable=cell-var-from-loop
289 for block
in iter(lambda: f
.read(4096), b
''):
291 return Digest16(hasher
.hexdigest())
294 def to_Digest16(v
: Verification
, digest32
: Digest32
) -> Digest16
:
295 v
.status('Converting digest to base16')
296 process
= subprocess
.run(
297 ['nix', 'to-base16', '--type', 'sha256', digest32
], stdout
=subprocess
.PIPE
)
298 v
.result(process
.returncode
== 0)
299 return Digest16(process
.stdout
.decode().strip())
302 def to_Digest32(v
: Verification
, digest16
: Digest16
) -> Digest32
:
303 v
.status('Converting digest to base32')
304 process
= subprocess
.run(
305 ['nix', 'to-base32', '--type', 'sha256', digest16
], stdout
=subprocess
.PIPE
)
306 v
.result(process
.returncode
== 0)
307 return Digest32(process
.stdout
.decode().strip())
310 def fetch_with_nix_prefetch_url(
313 digest
: Digest16
) -> str:
314 v
.status('Fetching %s' % url
)
315 process
= subprocess
.run(
316 ['nix-prefetch-url', '--print-path', url
, digest
], stdout
=subprocess
.PIPE
)
317 v
.result(process
.returncode
== 0)
318 prefetch_digest
, path
, empty
= process
.stdout
.decode().split('\n')
320 v
.check("Verifying nix-prefetch-url's digest",
321 to_Digest16(v
, Digest32(prefetch_digest
)) == digest
)
322 v
.status("Verifying file digest")
323 file_digest
= digest_file(path
)
324 v
.result(file_digest
== digest
)
325 return path
# type: ignore # (for old mypy)
332 table
: Dict
[str, ChannelTableEntry
]) -> None:
333 for resource
in ['git-revision', 'nixexprs.tar.xz']:
334 fields
= table
[resource
]
335 fields
.absolute_url
= urllib
.parse
.urljoin(forwarded_url
, fields
.url
)
336 fields
.file = fetch_with_nix_prefetch_url(
337 v
, fields
.absolute_url
, fields
.digest
)
338 v
.status('Verifying git commit on main page matches git commit in table')
339 v
.result(open(table
['git-revision'].file).read(999) == pin
.git_revision
)
342 def git_cachedir(git_repo
: str) -> str:
346 digest_string(git_repo
.encode()))
349 def tarball_cache_file(channel
: TarrableSearchPath
, pin
: GitPin
) -> str:
354 (digest_string(channel
.git_repo
.encode()),
359 def verify_git_ancestry(
361 channel
: TarrableSearchPath
,
363 old_revision
: Optional
[str]) -> None:
364 cachedir
= git_cachedir(channel
.git_repo
)
365 v
.status('Verifying rev is an ancestor of ref')
366 process
= subprocess
.run(['git',
373 v
.result(process
.returncode
== 0)
375 if old_revision
is not None:
377 'Verifying rev is an ancestor of previous rev %s' %
379 process
= subprocess
.run(['git',
386 v
.result(process
.returncode
== 0)
391 channel
: TarrableSearchPath
,
392 desired_revision
: Optional
[str],
393 old_revision
: Optional
[str]) -> str:
394 # It would be nice if we could share the nix git cache, but as of the time
395 # of writing it is transitioning from gitv2 (deprecated) to gitv3 (not ready
396 # yet), and trying to straddle them both is too far into nix implementation
397 # details for my comfort. So we re-implement here half of nix.fetchGit.
400 cachedir
= git_cachedir(channel
.git_repo
)
401 if not os
.path
.exists(cachedir
):
402 v
.status("Initializing git repo")
403 process
= subprocess
.run(
404 ['git', 'init', '--bare', cachedir
])
405 v
.result(process
.returncode
== 0)
407 v
.status('Fetching ref "%s" from %s' % (channel
.git_ref
, channel
.git_repo
))
408 # We don't use --force here because we want to abort and freak out if forced
409 # updates are happening.
410 process
= subprocess
.run(['git',
415 '%s:%s' % (channel
.git_ref
,
417 v
.result(process
.returncode
== 0)
419 if desired_revision
is not None:
420 v
.status('Verifying that fetch retrieved this rev')
421 process
= subprocess
.run(
422 ['git', '-C', cachedir
, 'cat-file', '-e', desired_revision
])
423 v
.result(process
.returncode
== 0)
430 channel
.git_ref
)).read(999).strip()
432 verify_git_ancestry(v
, channel
, new_revision
, old_revision
)
437 def ensure_git_rev_available(
439 channel
: TarrableSearchPath
,
441 old_revision
: Optional
[str]) -> None:
442 cachedir
= git_cachedir(channel
.git_repo
)
443 if os
.path
.exists(cachedir
):
444 v
.status('Checking if we already have this rev:')
445 process
= subprocess
.run(
446 ['git', '-C', cachedir
, 'cat-file', '-e', pin
.git_revision
])
447 if process
.returncode
== 0:
449 if process
.returncode
== 1:
451 v
.result(process
.returncode
== 0 or process
.returncode
== 1)
452 if process
.returncode
== 0:
453 verify_git_ancestry(v
, channel
, pin
.git_revision
, old_revision
)
455 git_fetch(v
, channel
, pin
.git_revision
, old_revision
)
458 def compare_tarball_and_git(
461 channel_contents
: str,
462 git_contents
: str) -> None:
463 v
.status('Comparing channel tarball with git checkout')
464 match
, mismatch
, errors
= compare(os
.path
.join(
465 channel_contents
, pin
.release_name
), git_contents
)
467 v
.check('%d files match' % len(match
), len(match
) > 0)
468 v
.check('%d files differ' % len(mismatch
), len(mismatch
) == 0)
476 for ee
in expected_errors
:
479 benign_errors
.append(ee
)
481 '%d unexpected incomparable files' %
485 '(%d of %d expected incomparable files)' %
487 len(expected_errors
)),
488 len(benign_errors
) == len(expected_errors
))
493 table
: Dict
[str, ChannelTableEntry
],
495 v
.status('Extracting tarball %s' % table
['nixexprs.tar.xz'].file)
496 shutil
.unpack_archive(table
['nixexprs.tar.xz'].file, dest
)
502 channel
: TarrableSearchPath
,
505 v
.status('Checking out corresponding git revision')
506 git
= subprocess
.Popen(['git',
508 git_cachedir(channel
.git_repo
),
511 stdout
=subprocess
.PIPE
)
512 tar
= subprocess
.Popen(
513 ['tar', 'x', '-C', dest
, '-f', '-'], stdin
=git
.stdout
)
518 v
.result(git
.returncode
== 0 and tar
.returncode
== 0)
523 channel
: TarrableSearchPath
,
525 cache_file
= tarball_cache_file(channel
, pin
)
526 if os
.path
.exists(cache_file
):
527 cached_tarball
= open(cache_file
).read(9999)
528 if os
.path
.exists(cached_tarball
):
529 return cached_tarball
531 with tempfile
.TemporaryDirectory() as output_dir
:
532 output_filename
= os
.path
.join(
533 output_dir
, pin
.release_name
+ '.tar.xz')
534 with open(output_filename
, 'w') as output_file
:
536 'Generating tarball for git revision %s' %
538 git
= subprocess
.Popen(['git',
540 git_cachedir(channel
.git_repo
),
542 '--prefix=%s/' % pin
.release_name
,
544 stdout
=subprocess
.PIPE
)
545 xz
= subprocess
.Popen(['xz'], stdin
=git
.stdout
, stdout
=output_file
)
548 v
.result(git
.returncode
== 0 and xz
.returncode
== 0)
550 store_tarball
= copy_to_nix_store(v
, output_filename
)
552 os
.makedirs(os
.path
.dirname(cache_file
), exist_ok
=True)
553 open(cache_file
, 'w').write(store_tarball
)
554 return store_tarball
# type: ignore # (for old mypy)
557 def check_channel_metadata(
560 channel_contents
: str) -> None:
561 v
.status('Verifying git commit in channel tarball')
567 '.git-revision')).read(999) == pin
.git_revision
)
570 'Verifying version-suffix is a suffix of release name %s:' %
572 version_suffix
= open(
576 '.version-suffix')).read(999)
577 v
.status(version_suffix
)
578 v
.result(pin
.release_name
.endswith(version_suffix
))
581 def check_channel_contents(
583 channel
: TarrableSearchPath
,
584 table
: Dict
[str, ChannelTableEntry
],
585 pin
: GitPin
) -> None:
586 with tempfile
.TemporaryDirectory() as channel_contents
, \
587 tempfile
.TemporaryDirectory() as git_contents
:
589 extract_tarball(v
, table
, channel_contents
)
590 check_channel_metadata(v
, pin
, channel_contents
)
592 git_checkout(v
, channel
, pin
, git_contents
)
594 compare_tarball_and_git(v
, pin
, channel_contents
, git_contents
)
596 v
.status('Removing temporary directories')
600 def git_revision_name(
602 channel
: TarrableSearchPath
,
603 git_revision
: str) -> str:
604 v
.status('Getting commit date')
605 process
= subprocess
.run(['git',
607 git_cachedir(channel
.git_repo
),
612 '--no-show-signature',
614 stdout
=subprocess
.PIPE
)
615 v
.result(process
.returncode
== 0 and process
.stdout
!= b
'')
616 return '%s-%s' % (os
.path
.basename(channel
.git_repo
),
617 process
.stdout
.decode().strip())
624 def partition_dict(pred
: Callable
[[K
, V
], bool],
625 d
: Dict
[K
, V
]) -> Tuple
[Dict
[K
, V
], Dict
[K
, V
]]:
626 selected
: Dict
[K
, V
] = {}
627 remaining
: Dict
[K
, V
] = {}
628 for k
, v
in d
.items():
633 return selected
, remaining
636 def filter_dict(d
: Dict
[K
, V
], fields
: Set
[K
]
637 ) -> Tuple
[Dict
[K
, V
], Dict
[K
, V
]]:
638 return partition_dict(lambda k
, v
: k
in fields
, d
)
641 def read_config_section(
642 conf
: configparser
.SectionProxy
) -> Tuple
[SearchPath
, Optional
[Pin
]]:
643 mapping
: Mapping
[str, Tuple
[Type
[SearchPath
], Type
[Pin
]]] = {
644 'alias': (AliasSearchPath
, AliasPin
),
645 'channel': (ChannelSearchPath
, ChannelPin
),
646 'git': (GitSearchPath
, GitPin
),
647 'symlink': (SymlinkSearchPath
, SymlinkPin
),
649 SP
, P
= mapping
[conf
['type']]
650 _
, all_fields
= filter_dict(dict(conf
.items()), set(['type']))
651 pin_fields
, remaining_fields
= filter_dict(all_fields
, set(P
._fields
))
652 # Error suppression works around https://github.com/python/mypy/issues/9007
653 pin_present
= pin_fields
!= {} or P
._fields
== ()
654 pin
= P(**pin_fields
) if pin_present
else None # type:ignore[call-arg]
655 return SP(**remaining_fields
), pin
658 def read_pinned_config_section(
659 section
: str, conf
: configparser
.SectionProxy
) -> Tuple
[SearchPath
, Pin
]:
660 sp
, pin
= read_config_section(conf
)
663 'Cannot update unpinned channel "%s" (Run "pin" before "update")' %
668 def read_config(filename
: str) -> configparser
.ConfigParser
:
669 config
= configparser
.ConfigParser()
670 config
.read_file(open(filename
), filename
)
674 def read_config_files(
675 filenames
: Iterable
[str]) -> Dict
[str, configparser
.SectionProxy
]:
676 merged_config
: Dict
[str, configparser
.SectionProxy
] = {}
677 for file in filenames
:
678 config
= read_config(file)
679 for section
in config
.sections():
680 if section
in merged_config
:
681 raise Exception('Duplicate channel "%s"' % section
)
682 merged_config
[section
] = config
[section
]
686 def pinCommand(args
: argparse
.Namespace
) -> None:
688 config
= read_config(args
.channels_file
)
689 for section
in config
.sections():
690 if args
.channels
and section
not in args
.channels
:
693 sp
, old_pin
= read_config_section(config
[section
])
695 config
[section
].update(sp
.pin(v
, old_pin
)._asdict
())
697 with open(args
.channels_file
, 'w') as configfile
:
698 config
.write(configfile
)
701 def updateCommand(args
: argparse
.Namespace
) -> None:
703 exprs
: Dict
[str, str] = {}
705 section
: read_pinned_config_section(section
, conf
) for section
,
706 conf
in read_config_files(
707 args
.channels_file
).items()}
708 alias
, nonalias
= partition_dict(
709 lambda k
, v
: isinstance(v
[0], AliasSearchPath
), config
)
711 for section
, (sp
, pin
) in nonalias
.items():
712 assert not isinstance(sp
, AliasSearchPath
) # mypy can't see through
713 assert not isinstance(pin
, AliasPin
) # partition_dict()
714 tarball
= sp
.fetch(v
, pin
)
716 'f: f { name = "%s"; channelName = "%%s"; src = builtins.storePath "%s"; }' %
717 (pin
.release_name
, tarball
))
719 for section
, (sp
, pin
) in alias
.items():
720 assert isinstance(sp
, AliasSearchPath
) # For mypy
721 exprs
[section
] = exprs
[sp
.alias_of
]
726 '/nix/var/nix/profiles/per-user/%s/channels' %
730 '<nix/unpack-channel.nix>',
732 '--from-expression'] + [exprs
[name
] % name
for name
in sorted(exprs
.keys())]
734 print(' '.join(map(shlex
.quote
, command
)))
736 v
.status('Installing channels with nix-env')
737 process
= subprocess
.run(command
)
738 v
.result(process
.returncode
== 0)
742 parser
= argparse
.ArgumentParser(prog
='pinch')
743 subparsers
= parser
.add_subparsers(dest
='mode', required
=True)
744 parser_pin
= subparsers
.add_parser('pin')
745 parser_pin
.add_argument('channels_file', type=str)
746 parser_pin
.add_argument('channels', type=str, nargs
='*')
747 parser_pin
.set_defaults(func
=pinCommand
)
748 parser_update
= subparsers
.add_parser('update')
749 parser_update
.add_argument('--dry-run', action
='store_true')
750 parser_update
.add_argument('channels_file', type=str, nargs
='+')
751 parser_update
.set_defaults(func
=updateCommand
)
752 args
= parser
.parse_args()
756 if __name__
== '__main__':