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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# Author: Manuel Kaufmann
# License: GPL
# Version: 0.3
import os
import urllib
import urllib2
import sys
try:
from bs4 import BeautifulSoup # rpm: python-beautifulsoup4
except ImportError:
print 'You don\'t have "python-beautifulsoup4" package'
print 'Please install it running "sudo yum install python-beautifulsoup4"'
sys.exit(1)
# http://code.activestate.com/recipes/576530-download-a-url-with-a-console-progress-meter/
def _reporthook(numblocks, blocksize, filesize, url=None):
#print "reporthook(%s, %s, %s)" % (numblocks, blocksize, filesize)
base = os.path.basename(url)
#XXX Should handle possible filesize=-1.
try:
percent = min((numblocks * blocksize * 100) / filesize, 100)
except:
percent = 100
if numblocks != 0:
sys.stdout.write("\b" * 70)
sys.stdout.write("%-66s%3d%%" % (base, percent))
# TODO: pass version via argument
VERSION = '13.1.0'
XO_VERSIONS = {
'xo-1.75': '2',
'xo-4': '4',
'xo-1.5': '1',
}
HOME_URL = 'http://build.laptop.org/%(version)s' % {'version': VERSION}
FILE_URL = '%(home_url)s/%(os)s/%(xo_version)s/%(filename)s'
OUTPUT_PATH = sys.argv[1] or '/tmp/OLPC-XO'
# Get latest version
html = urllib2.urlopen(HOME_URL).read()
soup = BeautifulSoup(html)
links = soup.find_all('a')
latest_os = links[-1].text[:-1]
print 'LATEST XO RELEASE:', latest_os
for v in XO_VERSIONS:
# Generate filename
filename = VERSION.replace('.', '')[1:] + latest_os[2:].zfill(2) + \
'o' + XO_VERSIONS[v] + '.zd'
print 'FILENAME:', filename
output_path = os.path.join(OUTPUT_PATH, VERSION, latest_os, v)
# Create directories
try:
os.makedirs(output_path)
except OSError:
# This directory alredy exists
pass
output_filename = os.path.join(output_path, filename)
print 'OUTPUT_PATH:', output_filename
if os.path.exists(output_filename):
print 'Already downloaded, skipping...'
print
continue
url = FILE_URL % \
{'os': latest_os,
'xo_version': v,
'filename': filename,
'home_url': HOME_URL,
}
print 'Downloading', url, '...'
urllib.urlretrieve(
url, output_filename,
lambda nb, bs, fs, url=url: _reporthook(nb, bs, fs, url))
print '\n\n'
print 'DONE!'
|