diff options
author | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-18 18:22:29 +0000 |
---|---|---|
committer | James Tunnicliffe <james.tunnicliffe@linaro.org> | 2013-03-18 18:22:29 +0000 |
commit | 7add8558c7ba0b61c2db20f2f8ca2e039fd8854b (patch) | |
tree | b616839f8b600f0bd6e872c09a89e88285f321e6 /scripts | |
parent | 14cf852830533900c42031eef6d95f318e82adc8 (diff) |
/api/ls/<path to file> now returns information for a single file.
Updated download.py to be more pythonic. Put the interesting bit in download, rest is in main and a convinience class.
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/download.py | 177 |
1 files changed, 99 insertions, 78 deletions
diff --git a/scripts/download.py b/scripts/download.py index a4cd21e..e980424 100644 --- a/scripts/download.py +++ b/scripts/download.py @@ -6,84 +6,105 @@ import shutil import urllib2 import os from html2text import html2text +import sys +import xdg.BaseDirectory as xdgBaseDir + + +def download(api_urls, accepted_licenses): + """Example of how to use the API to download a/all files in a directory.""" + + # Get listing for file(s) pointed to by URL we were given + request = urllib2.urlopen(api_urls.ls()) + listing = json.loads(request.read())["files"] + + for file_info in listing: + if file_info["type"] == "folder": + # Skip folders... + continue + + # 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. + request = urllib2.urlopen(api_urls.license(file_info["url"])) + licenses = json.loads(request.read())["licenses"] + + if licenses[0] == "Open": + headers = {} + else: + # Present each license to the user... + for lic in licenses: + if lic["digest"] not in accepted_licenses: + # Licenses are stored as HTML. Convert them to markdown + # (text) and print it to the terminal. + print html2text(lic["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) + + # Remember this license acceptance for another download. + accepted_licenses.append(lic["digest"]) + + # To accept a license, place the digest in the LICENSE_ACCEPTED + # header. For multiple licenses, they are stored space separated. + digests = [lic["digest"] for lic in licenses] + headers = {"LICENSE_ACCEPTED": " ".join(digests)} + + # Once the header has been generated, just download the file. + req = urllib2.urlopen(urllib2.Request(api_urls.file(file_info["url"]), + headers=headers)) + with open(os.path.basename(file_info["url"]), 'wb') as fp: + shutil.copyfileobj(req, fp) + + +class ApiUrls(): + """Since we want to manipulate URLS, but urlsplit returns an immutable + object this is a convenience object to perform the manipulations for us""" + def __init__(self, input_url): + self.parsed_url = [c for c in urlparse.urlsplit(input_url)] + + def ls(self): + self.parsed_url[2] = "/api/ls" + self.parsed_url[2] + return urlparse.urlunsplit(self.parsed_url) + + def license(self, path): + self.parsed_url[2] = "/api/license" + path + return urlparse.urlunsplit(self.parsed_url) + + def file(self, path): + self.parsed_url[2] = path + return urlparse.urlunsplit(self.parsed_url) + + +if __name__ == '__main__': + if len(sys.argv) != 2: + # Check that a URL has been supplied. + print >> sys.stderr, "Usage: download.py <URL>" + exit(1) + + accepted_licenses_path = os.path.join(xdgBaseDir.xdg_data_home, + "linaro", + "accepted_licenses") + + # Later we ask the user to accept each license in turn. Store which + # licenses are accepted so the user only has to accept them once. + if os.path.isfile(accepted_licenses_path): + with open(accepted_licenses_path) as accepted_licenses_file: + accepted_licenses = accepted_licenses_file.read().split() + else: + accepted_licenses = [] -# 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) + api_urls = ApiUrls(sys.argv[1]) - # 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"] + download(api_urls, accepted_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) + # Store the licenses that the user accepted + with open(accepted_licenses_path, "w") as accepted_licenses_file: + accepted_licenses_file.write(" ".join(accepted_licenses)) |