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