import os import re import shutil import urllib2 import urlparse from datetime import datetime from download_content_yes_to_lic import LicenseProtectedFileFetcher # Snapshots base URL snapshots_url = "http://snapshots.linaro.org" # Current default distribution distribution = "ubuntu" def geturl(path): try: fd = urllib2.urlopen(path) except urllib2.HTTPError: return "" data = fd.read() fd.close() return data def download(url, path=""): urlpath = urlparse.urlsplit(url).path filename = os.path.basename(urlpath) if path: filename = os.path.join(path,filename) fd = open(filename, "w") try: response = urllib2.urlopen(urllib2.quote(url, safe=":/")) fd = open(filename, 'wb') shutil.copyfileobj(response,fd,0x10000) fd.close() response.close() except: raise RuntimeError("Could not retrieve %s" % url) return filename def find_builds(data): found = [] pattern = re.compile( r'\s*(?P\d+)/?', flags=re.MULTILINE) for search in pattern.finditer(data): found.append(search.group('buildstamp')) return found def _get_filename(filelist): match = None tgzpattern = re.compile( r']*>(?P[\w\-]+(?!\.config)\.tar\.gz)', flags=re.MULTILINE) for match in tgzpattern.finditer(filelist): pass if match is None: # A file may not be found if the build failed return None return match.group('tgzfilename') def find_latest(url): """Find the latest image at the specified url :param url: The base url to search :param extra: The extra path needed to complete the url """ fetcher = LicenseProtectedFileFetcher() builddates = fetcher.get(url) dates = find_builds(builddates) dates = [int(date) for date in dates] dates.sort() if not dates: raise ValueError("No builds found at index url: " + url) lastdate = dates.pop() lastdate = str(lastdate) + "/" latesturl = urlparse.urljoin(url, lastdate) content = fetcher.get(latesturl) filename = _get_filename(content) tgz_url = os.path.join(latesturl, filename) fetcher.close() return tgz_url def find_latest_rootfs(rootfs): """Find the latest root fs image and return the build_id and url :param rootfs: The short name of the rootfs type. """ rootfs_url= "%s/%s/images/%s/" % (snapshots_url, distribution, rootfs) filename = find_latest(rootfs_url) return filename def find_latest_hwpack(hwpack): """Find the latest hardware pack and return the build_id and url :param hwpack: The name of the hardware pack to look for. """ hwpack_url="%s/%s/hwpacks/%s/" % (snapshots_url, distribution, hwpack) filename = find_latest(hwpack_url) return filename def find_ci_builds(data, latest_hwpack): found = False for line in data.splitlines(): if latest_hwpack in line: found = True break return found def find_ci_latest(url, latest_hwpack): """Find the latest image at the specified url :param url: The base url to search :param extra: The extra path needed to complete the url """ filename = None fetcher = LicenseProtectedFileFetcher() builddates = fetcher.get(url) found = find_ci_builds(builddates, latest_hwpack) try: if found: filename = os.path.join(url, latest_hwpack) except Exception: pass fetcher.close() return filename