blob: aaf146b3c324d4792c320c2548b253729a55276b [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
Marcin Juszkiewicz25ba61f2013-04-02 14:37:54 +020027import snapshotsapi
Marcin Juszkiewicz2dac3392013-04-02 12:48:00 +020028
29def download(api_urls, file_regexp):
30 """Example of how to use the API to download a/all files in a directory."""
31
32 # Get listing for file(s) pointed to by URL we were given
33 request = urllib2.urlopen(api_urls.ls())
34 listing = json.loads(request.read())["files"]
35
36 for file_info in listing:
37 if file_info["type"] == "folder":
38 # Skip folders...
39 continue
40
41 if not re.match(file_regexp, file_info["name"]):
42 continue
43
44 print "downloading %s" % file_info["url"]
45 # just download the file.
46 req = urllib2.urlopen(urllib2.Request(api_urls.file(file_info["url"])))
47 with open(os.path.basename(file_info["url"]), 'wb') as fp:
48 shutil.copyfileobj(req, fp)
49
Marcin Juszkiewicz2dac3392013-04-02 12:48:00 +020050def check_for_latest(api_urls):
51
52 request = urllib2.urlopen(api_urls.ls())
53 listing = json.loads(request.read())["files"]
54
55 latest = 0
56 for file_info in listing:
57 if file_info["name"] != "latest" and file_info["name"] > latest:
58 latest = file_info["name"]
59
60 print "latest is %s" % latest
61 return latest
62
63if __name__ == '__main__':
64 if len(sys.argv) != 3:
65 print >> sys.stderr, "Usage: fetch-from-snapshots.py base_url filenameregexp"
66 print >> sys.stderr, ""
67 print >> sys.stderr, "Example: fetch-from-snapshots.py http://snapshots.linaro.org/openembedded/images/minimal-armv8 linaro-image-minimal.*tar.gz"
68 exit(1)
69
Marcin Juszkiewicz25ba61f2013-04-02 14:37:54 +020070 latest = check_for_latest(snapshotsapi.ApiUrls(sys.argv[1]))
71 download(snapshotsapi.ApiUrls(sys.argv[1] + "/" + latest), sys.argv[2])