[elbe-devel] [PATCH 14/20] elbexml: use urllib2.ProxyHandler() instead of environment variables

Torben Hohn torben.hohn at linutronix.de
Fri Oct 12 11:28:00 CEST 2018


to prevent race conditions with process wide environment variables,
use urllib2.ProxyHandler() to setup Proxies in url validation.

Signed-off-by: Torben Hohn <torben.hohn at linutronix.de>
---
 elbepack/elbexml.py | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/elbepack/elbexml.py b/elbepack/elbexml.py
index 8cda7987..42cd8bc7 100644
--- a/elbepack/elbexml.py
+++ b/elbepack/elbexml.py
@@ -163,12 +163,12 @@ class ElbeXML(object):
 
         return mirror.replace("LOCALMACHINE", "10.0.2.2")
 
-    def validate_repo(self, r, url_validation):
+    def validate_repo(self, opener, r, url_validation):
         try:
-            fp = urllib2.urlopen(r["url"] + "InRelease", None, 10)
+            fp = opener.open(r["url"] + "InRelease", None, 10)
         except urllib2.URLError:
             try:
-                fp = urllib2.urlopen(r["url"] + "Release", None, 10)
+                fp = opener.open(r["url"] + "Release", None, 10)
             except urllib2.URLError:
                 return False
 
@@ -237,21 +237,21 @@ class ElbeXML(object):
             return
 
         if self.prj.has("mirror/primary_proxy"):
-            os.environ["no_proxy"] = "10.0.2.2,localhost,127.0.0.1"
             proxy = self.prj.text(
                 "mirror/primary_proxy").strip().replace("LOCALMACHINE",
                                                         "10.0.2.2")
-            os.environ["http_proxy"] = proxy
-            os.environ["https_proxy"] = proxy
+            proxies = {"no": "10.0.2.2,localhost,127.0.0.1",
+                       "http": proxy,
+                       "https": proxy}
         else:
-            os.environ["http_proxy"] = ""
-            os.environ["https_proxy"] = ""
-            os.environ["no_proxy"] = ""
+            proxies = {}
+
+        proxy_handler = urllib2.ProxyHandler(proxies)
 
         passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
         authhandler = urllib2.HTTPBasicAuthHandler(passman)
-        opener = urllib2.build_opener(authhandler)
-        urllib2.install_opener(opener)
+
+        opener = urllib2.build_opener(proxy_handler, authhandler)
 
         for r in repos:
             if '@' in r["url"]:
@@ -265,7 +265,7 @@ class ElbeXML(object):
                 r["url"] = scheme + t[1]
                 usr, passwd = auth.split(':')
                 passman.add_password(None, r["url"], usr, passwd)
-            if not self.validate_repo(r, url_validation):
+            if not self.validate_repo(opener, r, url_validation):
                 raise ValidationError(
                     ["Repository %s can not be validated" % r["url"]])
 
-- 
2.11.0




More information about the elbe-devel mailing list