(internal) Download an ``url`` to a ``target``.
(self, url, target, cwd=None)
| 207 | return environ.get(key, self._download_headers) |
| 208 | |
| 209 | def download_file(self, url, target, cwd=None): |
| 210 | """ |
| 211 | (internal) Download an ``url`` to a ``target``. |
| 212 | """ |
| 213 | if not url: |
| 214 | return |
| 215 | |
| 216 | info('Downloading {} from {}'.format(self.name, url)) |
| 217 | |
| 218 | if cwd: |
| 219 | target = join(cwd, target) |
| 220 | |
| 221 | parsed_url = urlparse(url) |
| 222 | if parsed_url.scheme in ('http', 'https'): |
| 223 | def report_hook(index, blksize, size): |
| 224 | if size <= 0: |
| 225 | progression = '{0} bytes'.format(index * blksize) |
| 226 | else: |
| 227 | progression = '{0:.2f}%'.format( |
| 228 | index * blksize * 100. / float(size)) |
| 229 | if "CI" not in environ: |
| 230 | stdout.write('- Download {}\r'.format(progression)) |
| 231 | stdout.flush() |
| 232 | |
| 233 | if exists(target): |
| 234 | unlink(target) |
| 235 | |
| 236 | # Download item with multiple attempts (for bad connections): |
| 237 | attempts = 0 |
| 238 | seconds = 1 |
| 239 | while True: |
| 240 | try: |
| 241 | # jqueryui.com returns a 403 w/ the default user agent |
| 242 | # Mozilla/5.0 does not handle redirection for liblzma |
| 243 | url_opener.addheaders = [('User-agent', 'Wget/1.0')] |
| 244 | if self.download_headers: |
| 245 | url_opener.addheaders += self.download_headers |
| 246 | urlretrieve(url, target, report_hook) |
| 247 | except OSError as e: |
| 248 | attempts += 1 |
| 249 | if attempts >= 5: |
| 250 | raise |
| 251 | stdout.write('Download failed: {}; retrying in {} second(s)...'.format(e, seconds)) |
| 252 | time.sleep(seconds) |
| 253 | seconds *= 2 |
| 254 | continue |
| 255 | finally: |
| 256 | url_opener.addheaders = url_orig_headers |
| 257 | break |
| 258 | return target |
| 259 | elif parsed_url.scheme in ('git', 'git+file', 'git+ssh', 'git+http', 'git+https'): |
| 260 | if not isdir(target): |
| 261 | if url.startswith('git+'): |
| 262 | url = url[4:] |
| 263 | # if 'version' is specified, do a shallow clone |
| 264 | if self.version: |
| 265 | ensure_dir(target) |
| 266 | with current_directory(target): |