Marcel Telka
2015-12-14 a9be2dae309b16685f70db3dc19faa9d0198caca
userland-fetch: Add support for custom User-Agent
2 files modified
29 ■■■■■ changed files
make-rules/prep-download.mk 3 ●●●● patch | view | raw | blame | history
tools/userland-fetch 26 ●●●●● patch | view | raw | blame | history
make-rules/prep-download.mk
@@ -51,7 +51,8 @@
    $$(FETCH) --file $$@ \
        $$(COMPONENT_ARCHIVE_URL$(1):%=--url %) \
        $$(COMPONENT_ARCHIVE_HASH$(1):%=--hash %) \
        $$(COMPONENT_SIG_URL$(1):%=--sigurl %)
        $$(COMPONENT_SIG_URL$(1):%=--sigurl %) \
        $$(if $$(COMPONENT_FETCH_USER_AGENT$(1)),--user-agent $$(COMPONENT_FETCH_USER_AGENT$(1)))
    $$(TOUCH) $$@
REQUIRED_PACKAGES += runtime/python-26
tools/userland-fetch
@@ -1,4 +1,4 @@
#!/usr/bin/python2.6
#!/usr/bin/python2.7
#
# CDDL HEADER START
#
@@ -33,6 +33,7 @@
import shutil
from urllib import splittype
from urllib2 import urlopen
from urllib2 import Request
import hashlib
def printIOError(e, txt):
@@ -101,11 +102,14 @@
    return validate(file, hash)
def download(url, filename = None):
def download(url, filename = None, user_agent_arg = None):
    src = None
    try:
        src = urlopen(url)
        req = Request(url)
        if user_agent_arg != None:
            req.add_header("User-Agent", user_agent_arg)
        src = urlopen(req)
    except IOError as e:
        printIOError(e, "Can't open url " + url)
        return None
@@ -161,7 +165,9 @@
    return urls
def usage():
    print "Usage: %s [-f|--file (file)] [-l|--link] [-h|--hash (hash)] [-s|--search (search-dir)] --url (url)" % (sys.argv[0].split('/')[-1])
    print "Usage: %s [-a|--user-agent (user-agent)] [-f|--file (file)] " \
        "[-l|--link] [-h|--hash (hash)] [-s|--search (search-dir)] " \
        "-u|--url (url)" % (sys.argv[0].split('/')[-1])
    sys.exit(1)
def main():
@@ -170,6 +176,7 @@
    # FLUSH STDOUT 
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
    user_agent_arg = None
    file_arg = None
    link_arg = False
    hash_arg = None
@@ -177,14 +184,17 @@
    search_list = list()
    try:
        opts, args = getopt.getopt(sys.argv[1:], "f:h:ls:u:",
            ["file=", "link", "hash=", "search=", "url="])
        opts, args = getopt.getopt(sys.argv[1:], "a:f:h:ls:u:",
            ["file=", "link", "hash=", "search=", "url=",
            "user-agent="])
    except getopt.GetoptError, err:
        print str(err)
        usage()
    for opt, arg in opts:
        if opt in [ "-f", "--file" ]:
        if opt in [ "-a", "--user-agent" ]:
            user_agent_arg = arg
        elif opt in [ "-f", "--file" ]:
            file_arg = arg
        elif opt in [ "-l", "--link" ]:
            link_arg = True
@@ -221,7 +231,7 @@
                pass
        elif scheme in [ 'http', 'https', 'ftp' ]:
            print "\n    downloading...",
            name = download(url, file_arg)
            name = download(url, file_arg, user_agent_arg)
            if name == None:
                print "failed"
                continue