blob: 9962b4792cbcf0187c1d39ea10dfacb173b60c5f [file] [log] [blame]
Marcin Juszkiewicz2dac3392013-04-02 12:48:00 +02001#!/usr/bin/python
2#
3# (C) 2013 Linaro Ltd.
4#
5# Based on scripts/download.py from lp:linaro-licence-protection
6#
7# This program is free software: you can redistribute it and/or modify it under
8# the terms of the GNU Affero General Public License as published by the Free
9# Software Foundation, either version 3 of the License, or (at your option) any
10# later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14# FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
15# details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20import json
21import urlparse
22import shutil
23import urllib2
24import os
25import sys
26import re
27
28def download(api_urls, file_regexp):
29 """Example of how to use the API to download a/all files in a directory."""
30
31 # Get listing for file(s) pointed to by URL we were given
32 request = urllib2.urlopen(api_urls.ls())
33 listing = json.loads(request.read())["files"]
34
35 for file_info in listing:
36 if file_info["type"] == "folder":
37 # Skip folders...
38 continue
39
40 if not re.match(file_regexp, file_info["name"]):
41 continue
42
43 print "downloading %s" % file_info["url"]
44 # just download the file.
45 req = urllib2.urlopen(urllib2.Request(api_urls.file(file_info["url"])))
46 with open(os.path.basename(file_info["url"]), 'wb') as fp:
47 shutil.copyfileobj(req, fp)
48
49class ApiUrls():
50 """Since we want to manipulate URLS, but urlsplit returns an immutable
51 object this is a convenience object to perform the manipulations for us"""
52 def __init__(self, input_url):
53 self.parsed_url = [c for c in urlparse.urlsplit(input_url)]
54 self.path = self.parsed_url[2]
55
56 def ls(self, path=None):
57 if not path:
58 path = self.path
59 self.parsed_url[2] = "/api/ls" + path
60 return urlparse.urlunsplit(self.parsed_url)
61
62 def license(self, path):
63 self.parsed_url[2] = "/api/license" + path
64 return urlparse.urlunsplit(self.parsed_url)
65
66 def file(self, path):
67 self.parsed_url[2] = path
68 return urlparse.urlunsplit(self.parsed_url)
69
70def check_for_latest(api_urls):
71
72 request = urllib2.urlopen(api_urls.ls())
73 listing = json.loads(request.read())["files"]
74
75 latest = 0
76 for file_info in listing:
77 if file_info["name"] != "latest" and file_info["name"] > latest:
78 latest = file_info["name"]
79
80 print "latest is %s" % latest
81 return latest
82
83if __name__ == '__main__':
84 if len(sys.argv) != 3:
85 print >> sys.stderr, "Usage: fetch-from-snapshots.py base_url filenameregexp"
86 print >> sys.stderr, ""
87 print >> sys.stderr, "Example: fetch-from-snapshots.py http://snapshots.linaro.org/openembedded/images/minimal-armv8 linaro-image-minimal.*tar.gz"
88 exit(1)
89
90 latest = check_for_latest(ApiUrls(sys.argv[1]))
91 download(ApiUrls(sys.argv[1] + "/" + latest), sys.argv[2])