Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Andy Doan | 1c6ef8b | 2012-04-23 12:28:50 -0500 | [diff] [blame] | 3 | import cookielib |
| 4 | import os |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 5 | import re |
| 6 | import urllib2 |
Fathi Boudra | 75e299a | 2012-08-26 19:04:03 +0300 | [diff] [blame] | 7 | import sys |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 8 | import json |
| 9 | import urlparse |
| 10 | import snapshotsapi |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 11 | |
Andy Doan | 1c6ef8b | 2012-04-23 12:28:50 -0500 | [diff] [blame] | 12 | def cookie_setup(): |
| 13 | cookies = os.getenv('LMC_COOKIES') |
| 14 | if cookies: |
| 15 | cj = cookielib.LWPCookieJar() |
| 16 | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
| 17 | opener.addheaders.append(('Cookie', cookies)) |
| 18 | urllib2.install_opener(opener) |
| 19 | |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 20 | def list_links(url, regex=""): |
Andy Doan | 8121190 | 2012-03-14 14:37:30 -0500 | [diff] [blame] | 21 | try: |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 22 | api = snapshotsapi.ApiUrls(url) |
| 23 | response = urllib2.urlopen(api.ls()) |
| 24 | listing = json.loads(response.read())["files"] |
| 25 | links = [] |
| 26 | for file_info in listing: |
| 27 | # we do not care about /latest/ - we want numbers |
| 28 | if file_info["name"] == "latest": |
| 29 | continue |
| 30 | |
| 31 | if regex == "" or re.match(regex, file_info["name"]): |
| 32 | # for folders we will dig deeper but not for files |
| 33 | if file_info["type"] == "folder": |
| 34 | links.append([int(file_info["name"]), file_info["url"]]) |
| 35 | else: |
| 36 | links.append([file_info["name"], file_info["name"]]) |
| 37 | |
Andy Doan | 8121190 | 2012-03-14 14:37:30 -0500 | [diff] [blame] | 38 | except urllib2.HTTPError,e: |
Ricardo Salveti de Araujo | ba7dacf | 2012-06-23 01:08:24 -0300 | [diff] [blame] | 39 | print "ERROR: finding links for (%s): %s" % (url, e) |
Andy Doan | 8121190 | 2012-03-14 14:37:30 -0500 | [diff] [blame] | 40 | links = [] #return empty array |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 41 | return links |
| 42 | |
Andy Doan | 540a4b1 | 2012-04-17 11:48:11 -0500 | [diff] [blame] | 43 | def list_hwpack(url): |
| 44 | ''' returns tuple of (buildate, url) |
| 45 | ''' |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 46 | urls = list_links(url, r'.*hwpack.*?\.tar\.gz') |
Andy Doan | 5f20efb | 2012-03-29 21:35:16 -0500 | [diff] [blame] | 47 | for link in urls: |
Ricardo Salveti de Araujo | 688f24f | 2012-10-17 02:40:36 -0300 | [diff] [blame] | 48 | try: |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 49 | build_date = re.compile('_(\d+)-').findall(link[1]) |
| 50 | return (build_date[0], '%s/%s' % (url,link[1])) |
Ricardo Salveti de Araujo | 688f24f | 2012-10-17 02:40:36 -0300 | [diff] [blame] | 51 | except: |
| 52 | return None |
Andy Doan | 540a4b1 | 2012-04-17 11:48:11 -0500 | [diff] [blame] | 53 | return None |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 54 | |
| 55 | def latest_hwpacks(url, limit=7): |
| 56 | '''returns an array of tuples (build-date, hwpack url) like: |
| 57 | [ (20120210, http://foo.bar/hwpack.tar.gz), (20120209, blah.tar.gz) ] |
| 58 | ''' |
| 59 | # only analyze the last few builds |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 60 | links = list_links(url) |
| 61 | links = sorted(links, reverse=True)[:limit] |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 62 | hwpacks = [] |
Andy Doan | 1c6ef8b | 2012-04-23 12:28:50 -0500 | [diff] [blame] | 63 | for link in links: |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 64 | build = list_hwpack('%s/%s'% (url, link[0])) |
Andy Doan | 540a4b1 | 2012-04-17 11:48:11 -0500 | [diff] [blame] | 65 | if build is not None: |
| 66 | hwpacks.append(build) |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 67 | return hwpacks |
| 68 | |
Andy Doan | 5f20efb | 2012-03-29 21:35:16 -0500 | [diff] [blame] | 69 | def list_rfs(url): |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 70 | links = list_links(url, r'.*(?!config)(?:rootfs\.)?tar\.gz') |
Marcin Juszkiewicz | 3b3eed6 | 2013-04-17 11:45:09 +0200 | [diff] [blame] | 71 | if len(links): |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 72 | return "%s/%s" %(url,links[0][0]) |
Andy Doan | 5f20efb | 2012-03-29 21:35:16 -0500 | [diff] [blame] | 73 | return None |
| 74 | |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 75 | def latest_rfs(url, limit=7): |
| 76 | ''' |
| 77 | Returns a tuple of (builddate, url) |
| 78 | ''' |
| 79 | # only analyze the last few builds |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 80 | links = list_links(url) |
| 81 | links = sorted(links, reverse=True)[:limit] |
Andy Doan | 1c6ef8b | 2012-04-23 12:28:50 -0500 | [diff] [blame] | 82 | for link in links: |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 83 | build = list_rfs('%s/%s' %(url, link[0])) |
Andy Doan | 540a4b1 | 2012-04-17 11:48:11 -0500 | [diff] [blame] | 84 | if build is not None: |
Marcin Juszkiewicz | c583264 | 2013-04-02 14:39:14 +0200 | [diff] [blame] | 85 | return (link[0], build) |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 86 | |
| 87 | return None |
| 88 | |
| 89 | if __name__ == '__main__': |
Andy Doan | 1c6ef8b | 2012-04-23 12:28:50 -0500 | [diff] [blame] | 90 | cookie_setup() |
| 91 | |
Andy Doan | 8ff38f0 | 2012-02-20 16:32:37 -0600 | [diff] [blame] | 92 | for arg in sys.argv[1:]: |
| 93 | print "HWPACKS for: %s" % arg |
| 94 | hwpacks = latest_hwpacks(arg, 4) |
| 95 | for hwpack in hwpacks: |
| 96 | print " %s: %s" % hwpack |
| 97 | |
| 98 | print "latest nano:" |
Fathi Boudra | aa6cbf8 | 2013-12-09 20:46:04 +0200 | [diff] [blame] | 99 | print " %s %s" % latest_rfs('http://snapshots.linaro.org/ubuntu/images/nano') |