1515from xml .sax .saxutils import escape
1616from distutils import version
1717from email .parser import HeaderParser
18+ import urllib .request
19+ import xmlrpc .client
1820
1921import pkg_resources
2022
@@ -629,14 +631,26 @@ def __on_installer_finished(self):
629631 self .accept ()
630632
631633
634+ class SafeUrllibTransport (xmlrpc .client .Transport ):
635+ """Urllib for HTTPS connections that automatically handles proxies."""
636+
637+ def single_request (self , host , handler , request_body , verbose = False ):
638+ req = urllib .request .Request ('https://%s%s' % (host , handler ), request_body )
639+ req .add_header ('User-agent' , self .user_agent )
640+ req .add_header ('Content-Type' , 'text/xml' )
641+ self .verbose = verbose
642+ opener = urllib .request .build_opener ()
643+ return self .parse_response (opener .open (req ))
644+
645+
632646def list_pypi_addons ():
633647 """
634648 List add-ons available on pypi.
635649 """
636650 from ..config import ADDON_PYPI_SEARCH_SPEC
637- import xmlrpc . client
651+
638652 pypi = xmlrpc .client .ServerProxy (
639- "https://pypi.python.org/pypi" ,
653+ "https://pypi.python.org/pypi/ " ,
640654 transport = xmlrpc .client .SafeTransport ()
641655 )
642656 addons = pypi .search (ADDON_PYPI_SEARCH_SPEC )
@@ -701,6 +715,20 @@ def observed(el):
701715 return (el for el in iterable if not observed (el ))
702716
703717
718+ def _env_with_proxies ():
719+ """
720+ Return system environment with proxies obtained from urllib so that
721+ they can be used with pip.
722+ """
723+ proxies = urllib .request .getproxies ()
724+ env = dict (os .environ )
725+ if "http" in proxies :
726+ env ["HTTP_PROXY" ] = proxies ["http" ]
727+ if "https" in proxies :
728+ env ["HTTPS_PROXY" ] = proxies ["https" ]
729+ return env
730+
731+
704732Install , Upgrade , Uninstall = 1 , 2 , 3
705733
706734
@@ -740,7 +768,7 @@ def fmt_cmd(cmd):
740768 cmd = (["-m" , "pip" , "install" ] +
741769 (["--user" ] if self .__user_install else []) +
742770 [inst_name ])
743- process = python_process (cmd , bufsize = - 1 , universal_newlines = True )
771+ process = python_process (cmd , bufsize = - 1 , universal_newlines = True , env = _env_with_proxies () )
744772 retcode , output = self .__subprocessrun (process )
745773
746774 if retcode != 0 :
@@ -755,7 +783,7 @@ def fmt_cmd(cmd):
755783 cmd = (["-m" , "pip" , "install" , "--upgrade" , "--no-deps" ] +
756784 (["--user" ] if self .__user_install else []) +
757785 [inst_name ])
758- process = python_process (cmd , bufsize = - 1 , universal_newlines = True )
786+ process = python_process (cmd , bufsize = - 1 , universal_newlines = True , env = _env_with_proxies () )
759787 retcode , output = self .__subprocessrun (process )
760788
761789 if retcode != 0 :
@@ -766,7 +794,7 @@ def fmt_cmd(cmd):
766794 cmd = (["-m" , "pip" , "install" ] +
767795 (["--user" ] if self .__user_install else []) +
768796 [inst_name ])
769- process = python_process (cmd , bufsize = - 1 , universal_newlines = True )
797+ process = python_process (cmd , bufsize = - 1 , universal_newlines = True , env = _env_with_proxies () )
770798 retcode , output = self .__subprocessrun (process )
771799
772800 if retcode != 0 :
@@ -778,7 +806,7 @@ def fmt_cmd(cmd):
778806 self .setStatusMessage ("Uninstalling {}" .format (dist .project_name ))
779807
780808 cmd = ["-m" , "pip" , "uninstall" , "--yes" , dist .project_name ]
781- process = python_process (cmd , bufsize = - 1 , universal_newlines = True )
809+ process = python_process (cmd , bufsize = - 1 , universal_newlines = True , env = _env_with_proxies () )
782810 retcode , output = self .__subprocessrun (process )
783811
784812 if self .__user_install :
0 commit comments