Skip to content

Commit

Permalink
Directly use python’s urllib instead of curl in etc/dl-snapshot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
linkmauve committed Nov 17, 2014
1 parent a3fd7bd commit aceba88
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/etc/dl-snapshot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import distutils.spawn
import hashlib
import os
import subprocess
import sys
import tarfile
import shutil

f = open('src/snapshots.txt')
lines = f.readlines()
try:
from urllib.request import urlopen
except ImportError:
# We are in python2
from urllib2 import urlopen as urlopen2
from contextlib import closing
urlopen = lambda url: closing(urlopen2(url))

with open('src/snapshots.txt') as f:
lines = f.readlines()

date = lines[0]
linux32 = lines[1]
Expand All @@ -33,7 +40,7 @@
else:
raise Exception("no snapshot for the triple: " + triple)

platform, hash = me.strip().split(' ')
platform, hash = me.strip().split()

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

ret = subprocess.call(["curl", "-o", dl_path, url])
if ret != 0:
raise Exception("failed to fetch url")
h = hashlib.sha1(open(dl_path, 'rb').read()).hexdigest()
if h != hash:
raise Exception("failed to verify the checksum of the snapshot")
with urlopen(url) as in_file:
data = in_file.read()
h = hashlib.sha1(data).hexdigest()
if h != hash:
raise Exception("failed to verify the checksum of the snapshot")
with open(dl_path, 'wb') as out_file:
out_file.write(data)

tar = tarfile.open(dl_path)
for p in tar.getnames():
name = p.replace("cargo-nightly-" + triple + "/", "", 1)
fp = os.path.join(dst, name)
print("extracting " + p)
tar.extract(p, dst)
tp = os.path.join(dst, p)
if os.path.isdir(tp) and os.path.exists(fp):
continue
shutil.move(tp, fp)
tar.close()
with tarfile.open(dl_path) as tar:
for p in tar.getnames():
name = p.replace("cargo-nightly-" + triple + "/", "", 1)
fp = os.path.join(dst, name)
print("extracting " + p)
tar.extract(p, dst)
tp = os.path.join(dst, p)
if os.path.isdir(tp) and os.path.exists(fp):
continue
shutil.move(tp, fp)
shutil.rmtree(os.path.join(dst, 'cargo-nightly-' + triple))

9 comments on commit aceba88

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging linkmauve/cargo/master = aceba88 into auto-cargo

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linkmauve/cargo/master = aceba88 merged ok, testing candidate = ddc791a0

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging linkmauve/cargo/master = aceba88 into auto-cargo

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linkmauve/cargo/master = aceba88 merged ok, testing candidate = 36be045

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on aceba88 Nov 18, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto-cargo = 36be045

Please sign in to comment.