Andy Doan | 15aa23b | 2012-02-02 16:40:58 -0600 | [diff] [blame^] | 1 | #!/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 | |
| 23 | from linaro_image_tools import fetch_image |
| 24 | from linaro_image_tools import cmd_runner |
| 25 | |
| 26 | import argparse |
| 27 | import bz2 |
| 28 | import datetime |
| 29 | import re |
| 30 | |
| 31 | # a mapping of hwpack name to l-m-c name |
| 32 | HWPACKS = { |
| 33 | 'lt-panda-oneiric': 'panda', |
| 34 | 'overo-oneiric': 'overo', |
| 35 | } |
| 36 | # a mapping of binary image to its image_file size |
| 37 | BINARIES = { |
| 38 | 'linaro-o-nano': '512M', |
| 39 | 'linaro-o-alip': '2G', |
| 40 | 'linaro-o-ubuntu-desktop': '3G', |
| 41 | } |
| 42 | |
| 43 | class DailyDB(fetch_image.DB): |
| 44 | def __init__(self, index_name, platform, date): |
| 45 | fetch_image.DB.__init__(self, index_name) |
| 46 | self.platform = platform |
| 47 | self.date = date |
| 48 | |
| 49 | def get_todays_hwpacks(self): |
| 50 | hwpacks = [] |
| 51 | for hw in self.execute_return_list( |
| 52 | 'select hardware,url from snapshot_hwpacks ' |
| 53 | 'where platform == ? ' |
| 54 | 'and date == ? ', |
| 55 | (self.platform, self.date)): |
| 56 | hwpacks.append(hw) |
| 57 | return hwpacks |
| 58 | |
| 59 | def get_latest_binary(self, image): |
| 60 | return self.execute_return_list( |
| 61 | 'select date,url from snapshot_binaries ' |
| 62 | 'where image == ? ' |
| 63 | 'order by date desc limit 1', |
| 64 | (image,))[0] |
| 65 | |
| 66 | def get_server_index(download_manager): |
| 67 | f = download_manager.download( |
| 68 | 'http://releases.linaro.org/fetch_image/server_index.bz2', |
| 69 | None) |
| 70 | |
| 71 | zip_search = re.search(r"^(.*)\.bz2$", f) |
| 72 | zipped = bz2.BZ2File(f) |
| 73 | unzipped = open(zip_search.group(1), "w") |
| 74 | unzipped.write(zipped.read()) |
| 75 | zipped.close() |
| 76 | unzipped.close() |
| 77 | |
| 78 | return zip_search.group(1) |
| 79 | |
| 80 | def today(): |
| 81 | d = datetime.date.today() |
| 82 | return "%d%02d%02d" % (d.year, d.month, d.day) |
| 83 | |
| 84 | def hwpack_available(today_hwpacks, hwpack): |
| 85 | for hwpair in today_hwpacks: |
| 86 | if hwpair[0] == hwpack: |
| 87 | return hwpair[1] |
| 88 | return False |
| 89 | |
| 90 | def compress_image(imgfile): |
| 91 | print "compressing %s" % imgfile |
| 92 | args = ('bzip2', imgfile) |
| 93 | cmd_runner.run(args).wait() |
| 94 | |
| 95 | def build_image(outdir, hwpack, hwpack_file, binary, binary_file): |
| 96 | imgfile = "%s/%s_%s.img" %(outdir,hwpack,binary) |
| 97 | print "building image: %s" % imgfile |
| 98 | dev = HWPACKS[hwpack] |
| 99 | size = BINARIES[binary] |
| 100 | |
| 101 | args = ('linaro-media-create', |
| 102 | '--dev', dev, |
| 103 | '--image_file', imgfile, |
| 104 | '--image_size', size, |
| 105 | '--hwpack-force-yes', |
| 106 | '--hwpack', hwpack_file, |
| 107 | '--binary', binary_file, |
| 108 | ) |
| 109 | cmd_runner.run(args, as_root=True).wait() |
| 110 | compress_image(imgfile) |
| 111 | |
| 112 | def main(): |
| 113 | day = today() |
| 114 | hwpacks = HWPACKS.keys() |
| 115 | binaries = BINARIES.keys() |
| 116 | |
| 117 | p = argparse.ArgumentParser(description= |
| 118 | 'Builds a matrix of builds from the latest build on snapshots.linaro.org') |
| 119 | |
| 120 | p.add_argument('-o', dest='out_dir', default='./', |
| 121 | help='The out directory for downloaded and built files, default=./') |
| 122 | p.add_argument('-d', dest='date', default=day, |
| 123 | help='The date, default=%s' % day) |
| 124 | p.add_argument('-w', dest='hwpacks', action='append', |
| 125 | help='The hwpacks to generate for, default=%s' % |
| 126 | ', '.join(hwpacks)) |
| 127 | p.add_argument('-b', dest='binaries', action='append', |
| 128 | help='The binaries to generate for, default=%s' % |
| 129 | ', '.join(binaries)) |
| 130 | p.add_argument('-p', dest='platform', default='oneiric', |
| 131 | help='The platform, default=oneiric') |
| 132 | |
| 133 | args = p.parse_args() |
| 134 | if args.hwpacks: |
| 135 | hwpacks = args.hwpacks |
| 136 | if args.binaries: |
| 137 | binaries = args.binaries |
| 138 | |
| 139 | dm = fetch_image.DownloadManager(args.out_dir) |
| 140 | server_index = get_server_index(dm) |
| 141 | |
| 142 | db = DailyDB(server_index, args.platform, args.date) |
| 143 | today_hwpacks = db.get_todays_hwpacks() |
| 144 | |
| 145 | for hwpack in hwpacks: |
| 146 | url = hwpack_available(today_hwpacks, hwpack) |
| 147 | if url is not None: |
| 148 | hwpf = dm.download(url, None) |
| 149 | for binary in binaries: |
| 150 | (date,url) = db.get_latest_binary(binary) |
| 151 | binaryf = dm.download(url, None) |
| 152 | build_image(args.out_dir, hwpack, hwpf, binary, binaryf) |
| 153 | else: |
| 154 | print 'No hwpack for %s' % hwpack |
| 155 | |
| 156 | if __name__ == '__main__': |
| 157 | main() |
| 158 | |