-
Notifications
You must be signed in to change notification settings - Fork 46
/
tarballfetcher.py
53 lines (41 loc) · 1.55 KB
/
tarballfetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import hashlib
import os
import sys
import tarfile
try:
from urllib import urlretrieve
from urlparse import urlparse
except ImportError:
from urllib.request import urlretrieve
from urllib.parse import urlparse
def download_file(url, filename):
sys.stdout.write('Downloading %s... ' % url)
sys.stdout.flush()
urlretrieve(url, filename)
sys.stdout.write('DONE\n')
def extract_tarball(tarball_filename):
tarball = tarfile.open(tarball_filename, 'r:gz')
sys.stdout.write('Extracting %s... ' % tarball_filename)
sys.stdout.flush()
tarball.extractall('.')
sys.stdout.write('DONE\n')
def sha256_file(filename):
return hashlib.sha256(open(filename, 'rb').read()).hexdigest()
def download_and_extract_tarball(tarball_url,
tarball_filename=None,
expected_sha256=None):
if tarball_filename is None:
tarball_filename = os.path.basename(urlparse(tarball_url).path)
if not os.path.exists(tarball_filename):
download_file(tarball_url, tarball_filename)
if expected_sha256 is not None:
sys.stdout.write('Checking that SHA256 of %s is %s... ' %
(tarball_filename, expected_sha256))
actual_sha256 = sha256_file(tarball_filename)
sys.stdout.write('SHA256 is %s. ' % actual_sha256)
if actual_sha256 == expected_sha256:
sys.stdout.write('OK\n')
else:
sys.stdout.write('Incorrect SHA256!\n')
sys.exit(1)
extract_tarball(tarball_filename)