Skip to content

Commit aceba88

Browse files
committed
Directly use python’s urllib instead of curl in etc/dl-snapshot.py
1 parent a3fd7bd commit aceba88

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/etc/dl-snapshot.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import distutils.spawn
22
import hashlib
33
import os
4-
import subprocess
54
import sys
65
import tarfile
76
import shutil
87

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()
1118

1219
date = lines[0]
1320
linux32 = lines[1]
@@ -33,7 +40,7 @@
3340
else:
3441
raise Exception("no snapshot for the triple: " + triple)
3542

36-
platform, hash = me.strip().split(' ')
43+
platform, hash = me.strip().split()
3744

3845
tarball = 'cargo-nightly-' + triple + '.tar.gz'
3946
url = 'https://static-rust-lang-org.s3.amazonaws.com/cargo-dist/' + date.strip() + '/' + tarball
@@ -46,22 +53,22 @@
4653
if os.path.isdir(dst):
4754
shutil.rmtree(dst)
4855

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)
5563

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)
6774
shutil.rmtree(os.path.join(dst, 'cargo-nightly-' + triple))

0 commit comments

Comments
 (0)