blob: e7f000695a0c188b27076e20381652587b27b602 [file] [log] [blame]
Andy Doan15aa23b2012-02-02 16:40:58 -06001#!/usr/bin/env python
2# Copyright (C) 2012 Linaro
3#
4# Author: Andy Doan <andy.doan@linaro.org>
5#
6# This file is part of Linaro Daily Prebuilt Images.
7#
8# Linaro Daily Prebuilt Images is free software; you can redistribute it and/or
9# modify it under the terms of the GNU General Public License
10# as published by the Free Software Foundation; either version 2
11# of the License, or (at your option) any later version.
12#
13# Linaro Daily Prebuilt Images is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with Linaro Image Tools; if not, write to the Free Software
20# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21# USA.
22
Andy Doanfccf4842012-03-08 10:42:19 -060023from linaro_fetch_image import fetch_image
Andy Doan15aa23b2012-02-02 16:40:58 -060024from linaro_image_tools import cmd_runner
25
Andy Doan8ff38f02012-02-20 16:32:37 -060026import crawler
27
Andy Doan15aa23b2012-02-02 16:40:58 -060028import argparse
29import bz2
30import datetime
Andy Doan3634c472012-04-17 12:50:58 -050031import hashlib
Andy Doanc6500af2012-03-15 14:44:39 -050032import os
Andy Doan15aa23b2012-02-02 16:40:58 -060033import re
Andy Doane449bce2012-03-21 15:22:31 -050034import urlparse
Andy Doan4202d882012-03-29 21:36:09 -050035import sys
Andy Doan15aa23b2012-02-02 16:40:58 -060036
Andy Doanea2e1ea2012-04-23 11:56:29 -050037class HwPack:
38 def __init__(self, lmcname, eula=False, pre_inst_script=None):
39 self.lmcname = lmcname
40 if eula:
41 self.eula = 'EULA.txt'
42 else:
43 self.eula = 'OPEN_EULA.txt'
44 self.pre_inst_script = pre_inst_script
45
46 def do_eula(self, imgfile):
47 fname = '%s/%s' % (os.path.dirname(imgfile), self.eula)
48 with file(fname, 'a'):
49 pass #just need the file to exist
50
Andy Doan15aa23b2012-02-02 16:40:58 -060051# a mapping of hwpack name to l-m-c name
52HWPACKS = {
Andy Doanea2e1ea2012-04-23 11:56:29 -050053 'beagleboard': HwPack('beagle'),
54 'overo': HwPack('overo'),
55 'lt-panda': HwPack('panda'),
56 'lt-mx5': HwPack('mx53loco'),
57 'lt-mx6': HwPack('mx6qsabrelite'),
58 'lt-origen': HwPack('origen', True),
59 'leb-origen': HwPack('origen', True),
60 'lt-snowball': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
61 'lt-snowball-x11-base': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
62 'vexpress-a9': HwPack('vexpress-a9'),
63 'efikamx': HwPack('efikamx'),
64 'igep': HwPack('igep'),
Andy Doan15aa23b2012-02-02 16:40:58 -060065}
66# a mapping of binary image to its image_file size
67BINARIES = {
Andy Doan4202d882012-03-29 21:36:09 -050068 # release images
69 'nano': '512M',
70 'alip': '2G',
71 'ubuntu-desktop': '3G',
72 'linarotv-xbmc': '3G',
Andy Doan15aa23b2012-02-02 16:40:58 -060073}
74
Andy Doan15aa23b2012-02-02 16:40:58 -060075def today():
76 d = datetime.date.today()
77 return "%d%02d%02d" % (d.year, d.month, d.day)
78
Andy Doan8ff38f02012-02-20 16:32:37 -060079def hwpack_available(date, hwpack, platform):
Andy Doan540a4b12012-04-17 11:48:11 -050080 url = 'http://snapshots.linaro.org/%s/hwpacks/%s' % (platform, hwpack)
Andy Doan8ff38f02012-02-20 16:32:37 -060081 hwpacks = crawler.latest_hwpacks(url)
82 for hwpack in hwpacks:
83 if hwpack[0] == date:
84 return hwpack[1]
85
Andy Doan15aa23b2012-02-02 16:40:58 -060086 return False
87
88def compress_image(imgfile):
89 print "compressing %s" % imgfile
90 args = ('bzip2', imgfile)
91 cmd_runner.run(args).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -050092 return '%s.bz2' % imgfile
93
94def zsync_image(imgfile):
95 print "making zsync file for %s" % imgfile
96 args = ('zsyncmake', '-o', '%s.zsync'%imgfile, imgfile)
97 cmd_runner.run(args).wait()
Andy Doan15aa23b2012-02-02 16:40:58 -060098
Andy Doan3634c472012-04-17 12:50:58 -050099def md5sum(fname):
100 md5 = hashlib.md5()
101 with open(fname, 'rb') as f:
102 while True:
103 data = f.read(4096)
104 if not data: break
105 md5.update(data)
106 return md5.hexdigest()
107
108def image_info(bz2file, binary_file):
109 ''' prints out the md5sum and rfs info for the given image
110 '''
111 print "building image info for: %s" % bz2file
112 md5 = md5sum(bz2file)
113
114 binary_file = os.path.basename(binary_file)
115
Andy Doand7aa58e2012-04-23 11:58:22 -0500116 with open('%s.info.txt' % bz2file, 'w') as f:
Andy Doan3634c472012-04-17 12:50:58 -0500117 f.write("%s md5sum(%s)\n" % (os.path.basename(bz2file), md5))
118 f.write("root file system: %s\n" % binary_file)
119
Andy Doanb00dbc02012-03-15 16:23:06 -0500120def build_image(lmc, imgfile, hwpack, hwpack_file, binary, binary_file):
Andy Doan15aa23b2012-02-02 16:40:58 -0600121 print "building image: %s" % imgfile
Andy Doanea2e1ea2012-04-23 11:56:29 -0500122 hwpi = HWPACKS[hwpack]
Andy Doan15aa23b2012-02-02 16:40:58 -0600123 size = BINARIES[binary]
124
Andy Doan4fa16dc2012-02-09 17:47:45 -0800125 print 'running l-m-c: %s' % lmc
Andy Doanc6500af2012-03-15 14:44:39 -0500126 args = [lmc,
Andy Doanea2e1ea2012-04-23 11:56:29 -0500127 '--dev', hwpi.lmcname,
Andy Doan15aa23b2012-02-02 16:40:58 -0600128 '--image_file', imgfile,
129 '--image_size', size,
130 '--hwpack-force-yes',
131 '--hwpack', hwpack_file,
132 '--binary', binary_file,
Andy Doanc6500af2012-03-15 14:44:39 -0500133 ]
134
Andy Doanea2e1ea2012-04-23 11:56:29 -0500135 if hwpi.pre_inst_script is not None:
Andy Doanc6500af2012-03-15 14:44:39 -0500136 sdir = os.path.abspath(os.path.dirname(__file__))
Andy Doanea2e1ea2012-04-23 11:56:29 -0500137 script = "%s/%s" % (sdir, hwpi.pre_inst_script)
Andy Doanc6500af2012-03-15 14:44:39 -0500138 print "using presintall script: %s" % script
139 args.append('--preinstall-script')
140 args.append(script)
141
Andy Doanea2e1ea2012-04-23 11:56:29 -0500142 hwpi.do_eula(imgfile)
Andy Doan15aa23b2012-02-02 16:40:58 -0600143 cmd_runner.run(args, as_root=True).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -0500144 bz2file = compress_image(imgfile)
145 zsync_image(bz2file)
Andy Doan3634c472012-04-17 12:50:58 -0500146 image_info(bz2file, binary_file)
Andy Doan15aa23b2012-02-02 16:40:58 -0600147
Andy Doan184cd3b2012-03-29 22:20:02 -0500148class SnapshotCrawler:
149 def __init__(self, date, platform, hwpacks, binaries):
150 self.date = date
151 self.platform = platform
152 self.hwpacks = hwpacks
153 self.binaries = binaries
Andy Doane449bce2012-03-21 15:22:31 -0500154
Andy Doan184cd3b2012-03-29 22:20:02 -0500155 def get_binaries(self):
156 '''return a hash table of binary->url'''
157 binaryf = {}
158 for binary in self.binaries:
Andy Doan540a4b12012-04-17 11:48:11 -0500159 url = 'http://snapshots.linaro.org/%s/images/%s' % (self.platform, binary)
Andy Doan184cd3b2012-03-29 22:20:02 -0500160 (date, url) = crawler.latest_rfs(url)
Andy Doan4202d882012-03-29 21:36:09 -0500161 binaryf[binary] = url
Andy Doan4202d882012-03-29 21:36:09 -0500162
Andy Doan184cd3b2012-03-29 22:20:02 -0500163 return binaryf
Andy Doana3977372012-03-29 12:22:44 -0500164
Andy Doan184cd3b2012-03-29 22:20:02 -0500165 def get_hwpack_url(self, hwpack):
166 hwpackname = "%s-%s" % (hwpack, self.platform)
167 return hwpack_available(self.date, hwpack, self.platform)
Andy Doan4202d882012-03-29 21:36:09 -0500168
Andy Doan184cd3b2012-03-29 22:20:02 -0500169 def get_image_name(self, odir, hwpack, hwpack_url, binary):
170
171 #covert a url like:
Andy Doan540a4b12012-04-17 11:48:11 -0500172 # http://snapshots.linaro.org/precise/hwpacks/lt-panda/87/hwpack_linaro-lt-panda_20120417-87_armhf_supported.tar.gz
Andy Doan184cd3b2012-03-29 22:20:02 -0500173 # to:
Andy Doan66e2d5c2012-04-18 23:01:59 -0500174 # precise/pre-built/lt-panda/87/<file>.img
Andy Doan184cd3b2012-03-29 22:20:02 -0500175 path = urlparse.urlparse(hwpack_url).path
Andy Doan66e2d5c2012-04-18 23:01:59 -0500176 parts = path.split('/')
177 name = parts[-1] #use for the file name below
178 path = '/'.join(parts[-3:-1])
Andy Doan184cd3b2012-03-29 22:20:02 -0500179
180 path = '%s/%s' % (odir, path)
181 if not os.path.exists(path):
182 os.makedirs(path)
Andy Doan66e2d5c2012-04-18 23:01:59 -0500183
184 date = re.search(r'_(\d+-\d+)_', name).group(1)
185 return "%s/%s-%s_%s_%s.img" %(path,hwpack,self.platform,binary,date)
Andy Doan184cd3b2012-03-29 22:20:02 -0500186
Andy Doan540a4b12012-04-17 11:48:11 -0500187# XXX: this is now broken for precise. need to wait for release images to see
188# what changes are needed
Andy Doan184cd3b2012-03-29 22:20:02 -0500189class ReleaseCrawler:
190 def __init__(self, cycle, platform, hwpacks, binaries):
191 self.cycle = cycle
192 self.platform = platform
193 self.hwpacks = hwpacks
194 self.binaries = binaries
195
196 #inialize our list of hwpacks
197 url = 'http://releases.linaro.org/%s/ubuntu/%s-hwpacks' % (cycle,platform)
198 self.hwpack_urls = crawler.list_hwpacks(url)
199
200 def get_hwpack_url(self, hwpack):
201 pat = re.compile('hwpack_linaro-%s_' % hwpack)
202 for url in self.hwpack_urls:
203 if pat.search(url):
204 return url
205 return None
206
207 def get_binaries(self):
208 '''return a hash table of binary->url'''
209 binaryf = {}
210 for binary in self.binaries:
211 url = 'http://releases.linaro.org/%s/ubuntu/%s-images/%s' % (
212 self.cycle, self.platform, binary)
213 url = crawler.list_rfs(url)
214 if url is not None:
215 binaryf[binary] = url
216 return binaryf
217
218 def get_image_name(self, odir, hwpack, hwpack_url, binary):
219 path = '%s/%s/%s/%s' % (odir, self.cycle, self.platform, binary)
220 if not os.path.exists(path):
221 os.makedirs(path)
222 return "%s/%s-%s.img" %(path,hwpack,binary)
Andy Doan4202d882012-03-29 21:36:09 -0500223
Andy Doan15aa23b2012-02-02 16:40:58 -0600224def main():
225 day = today()
226 hwpacks = HWPACKS.keys()
227 binaries = BINARIES.keys()
228
229 p = argparse.ArgumentParser(description=
Andy Doana3977372012-03-29 12:22:44 -0500230 'Builds a matrix of builds from the latest build on '
231 'snapshots.linaro.org or release images on releases.linaro.org')
Andy Doan15aa23b2012-02-02 16:40:58 -0600232
Andy Doan94bfe312012-02-09 17:57:29 -0800233 p.add_argument('-o', dest='out_dir', default='./out',
Andy Doan15aa23b2012-02-02 16:40:58 -0600234 help='The out directory for downloaded and built files, default=./')
235 p.add_argument('-d', dest='date', default=day,
236 help='The date, default=%s' % day)
237 p.add_argument('-w', dest='hwpacks', action='append',
238 help='The hwpacks to generate for, default=%s' %
239 ', '.join(hwpacks))
240 p.add_argument('-b', dest='binaries', action='append',
241 help='The binaries to generate for, default=%s' %
242 ', '.join(binaries))
Andy Doan540a4b12012-04-17 11:48:11 -0500243 p.add_argument('-p', dest='platform', default='precise',
244 help='The platform, default=precise')
Andy Doan4202d882012-03-29 21:36:09 -0500245 p.add_argument('-r', dest='release', action='store_true', default=False,
246 help='If this is for release images. NOTE: the "date" arg'
247 'will then become the cycle ie -d 12.03')
Andy Doan15aa23b2012-02-02 16:40:58 -0600248
249 args = p.parse_args()
Andy Doana3977372012-03-29 12:22:44 -0500250
Andy Doan15aa23b2012-02-02 16:40:58 -0600251 if args.hwpacks:
252 hwpacks = args.hwpacks
253 if args.binaries:
254 binaries = args.binaries
255
256 dm = fetch_image.DownloadManager(args.out_dir)
Andy Doan15aa23b2012-02-02 16:40:58 -0600257
Andy Doan4202d882012-03-29 21:36:09 -0500258 if args.release:
Andy Doan184cd3b2012-03-29 22:20:02 -0500259 site = ReleaseCrawler(args.date, args.platform, hwpacks, binaries)
Andy Doan4202d882012-03-29 21:36:09 -0500260 else:
Andy Doan184cd3b2012-03-29 22:20:02 -0500261 site = SnapshotCrawler(args.date, args.platform, hwpacks, binaries)
262
263 binaryf = site.get_binaries()
Andy Doan15aa23b2012-02-02 16:40:58 -0600264
Andy Doanb00dbc02012-03-15 16:23:06 -0500265 lmc = '%s/linaro-image-tools/linaro-media-create' % args.out_dir
Andy Doan184cd3b2012-03-29 22:20:02 -0500266 odir = '%s/pre-built' % args.out_dir
Andy Doanb00dbc02012-03-15 16:23:06 -0500267
Andy Doan15aa23b2012-02-02 16:40:58 -0600268 for hwpack in hwpacks:
Andy Doan184cd3b2012-03-29 22:20:02 -0500269 url = site.get_hwpack_url(hwpack)
Andy Doanb00dbc02012-03-15 16:23:06 -0500270
Andy Doan2cfe5e62012-02-09 17:41:45 -0800271 if url is not None and url is not False:
Andy Doan15aa23b2012-02-02 16:40:58 -0600272 hwpf = dm.download(url, None)
273 for binary in binaries:
Andy Doan184cd3b2012-03-29 22:20:02 -0500274 imgfile = site.get_image_name(odir,hwpack,url,binary)
Andy Doan81211902012-03-14 14:37:30 -0500275 rfsf = dm.download(binaryf[binary], None)
Andy Doanb00dbc02012-03-15 16:23:06 -0500276 build_image(lmc, imgfile, hwpack, hwpf, binary, rfsf)
Andy Doan15aa23b2012-02-02 16:40:58 -0600277 else:
Andy Doan8ff38f02012-02-20 16:32:37 -0600278 print '%s hwpack not available for %s' % (args.date,hwpack)
Andy Doan15aa23b2012-02-02 16:40:58 -0600279
280if __name__ == '__main__':
281 main()
282