1 from abc
import ABC
, abstractmethod
19 import xml
.dom
.minidom
34 # Use xdg module when it's less painful to have as a dependency
37 class XDG(types
.SimpleNamespace
):
42 XDG_CACHE_HOME
=os
.getenv(
44 os
.path
.expanduser('~/.cache')))
47 class VerificationError(Exception):
53 def __init__(self
) -> None:
56 def status(self
, s
: str) -> None:
57 print(s
, end
=' ', file=sys
.stderr
, flush
=True)
58 self
.line_length
+= 1 + len(s
) # Unicode??
61 def _color(s
: str, c
: int) -> str:
62 return '\033[%2dm%s\033[00m' % (c
, s
)
64 def result(self
, r
: bool) -> None:
65 message
, color
= {True: ('OK ', 92), False: ('FAIL', 91)}
[r
]
67 cols
= shutil
.get_terminal_size().columns
or 80
68 pad
= (cols
- (self
.line_length
+ length
)) % cols
69 print(' ' * pad
+ self
._color
(message
, color
), file=sys
.stderr
)
72 raise VerificationError()
74 def check(self
, s
: str, r
: bool) -> None:
82 Digest16
= NewType('Digest16', str)
83 Digest32
= NewType('Digest32', str)
86 class ChannelTableEntry(types
.SimpleNamespace
):
94 class AliasPin(NamedTuple
):
98 class GitPin(NamedTuple
):
103 class ChannelPin(NamedTuple
):
110 Pin
= Union
[AliasPin
, GitPin
, ChannelPin
]
113 class SearchPath(types
.SimpleNamespace
, ABC
):
116 def pin(self
, v
: Verification
) -> Pin
:
120 class AliasSearchPath(SearchPath
):
123 def pin(self
, v
: Verification
) -> AliasPin
:
124 assert not hasattr(self
, 'git_repo')
128 # (This lint-disable is for pylint bug https://github.com/PyCQA/pylint/issues/179
129 # which is fixed in pylint 2.5.)
130 class TarrableSearchPath(SearchPath
, ABC
): # pylint: disable=abstract-method
136 table
: Dict
[str, ChannelTableEntry
]
139 class GitSearchPath(TarrableSearchPath
):
140 def pin(self
, v
: Verification
) -> GitPin
:
142 self
.git_revision
if hasattr(self
, 'git_revision') else None)
143 if hasattr(self
, 'git_revision'):
144 del self
.git_revision
146 new_revision
= git_fetch(v
, self
, None, old_revision
)
147 return GitPin(release_name
=git_revision_name(v
, self
, new_revision
),
148 git_revision
=new_revision
)
150 def fetch(self
, v
: Verification
, section
: str,
151 conf
: configparser
.SectionProxy
) -> str:
152 if 'git_revision' not in conf
or 'release_name' not in conf
:
154 'Cannot update unpinned channel "%s" (Run "pin" before "update")' %
157 release_name
=conf
['release_name'],
158 git_revision
=conf
['git_revision'])
160 ensure_git_rev_available(v
, self
, the_pin
, None)
161 return git_get_tarball(v
, self
, the_pin
)
164 class ChannelSearchPath(TarrableSearchPath
):
165 def pin(self
, v
: Verification
) -> ChannelPin
:
167 self
.git_revision
if hasattr(self
, 'git_revision') else None)
168 if hasattr(self
, 'git_revision'):
169 del self
.git_revision
172 new_gitpin
= parse_channel(v
, self
)
173 fetch_resources(v
, self
, new_gitpin
)
174 ensure_git_rev_available(v
, self
, new_gitpin
, old_revision
)
175 check_channel_contents(v
, self
)
177 release_name
=self
.release_name
,
178 tarball_url
=self
.table
['nixexprs.tar.xz'].absolute_url
,
179 tarball_sha256
=self
.table
['nixexprs.tar.xz'].digest
,
180 git_revision
=self
.git_revision
)
182 # Lint TODO: Put tarball_url and tarball_sha256 in ChannelSearchPath
183 # pylint: disable=no-self-use
184 def fetch(self
, v
: Verification
, section
: str,
185 conf
: configparser
.SectionProxy
) -> str:
186 if 'git_repo' not in conf
or 'release_name' not in conf
:
188 'Cannot update unpinned channel "%s" (Run "pin" before "update")' %
191 return fetch_with_nix_prefetch_url(
192 v
, conf
['tarball_url'], Digest16(
193 conf
['tarball_sha256']))
196 def compare(a
: str, b
: str) -> Tuple
[List
[str], List
[str], List
[str]]:
198 def throw(error
: OSError) -> None:
201 def join(x
: str, y
: str) -> str:
202 return y
if x
== '.' else os
.path
.join(x
, y
)
204 def recursive_files(d
: str) -> Iterable
[str]:
205 all_files
: List
[str] = []
206 for path
, dirs
, files
in os
.walk(d
, onerror
=throw
):
207 rel
= os
.path
.relpath(path
, start
=d
)
208 all_files
.extend(join(rel
, f
) for f
in files
)
209 for dir_or_link
in dirs
:
210 if os
.path
.islink(join(path
, dir_or_link
)):
211 all_files
.append(join(rel
, dir_or_link
))
214 def exclude_dot_git(files
: Iterable
[str]) -> Iterable
[str]:
215 return (f
for f
in files
if not f
.startswith('.git/'))
217 files
= functools
.reduce(
220 recursive_files(x
))) for x
in [a
, b
]))
221 return filecmp
.cmpfiles(a
, b
, files
, shallow
=False)
224 def fetch(v
: Verification
, channel
: TarrableSearchPath
) -> None:
225 v
.status('Fetching channel')
226 request
= urllib
.request
.urlopen(channel
.channel_url
, timeout
=10)
227 channel
.channel_html
= request
.read()
228 channel
.forwarded_url
= request
.geturl()
229 v
.result(request
.status
== 200) # type: ignore # (for old mypy)
230 v
.check('Got forwarded', channel
.channel_url
!= channel
.forwarded_url
)
233 def parse_channel(v
: Verification
, channel
: TarrableSearchPath
) -> GitPin
:
234 v
.status('Parsing channel description as XML')
235 d
= xml
.dom
.minidom
.parseString(channel
.channel_html
)
238 v
.status('Extracting release name:')
239 title_name
= d
.getElementsByTagName(
240 'title')[0].firstChild
.nodeValue
.split()[2]
241 h1_name
= d
.getElementsByTagName('h1')[0].firstChild
.nodeValue
.split()[2]
243 v
.result(title_name
== h1_name
)
244 channel
.release_name
= title_name
246 v
.status('Extracting git commit:')
247 git_commit_node
= d
.getElementsByTagName('tt')[0]
248 channel
.git_revision
= git_commit_node
.firstChild
.nodeValue
249 v
.status(channel
.git_revision
)
251 v
.status('Verifying git commit label')
252 v
.result(git_commit_node
.previousSibling
.nodeValue
== 'Git commit ')
254 v
.status('Parsing table')
256 for row
in d
.getElementsByTagName('tr')[1:]:
257 name
= row
.childNodes
[0].firstChild
.firstChild
.nodeValue
258 url
= row
.childNodes
[0].firstChild
.getAttribute('href')
259 size
= int(row
.childNodes
[1].firstChild
.nodeValue
)
260 digest
= Digest16(row
.childNodes
[2].firstChild
.firstChild
.nodeValue
)
261 channel
.table
[name
] = ChannelTableEntry(
262 url
=url
, digest
=digest
, size
=size
)
264 return GitPin(release_name
=title_name
, git_revision
=channel
.git_revision
)
267 def digest_string(s
: bytes) -> Digest16
:
268 return Digest16(hashlib
.sha256(s
).hexdigest())
271 def digest_file(filename
: str) -> Digest16
:
272 hasher
= hashlib
.sha256()
273 with open(filename
, 'rb') as f
:
274 # pylint: disable=cell-var-from-loop
275 for block
in iter(lambda: f
.read(4096), b
''):
277 return Digest16(hasher
.hexdigest())
280 def to_Digest16(v
: Verification
, digest32
: Digest32
) -> Digest16
:
281 v
.status('Converting digest to base16')
282 process
= subprocess
.run(
283 ['nix', 'to-base16', '--type', 'sha256', digest32
], stdout
=subprocess
.PIPE
)
284 v
.result(process
.returncode
== 0)
285 return Digest16(process
.stdout
.decode().strip())
288 def to_Digest32(v
: Verification
, digest16
: Digest16
) -> Digest32
:
289 v
.status('Converting digest to base32')
290 process
= subprocess
.run(
291 ['nix', 'to-base32', '--type', 'sha256', digest16
], stdout
=subprocess
.PIPE
)
292 v
.result(process
.returncode
== 0)
293 return Digest32(process
.stdout
.decode().strip())
296 def fetch_with_nix_prefetch_url(
299 digest
: Digest16
) -> str:
300 v
.status('Fetching %s' % url
)
301 process
= subprocess
.run(
302 ['nix-prefetch-url', '--print-path', url
, digest
], stdout
=subprocess
.PIPE
)
303 v
.result(process
.returncode
== 0)
304 prefetch_digest
, path
, empty
= process
.stdout
.decode().split('\n')
306 v
.check("Verifying nix-prefetch-url's digest",
307 to_Digest16(v
, Digest32(prefetch_digest
)) == digest
)
308 v
.status("Verifying file digest")
309 file_digest
= digest_file(path
)
310 v
.result(file_digest
== digest
)
311 return path
# type: ignore # (for old mypy)
316 channel
: ChannelSearchPath
,
317 pin
: GitPin
) -> None:
318 for resource
in ['git-revision', 'nixexprs.tar.xz']:
319 fields
= channel
.table
[resource
]
320 fields
.absolute_url
= urllib
.parse
.urljoin(
321 channel
.forwarded_url
, fields
.url
)
322 fields
.file = fetch_with_nix_prefetch_url(
323 v
, fields
.absolute_url
, fields
.digest
)
324 v
.status('Verifying git commit on main page matches git commit in table')
327 channel
.table
['git-revision'].file).read(999) == pin
.git_revision
)
330 def git_cachedir(git_repo
: str) -> str:
334 digest_string(git_repo
.encode()))
337 def tarball_cache_file(channel
: TarrableSearchPath
) -> str:
342 (digest_string(channel
.git_repo
.encode()),
343 channel
.git_revision
,
344 channel
.release_name
))
347 def verify_git_ancestry(
349 channel
: TarrableSearchPath
,
351 old_revision
: Optional
[str]) -> None:
352 cachedir
= git_cachedir(channel
.git_repo
)
353 v
.status('Verifying rev is an ancestor of ref')
354 process
= subprocess
.run(['git',
361 v
.result(process
.returncode
== 0)
363 if old_revision
is not None:
365 'Verifying rev is an ancestor of previous rev %s' %
367 process
= subprocess
.run(['git',
374 v
.result(process
.returncode
== 0)
379 channel
: TarrableSearchPath
,
380 desired_revision
: Optional
[str],
381 old_revision
: Optional
[str]) -> str:
382 # It would be nice if we could share the nix git cache, but as of the time
383 # of writing it is transitioning from gitv2 (deprecated) to gitv3 (not ready
384 # yet), and trying to straddle them both is too far into nix implementation
385 # details for my comfort. So we re-implement here half of nix.fetchGit.
388 cachedir
= git_cachedir(channel
.git_repo
)
389 if not os
.path
.exists(cachedir
):
390 v
.status("Initializing git repo")
391 process
= subprocess
.run(
392 ['git', 'init', '--bare', cachedir
])
393 v
.result(process
.returncode
== 0)
395 v
.status('Fetching ref "%s" from %s' % (channel
.git_ref
, channel
.git_repo
))
396 # We don't use --force here because we want to abort and freak out if forced
397 # updates are happening.
398 process
= subprocess
.run(['git',
403 '%s:%s' % (channel
.git_ref
,
405 v
.result(process
.returncode
== 0)
407 if desired_revision
is not None:
408 v
.status('Verifying that fetch retrieved this rev')
409 process
= subprocess
.run(
410 ['git', '-C', cachedir
, 'cat-file', '-e', desired_revision
])
411 v
.result(process
.returncode
== 0)
418 channel
.git_ref
)).read(999).strip()
420 verify_git_ancestry(v
, channel
, new_revision
, old_revision
)
425 def ensure_git_rev_available(
427 channel
: TarrableSearchPath
,
429 old_revision
: Optional
[str]) -> None:
430 cachedir
= git_cachedir(channel
.git_repo
)
431 if os
.path
.exists(cachedir
):
432 v
.status('Checking if we already have this rev:')
433 process
= subprocess
.run(
434 ['git', '-C', cachedir
, 'cat-file', '-e', pin
.git_revision
])
435 if process
.returncode
== 0:
437 if process
.returncode
== 1:
439 v
.result(process
.returncode
== 0 or process
.returncode
== 1)
440 if process
.returncode
== 0:
441 verify_git_ancestry(v
, channel
, pin
.git_revision
, old_revision
)
443 git_fetch(v
, channel
, pin
.git_revision
, old_revision
)
446 def compare_tarball_and_git(
448 channel
: TarrableSearchPath
,
449 channel_contents
: str,
450 git_contents
: str) -> None:
451 v
.status('Comparing channel tarball with git checkout')
452 match
, mismatch
, errors
= compare(os
.path
.join(
453 channel_contents
, channel
.release_name
), git_contents
)
455 v
.check('%d files match' % len(match
), len(match
) > 0)
456 v
.check('%d files differ' % len(mismatch
), len(mismatch
) == 0)
464 for ee
in expected_errors
:
467 benign_errors
.append(ee
)
469 '%d unexpected incomparable files' %
473 '(%d of %d expected incomparable files)' %
475 len(expected_errors
)),
476 len(benign_errors
) == len(expected_errors
))
481 channel
: TarrableSearchPath
,
483 v
.status('Extracting tarball %s' %
484 channel
.table
['nixexprs.tar.xz'].file)
485 shutil
.unpack_archive(
486 channel
.table
['nixexprs.tar.xz'].file,
493 channel
: TarrableSearchPath
,
495 v
.status('Checking out corresponding git revision')
496 git
= subprocess
.Popen(['git',
498 git_cachedir(channel
.git_repo
),
500 channel
.git_revision
],
501 stdout
=subprocess
.PIPE
)
502 tar
= subprocess
.Popen(
503 ['tar', 'x', '-C', dest
, '-f', '-'], stdin
=git
.stdout
)
508 v
.result(git
.returncode
== 0 and tar
.returncode
== 0)
513 channel
: TarrableSearchPath
,
515 cache_file
= tarball_cache_file(channel
)
516 if os
.path
.exists(cache_file
):
517 cached_tarball
= open(cache_file
).read(9999)
518 if os
.path
.exists(cached_tarball
):
519 return cached_tarball
521 with tempfile
.TemporaryDirectory() as output_dir
:
522 output_filename
= os
.path
.join(
523 output_dir
, pin
.release_name
+ '.tar.xz')
524 with open(output_filename
, 'w') as output_file
:
526 'Generating tarball for git revision %s' %
528 git
= subprocess
.Popen(['git',
530 git_cachedir(channel
.git_repo
),
532 '--prefix=%s/' % pin
.release_name
,
534 stdout
=subprocess
.PIPE
)
535 xz
= subprocess
.Popen(['xz'], stdin
=git
.stdout
, stdout
=output_file
)
538 v
.result(git
.returncode
== 0 and xz
.returncode
== 0)
540 v
.status('Putting tarball in Nix store')
541 process
= subprocess
.run(
542 ['nix-store', '--add', output_filename
], stdout
=subprocess
.PIPE
)
543 v
.result(process
.returncode
== 0)
544 store_tarball
= process
.stdout
.decode().strip()
546 os
.makedirs(os
.path
.dirname(cache_file
), exist_ok
=True)
547 open(cache_file
, 'w').write(store_tarball
)
548 return store_tarball
# type: ignore # (for old mypy)
551 def check_channel_metadata(
553 channel
: TarrableSearchPath
,
554 channel_contents
: str) -> None:
555 v
.status('Verifying git commit in channel tarball')
560 channel
.release_name
,
561 '.git-revision')).read(999) == channel
.git_revision
)
564 'Verifying version-suffix is a suffix of release name %s:' %
565 channel
.release_name
)
566 version_suffix
= open(
569 channel
.release_name
,
570 '.version-suffix')).read(999)
571 v
.status(version_suffix
)
572 v
.result(channel
.release_name
.endswith(version_suffix
))
575 def check_channel_contents(
577 channel
: TarrableSearchPath
) -> None:
578 with tempfile
.TemporaryDirectory() as channel_contents
, \
579 tempfile
.TemporaryDirectory() as git_contents
:
581 extract_tarball(v
, channel
, channel_contents
)
582 check_channel_metadata(v
, channel
, channel_contents
)
584 git_checkout(v
, channel
, git_contents
)
586 compare_tarball_and_git(v
, channel
, channel_contents
, git_contents
)
588 v
.status('Removing temporary directories')
592 def git_revision_name(
594 channel
: TarrableSearchPath
,
595 git_revision
: str) -> str:
596 v
.status('Getting commit date')
597 process
= subprocess
.run(['git',
599 git_cachedir(channel
.git_repo
),
604 '--no-show-signature',
606 stdout
=subprocess
.PIPE
)
607 v
.result(process
.returncode
== 0 and process
.stdout
!= b
'')
608 return '%s-%s' % (os
.path
.basename(channel
.git_repo
),
609 process
.stdout
.decode().strip())
612 def read_search_path(conf
: configparser
.SectionProxy
) -> SearchPath
:
613 mapping
: Mapping
[str, Type
[SearchPath
]] = {
614 'alias': AliasSearchPath
,
615 'channel': ChannelSearchPath
,
616 'git': GitSearchPath
,
618 return mapping
[conf
['type']](**dict(conf
.items()))
621 def read_config(filename
: str) -> configparser
.ConfigParser
:
622 config
= configparser
.ConfigParser()
623 config
.read_file(open(filename
), filename
)
627 def read_config_files(
628 filenames
: Iterable
[str]) -> Dict
[str, configparser
.SectionProxy
]:
629 merged_config
: Dict
[str, configparser
.SectionProxy
] = {}
630 for file in filenames
:
631 config
= read_config(file)
632 for section
in config
.sections():
633 if section
in merged_config
:
634 raise Exception('Duplicate channel "%s"' % section
)
635 merged_config
[section
] = config
[section
]
639 def pinCommand(args
: argparse
.Namespace
) -> None:
641 config
= read_config(args
.channels_file
)
642 for section
in config
.sections():
643 if args
.channels
and section
not in args
.channels
:
646 sp
= read_search_path(config
[section
])
648 config
[section
].update(sp
.pin(v
)._asdict
())
650 with open(args
.channels_file
, 'w') as configfile
:
651 config
.write(configfile
)
654 def updateCommand(args
: argparse
.Namespace
) -> None:
656 exprs
: Dict
[str, str] = {}
657 config
= read_config_files(args
.channels_file
)
658 for section
in config
:
659 sp
= read_search_path(config
[section
])
660 if isinstance(sp
, AliasSearchPath
):
661 assert 'git_repo' not in config
[section
]
663 tarball
= sp
.fetch(v
, section
, config
[section
])
665 'f: f { name = "%s"; channelName = "%%s"; src = builtins.storePath "%s"; }' %
666 (config
[section
]['release_name'], tarball
))
668 for section
in config
:
669 if 'alias_of' in config
[section
]:
670 exprs
[section
] = exprs
[str(config
[section
]['alias_of'])]
675 '/nix/var/nix/profiles/per-user/%s/channels' %
679 '<nix/unpack-channel.nix>',
681 '--from-expression'] + [exprs
[name
] % name
for name
in sorted(exprs
.keys())]
683 print(' '.join(map(shlex
.quote
, command
)))
685 v
.status('Installing channels with nix-env')
686 process
= subprocess
.run(command
)
687 v
.result(process
.returncode
== 0)
691 parser
= argparse
.ArgumentParser(prog
='pinch')
692 subparsers
= parser
.add_subparsers(dest
='mode', required
=True)
693 parser_pin
= subparsers
.add_parser('pin')
694 parser_pin
.add_argument('channels_file', type=str)
695 parser_pin
.add_argument('channels', type=str, nargs
='*')
696 parser_pin
.set_defaults(func
=pinCommand
)
697 parser_update
= subparsers
.add_parser('update')
698 parser_update
.add_argument('--dry-run', action
='store_true')
699 parser_update
.add_argument('channels_file', type=str, nargs
='+')
700 parser_update
.set_defaults(func
=updateCommand
)
701 args
= parser
.parse_args()
705 if __name__
== '__main__':