|
1 | 1 | import distutils.spawn
|
2 | 2 | import hashlib
|
3 | 3 | import os
|
4 |
| -import subprocess |
5 | 4 | import sys
|
6 | 5 | import tarfile
|
7 | 6 | import shutil
|
8 | 7 |
|
9 |
| -f = open('src/snapshots.txt') |
10 |
| -lines = f.readlines() |
| 8 | +try: |
| 9 | + from urllib.request import urlopen |
| 10 | +except ImportError: |
| 11 | + # We are in python2 |
| 12 | + from urllib2 import urlopen as urlopen2 |
| 13 | + from contextlib import closing |
| 14 | + urlopen = lambda url: closing(urlopen2(url)) |
| 15 | + |
| 16 | +with open('src/snapshots.txt') as f: |
| 17 | + lines = f.readlines() |
11 | 18 |
|
12 | 19 | date = lines[0]
|
13 | 20 | linux32 = lines[1]
|
|
33 | 40 | else:
|
34 | 41 | raise Exception("no snapshot for the triple: " + triple)
|
35 | 42 |
|
36 |
| -platform, hash = me.strip().split(' ') |
| 43 | +platform, hash = me.strip().split() |
37 | 44 |
|
38 | 45 | tarball = 'cargo-nightly-' + triple + '.tar.gz'
|
39 | 46 | url = 'https://static-rust-lang-org.s3.amazonaws.com/cargo-dist/' + date.strip() + '/' + tarball
|
|
46 | 53 | if os.path.isdir(dst):
|
47 | 54 | shutil.rmtree(dst)
|
48 | 55 |
|
49 |
| -ret = subprocess.call(["curl", "-o", dl_path, url]) |
50 |
| -if ret != 0: |
51 |
| - raise Exception("failed to fetch url") |
52 |
| -h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest() |
53 |
| -if h != hash: |
54 |
| - raise Exception("failed to verify the checksum of the snapshot") |
| 56 | +with urlopen(url) as in_file: |
| 57 | + data = in_file.read() |
| 58 | + h = hashlib.sha1(data).hexdigest() |
| 59 | + if h != hash: |
| 60 | + raise Exception("failed to verify the checksum of the snapshot") |
| 61 | + with open(dl_path, 'wb') as out_file: |
| 62 | + out_file.write(data) |
55 | 63 |
|
56 |
| -tar = tarfile.open(dl_path) |
57 |
| -for p in tar.getnames(): |
58 |
| - name = p.replace("cargo-nightly-" + triple + "/", "", 1) |
59 |
| - fp = os.path.join(dst, name) |
60 |
| - print("extracting " + p) |
61 |
| - tar.extract(p, dst) |
62 |
| - tp = os.path.join(dst, p) |
63 |
| - if os.path.isdir(tp) and os.path.exists(fp): |
64 |
| - continue |
65 |
| - shutil.move(tp, fp) |
66 |
| -tar.close() |
| 64 | +with tarfile.open(dl_path) as tar: |
| 65 | + for p in tar.getnames(): |
| 66 | + name = p.replace("cargo-nightly-" + triple + "/", "", 1) |
| 67 | + fp = os.path.join(dst, name) |
| 68 | + print("extracting " + p) |
| 69 | + tar.extract(p, dst) |
| 70 | + tp = os.path.join(dst, p) |
| 71 | + if os.path.isdir(tp) and os.path.exists(fp): |
| 72 | + continue |
| 73 | + shutil.move(tp, fp) |
67 | 74 | shutil.rmtree(os.path.join(dst, 'cargo-nightly-' + triple))
|
0 commit comments