648d58
From b7b8a713d9f1ebac6430fd0fc10175ed37b834ee Mon Sep 17 00:00:00 2001
648d58
From: Lumir Balhar <lbalhar@redhat.com>
648d58
Date: Thu, 18 Mar 2021 13:08:52 +0100
648d58
Subject: [PATCH] rpm
648d58
648d58
---
648d58
 virtualenv.py | 66 ++++++++++++++++++++++++++++++++++++++++++---------
648d58
 1 file changed, 55 insertions(+), 11 deletions(-)
648d58
648d58
diff --git a/virtualenv.py b/virtualenv.py
648d58
index 5699998..9854324 100755
648d58
--- a/virtualenv.py
648d58
+++ b/virtualenv.py
648d58
@@ -39,9 +39,9 @@ except ImportError:
648d58
 __version__ = "15.1.0"
648d58
 virtualenv_version = __version__  # legacy
648d58
 
648d58
-if sys.version_info < (2, 6):
648d58
+if sys.version_info < (2, 7):
648d58
     print('ERROR: %s' % sys.exc_info()[1])
648d58
-    print('ERROR: this script requires Python 2.6 or greater.')
648d58
+    print('ERROR: this script requires Python 2.7 or greater.')
648d58
     sys.exit(101)
648d58
 
648d58
 try:
648d58
@@ -399,6 +399,8 @@ def _find_file(filename, dirs):
648d58
 def file_search_dirs():
648d58
     here = os.path.dirname(os.path.abspath(__file__))
648d58
     dirs = [here, join(here, 'virtualenv_support')]
648d58
+    dirs.insert(1, '/usr/share/python{}-wheels'.format(sys.version_info[0]))
648d58
+    dirs.insert(1, '/usr/share/python{}{}-wheels'.format(*sys.version_info[:2]))
648d58
     if os.path.splitext(os.path.dirname(__file__))[0] != 'virtualenv':
648d58
         # Probably some boot script; just in case virtualenv is installed...
648d58
         try:
648d58
@@ -859,7 +861,12 @@ def install_wheel(project_names, py_executable, search_dirs=None,
648d58
         import tempfile
648d58
         import os
648d58
 
648d58
-        import pip
648d58
+        try:
648d58
+            from pip._internal import main as _main
648d58
+            if type(_main) is type(sys):  # <type 'module'>
648d58
+                _main = _main.main  # nested starting in Pip 19.3
648d58
+        except ImportError:
648d58
+            from pip import main as _main
648d58
 
648d58
         try:
648d58
             cert_data = pkgutil.get_data("pip._vendor.requests", "cacert.pem")
648d58
@@ -878,7 +885,7 @@ def install_wheel(project_names, py_executable, search_dirs=None,
648d58
                 args += ["--cert", cert_file.name]
648d58
             args += sys.argv[1:]
648d58
 
648d58
-            sys.exit(pip.main(args))
648d58
+            sys.exit(_main(args))
648d58
         finally:
648d58
             if cert_file is not None:
648d58
                 os.remove(cert_file.name)
648d58
@@ -1038,20 +1045,57 @@ def change_prefix(filename, dst_prefix):
648d58
     assert False, "Filename %s does not start with any of these prefixes: %s" % \
648d58
         (filename, prefixes)
648d58
 
648d58
-def copy_required_modules(dst_prefix, symlink):
648d58
-    import imp
648d58
+def find_module_filename(modname):
648d58
+    if sys.version_info < (3, 4):
648d58
+        # noinspection PyDeprecation
648d58
+        import imp
648d58
+
648d58
+        try:
648d58
+            file_handler, filepath, _ = imp.find_module(modname)
648d58
+        except ImportError:
648d58
+            return None
648d58
+        else:
648d58
+            if file_handler is not None:
648d58
+                file_handler.close()
648d58
+            return filepath
648d58
+    else:
648d58
+        import importlib.util
648d58
 
648d58
+        if sys.version_info < (3, 5):
648d58
+
648d58
+            def find_spec(modname):
648d58
+                # noinspection PyDeprecation
648d58
+                loader = importlib.find_loader(modname)
648d58
+                if loader is None:
648d58
+                    return None
648d58
+                else:
648d58
+                    return importlib.util.spec_from_loader(modname, loader)
648d58
+
648d58
+        else:
648d58
+            find_spec = importlib.util.find_spec
648d58
+
648d58
+        spec = find_spec(modname)
648d58
+        if spec is None:
648d58
+            return None
648d58
+        if not os.path.exists(spec.origin):
648d58
+            # https://bitbucket.org/pypy/pypy/issues/2944/origin-for-several-builtin-modules
648d58
+            # on pypy3, some builtin modules have a bogus build-time file path, ignore them
648d58
+            return None
648d58
+        filepath = spec.origin
648d58
+        # https://www.python.org/dev/peps/pep-3147/#file guarantee to be non-cached
648d58
+        if os.path.basename(filepath) == "__init__.py":
648d58
+            filepath = os.path.dirname(filepath)
648d58
+        return filepath
648d58
+
648d58
+def copy_required_modules(dst_prefix, symlink):
648d58
     for modname in REQUIRED_MODULES:
648d58
         if modname in sys.builtin_module_names:
648d58
             logger.info("Ignoring built-in bootstrap module: %s" % modname)
648d58
             continue
648d58
-        try:
648d58
-            f, filename, _ = imp.find_module(modname)
648d58
-        except ImportError:
648d58
+        filename = find_module_filename(modname)
648d58
+        if filename is None:
648d58
             logger.info("Cannot import bootstrap module: %s" % modname)
648d58
         else:
648d58
-            if f is not None:
648d58
-                f.close()
648d58
             # special-case custom readline.so on OS X, but not for pypy:
648d58
             if modname == 'readline' and sys.platform == 'darwin' and not (
648d58
                     is_pypy or filename.endswith(join('lib-dynload', 'readline.so'))):
648d58
-- 
648d58
2.30.2
648d58