diff options
author | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-14 14:46:01 +0000 |
---|---|---|
committer | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-14 14:46:01 +0000 |
commit | cc5b28416be6d6954e922cd0be1a333c0a2700b5 (patch) | |
tree | 7a741a0c9cf0852e6ec45a53e8545efedc12597f /scripts | |
parent | bd2b90db81621055191e8b2291d11e0d6da40b2a (diff) | |
parent | f762e4f90cb4c3c1016443b7766569b28454610b (diff) |
Merge in API support
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/download.py | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/download.py b/scripts/download.py new file mode 100644 index 0000000..a4cd21e --- /dev/null +++ b/scripts/download.py @@ -0,0 +1,89 @@ +#!/usr/bin/python + +import json +import urlparse +import shutil +import urllib2 +import os +from html2text import html2text + +# Example of how to use the API to download all files in a directory. This is +# written as one procedural script without functions +directory_url = "http://localhost:8001/build-info" + +# Generate the URL that will return the license information. This is the URL +# of 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(directory_url)] +url_path_section = parsed_url[2] + +parsed_url[2] = "/api/ls" + url_path_section +listing_url = urlparse.urlunsplit(parsed_url) + +u = urllib2.urlopen(listing_url) +data = json.loads(u.read())["files"] + +for file_info in data: + if file_info["type"] == "folder": + # Skip folders... + continue + + parsed_url[2] = "/api/license" + file_info["url"] + license_url = urlparse.urlunsplit(parsed_url) + + parsed_url[2] = file_info["url"] + file_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 data[0] == "Open": + headers = {} + else: + # If this were a command line client designed to ask the user to accept + # each license, you could use this code to ask the user to accept each + # license in turn. In this example we store which licenses are accepted + # so the user only has to accept them once. + if os.path.isfile("accepted_licenses"): + with open("accepted_licenses") as accepted_licenses_file: + accepted_licenses = accepted_licenses_file.read().split() + else: + accepted_licenses = [] + + # Present each license to the user... + for d in data: + if d["digest"] not in accepted_licenses: + # Licenses are stored as HTML. Convert them to markdown (text) + # and print it to the terminal. + print html2text(d["text"]) + + # Ask the user if they accept the license. If they don't we + # terminate the script. + user_response = raw_input("Do you accept this license? (y/N)") + if user_response != "y": + exit(1) + + accepted_licenses.append(d["digest"]) + + # Store the licenses that the user accepted + with open("accepted_licenses", "w") as accepted_licenses_file: + accepted_licenses_file.write(" ".join(accepted_licenses)) + + # 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(file_url, headers=headers)) + with open(os.path.basename(parsed_url[2]), 'wb') as fp: + shutil.copyfileobj(req, fp) |