aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArfrever Frehtes Taifersar Arahesis <Arfrever@Apache.Org>2012-05-01 21:51:16 +0200
committerArfrever Frehtes Taifersar Arahesis <Arfrever@Apache.Org>2012-05-01 21:51:16 +0200
commit0a9cc38a66ded0cf0e5b534cb24b970fc9c21920 (patch)
tree85ac09a67f008adf2ab8f85ab6f20e051c642ca1 /pym/portage/util
parentQuote atoms in die message for bug #414201. (diff)
downloadportage-0a9cc38a66ded0cf0e5b534cb24b970fc9c21920.tar.gz
portage-0a9cc38a66ded0cf0e5b534cb24b970fc9c21920.tar.bz2
portage-0a9cc38a66ded0cf0e5b534cb24b970fc9c21920.zip
Bug #413983: Add portage.util.urlopen(), which transparently
handles authentication in the way compatible with Python 3.
Diffstat (limited to 'pym/portage/util')
-rw-r--r--pym/portage/util/__init__.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py
index d6ac46c83..2b50733c5 100644
--- a/pym/portage/util/__init__.py
+++ b/pym/portage/util/__init__.py
@@ -1,4 +1,4 @@
-# Copyright 2004-2011 Gentoo Foundation
+# Copyright 2004-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
__all__ = ['apply_permissions', 'apply_recursive_permissions',
@@ -26,6 +26,14 @@ import string
import sys
import traceback
import glob
+try:
+ import urllib.parse as urllib_parse
+ import urllib.request as urllib_request
+ from urllib.parse import splituser as urllib_parse_splituser
+except ImportError:
+ import urlparse as urllib_parse
+ import urllib2 as urllib_request
+ from urllib import splituser as urllib_parse_splituser
import portage
portage.proxy.lazyimport.lazyimport(globals(),
@@ -1640,3 +1648,14 @@ def getlibpaths(root, env=None):
rval.append("/lib")
return [normalize_path(x) for x in rval if x]
+
+def urlopen(url):
+ parse_result = urllib_parse.urlparse(url)
+ netloc = urllib_parse_splituser(parse_result.netloc)[1]
+ url = urllib_parse.urlunparse((parse_result.scheme, netloc, parse_result.path, parse_result.params, parse_result.query, parse_result.fragment))
+ password_manager = urllib_request.HTTPPasswordMgrWithDefaultRealm()
+ if parse_result.username is not None:
+ password_manager.add_password(None, url, parse_result.username, parse_result.password)
+ auth_handler = urllib_request.HTTPBasicAuthHandler(password_manager)
+ opener = urllib_request.build_opener(auth_handler)
+ return opener.open(url)