Mercurial > hg
comparison contrib/packaging/hgpackaging/downloads.py @ 43076:2372284d9457
formatting: blacken the codebase
This is using my patch to black
(https://github.com/psf/black/pull/826) so we don't un-wrap collection
literals.
Done with:
hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S
# skip-blame mass-reformatting only
# no-check-commit reformats foo_bar functions
Differential Revision: https://phab.mercurial-scm.org/D6971
author | Augie Fackler <augie@google.com> |
---|---|
date | Sun, 06 Oct 2019 09:45:02 -0400 |
parents | b83de9150c1c |
children | 17d5e25b8e78 |
comparison
equal
deleted
inserted
replaced
43075:57875cf423c9 | 43076:2372284d9457 |
---|---|
96 """ | 96 """ |
97 h = hashlib.sha256() | 97 h = hashlib.sha256() |
98 length = 0 | 98 length = 0 |
99 | 99 |
100 with urllib.request.urlopen(url) as fh: | 100 with urllib.request.urlopen(url) as fh: |
101 if not url.endswith('.gz') and fh.info().get('Content-Encoding') == 'gzip': | 101 if ( |
102 not url.endswith('.gz') | |
103 and fh.info().get('Content-Encoding') == 'gzip' | |
104 ): | |
102 fh = gzip.GzipFile(fileobj=fh) | 105 fh = gzip.GzipFile(fileobj=fh) |
103 | 106 |
104 while True: | 107 while True: |
105 chunk = fh.read(65536) | 108 chunk = fh.read(65536) |
106 if not chunk: | 109 if not chunk: |
112 yield chunk | 115 yield chunk |
113 | 116 |
114 digest = h.hexdigest() | 117 digest = h.hexdigest() |
115 | 118 |
116 if length != size: | 119 if length != size: |
117 raise IntegrityError('size mismatch on %s: wanted %d; got %d' % ( | 120 raise IntegrityError( |
118 url, size, length)) | 121 'size mismatch on %s: wanted %d; got %d' % (url, size, length) |
122 ) | |
119 | 123 |
120 if digest != sha256: | 124 if digest != sha256: |
121 raise IntegrityError('sha256 mismatch on %s: wanted %s; got %s' % ( | 125 raise IntegrityError( |
122 url, sha256, digest)) | 126 'sha256 mismatch on %s: wanted %s; got %s' % (url, sha256, digest) |
127 ) | |
123 | 128 |
124 | 129 |
125 def download_to_path(url: str, path: pathlib.Path, size: int, sha256: str): | 130 def download_to_path(url: str, path: pathlib.Path, size: int, sha256: str): |
126 """Download a URL to a filesystem path, possibly with verification.""" | 131 """Download a URL to a filesystem path, possibly with verification.""" |
127 | 132 |
160 | 165 |
161 tmp.rename(path) | 166 tmp.rename(path) |
162 print('successfully downloaded %s' % url) | 167 print('successfully downloaded %s' % url) |
163 | 168 |
164 | 169 |
165 def download_entry(name: dict, dest_path: pathlib.Path, local_name=None) -> pathlib.Path: | 170 def download_entry( |
171 name: dict, dest_path: pathlib.Path, local_name=None | |
172 ) -> pathlib.Path: | |
166 entry = DOWNLOADS[name] | 173 entry = DOWNLOADS[name] |
167 | 174 |
168 url = entry['url'] | 175 url = entry['url'] |
169 | 176 |
170 local_name = local_name or url[url.rindex('/') + 1:] | 177 local_name = local_name or url[url.rindex('/') + 1 :] |
171 | 178 |
172 local_path = dest_path / local_name | 179 local_path = dest_path / local_name |
173 download_to_path(url, local_path, entry['size'], entry['sha256']) | 180 download_to_path(url, local_path, entry['size'], entry['sha256']) |
174 | 181 |
175 return local_path, entry | 182 return local_path, entry |