summaryrefslogtreecommitdiff
path: root/find_latest.py
blob: 7441a4e2c196326eeb72aba0f8c551a697b59ba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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'<td>\s*<a href="(/.*/)?\d+/?"[\n ]*>(?P<buildstamp>\d+)/?</a>',
         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'<a href=".*"[^>]*>(?P<tgzfilename>[\w\-]+(?!\.config)\.tar\.gz)</a>',
        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()
    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