blob: b0b8fe6ab7f7936f673d4fe558d1781748041075 [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 Doan35ee9dc2012-05-11 01:10:12 -050035import string
Andy Doan4202d882012-03-29 21:36:09 -050036import sys
Andy Doan15aa23b2012-02-02 16:40:58 -060037
Andy Doanea2e1ea2012-04-23 11:56:29 -050038class HwPack:
39 def __init__(self, lmcname, eula=False, pre_inst_script=None):
40 self.lmcname = lmcname
41 if eula:
42 self.eula = 'EULA.txt'
43 else:
Andy Doan4aabf022012-04-26 14:30:29 -050044 self.eula = 'OPEN-EULA.txt'
Andy Doanea2e1ea2012-04-23 11:56:29 -050045 self.pre_inst_script = pre_inst_script
46
47 def do_eula(self, imgfile):
48 fname = '%s/%s' % (os.path.dirname(imgfile), self.eula)
49 with file(fname, 'a'):
50 pass #just need the file to exist
51
Andy Doan15aa23b2012-02-02 16:40:58 -060052# a mapping of hwpack name to l-m-c name
53HWPACKS = {
Andy Doanea2e1ea2012-04-23 11:56:29 -050054 'beagleboard': HwPack('beagle'),
55 'overo': HwPack('overo'),
56 'lt-panda': HwPack('panda'),
Andy Doan7a8cc102012-04-26 09:25:33 -050057 'lt-panda-x11-base': HwPack('panda'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050058 'lt-mx5': HwPack('mx53loco'),
59 'lt-mx6': HwPack('mx6qsabrelite'),
Andy Doan490f6202012-04-25 10:56:08 -050060 'lt-origen': HwPack('origen'),
61 'leb-origen': HwPack('origen'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050062 'lt-snowball': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
63 'lt-snowball-x11-base': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
Andy Doanedffb5d2012-04-25 11:22:19 -050064 'vexpress': HwPack('vexpress'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050065 'efikamx': HwPack('efikamx'),
66 'igep': HwPack('igep'),
Andy Doan15aa23b2012-02-02 16:40:58 -060067}
68# a mapping of binary image to its image_file size
69BINARIES = {
Andy Doan4202d882012-03-29 21:36:09 -050070 # release images
71 'nano': '512M',
72 'alip': '2G',
73 'ubuntu-desktop': '3G',
74 'linarotv-xbmc': '3G',
Andy Doan15aa23b2012-02-02 16:40:58 -060075}
76
Andy Doan15aa23b2012-02-02 16:40:58 -060077def today():
78 d = datetime.date.today()
79 return "%d%02d%02d" % (d.year, d.month, d.day)
80
Andy Doan8ff38f02012-02-20 16:32:37 -060081def hwpack_available(date, hwpack, platform):
Andy Doan540a4b12012-04-17 11:48:11 -050082 url = 'http://snapshots.linaro.org/%s/hwpacks/%s' % (platform, hwpack)
Andy Doan8ff38f02012-02-20 16:32:37 -060083 hwpacks = crawler.latest_hwpacks(url)
84 for hwpack in hwpacks:
85 if hwpack[0] == date:
86 return hwpack[1]
87
Andy Doan15aa23b2012-02-02 16:40:58 -060088 return False
89
90def compress_image(imgfile):
91 print "compressing %s" % imgfile
92 args = ('bzip2', imgfile)
93 cmd_runner.run(args).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -050094 return '%s.bz2' % imgfile
95
96def zsync_image(imgfile):
97 print "making zsync file for %s" % imgfile
98 args = ('zsyncmake', '-o', '%s.zsync'%imgfile, imgfile)
99 cmd_runner.run(args).wait()
Andy Doan15aa23b2012-02-02 16:40:58 -0600100
Andy Doan3634c472012-04-17 12:50:58 -0500101def md5sum(fname):
102 md5 = hashlib.md5()
103 with open(fname, 'rb') as f:
104 while True:
105 data = f.read(4096)
106 if not data: break
107 md5.update(data)
108 return md5.hexdigest()
109
Andy Doan3c0c3892012-04-25 11:09:53 -0500110def image_info(infofile, bz2file, binary_file):
Andy Doan3634c472012-04-17 12:50:58 -0500111 ''' prints out the md5sum and rfs info for the given image
112 '''
113 print "building image info for: %s" % bz2file
114 md5 = md5sum(bz2file)
115
116 binary_file = os.path.basename(binary_file)
Andy Doan35ee9dc2012-05-11 01:10:12 -0500117 tfile = '%s/image_info_template.html' % os.path.dirname(__file__)
Andy Doan3634c472012-04-17 12:50:58 -0500118
Andy Doan35ee9dc2012-05-11 01:10:12 -0500119 tmpl = None
120 with open(tfile) as f:
121 buff = f.read()
122 tmpl = string.Template(buff)
123
124 title = 'Pre-Built Image Info'
125 buff = tmpl.substitute(title=title, md5=md5, rfs=binary_file,
126 image_name=os.path.basename(bz2file))
Andy Doan3c0c3892012-04-25 11:09:53 -0500127 with open(infofile, 'w') as f:
Andy Doan35ee9dc2012-05-11 01:10:12 -0500128 f.write(buff)
Andy Doan3634c472012-04-17 12:50:58 -0500129
Andy Doanb00dbc02012-03-15 16:23:06 -0500130def build_image(lmc, imgfile, hwpack, hwpack_file, binary, binary_file):
Andy Doan15aa23b2012-02-02 16:40:58 -0600131 print "building image: %s" % imgfile
Andy Doanea2e1ea2012-04-23 11:56:29 -0500132 hwpi = HWPACKS[hwpack]
Andy Doan15aa23b2012-02-02 16:40:58 -0600133 size = BINARIES[binary]
Andy Doan35ee9dc2012-05-11 01:10:12 -0500134 infofile = '%s.html' % os.path.splitext(imgfile)[0]
Andy Doan15aa23b2012-02-02 16:40:58 -0600135
Andy Doan4fa16dc2012-02-09 17:47:45 -0800136 print 'running l-m-c: %s' % lmc
Andy Doanc6500af2012-03-15 14:44:39 -0500137 args = [lmc,
Andy Doanea2e1ea2012-04-23 11:56:29 -0500138 '--dev', hwpi.lmcname,
Andy Doan15aa23b2012-02-02 16:40:58 -0600139 '--image_file', imgfile,
140 '--image_size', size,
141 '--hwpack-force-yes',
142 '--hwpack', hwpack_file,
143 '--binary', binary_file,
Andy Doanc6500af2012-03-15 14:44:39 -0500144 ]
145
Andy Doanea2e1ea2012-04-23 11:56:29 -0500146 if hwpi.pre_inst_script is not None:
Andy Doanc6500af2012-03-15 14:44:39 -0500147 sdir = os.path.abspath(os.path.dirname(__file__))
Andy Doanea2e1ea2012-04-23 11:56:29 -0500148 script = "%s/%s" % (sdir, hwpi.pre_inst_script)
Andy Doanc6500af2012-03-15 14:44:39 -0500149 print "using presintall script: %s" % script
150 args.append('--preinstall-script')
151 args.append(script)
152
Andy Doanea2e1ea2012-04-23 11:56:29 -0500153 hwpi.do_eula(imgfile)
Andy Doan15aa23b2012-02-02 16:40:58 -0600154 cmd_runner.run(args, as_root=True).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -0500155 bz2file = compress_image(imgfile)
156 zsync_image(bz2file)
Andy Doan3c0c3892012-04-25 11:09:53 -0500157 image_info(infofile, bz2file, binary_file)
Andy Doan15aa23b2012-02-02 16:40:58 -0600158
Andy Doan184cd3b2012-03-29 22:20:02 -0500159class SnapshotCrawler:
160 def __init__(self, date, platform, hwpacks, binaries):
161 self.date = date
162 self.platform = platform
163 self.hwpacks = hwpacks
164 self.binaries = binaries
Andy Doane449bce2012-03-21 15:22:31 -0500165
Andy Doan184cd3b2012-03-29 22:20:02 -0500166 def get_binaries(self):
167 '''return a hash table of binary->url'''
168 binaryf = {}
169 for binary in self.binaries:
Andy Doan540a4b12012-04-17 11:48:11 -0500170 url = 'http://snapshots.linaro.org/%s/images/%s' % (self.platform, binary)
Andy Doan184cd3b2012-03-29 22:20:02 -0500171 (date, url) = crawler.latest_rfs(url)
Andy Doan4202d882012-03-29 21:36:09 -0500172 binaryf[binary] = url
Andy Doan4202d882012-03-29 21:36:09 -0500173
Andy Doan184cd3b2012-03-29 22:20:02 -0500174 return binaryf
Andy Doana3977372012-03-29 12:22:44 -0500175
Andy Doan184cd3b2012-03-29 22:20:02 -0500176 def get_hwpack_url(self, hwpack):
177 hwpackname = "%s-%s" % (hwpack, self.platform)
178 return hwpack_available(self.date, hwpack, self.platform)
Andy Doan4202d882012-03-29 21:36:09 -0500179
Andy Doan184cd3b2012-03-29 22:20:02 -0500180 def get_image_name(self, odir, hwpack, hwpack_url, binary):
181
182 #covert a url like:
Andy Doan540a4b12012-04-17 11:48:11 -0500183 # 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 -0500184 # to:
Andy Doan66e2d5c2012-04-18 23:01:59 -0500185 # precise/pre-built/lt-panda/87/<file>.img
Andy Doan184cd3b2012-03-29 22:20:02 -0500186 path = urlparse.urlparse(hwpack_url).path
Andy Doan66e2d5c2012-04-18 23:01:59 -0500187 parts = path.split('/')
188 name = parts[-1] #use for the file name below
189 path = '/'.join(parts[-3:-1])
Andy Doan184cd3b2012-03-29 22:20:02 -0500190
191 path = '%s/%s' % (odir, path)
192 if not os.path.exists(path):
193 os.makedirs(path)
Andy Doan66e2d5c2012-04-18 23:01:59 -0500194
195 date = re.search(r'_(\d+-\d+)_', name).group(1)
196 return "%s/%s-%s_%s_%s.img" %(path,hwpack,self.platform,binary,date)
Andy Doan184cd3b2012-03-29 22:20:02 -0500197
Andy Doan15aa23b2012-02-02 16:40:58 -0600198def main():
199 day = today()
200 hwpacks = HWPACKS.keys()
201 binaries = BINARIES.keys()
202
203 p = argparse.ArgumentParser(description=
Andy Doana3977372012-03-29 12:22:44 -0500204 'Builds a matrix of builds from the latest build on '
205 'snapshots.linaro.org or release images on releases.linaro.org')
Andy Doan15aa23b2012-02-02 16:40:58 -0600206
Andy Doan94bfe312012-02-09 17:57:29 -0800207 p.add_argument('-o', dest='out_dir', default='./out',
Andy Doan15aa23b2012-02-02 16:40:58 -0600208 help='The out directory for downloaded and built files, default=./')
209 p.add_argument('-d', dest='date', default=day,
210 help='The date, default=%s' % day)
211 p.add_argument('-w', dest='hwpacks', action='append',
212 help='The hwpacks to generate for, default=%s' %
213 ', '.join(hwpacks))
214 p.add_argument('-b', dest='binaries', action='append',
215 help='The binaries to generate for, default=%s' %
216 ', '.join(binaries))
Andy Doan540a4b12012-04-17 11:48:11 -0500217 p.add_argument('-p', dest='platform', default='precise',
218 help='The platform, default=precise')
Andy Doan4202d882012-03-29 21:36:09 -0500219 p.add_argument('-r', dest='release', action='store_true', default=False,
220 help='If this is for release images. NOTE: the "date" arg'
221 'will then become the cycle ie -d 12.03')
Andy Doan15aa23b2012-02-02 16:40:58 -0600222
223 args = p.parse_args()
Andy Doana3977372012-03-29 12:22:44 -0500224
Andy Doan15aa23b2012-02-02 16:40:58 -0600225 if args.hwpacks:
226 hwpacks = args.hwpacks
227 if args.binaries:
228 binaries = args.binaries
229
Andy Doan1c6ef8b2012-04-23 12:28:50 -0500230 crawler.cookie_setup()
231
Andy Doan15aa23b2012-02-02 16:40:58 -0600232 dm = fetch_image.DownloadManager(args.out_dir)
Andy Doan15aa23b2012-02-02 16:40:58 -0600233
Andy Doan4202d882012-03-29 21:36:09 -0500234 if args.release:
Andy Doan184cd3b2012-03-29 22:20:02 -0500235 site = ReleaseCrawler(args.date, args.platform, hwpacks, binaries)
Andy Doan4202d882012-03-29 21:36:09 -0500236 else:
Andy Doan184cd3b2012-03-29 22:20:02 -0500237 site = SnapshotCrawler(args.date, args.platform, hwpacks, binaries)
238
239 binaryf = site.get_binaries()
Andy Doan15aa23b2012-02-02 16:40:58 -0600240
Andy Doanb00dbc02012-03-15 16:23:06 -0500241 lmc = '%s/linaro-image-tools/linaro-media-create' % args.out_dir
Andy Doan184cd3b2012-03-29 22:20:02 -0500242 odir = '%s/pre-built' % args.out_dir
Andy Doanb00dbc02012-03-15 16:23:06 -0500243
Andy Doan15aa23b2012-02-02 16:40:58 -0600244 for hwpack in hwpacks:
Andy Doan184cd3b2012-03-29 22:20:02 -0500245 url = site.get_hwpack_url(hwpack)
Andy Doanb00dbc02012-03-15 16:23:06 -0500246
Andy Doan2cfe5e62012-02-09 17:41:45 -0800247 if url is not None and url is not False:
Andy Doan15aa23b2012-02-02 16:40:58 -0600248 hwpf = dm.download(url, None)
249 for binary in binaries:
Andy Doan184cd3b2012-03-29 22:20:02 -0500250 imgfile = site.get_image_name(odir,hwpack,url,binary)
Andy Doan81211902012-03-14 14:37:30 -0500251 rfsf = dm.download(binaryf[binary], None)
Andy Doanb00dbc02012-03-15 16:23:06 -0500252 build_image(lmc, imgfile, hwpack, hwpf, binary, rfsf)
Andy Doan15aa23b2012-02-02 16:40:58 -0600253 else:
Andy Doan8ff38f02012-02-20 16:32:37 -0600254 print '%s hwpack not available for %s' % (args.date,hwpack)
Andy Doan15aa23b2012-02-02 16:40:58 -0600255
256if __name__ == '__main__':
257 main()
258