Skip to content

Commit 365155a

Browse files
committed
Added simple progress bar to downloads. Closes #152.
1 parent 1d4af5c commit 365155a

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

wrap.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,53 @@ def get_git(self, p):
9090

9191

9292
def get_data(self, url):
93-
u = urllib.request.urlopen(url)
94-
data = u.read()
95-
u.close()
93+
blocksize = 10*1024
94+
resp = urllib.request.urlopen(url)
95+
dlsize = int(resp.info()['Content-Length'])
96+
print('Download size:', dlsize)
97+
print('Downloading: ', end='')
98+
sys.stdout.flush()
99+
printed_dots = 0
100+
blocks = []
101+
downloaded = 0
102+
while True:
103+
block = resp.read(blocksize)
104+
if block == b'':
105+
break
106+
downloaded += len(block)
107+
blocks.append(block)
108+
ratio = int(downloaded/dlsize * 10)
109+
while printed_dots < ratio:
110+
print('.', end='')
111+
sys.stdout.flush()
112+
printed_dots += 1
113+
print('')
114+
resp.close()
115+
return b''.join(blocks)
116+
117+
def get_hash(self, data):
96118
h = hashlib.sha256()
97119
h.update(data)
98120
hashvalue = h.hexdigest()
99-
return (data, hashvalue)
121+
return hashvalue
100122

101123
def download(self, p, packagename):
102124
ofname = os.path.join(self.cachedir, p.get('source_filename'))
103125
if os.path.exists(ofname):
104126
mlog.log('Using', mlog.bold(packagename), 'from cache.')
105127
return
106128
srcurl = p.get('source_url')
107-
mlog.log('Dowloading', mlog.bold(packagename), 'from', srcurl)
108-
(srcdata, dhash) = self.get_data(srcurl)
129+
mlog.log('Dowloading', mlog.bold(packagename), 'from', mlog.bold(srcurl))
130+
srcdata = self.get_data(srcurl)
131+
dhash = self.get_hash(srcdata)
109132
expected = p.get('source_hash')
110133
if dhash != expected:
111134
raise RuntimeError('Incorrect hash for source %s:\n %s expected\n %s actual.' % (packagename, expected, dhash))
112135
if p.has_patch():
113136
purl = p.get('patch_url')
114137
mlog.log('Downloading patch from', mlog.bold(purl))
115-
(pdata, phash) = self.get_data(purl)
138+
pdata = self.get_data(purl)
139+
phash = self.get_hash(pdata)
116140
expected = p.get('patch_hash')
117141
if phash != expected:
118142
raise RuntimeError('Incorrect hash for patch %s:\n %s expected\n %s actual' % (packagename, expected, phash))

0 commit comments

Comments
 (0)