blob: 1ae0a43d7aa75c8a319f6ee6a2f066df58df6b4d [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'),
Andy Doan7a8cc102012-04-26 09:25:33 -050056 'lt-panda-x11-base': HwPack('panda'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050057 'lt-mx5': HwPack('mx53loco'),
58 'lt-mx6': HwPack('mx6qsabrelite'),
Andy Doan490f6202012-04-25 10:56:08 -050059 'lt-origen': HwPack('origen'),
60 'leb-origen': HwPack('origen'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050061 'lt-snowball': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
62 'lt-snowball-x11-base': HwPack('snowball_sd', True, 'ste-preinstall.sh'),
Andy Doanedffb5d2012-04-25 11:22:19 -050063 'vexpress': HwPack('vexpress'),
Andy Doanea2e1ea2012-04-23 11:56:29 -050064 'efikamx': HwPack('efikamx'),
65 'igep': HwPack('igep'),
Andy Doan15aa23b2012-02-02 16:40:58 -060066}
67# a mapping of binary image to its image_file size
68BINARIES = {
Andy Doan4202d882012-03-29 21:36:09 -050069 # release images
70 'nano': '512M',
71 'alip': '2G',
72 'ubuntu-desktop': '3G',
73 'linarotv-xbmc': '3G',
Andy Doan15aa23b2012-02-02 16:40:58 -060074}
75
Andy Doan15aa23b2012-02-02 16:40:58 -060076def today():
77 d = datetime.date.today()
78 return "%d%02d%02d" % (d.year, d.month, d.day)
79
Andy Doan8ff38f02012-02-20 16:32:37 -060080def hwpack_available(date, hwpack, platform):
Andy Doan540a4b12012-04-17 11:48:11 -050081 url = 'http://snapshots.linaro.org/%s/hwpacks/%s' % (platform, hwpack)
Andy Doan8ff38f02012-02-20 16:32:37 -060082 hwpacks = crawler.latest_hwpacks(url)
83 for hwpack in hwpacks:
84 if hwpack[0] == date:
85 return hwpack[1]
86
Andy Doan15aa23b2012-02-02 16:40:58 -060087 return False
88
89def compress_image(imgfile):
90 print "compressing %s" % imgfile
91 args = ('bzip2', imgfile)
92 cmd_runner.run(args).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -050093 return '%s.bz2' % imgfile
94
95def zsync_image(imgfile):
96 print "making zsync file for %s" % imgfile
97 args = ('zsyncmake', '-o', '%s.zsync'%imgfile, imgfile)
98 cmd_runner.run(args).wait()
Andy Doan15aa23b2012-02-02 16:40:58 -060099
Andy Doan3634c472012-04-17 12:50:58 -0500100def md5sum(fname):
101 md5 = hashlib.md5()
102 with open(fname, 'rb') as f:
103 while True:
104 data = f.read(4096)
105 if not data: break
106 md5.update(data)
107 return md5.hexdigest()
108
Andy Doan3c0c3892012-04-25 11:09:53 -0500109def image_info(infofile, bz2file, binary_file):
Andy Doan3634c472012-04-17 12:50:58 -0500110 ''' prints out the md5sum and rfs info for the given image
111 '''
112 print "building image info for: %s" % bz2file
113 md5 = md5sum(bz2file)
114
115 binary_file = os.path.basename(binary_file)
116
Andy Doan3c0c3892012-04-25 11:09:53 -0500117 with open(infofile, 'w') as f:
Andy Doan3634c472012-04-17 12:50:58 -0500118 f.write("%s md5sum(%s)\n" % (os.path.basename(bz2file), md5))
119 f.write("root file system: %s\n" % binary_file)
120
Andy Doanb00dbc02012-03-15 16:23:06 -0500121def build_image(lmc, imgfile, hwpack, hwpack_file, binary, binary_file):
Andy Doan15aa23b2012-02-02 16:40:58 -0600122 print "building image: %s" % imgfile
Andy Doanea2e1ea2012-04-23 11:56:29 -0500123 hwpi = HWPACKS[hwpack]
Andy Doan15aa23b2012-02-02 16:40:58 -0600124 size = BINARIES[binary]
Andy Doan3c0c3892012-04-25 11:09:53 -0500125 infofile = '%s.info.txt' % os.path.splitext(imgfile)[0]
Andy Doan15aa23b2012-02-02 16:40:58 -0600126
Andy Doan4fa16dc2012-02-09 17:47:45 -0800127 print 'running l-m-c: %s' % lmc
Andy Doanc6500af2012-03-15 14:44:39 -0500128 args = [lmc,
Andy Doanea2e1ea2012-04-23 11:56:29 -0500129 '--dev', hwpi.lmcname,
Andy Doan15aa23b2012-02-02 16:40:58 -0600130 '--image_file', imgfile,
131 '--image_size', size,
132 '--hwpack-force-yes',
133 '--hwpack', hwpack_file,
134 '--binary', binary_file,
Andy Doanc6500af2012-03-15 14:44:39 -0500135 ]
136
Andy Doanea2e1ea2012-04-23 11:56:29 -0500137 if hwpi.pre_inst_script is not None:
Andy Doanc6500af2012-03-15 14:44:39 -0500138 sdir = os.path.abspath(os.path.dirname(__file__))
Andy Doanea2e1ea2012-04-23 11:56:29 -0500139 script = "%s/%s" % (sdir, hwpi.pre_inst_script)
Andy Doanc6500af2012-03-15 14:44:39 -0500140 print "using presintall script: %s" % script
141 args.append('--preinstall-script')
142 args.append(script)
143
Andy Doanea2e1ea2012-04-23 11:56:29 -0500144 hwpi.do_eula(imgfile)
Andy Doan15aa23b2012-02-02 16:40:58 -0600145 cmd_runner.run(args, as_root=True).wait()
Andy Doan23ce4e12012-03-21 12:46:21 -0500146 bz2file = compress_image(imgfile)
147 zsync_image(bz2file)
Andy Doan3c0c3892012-04-25 11:09:53 -0500148 image_info(infofile, bz2file, binary_file)
Andy Doan15aa23b2012-02-02 16:40:58 -0600149
Andy Doan184cd3b2012-03-29 22:20:02 -0500150class SnapshotCrawler:
151 def __init__(self, date, platform, hwpacks, binaries):
152 self.date = date
153 self.platform = platform
154 self.hwpacks = hwpacks
155 self.binaries = binaries
Andy Doane449bce2012-03-21 15:22:31 -0500156
Andy Doan184cd3b2012-03-29 22:20:02 -0500157 def get_binaries(self):
158 '''return a hash table of binary->url'''
159 binaryf = {}
160 for binary in self.binaries:
Andy Doan540a4b12012-04-17 11:48:11 -0500161 url = 'http://snapshots.linaro.org/%s/images/%s' % (self.platform, binary)
Andy Doan184cd3b2012-03-29 22:20:02 -0500162 (date, url) = crawler.latest_rfs(url)
Andy Doan4202d882012-03-29 21:36:09 -0500163 binaryf[binary] = url
Andy Doan4202d882012-03-29 21:36:09 -0500164
Andy Doan184cd3b2012-03-29 22:20:02 -0500165 return binaryf
Andy Doana3977372012-03-29 12:22:44 -0500166
Andy Doan184cd3b2012-03-29 22:20:02 -0500167 def get_hwpack_url(self, hwpack):
168 hwpackname = "%s-%s" % (hwpack, self.platform)
169 return hwpack_available(self.date, hwpack, self.platform)
Andy Doan4202d882012-03-29 21:36:09 -0500170
Andy Doan184cd3b2012-03-29 22:20:02 -0500171 def get_image_name(self, odir, hwpack, hwpack_url, binary):
172
173 #covert a url like:
Andy Doan540a4b12012-04-17 11:48:11 -0500174 # 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 -0500175 # to:
Andy Doan66e2d5c2012-04-18 23:01:59 -0500176 # precise/pre-built/lt-panda/87/<file>.img
Andy Doan184cd3b2012-03-29 22:20:02 -0500177 path = urlparse.urlparse(hwpack_url).path
Andy Doan66e2d5c2012-04-18 23:01:59 -0500178 parts = path.split('/')
179 name = parts[-1] #use for the file name below
180 path = '/'.join(parts[-3:-1])
Andy Doan184cd3b2012-03-29 22:20:02 -0500181
182 path = '%s/%s' % (odir, path)
183 if not os.path.exists(path):
184 os.makedirs(path)
Andy Doan66e2d5c2012-04-18 23:01:59 -0500185
186 date = re.search(r'_(\d+-\d+)_', name).group(1)
187 return "%s/%s-%s_%s_%s.img" %(path,hwpack,self.platform,binary,date)
Andy Doan184cd3b2012-03-29 22:20:02 -0500188
Andy Doan15aa23b2012-02-02 16:40:58 -0600189def main():
190 day = today()
191 hwpacks = HWPACKS.keys()
192 binaries = BINARIES.keys()
193
194 p = argparse.ArgumentParser(description=
Andy Doana3977372012-03-29 12:22:44 -0500195 'Builds a matrix of builds from the latest build on '
196 'snapshots.linaro.org or release images on releases.linaro.org')
Andy Doan15aa23b2012-02-02 16:40:58 -0600197
Andy Doan94bfe312012-02-09 17:57:29 -0800198 p.add_argument('-o', dest='out_dir', default='./out',
Andy Doan15aa23b2012-02-02 16:40:58 -0600199 help='The out directory for downloaded and built files, default=./')
200 p.add_argument('-d', dest='date', default=day,
201 help='The date, default=%s' % day)
202 p.add_argument('-w', dest='hwpacks', action='append',
203 help='The hwpacks to generate for, default=%s' %
204 ', '.join(hwpacks))
205 p.add_argument('-b', dest='binaries', action='append',
206 help='The binaries to generate for, default=%s' %
207 ', '.join(binaries))
Andy Doan540a4b12012-04-17 11:48:11 -0500208 p.add_argument('-p', dest='platform', default='precise',
209 help='The platform, default=precise')
Andy Doan4202d882012-03-29 21:36:09 -0500210 p.add_argument('-r', dest='release', action='store_true', default=False,
211 help='If this is for release images. NOTE: the "date" arg'
212 'will then become the cycle ie -d 12.03')
Andy Doan15aa23b2012-02-02 16:40:58 -0600213
214 args = p.parse_args()
Andy Doana3977372012-03-29 12:22:44 -0500215
Andy Doan15aa23b2012-02-02 16:40:58 -0600216 if args.hwpacks:
217 hwpacks = args.hwpacks
218 if args.binaries:
219 binaries = args.binaries
220
Andy Doan1c6ef8b2012-04-23 12:28:50 -0500221 crawler.cookie_setup()
222
Andy Doan15aa23b2012-02-02 16:40:58 -0600223 dm = fetch_image.DownloadManager(args.out_dir)
Andy Doan15aa23b2012-02-02 16:40:58 -0600224
Andy Doan4202d882012-03-29 21:36:09 -0500225 if args.release:
Andy Doan184cd3b2012-03-29 22:20:02 -0500226 site = ReleaseCrawler(args.date, args.platform, hwpacks, binaries)
Andy Doan4202d882012-03-29 21:36:09 -0500227 else:
Andy Doan184cd3b2012-03-29 22:20:02 -0500228 site = SnapshotCrawler(args.date, args.platform, hwpacks, binaries)
229
230 binaryf = site.get_binaries()
Andy Doan15aa23b2012-02-02 16:40:58 -0600231
Andy Doanb00dbc02012-03-15 16:23:06 -0500232 lmc = '%s/linaro-image-tools/linaro-media-create' % args.out_dir
Andy Doan184cd3b2012-03-29 22:20:02 -0500233 odir = '%s/pre-built' % args.out_dir
Andy Doanb00dbc02012-03-15 16:23:06 -0500234
Andy Doan15aa23b2012-02-02 16:40:58 -0600235 for hwpack in hwpacks:
Andy Doan184cd3b2012-03-29 22:20:02 -0500236 url = site.get_hwpack_url(hwpack)
Andy Doanb00dbc02012-03-15 16:23:06 -0500237
Andy Doan2cfe5e62012-02-09 17:41:45 -0800238 if url is not None and url is not False:
Andy Doan15aa23b2012-02-02 16:40:58 -0600239 hwpf = dm.download(url, None)
240 for binary in binaries:
Andy Doan184cd3b2012-03-29 22:20:02 -0500241 imgfile = site.get_image_name(odir,hwpack,url,binary)
Andy Doan81211902012-03-14 14:37:30 -0500242 rfsf = dm.download(binaryf[binary], None)
Andy Doanb00dbc02012-03-15 16:23:06 -0500243 build_image(lmc, imgfile, hwpack, hwpf, binary, rfsf)
Andy Doan15aa23b2012-02-02 16:40:58 -0600244 else:
Andy Doan8ff38f02012-02-20 16:32:37 -0600245 print '%s hwpack not available for %s' % (args.date,hwpack)
Andy Doan15aa23b2012-02-02 16:40:58 -0600246
247if __name__ == '__main__':
248 main()
249