Permalink
Browse files

Implemented support for basic authentication (#36)

  • Loading branch information...
1 parent 722e566 commit 44efef0c9911a480cea49f3a990fc57a2c8a6519 @davehunt davehunt committed Dec 4, 2012
Showing with 37 additions and 3 deletions.
  1. +37 −3 mozdownload/scraper.py
View
@@ -13,6 +13,7 @@
import re
import sys
import urllib
+import urllib2
import mozinfo
@@ -50,7 +51,8 @@ class Scraper(object):
"""Generic class to download an application from the Mozilla server"""
def __init__(self, directory, version, platform=None,
- application='firefox', locale='en-US', extension=None):
+ application='firefox', locale='en-US', extension=None,
+ authentication=None):
# Private properties for caching
self._target = None
@@ -61,6 +63,7 @@ def __init__(self, directory, version, platform=None,
self.platform = platform or self.detect_platform()
self.version = version
self.extension = extension or DEFAULT_FILE_EXTENSIONS[self.platform]
+ self.authentication = authentication
# build the base URL
self.application = application
@@ -170,7 +173,24 @@ def download(self):
print 'Downloading from: %s' % (urllib.unquote(self.final_url))
tmp_file = self.target + ".part"
- urllib.urlretrieve(self.final_url, tmp_file)
+
+ if self.authentication \
+ and self.authentication['username'] \
+ and self.authentication['password']:
+ password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
+ password_mgr.add_password(None,
+ self.final_url,
+ self.authentication['username'],
+ self.authentication['password'])
+ handler = urllib2.HTTPBasicAuthHandler(password_mgr)
+ opener = urllib2.build_opener(urllib2.HTTPHandler, handler)
+ urllib2.install_opener(opener)
+
+ r = urllib2.urlopen(self.final_url)
+ CHUNK = 16 * 1024
+ with open(tmp_file, 'wb') as f:
+ for chunk in iter(lambda: r.read(CHUNK), ''):
+ f.write(chunk)
os.rename(tmp_file, self.target)
except:
try:
@@ -694,6 +714,16 @@ def cli():
metavar='EXTENSION',
help='File extension of the build (e.g. "zip"), default:\
the standard build extension on the platform.')
+ parser.add_option('--username',
+ dest='username',
+ default=None,
+ metavar='USERNAME',
+ help='Username for basic HTTP authentication.')
+ parser.add_option('--password',
+ dest='password',
+ default=None,
+ metavar='PASSWORD',
+ help='Password for basic HTTP authentication.')
# Option group for candidate builds
group = OptionGroup(parser, "Candidate builds",
@@ -750,7 +780,11 @@ def cli():
'platform': options.platform,
'version': options.version,
'directory': options.directory,
- 'extension': options.extension}
+ 'extension': options.extension,
+ 'authentication': {
+ 'username': options.username,
+ 'password': options.password}
+ }
scraper_options = {'candidate': {
'build_number': options.build_number,
'no_unsigned': options.no_unsigned},

0 comments on commit 44efef0

Please sign in to comment.