From f051b3d36871ccd9f0f5fc770047c497f196547a Mon Sep 17 00:00:00 2001 From: Kai Hoppert Date: Sat, 10 Aug 2013 18:24:23 +0200 Subject: Added .pypirc authentication support for getting eggs --HG-- extra : histedit_source : ebdc22b1f156f8af40265c5772addcfda6ae11d8 --- setuptools/package_index.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 9c9d76a1..ca228997 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -6,6 +6,8 @@ import shutil import socket import base64 +import ConfigParser + from pkg_resources import ( CHECKOUT_DIST, Distribution, BINARY_DIST, normalize_path, SOURCE_DIST, require, Environment, find_distributions, safe_name, safe_version, @@ -918,6 +920,37 @@ def _encode_auth(auth): # strip the trailing carriage return return encoded.rstrip() +class PyPirc: + + def __init__(self): + """ + Extract pypirc authentication information from home directory + """ + self.dict_ = {} + + if os.environ.has_key('HOME'): + rc = os.path.join(os.environ['HOME'], '.pypirc') + if os.path.exists(rc): + config = ConfigParser.ConfigParser({ + 'username' : '', + 'password' : '', + 'repository' : ''}) + config.read(rc) + + for section in config.sections(): + if config.get(section, 'repository').strip(): + value = '%s:%s'%(config.get(section, 'username').strip(), + config.get(section, 'password').strip()) + self.dict_[config.get(section, 'repository').strip()] = value + + def __call__(self, url): + """ """ + for base_url, auth in self.dict_.items(): + if url.startswith(base_url): + return auth + + + def open_with_auth(url, opener=urllib2.urlopen): """Open a urllib2 request, handling HTTP authentication""" @@ -933,6 +966,10 @@ def open_with_auth(url, opener=urllib2.urlopen): else: auth = None + if not auth: + auth = PyPirc()(url) + log.info('Authentication found for URL: %s'%url) + if auth: auth = "Basic " + _encode_auth(auth) new_url = urlunparse((scheme,host,path,params,query,frag)) -- cgit v1.2.1 From 3822e3e7ad8cda5012b602f7e8f52cdd44665dd3 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 09:35:20 -0500 Subject: Delint syntax --HG-- extra : amend_source : 0f58aa917ebc71efd2904638fac3838fd0b0e4dd --- setuptools/package_index.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index ca228997..a0bb936d 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -920,29 +920,29 @@ def _encode_auth(auth): # strip the trailing carriage return return encoded.rstrip() -class PyPirc: +class PyPirc(object): def __init__(self): """ - Extract pypirc authentication information from home directory + Extract pypirc authentication information from home directory """ self.dict_ = {} - if os.environ.has_key('HOME'): + if 'HOME' in os.environ: rc = os.path.join(os.environ['HOME'], '.pypirc') if os.path.exists(rc): config = ConfigParser.ConfigParser({ - 'username' : '', - 'password' : '', - 'repository' : ''}) + 'username': '', + 'password': '', + 'repository': ''}) config.read(rc) for section in config.sections(): if config.get(section, 'repository').strip(): - value = '%s:%s'%(config.get(section, 'username').strip(), + value = '%s:%s' % (config.get(section, 'username').strip(), config.get(section, 'password').strip()) self.dict_[config.get(section, 'repository').strip()] = value - + def __call__(self, url): """ """ for base_url, auth in self.dict_.items(): @@ -950,7 +950,6 @@ class PyPirc: return auth - def open_with_auth(url, opener=urllib2.urlopen): """Open a urllib2 request, handling HTTP authentication""" @@ -968,7 +967,7 @@ def open_with_auth(url, opener=urllib2.urlopen): if not auth: auth = PyPirc()(url) - log.info('Authentication found for URL: %s'%url) + log.info('Authentication found for URL: %s' % url) if auth: auth = "Basic " + _encode_auth(auth) -- cgit v1.2.1 From b79e972b8928224516e5361342c4d2f11c83fb24 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 09:44:07 -0500 Subject: Refactor for flatter code --- setuptools/package_index.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index a0bb936d..2e0f2092 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -928,20 +928,30 @@ class PyPirc(object): """ self.dict_ = {} - if 'HOME' in os.environ: - rc = os.path.join(os.environ['HOME'], '.pypirc') - if os.path.exists(rc): - config = ConfigParser.ConfigParser({ - 'username': '', - 'password': '', - 'repository': ''}) - config.read(rc) - - for section in config.sections(): - if config.get(section, 'repository').strip(): - value = '%s:%s' % (config.get(section, 'username').strip(), - config.get(section, 'password').strip()) - self.dict_[config.get(section, 'repository').strip()] = value + if 'HOME' not in os.environ: + return + + rc = os.path.join(os.environ['HOME'], '.pypirc') + if not os.path.exists(rc): + return + + initial = dict.fromkeys(['username', 'password', 'repository'], '') + config = ConfigParser.ConfigParser(initial) + config.read(rc) + + sections_with_repositories = [ + section for section in config.sections() + if config.get(section, 'repository').strip() + ] + + for section in sections_with_repositories: + auth = ( + config.get(section, 'username').strip(), + config.get(section, 'password').strip(), + ) + value = '%s:%s' % auth + repo = config.get(section, 'repository').strip() + self.dict_[repo] = value def __call__(self, url): """ """ -- cgit v1.2.1 From e405a2b4861216e3c6b50c79f7b26289656fdb41 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 09:52:58 -0500 Subject: Extract 'Credential' class for representing a username/password credential --- setuptools/package_index.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 2e0f2092..3154eccb 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -920,6 +920,21 @@ def _encode_auth(auth): # strip the trailing carriage return return encoded.rstrip() +class Credential(object): + """ + A username/password pair. Use like a namedtuple. + """ + def __init__(self, username, password): + self.username = username + self.password = password + + def __iter__(self): + yield self.username + yield self.password + + def __str__(self): + return '%(username)s:%(password)s' % vars(self) + class PyPirc(object): def __init__(self): @@ -945,13 +960,12 @@ class PyPirc(object): ] for section in sections_with_repositories: - auth = ( + cred = Credential( config.get(section, 'username').strip(), config.get(section, 'password').strip(), ) - value = '%s:%s' % auth repo = config.get(section, 'repository').strip() - self.dict_[repo] = value + self.dict_[repo] = cred def __call__(self, url): """ """ @@ -976,7 +990,7 @@ def open_with_auth(url, opener=urllib2.urlopen): auth = None if not auth: - auth = PyPirc()(url) + auth = str(PyPirc()(url)) log.info('Authentication found for URL: %s' % url) if auth: -- cgit v1.2.1 From fccf9357c484e4b475348dfbef321e544e2a3cab Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 09:56:22 -0500 Subject: Use 'expanduser' for better compatibilty. --- setuptools/package_index.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 3154eccb..5f328a03 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -943,10 +943,7 @@ class PyPirc(object): """ self.dict_ = {} - if 'HOME' not in os.environ: - return - - rc = os.path.join(os.environ['HOME'], '.pypirc') + rc = os.path.join(os.path.expanduser('~'), '.pypirc') if not os.path.exists(rc): return -- cgit v1.2.1 From bf742c3f6069802d26c09118dd6c36d55634e22d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:08:50 -0500 Subject: Derive PyPirc from ConfigParser for more general purpose use. --- setuptools/package_index.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 5f328a03..5b8dc357 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -935,34 +935,35 @@ class Credential(object): def __str__(self): return '%(username)s:%(password)s' % vars(self) -class PyPirc(object): +class PyPirc(ConfigParser.ConfigParser): def __init__(self): """ - Extract pypirc authentication information from home directory + Load from ~/.pypirc """ - self.dict_ = {} + defaults = dict.fromkeys(['username', 'password', 'repository'], '') + super(PyPirc, self).__init__(defaults) rc = os.path.join(os.path.expanduser('~'), '.pypirc') - if not os.path.exists(rc): - return - - initial = dict.fromkeys(['username', 'password', 'repository'], '') - config = ConfigParser.ConfigParser(initial) - config.read(rc) + if os.path.exists(rc): + self.read(rc) + @property + def dict_(self): + dict_ = {} sections_with_repositories = [ - section for section in config.sections() - if config.get(section, 'repository').strip() + section for section in self.sections() + if self.get(section, 'repository').strip() ] for section in sections_with_repositories: cred = Credential( - config.get(section, 'username').strip(), - config.get(section, 'password').strip(), + self.get(section, 'username').strip(), + self.get(section, 'password').strip(), ) - repo = config.get(section, 'repository').strip() - self.dict_[repo] = cred + repo = self.get(section, 'repository').strip() + dict_[repo] = cred + return dict_ def __call__(self, url): """ """ -- cgit v1.2.1 From b7ee88761b75f2c63eb5553f7b28e5e8a88be186 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:20:22 -0500 Subject: Replaced __call__ with find_credential (for clarity of purpose). Removed dict_. --- setuptools/package_index.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 5b8dc357..c63ae29a 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -949,8 +949,8 @@ class PyPirc(ConfigParser.ConfigParser): self.read(rc) @property - def dict_(self): - dict_ = {} + def creds_by_repository(self): + creds = {} sections_with_repositories = [ section for section in self.sections() if self.get(section, 'repository').strip() @@ -962,14 +962,17 @@ class PyPirc(ConfigParser.ConfigParser): self.get(section, 'password').strip(), ) repo = self.get(section, 'repository').strip() - dict_[repo] = cred - return dict_ + creds[repo] = cred + return creds - def __call__(self, url): - """ """ - for base_url, auth in self.dict_.items(): - if url.startswith(base_url): - return auth + def find_credential(self, url): + """ + If the URL indicated appears to be a repository defined in this + config, return the credential for that repository. + """ + for repository, cred in self.creds_by_repository.items(): + if url.startswith(repository): + return cred def open_with_auth(url, opener=urllib2.urlopen): @@ -988,8 +991,10 @@ def open_with_auth(url, opener=urllib2.urlopen): auth = None if not auth: - auth = str(PyPirc()(url)) - log.info('Authentication found for URL: %s' % url) + cred = PyPirc().find_credential(url) + if cred: + auth = str(cred) + log.info('Authentication found for URL: %s' % url) if auth: auth = "Basic " + _encode_auth(auth) -- cgit v1.2.1 From 113f55577768c9434b33b799521e4f70fdecd8de Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:22:59 -0500 Subject: Get ConfigParser module from compat module for Python 3 compatibility. --- setuptools/package_index.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index c63ae29a..482ba38b 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -6,8 +6,6 @@ import shutil import socket import base64 -import ConfigParser - from pkg_resources import ( CHECKOUT_DIST, Distribution, BINARY_DIST, normalize_path, SOURCE_DIST, require, Environment, find_distributions, safe_name, safe_version, @@ -19,7 +17,8 @@ from distutils.errors import DistutilsError from setuptools.compat import (urllib2, httplib, StringIO, HTTPError, urlparse, urlunparse, unquote, splituser, url2pathname, name2codepoint, - unichr, urljoin, urlsplit, urlunsplit) + unichr, urljoin, urlsplit, urlunsplit, + ConfigParser) from setuptools.compat import filterfalse from fnmatch import translate from setuptools.py24compat import hashlib -- cgit v1.2.1 From 117a8a57a9a06521f028eb4e1afc1f6fd4dd8dc8 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:28:27 -0500 Subject: Construct the 'creds' dictionary directly rather than building imperatively. --- setuptools/package_index.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 482ba38b..0a409604 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -949,20 +949,19 @@ class PyPirc(ConfigParser.ConfigParser): @property def creds_by_repository(self): - creds = {} sections_with_repositories = [ section for section in self.sections() if self.get(section, 'repository').strip() ] - for section in sections_with_repositories: - cred = Credential( - self.get(section, 'username').strip(), - self.get(section, 'password').strip(), - ) - repo = self.get(section, 'repository').strip() - creds[repo] = cred - return creds + return dict(map(self._get_repo_cred, sections_with_repositories)) + + def _get_repo_cred(self, section): + repo = self.get(section, 'repository').strip() + return repo, Credential( + self.get(section, 'username').strip(), + self.get(section, 'password').strip(), + ) def find_credential(self, url): """ -- cgit v1.2.1 From 75345928c3e2cb0f88cdf44d33752e1ed6b19290 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:29:59 -0500 Subject: Renamed class for proper capitalization and for clarity. --- setuptools/package_index.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index 0a409604..a61d3c74 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -934,14 +934,14 @@ class Credential(object): def __str__(self): return '%(username)s:%(password)s' % vars(self) -class PyPirc(ConfigParser.ConfigParser): +class PyPIConfig(ConfigParser.ConfigParser): def __init__(self): """ Load from ~/.pypirc """ defaults = dict.fromkeys(['username', 'password', 'repository'], '') - super(PyPirc, self).__init__(defaults) + super(PyPIConfig, self).__init__(defaults) rc = os.path.join(os.path.expanduser('~'), '.pypirc') if os.path.exists(rc): @@ -989,7 +989,7 @@ def open_with_auth(url, opener=urllib2.urlopen): auth = None if not auth: - cred = PyPirc().find_credential(url) + cred = PyPIConfig().find_credential(url) if cred: auth = str(cred) log.info('Authentication found for URL: %s' % url) -- cgit v1.2.1 From cee7279fb782fd97b8e7ce06642054ee45ca0564 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Thu, 14 Nov 2013 10:34:14 -0500 Subject: Updated message when credentials used from .pypirc --- setuptools/package_index.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'setuptools/package_index.py') diff --git a/setuptools/package_index.py b/setuptools/package_index.py index a61d3c74..231311ec 100755 --- a/setuptools/package_index.py +++ b/setuptools/package_index.py @@ -992,7 +992,8 @@ def open_with_auth(url, opener=urllib2.urlopen): cred = PyPIConfig().find_credential(url) if cred: auth = str(cred) - log.info('Authentication found for URL: %s' % url) + info = cred.username, url + log.info('Authenticating as %s for %s (from .pypirc)' % info) if auth: auth = "Basic " + _encode_auth(auth) -- cgit v1.2.1