18 import xml
.dom
.minidom
31 Digest16
= NewType('Digest16', str)
32 Digest32
= NewType('Digest32', str)
35 class ChannelTableEntry(types
.SimpleNamespace
):
43 class Channel(types
.SimpleNamespace
):
53 table
: Dict
[str, ChannelTableEntry
]
56 class VerificationError(Exception):
62 def __init__(self
) -> None:
65 def status(self
, s
: str) -> None:
66 print(s
, end
=' ', file=sys
.stderr
, flush
=True)
67 self
.line_length
+= 1 + len(s
) # Unicode??
70 def _color(s
: str, c
: int) -> str:
71 return '\033[%2dm%s\033[00m' % (c
, s
)
73 def result(self
, r
: bool) -> None:
74 message
, color
= {True: ('OK ', 92), False: ('FAIL', 91)}
[r
]
76 cols
= shutil
.get_terminal_size().columns
77 pad
= (cols
- (self
.line_length
+ length
)) % cols
78 print(' ' * pad
+ self
._color
(message
, color
), file=sys
.stderr
)
81 raise VerificationError()
83 def check(self
, s
: str, r
: bool) -> None:
91 def compare(a
: str, b
: str) -> Tuple
[List
[str], List
[str], List
[str]]:
93 def throw(error
: OSError) -> None:
96 def join(x
: str, y
: str) -> str:
97 return y
if x
== '.' else os
.path
.join(x
, y
)
99 def recursive_files(d
: str) -> Iterable
[str]:
100 all_files
: List
[str] = []
101 for path
, dirs
, files
in os
.walk(d
, onerror
=throw
):
102 rel
= os
.path
.relpath(path
, start
=d
)
103 all_files
.extend(join(rel
, f
) for f
in files
)
104 for dir_or_link
in dirs
:
105 if os
.path
.islink(join(path
, dir_or_link
)):
106 all_files
.append(join(rel
, dir_or_link
))
109 def exclude_dot_git(files
: Iterable
[str]) -> Iterable
[str]:
110 return (f
for f
in files
if not f
.startswith('.git/'))
112 files
= functools
.reduce(
115 recursive_files(x
))) for x
in [a
, b
]))
116 return filecmp
.cmpfiles(a
, b
, files
, shallow
=False)
119 def fetch(v
: Verification
, channel
: Channel
) -> None:
120 v
.status('Fetching channel')
121 request
= urllib
.request
.urlopen(channel
.channel_url
, timeout
=10)
122 channel
.channel_html
= request
.read()
123 channel
.forwarded_url
= request
.geturl()
124 v
.result(request
.status
== 200)
125 v
.check('Got forwarded', channel
.channel_url
!= channel
.forwarded_url
)
128 def parse_channel(v
: Verification
, channel
: Channel
) -> None:
129 v
.status('Parsing channel description as XML')
130 d
= xml
.dom
.minidom
.parseString(channel
.channel_html
)
133 v
.status('Extracting release name:')
134 title_name
= d
.getElementsByTagName(
135 'title')[0].firstChild
.nodeValue
.split()[2]
136 h1_name
= d
.getElementsByTagName('h1')[0].firstChild
.nodeValue
.split()[2]
138 v
.result(title_name
== h1_name
)
139 channel
.release_name
= title_name
141 v
.status('Extracting git commit:')
142 git_commit_node
= d
.getElementsByTagName('tt')[0]
143 channel
.git_revision
= git_commit_node
.firstChild
.nodeValue
144 v
.status(channel
.git_revision
)
146 v
.status('Verifying git commit label')
147 v
.result(git_commit_node
.previousSibling
.nodeValue
== 'Git commit ')
149 v
.status('Parsing table')
151 for row
in d
.getElementsByTagName('tr')[1:]:
152 name
= row
.childNodes
[0].firstChild
.firstChild
.nodeValue
153 url
= row
.childNodes
[0].firstChild
.getAttribute('href')
154 size
= int(row
.childNodes
[1].firstChild
.nodeValue
)
155 digest
= Digest16(row
.childNodes
[2].firstChild
.firstChild
.nodeValue
)
156 channel
.table
[name
] = ChannelTableEntry(
157 url
=url
, digest
=digest
, size
=size
)
161 def digest_string(s
: bytes) -> Digest16
:
162 return Digest16(hashlib
.sha256(s
).hexdigest())
165 def digest_file(filename
: str) -> Digest16
:
166 hasher
= hashlib
.sha256()
167 with open(filename
, 'rb') as f
:
168 # pylint: disable=cell-var-from-loop
169 for block
in iter(lambda: f
.read(4096), b
''):
171 return Digest16(hasher
.hexdigest())
174 def to_Digest16(v
: Verification
, digest32
: Digest32
) -> Digest16
:
175 v
.status('Converting digest to base16')
176 process
= subprocess
.run(
177 ['nix', 'to-base16', '--type', 'sha256', digest32
], capture_output
=True)
178 v
.result(process
.returncode
== 0)
179 return Digest16(process
.stdout
.decode().strip())
182 def to_Digest32(v
: Verification
, digest16
: Digest16
) -> Digest32
:
183 v
.status('Converting digest to base32')
184 process
= subprocess
.run(
185 ['nix', 'to-base32', '--type', 'sha256', digest16
], capture_output
=True)
186 v
.result(process
.returncode
== 0)
187 return Digest32(process
.stdout
.decode().strip())
190 def fetch_with_nix_prefetch_url(
193 digest
: Digest16
) -> str:
194 v
.status('Fetching %s' % url
)
195 process
= subprocess
.run(
196 ['nix-prefetch-url', '--print-path', url
, digest
], capture_output
=True)
197 v
.result(process
.returncode
== 0)
198 prefetch_digest
, path
, empty
= process
.stdout
.decode().split('\n')
200 v
.check("Verifying nix-prefetch-url's digest",
201 to_Digest16(v
, Digest32(prefetch_digest
)) == digest
)
202 v
.status("Verifying file digest")
203 file_digest
= digest_file(path
)
204 v
.result(file_digest
== digest
)
208 def fetch_resources(v
: Verification
, channel
: Channel
) -> None:
209 for resource
in ['git-revision', 'nixexprs.tar.xz']:
210 fields
= channel
.table
[resource
]
211 fields
.absolute_url
= urllib
.parse
.urljoin(
212 channel
.forwarded_url
, fields
.url
)
213 fields
.file = fetch_with_nix_prefetch_url(
214 v
, fields
.absolute_url
, fields
.digest
)
215 v
.status('Verifying git commit on main page matches git commit in table')
218 channel
.table
['git-revision'].file).read(999) == channel
.git_revision
)
221 def git_cachedir(git_repo
: str) -> str:
229 def verify_git_ancestry(v
: Verification
, channel
: Channel
) -> None:
230 cachedir
= git_cachedir(channel
.git_repo
)
231 v
.status('Verifying rev is an ancestor of ref')
232 process
= subprocess
.run(['git',
237 channel
.git_revision
,
239 v
.result(process
.returncode
== 0)
241 if hasattr(channel
, 'old_git_revision'):
243 'Verifying rev is an ancestor of previous rev %s' %
244 channel
.old_git_revision
)
245 process
= subprocess
.run(['git',
250 channel
.old_git_revision
,
251 channel
.git_revision
])
252 v
.result(process
.returncode
== 0)
255 def git_fetch(v
: Verification
, channel
: Channel
) -> None:
256 # It would be nice if we could share the nix git cache, but as of the time
257 # of writing it is transitioning from gitv2 (deprecated) to gitv3 (not ready
258 # yet), and trying to straddle them both is too far into nix implementation
259 # details for my comfort. So we re-implement here half of nix.fetchGit.
262 cachedir
= git_cachedir(channel
.git_repo
)
263 if not os
.path
.exists(cachedir
):
264 v
.status("Initializing git repo")
265 process
= subprocess
.run(
266 ['git', 'init', '--bare', cachedir
])
267 v
.result(process
.returncode
== 0)
269 v
.status('Fetching ref "%s" from %s' % (channel
.git_ref
, channel
.git_repo
))
270 # We don't use --force here because we want to abort and freak out if forced
271 # updates are happening.
272 process
= subprocess
.run(['git',
277 '%s:%s' % (channel
.git_ref
,
279 v
.result(process
.returncode
== 0)
281 if hasattr(channel
, 'git_revision'):
282 v
.status('Verifying that fetch retrieved this rev')
283 process
= subprocess
.run(
284 ['git', '-C', cachedir
, 'cat-file', '-e', channel
.git_revision
])
285 v
.result(process
.returncode
== 0)
287 channel
.git_revision
= open(
292 channel
.git_ref
)).read(999).strip()
294 verify_git_ancestry(v
, channel
)
297 def ensure_git_rev_available(v
: Verification
, channel
: Channel
) -> None:
298 cachedir
= git_cachedir(channel
.git_repo
)
299 if os
.path
.exists(cachedir
):
300 v
.status('Checking if we already have this rev:')
301 process
= subprocess
.run(
302 ['git', '-C', cachedir
, 'cat-file', '-e', channel
.git_revision
])
303 if process
.returncode
== 0:
305 if process
.returncode
== 1:
307 v
.result(process
.returncode
== 0 or process
.returncode
== 1)
308 if process
.returncode
== 0:
309 verify_git_ancestry(v
, channel
)
311 git_fetch(v
, channel
)
314 def compare_tarball_and_git(
317 channel_contents
: str,
318 git_contents
: str) -> None:
319 v
.status('Comparing channel tarball with git checkout')
320 match
, mismatch
, errors
= compare(os
.path
.join(
321 channel_contents
, channel
.release_name
), git_contents
)
323 v
.check('%d files match' % len(match
), len(match
) > 0)
324 v
.check('%d files differ' % len(mismatch
), len(mismatch
) == 0)
332 for ee
in expected_errors
:
335 benign_errors
.append(ee
)
337 '%d unexpected incomparable files' %
341 '(%d of %d expected incomparable files)' %
343 len(expected_errors
)),
344 len(benign_errors
) == len(expected_errors
))
347 def extract_tarball(v
: Verification
, channel
: Channel
, dest
: str) -> None:
348 v
.status('Extracting tarball %s' %
349 channel
.table
['nixexprs.tar.xz'].file)
350 shutil
.unpack_archive(
351 channel
.table
['nixexprs.tar.xz'].file,
356 def git_checkout(v
: Verification
, channel
: Channel
, dest
: str) -> None:
357 v
.status('Checking out corresponding git revision')
358 git
= subprocess
.Popen(['git',
360 git_cachedir(channel
.git_repo
),
362 channel
.git_revision
],
363 stdout
=subprocess
.PIPE
)
364 tar
= subprocess
.Popen(
365 ['tar', 'x', '-C', dest
, '-f', '-'], stdin
=git
.stdout
)
370 v
.result(git
.returncode
== 0 and tar
.returncode
== 0)
373 def git_get_tarball(v
: Verification
, channel
: Channel
) -> str:
374 with tempfile
.TemporaryDirectory() as output_dir
:
375 output_filename
= os
.path
.join(
376 output_dir
, channel
.release_name
+ '.tar.xz')
377 with open(output_filename
, 'w') as output_file
:
379 'Generating tarball for git revision %s' %
380 channel
.git_revision
)
381 git
= subprocess
.Popen(['git',
383 git_cachedir(channel
.git_repo
),
385 '--prefix=%s/' % channel
.release_name
,
386 channel
.git_revision
],
387 stdout
=subprocess
.PIPE
)
388 xz
= subprocess
.Popen(['xz'], stdin
=git
.stdout
, stdout
=output_file
)
391 v
.result(git
.returncode
== 0 and xz
.returncode
== 0)
393 v
.status('Putting tarball in Nix store')
394 process
= subprocess
.run(
395 ['nix-store', '--add', output_filename
], capture_output
=True)
396 v
.result(process
.returncode
== 0)
397 return process
.stdout
.decode().strip()
400 def check_channel_metadata(
403 channel_contents
: str) -> None:
404 v
.status('Verifying git commit in channel tarball')
409 channel
.release_name
,
410 '.git-revision')).read(999) == channel
.git_revision
)
413 'Verifying version-suffix is a suffix of release name %s:' %
414 channel
.release_name
)
415 version_suffix
= open(
418 channel
.release_name
,
419 '.version-suffix')).read(999)
420 v
.status(version_suffix
)
421 v
.result(channel
.release_name
.endswith(version_suffix
))
424 def check_channel_contents(v
: Verification
, channel
: Channel
) -> None:
425 with tempfile
.TemporaryDirectory() as channel_contents
, \
426 tempfile
.TemporaryDirectory() as git_contents
:
428 extract_tarball(v
, channel
, channel_contents
)
429 check_channel_metadata(v
, channel
, channel_contents
)
431 git_checkout(v
, channel
, git_contents
)
433 compare_tarball_and_git(v
, channel
, channel_contents
, git_contents
)
435 v
.status('Removing temporary directories')
439 def pin_channel(v
: Verification
, channel
: Channel
) -> None:
441 parse_channel(v
, channel
)
442 fetch_resources(v
, channel
)
443 ensure_git_rev_available(v
, channel
)
444 check_channel_contents(v
, channel
)
447 def git_revision_name(v
: Verification
, channel
: Channel
) -> str:
448 v
.status('Getting commit date')
449 process
= subprocess
.run(['git',
451 git_cachedir(channel
.git_repo
),
456 channel
.git_revision
],
458 v
.result(process
.returncode
== 0 and process
.stdout
!= b
'')
459 return '%s-%s' % (os
.path
.basename(channel
.git_repo
),
460 process
.stdout
.decode().strip())
463 def read_config(filename
: str) -> configparser
.ConfigParser
:
464 config
= configparser
.ConfigParser()
465 config
.read_file(open(filename
), filename
)
469 def pin(args
: argparse
.Namespace
) -> None:
471 config
= read_config(args
.channels_file
)
472 for section
in config
.sections():
473 if args
.channels
and section
not in args
.channels
:
476 channel
= Channel(**dict(config
[section
].items()))
478 if hasattr(channel
, 'alias_of'):
479 assert not hasattr(channel
, 'git_repo')
482 if hasattr(channel
, 'git_revision'):
483 channel
.old_git_revision
= channel
.git_revision
484 del channel
.git_revision
486 if 'channel_url' in config
[section
]:
487 pin_channel(v
, channel
)
488 config
[section
]['release_name'] = channel
.release_name
489 config
[section
]['tarball_url'] = channel
.table
['nixexprs.tar.xz'].absolute_url
490 config
[section
]['tarball_sha256'] = channel
.table
['nixexprs.tar.xz'].digest
492 git_fetch(v
, channel
)
493 config
[section
]['release_name'] = git_revision_name(v
, channel
)
494 config
[section
]['git_revision'] = channel
.git_revision
496 with open(args
.channels_file
, 'w') as configfile
:
497 config
.write(configfile
)
500 def update(args
: argparse
.Namespace
) -> None:
502 config
= configparser
.ConfigParser()
503 exprs
: Dict
[str, str] = {}
504 configs
= [read_config(filename
) for filename
in args
.channels_file
]
505 for config
in configs
:
506 for section
in config
.sections():
508 if 'alias_of' in config
[section
]:
509 assert 'git_repo' not in config
[section
]
512 if 'git_repo' not in config
[section
] or 'release_name' not in config
[section
]:
514 'Cannot update unpinned channel "%s" (Run "pin" before "update")' %
517 if 'channel_url' in config
[section
]:
518 tarball
= fetch_with_nix_prefetch_url(
519 v
, config
[section
]['tarball_url'], Digest16(
520 config
[section
]['tarball_sha256']))
522 channel
= Channel(**dict(config
[section
].items()))
523 ensure_git_rev_available(v
, channel
)
524 tarball
= git_get_tarball(v
, channel
)
527 raise Exception('Duplicate channel "%s"' % section
)
529 'f: f { name = "%s"; channelName = "%%s"; src = builtins.storePath "%s"; }' %
530 (config
[section
]['release_name'], tarball
))
532 for config
in configs
:
533 for section
in config
.sections():
534 if 'alias_of' in config
[section
]:
536 raise Exception('Duplicate channel "%s"' % section
)
537 exprs
[section
] = exprs
[str(config
[section
]['alias_of'])]
542 '/nix/var/nix/profiles/per-user/%s/channels' %
546 '<nix/unpack-channel.nix>',
548 '--from-expression'] + [exprs
[name
] % name
for name
in sorted(exprs
.keys())]
550 print(' '.join(map(shlex
.quote
, command
)))
552 v
.status('Installing channels with nix-env')
553 process
= subprocess
.run(command
)
554 v
.result(process
.returncode
== 0)
558 parser
= argparse
.ArgumentParser(prog
='pinch')
559 subparsers
= parser
.add_subparsers(dest
='mode', required
=True)
560 parser_pin
= subparsers
.add_parser('pin')
561 parser_pin
.add_argument('channels_file', type=str)
562 parser_pin
.add_argument('channels', type=str, nargs
='*')
563 parser_pin
.set_defaults(func
=pin
)
564 parser_update
= subparsers
.add_parser('update')
565 parser_update
.add_argument('--dry-run', action
='store_true')
566 parser_update
.add_argument('channels_file', type=str, nargs
='+')
567 parser_update
.set_defaults(func
=update
)
568 args
= parser
.parse_args()