blob: af050e2479f95d026e59304d3f8dd4227271d91c [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
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300110def create_image_info(infofile, bz2file, binary_file, hwpack_url, rootfs_url):
111 ''' prints out the md5sum and hwpack/rootfs info for the given image
Andy Doan3634c472012-04-17 12:50:58 -0500112 '''
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'
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300125
126 # build hwpack manifest and rootfs packages links
127 hwpack_re = re.search(r'(hwpack_linaro.*\d\w*)(.*)$', hwpack_url)
128 hwpack_name = "".join(hwpack_re.groups())
129 hwpack_manifest = hwpack_name + '.manifest.txt'
130 hwpack_manifest_url = re.sub('.tar.gz$', '.manifest.txt', hwpack_url)
131
132 rootfs_re = re.search(r'(linaro-.*\d)(.*)$', rootfs_url)
133 rootfs_name = "".join(rootfs_re.groups())
134 rootfs_packages = rootfs_name + '.packages'
135 rootfs_packages_url = re.sub('.tar.gz$', '.packages', rootfs_url)
136
Ricardo Salveti de Araujo894040a2012-05-29 14:07:55 -0300137 # try to get information from Jenkins, if available
138 jenkins_buildn = os.environ.get('BUILD_NUMBER') or 'Not Found'
139 jenkins_build_url = os.environ.get('BUILD_URL') or 'http://ci.linaro.org'
140
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300141 buff = tmpl.substitute(
142 title=title, md5=md5, image_name=os.path.basename(bz2file),
143 hwpack_name=hwpack_name, hwpack_url=hwpack_url,
144 rootfs_name=rootfs_name, rootfs_url=rootfs_url,
145 hwpack_manifest_name=hwpack_manifest,
146 hwpack_manifest_url=hwpack_manifest_url,
147 rootfs_packages_name=rootfs_packages,
Ricardo Salveti de Araujo894040a2012-05-29 14:07:55 -0300148 rootfs_packages_url=rootfs_packages_url,
149 jenkins_buildn=jenkins_buildn,
150 jenkins_build_url=jenkins_build_url)
Andy Doan3c0c3892012-04-25 11:09:53 -0500151 with open(infofile, 'w') as f:
Andy Doan35ee9dc2012-05-11 01:10:12 -0500152 f.write(buff)
Andy Doan3634c472012-04-17 12:50:58 -0500153
Andy Doanb00dbc02012-03-15 16:23:06 -0500154def build_image(lmc, imgfile, hwpack, hwpack_file, binary, binary_file):
Andy Doan15aa23b2012-02-02 16:40:58 -0600155 print "building image: %s" % imgfile
Andy Doanea2e1ea2012-04-23 11:56:29 -0500156 hwpi = HWPACKS[hwpack]
Andy Doan15aa23b2012-02-02 16:40:58 -0600157 size = BINARIES[binary]
158
Andy Doan4fa16dc2012-02-09 17:47:45 -0800159 print 'running l-m-c: %s' % lmc
Andy Doanc6500af2012-03-15 14:44:39 -0500160 args = [lmc,
Andy Doanea2e1ea2012-04-23 11:56:29 -0500161 '--dev', hwpi.lmcname,
Andy Doan15aa23b2012-02-02 16:40:58 -0600162 '--image_file', imgfile,
163 '--image_size', size,
164 '--hwpack-force-yes',
165 '--hwpack', hwpack_file,
166 '--binary', binary_file,
Andy Doanc6500af2012-03-15 14:44:39 -0500167 ]
168
Andy Doanea2e1ea2012-04-23 11:56:29 -0500169 if hwpi.pre_inst_script is not None:
Andy Doanc6500af2012-03-15 14:44:39 -0500170 sdir = os.path.abspath(os.path.dirname(__file__))
Andy Doanea2e1ea2012-04-23 11:56:29 -0500171 script = "%s/%s" % (sdir, hwpi.pre_inst_script)
Andy Doanc6500af2012-03-15 14:44:39 -0500172 print "using presintall script: %s" % script
173 args.append('--preinstall-script')
174 args.append(script)
175
Andy Doanea2e1ea2012-04-23 11:56:29 -0500176 hwpi.do_eula(imgfile)
Andy Doan15aa23b2012-02-02 16:40:58 -0600177 cmd_runner.run(args, as_root=True).wait()
Andy Doan15aa23b2012-02-02 16:40:58 -0600178
Andy Doan184cd3b2012-03-29 22:20:02 -0500179class SnapshotCrawler:
180 def __init__(self, date, platform, hwpacks, binaries):
181 self.date = date
182 self.platform = platform
183 self.hwpacks = hwpacks
184 self.binaries = binaries
Andy Doane449bce2012-03-21 15:22:31 -0500185
Andy Doan184cd3b2012-03-29 22:20:02 -0500186 def get_binaries(self):
187 '''return a hash table of binary->url'''
188 binaryf = {}
189 for binary in self.binaries:
Andy Doan540a4b12012-04-17 11:48:11 -0500190 url = 'http://snapshots.linaro.org/%s/images/%s' % (self.platform, binary)
Andy Doan184cd3b2012-03-29 22:20:02 -0500191 (date, url) = crawler.latest_rfs(url)
Andy Doan4202d882012-03-29 21:36:09 -0500192 binaryf[binary] = url
Andy Doan4202d882012-03-29 21:36:09 -0500193
Andy Doan184cd3b2012-03-29 22:20:02 -0500194 return binaryf
Andy Doana3977372012-03-29 12:22:44 -0500195
Andy Doan184cd3b2012-03-29 22:20:02 -0500196 def get_hwpack_url(self, hwpack):
197 hwpackname = "%s-%s" % (hwpack, self.platform)
198 return hwpack_available(self.date, hwpack, self.platform)
Andy Doan4202d882012-03-29 21:36:09 -0500199
Andy Doan184cd3b2012-03-29 22:20:02 -0500200 def get_image_name(self, odir, hwpack, hwpack_url, binary):
201
202 #covert a url like:
Andy Doan540a4b12012-04-17 11:48:11 -0500203 # 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 -0500204 # to:
Andy Doan66e2d5c2012-04-18 23:01:59 -0500205 # precise/pre-built/lt-panda/87/<file>.img
Andy Doan184cd3b2012-03-29 22:20:02 -0500206 path = urlparse.urlparse(hwpack_url).path
Andy Doan66e2d5c2012-04-18 23:01:59 -0500207 parts = path.split('/')
208 name = parts[-1] #use for the file name below
209 path = '/'.join(parts[-3:-1])
Andy Doan184cd3b2012-03-29 22:20:02 -0500210
211 path = '%s/%s' % (odir, path)
212 if not os.path.exists(path):
213 os.makedirs(path)
Andy Doan66e2d5c2012-04-18 23:01:59 -0500214
215 date = re.search(r'_(\d+-\d+)_', name).group(1)
216 return "%s/%s-%s_%s_%s.img" %(path,hwpack,self.platform,binary,date)
Andy Doan184cd3b2012-03-29 22:20:02 -0500217
Andy Doan15aa23b2012-02-02 16:40:58 -0600218def main():
219 day = today()
220 hwpacks = HWPACKS.keys()
221 binaries = BINARIES.keys()
222
223 p = argparse.ArgumentParser(description=
Andy Doana3977372012-03-29 12:22:44 -0500224 'Builds a matrix of builds from the latest build on '
225 'snapshots.linaro.org or release images on releases.linaro.org')
Andy Doan15aa23b2012-02-02 16:40:58 -0600226
Andy Doan94bfe312012-02-09 17:57:29 -0800227 p.add_argument('-o', dest='out_dir', default='./out',
Andy Doan15aa23b2012-02-02 16:40:58 -0600228 help='The out directory for downloaded and built files, default=./')
229 p.add_argument('-d', dest='date', default=day,
230 help='The date, default=%s' % day)
231 p.add_argument('-w', dest='hwpacks', action='append',
232 help='The hwpacks to generate for, default=%s' %
233 ', '.join(hwpacks))
234 p.add_argument('-b', dest='binaries', action='append',
235 help='The binaries to generate for, default=%s' %
236 ', '.join(binaries))
Andy Doan540a4b12012-04-17 11:48:11 -0500237 p.add_argument('-p', dest='platform', default='precise',
238 help='The platform, default=precise')
Andy Doan4202d882012-03-29 21:36:09 -0500239 p.add_argument('-r', dest='release', action='store_true', default=False,
240 help='If this is for release images. NOTE: the "date" arg'
241 'will then become the cycle ie -d 12.03')
Andy Doan15aa23b2012-02-02 16:40:58 -0600242
243 args = p.parse_args()
Andy Doana3977372012-03-29 12:22:44 -0500244
Andy Doan15aa23b2012-02-02 16:40:58 -0600245 if args.hwpacks:
246 hwpacks = args.hwpacks
247 if args.binaries:
248 binaries = args.binaries
249
Andy Doan1c6ef8b2012-04-23 12:28:50 -0500250 crawler.cookie_setup()
251
Andy Doan15aa23b2012-02-02 16:40:58 -0600252 dm = fetch_image.DownloadManager(args.out_dir)
Andy Doan15aa23b2012-02-02 16:40:58 -0600253
Andy Doan4202d882012-03-29 21:36:09 -0500254 if args.release:
Andy Doan184cd3b2012-03-29 22:20:02 -0500255 site = ReleaseCrawler(args.date, args.platform, hwpacks, binaries)
Andy Doan4202d882012-03-29 21:36:09 -0500256 else:
Andy Doan184cd3b2012-03-29 22:20:02 -0500257 site = SnapshotCrawler(args.date, args.platform, hwpacks, binaries)
258
259 binaryf = site.get_binaries()
Andy Doan15aa23b2012-02-02 16:40:58 -0600260
Andy Doanb00dbc02012-03-15 16:23:06 -0500261 lmc = '%s/linaro-image-tools/linaro-media-create' % args.out_dir
Andy Doan184cd3b2012-03-29 22:20:02 -0500262 odir = '%s/pre-built' % args.out_dir
Andy Doanb00dbc02012-03-15 16:23:06 -0500263
Andy Doan15aa23b2012-02-02 16:40:58 -0600264 for hwpack in hwpacks:
Andy Doan184cd3b2012-03-29 22:20:02 -0500265 url = site.get_hwpack_url(hwpack)
Andy Doanb00dbc02012-03-15 16:23:06 -0500266
Andy Doan2cfe5e62012-02-09 17:41:45 -0800267 if url is not None and url is not False:
Andy Doan15aa23b2012-02-02 16:40:58 -0600268 hwpf = dm.download(url, None)
269 for binary in binaries:
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300270 # create the image and metadata
Andy Doan184cd3b2012-03-29 22:20:02 -0500271 imgfile = site.get_image_name(odir,hwpack,url,binary)
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300272 infofile = '%s.html' % os.path.splitext(imgfile)[0]
Andy Doan81211902012-03-14 14:37:30 -0500273 rfsf = dm.download(binaryf[binary], None)
Andy Doanb00dbc02012-03-15 16:23:06 -0500274 build_image(lmc, imgfile, hwpack, hwpf, binary, rfsf)
Ricardo Salveti de Araujob93a7ec2012-05-29 14:07:53 -0300275 bz2file = compress_image(imgfile)
276 zsync_image(bz2file)
277 create_image_info(infofile, bz2file,
278 imgfile, url, binaryf[binary])
Andy Doan15aa23b2012-02-02 16:40:58 -0600279 else:
Andy Doan8ff38f02012-02-20 16:32:37 -0600280 print '%s hwpack not available for %s' % (args.date,hwpack)
Andy Doan15aa23b2012-02-02 16:40:58 -0600281
282if __name__ == '__main__':
283 main()
284