Skip to content

Commit

Permalink
Added proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
wbond committed Sep 4, 2011
1 parent 222da4b commit 5b02620
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
48 changes: 38 additions & 10 deletions Package Control.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ def __str__(self):


class CliDownloader():
def __init__(self, settings):
self.settings = settings

def find_binary(self, name):
dirs = ['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin',
'/sbin', '/bin']
Expand All @@ -318,7 +321,21 @@ def execute(self, args):


class UrlLib2Downloader():
def __init__(self, settings):
self.settings = settings

def download(self, url, error_message, timeout, tries):
if self.settings.get('http_proxy') or self.settings.get('https_proxy'):
proxies = {}
if self.settings.get('http_proxy'):
proxies['http'] = self.settings.get('http_proxy')
if not self.settings.get('https_proxy'):
proxies['https'] = self.settings.get('http_proxy')
if self.settings.get('https_proxy'):
proxies['https'] = self.settings.get('https_proxy')
proxy_handler = urllib2.ProxyHandler(proxies)
urllib2.install_opener(urllib2.build_opener(proxy_handler))

while tries > 0:
tries -= 1
try:
Expand Down Expand Up @@ -346,13 +363,20 @@ def download(self, url, error_message, timeout, tries):


class WgetDownloader(CliDownloader):
def __init__(self):
self.binary = self.find_binary('wget')

def download(self, url, error_message, timeout, tries):
command = [self.binary, '--timeout', str(int(timeout)), '-o',
wget = self.find_binary('wget')
if not wget:
return False
command = [wget, '--timeout', str(int(timeout)), '-o',
'/dev/null', '-O', '-', '-U', 'Sublime Package Control', url]

if self.settings.get('http_proxy'):
os.putenv('http_proxy', self.settings.get('http_proxy'))
if not self.settings.get('https_proxy'):
os.putenv('https_proxy', self.settings.get('http_proxy'))
if self.settings.get('https_proxy'):
os.putenv('https_proxy', self.settings.get('https_proxy'))

while tries > 1:
tries -= 1
try:
Expand All @@ -377,16 +401,20 @@ def download(self, url, error_message, timeout, tries):


class CurlDownloader(CliDownloader):
def __init__(self):
self.binary = self.find_binary('curl')

def download(self, url, error_message, timeout, tries):
curl = self.find_binary('curl')
if not curl:
return False
command = [curl, '-f', '--user-agent', 'Sublime Package Control',
'--connect-timeout', str(int(timeout)), '-s', url]

if self.settings.get('http_proxy'):
os.putenv('http_proxy', self.settings.get('http_proxy'))
if not self.settings.get('https_proxy'):
os.putenv('HTTPS_PROXY', self.settings.get('http_proxy'))
if self.settings.get('https_proxy'):
os.putenv('HTTPS_PROXY', self.settings.get('https_proxy'))

while tries > 1:
tries -= 1
try:
Expand Down Expand Up @@ -603,7 +631,7 @@ def __init__(self):
'package_destination', 'cache_length', 'auto_upgrade',
'files_to_ignore_binary', 'files_to_keep', 'dirs_to_keep',
'git_binary', 'git_update_command', 'hg_binary',
'hg_update_command']:
'hg_update_command', 'http_proxy', 'https_proxy']:
if settings.get(setting) == None:
continue
self.settings[setting] = settings.get(setting)
Expand All @@ -618,11 +646,11 @@ def download_url(self, url, error_message):
is_ssl = re.search('^https://', url) != None

if (is_ssl and has_ssl) or not is_ssl:
downloader = UrlLib2Downloader()
downloader = UrlLib2Downloader(self.settings)
else:
for downloader_class in [CurlDownloader, WgetDownloader]:
try:
downloader = downloader_class()
downloader = downloader_class(self.settings)
break
except (BinaryNotFoundError):
pass
Expand Down
6 changes: 6 additions & 0 deletions Package Control.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
// The number of seconds to cache repository and package info for
"cache_length": 300,

// An HTTP proxy server to use for requests
"http_proxy": "",
// An HTTPS proxy server to use for requests - if not specified, but
// http_proxy is, http_proxy will be used for https_proxy also
"https_proxy": "",

// Custom paths to VCS binaries for when they can't be automatically
// found on the system and a package includes a VCS metadata directory
"git_binary": "",
Expand Down

0 comments on commit 5b02620

Please sign in to comment.