Commit a682bbd0 authored by Julien Muchembled's avatar Julien Muchembled

download: add netrc file support

Like for URL that contain credentials, we still skip auth challenge
because it's faster and:
- we only support one auth scheme (basic)
- netrc provides no way to specify realms, which seem anyway to be
  less and less used ( reports
  that recent browsers don't display them anymore)
parent 1137f2c9
......@@ -20,18 +20,20 @@ except ImportError:
# Python 3
from urllib.request import Request, splituser, urlopen
from urllib.request import Request, splitport, splituser, urlopen
from urllib.parse import urlparse, urlunparse
except ImportError:
# Python 2
from urlparse import urlparse
from urlparse import urlunparse
from urllib2 import Request, splituser, urlopen
from urllib2 import Request, splitport, splituser, urlopen
from zc.buildout.easy_install import realpath
from base64 import b64encode
from contextlib import closing
import errno
import logging
import netrc
import os
import os.path
import re
......@@ -40,6 +42,23 @@ import tempfile
import zc.buildout
from . import bytes2str, str2bytes
class netrc(netrc.netrc):
def __init__(*args):
def authenticators(self, host):
self.__class__, = self.__class__.__bases__
except IOError as e:
if e.errno != errno.ENOENT:
return self.authenticators(host)
netrc = netrc()
class ChecksumError(zc.buildout.UserError):
......@@ -258,13 +277,19 @@ class Download(object):
def urlretrieve(self, url, tmp_path):
scheme, netloc, path, params, query, frag = urlparse(url)
req = url
if scheme in ('http', 'https'):
while scheme in ('http', 'https'): # not a loop
auth, host = splituser(netloc)
if auth:
url = urlunparse((scheme, host, path, params, query, frag))
req = Request(url)
"Basic " + bytes2str(b64encode(str2bytes(auth))))
auth = netrc.authenticators(splitport(host)[0])
if not auth:
auth = '{0}:{2}'.format(*auth)
req = Request(url)
"Basic " + bytes2str(b64encode(str2bytes(auth))))
with closing(urlopen(req)) as url_obj:
with open(tmp_path, 'wb') as fp:
shutil.copyfileobj(url_obj, fp)
......@@ -166,6 +166,24 @@ True
Traceback (most recent call last):
UserError: Error downloading ...: HTTP Error 403: Forbidden
... with netrc:
>>> url = server_url + 'private/foo:bar'
>>> download(url)
Traceback (most recent call last):
UserError: Error downloading ...: HTTP Error 403: Forbidden
>>> import os,
>>> old_home = os.environ['HOME']
>>> home = os.environ['HOME'] = tmpdir('test-netrc')
>>> netrc = join(home, '.netrc')
>>> write(netrc, 'machine localhost login foo password bar')
>>> os.chmod(netrc, 0o600)
>>> path, is_temp = download(url)
>>> is_temp; remove(path)
>>> os.environ['HOME'] = old_home
Downloading using the download cache
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment