diff options
author | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-11 16:27:27 +0000 |
---|---|---|
committer | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-11 16:27:27 +0000 |
commit | 38715f1bf322be59ce5478839a1e856ff1263784 (patch) | |
tree | 1b9316f22110f03825e0af54eb5bfe4496b6c4e1 /scripts | |
parent | 3e6b54efeec74c8ad3d63efea0f4986870fec664 (diff) |
Moved download.py to a more sensible location
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/download.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/scripts/download.py b/scripts/download.py new file mode 100644 index 0000000..8ca5249 --- /dev/null +++ b/scripts/download.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import json +import urlparse +import shutil +import urllib2 +import os + +# Example of how to use the API to download a file from a known location +url = "http://localhost:8001/build-info/multi-license.txt" +url = "http://localhost:8001/build-info/snowball-blob.txt" + +# Generate the URL that will return the license information. This is the URL +# if the file with /api/license prepended to the path. + +# Unfortunately urlsplit returns an immutable object. Convert it to an array +# so we can modify the path section (index 2) +parsed_url = [c for c in urlparse.urlsplit(url)] +parsed_url[2] = "/api/license" + parsed_url[2] +license_url = urlparse.urlunsplit(parsed_url) + +# Get the licenses. They are returned as a JSON document in the form: +# {"licenses": +# [{"text": "<license text>", "digest": "<digest of license>"}, +# {"text": "<license text>", "digest": "<digest of license>"}, +# ... +# ]} +# Each license has a digest associated with it. +u = urllib2.urlopen(license_url) +data = json.loads(u.read())["licenses"] + +# If this were a command line client designed to ask the user to accept each +# license, you would print the licenses here and use +# raw_input("Do you accept this license? (y/N)") to get the user's response. + +# To accept a license, place the digest in the LICENSE_ACCEPTED header. For +# multiple licenses, they are stored space separated. +digests = [d["digest"] for d in data] +headers = {"LICENSE_ACCEPTED": " ".join(digests)} + +# Once the header has been generated, just download the file. +req = urllib2.urlopen(urllib2.Request(url, headers=headers)) +with open(os.path.basename(parsed_url[2]), 'wb') as fp: + shutil.copyfileobj(req, fp) |