#7 Don't fail on missing certifi's cert bundle
Merged 5 years ago by cstratak. Opened 5 years ago by churchyard.
rpms/ churchyard/python-virtualenv nocertifi  into  master

@@ -0,0 +1,79 @@ 

+ diff --git a/tests/test_virtualenv.py b/tests/test_virtualenv.py

+ index ce45ede..3cd200b 100644

+ --- a/tests/test_virtualenv.py

+ +++ b/tests/test_virtualenv.py

+ @@ -4,11 +4,16 @@ import os

+  import shutil

+  import sys

+  import tempfile

+ +import zipfile

+  import pytest

+  import platform  # noqa

+  

+  from mock import patch, Mock

+  

+ +try:

+ +    from pathlib import Path

+ +except ImportError:

+ +    from pathlib2 import Path

+  

+  def test_version():

+      """Should have a version string"""

+ @@ -139,3 +144,44 @@ def test_always_copy_option():

+                      " symlink (to %s)" % (full_name, os.readlink(full_name))

+      finally:

+          shutil.rmtree(tmp_virtualenv)

+ +

+ +

+ +def test_missing_certifi_pem(tmp_path):

+ +    """Make sure that we can still create virtual environment if pip is

+ +    patched to not use certifi's cacert.pem and the file is removed.

+ +    This can happen if pip is packaged by Linux distributions."""

+ +    proj_dir = Path(__file__).parent.parent

+ +    support_original = proj_dir / "virtualenv_support"

+ +    pip_wheel = sorted(support_original.glob("pip*whl"))[0]

+ +    whl_name = pip_wheel.name

+ +

+ +    wheeldir = tmp_path / "wheels"

+ +    wheeldir.mkdir()

+ +    tmpcert = tmp_path / "tmpcert.pem"

+ +    cacert = "pip/_vendor/certifi/cacert.pem"

+ +    certifi = "pip/_vendor/certifi/core.py"

+ +    oldpath = b"os.path.join(f, 'cacert.pem')"

+ +    newpath = "r'{}'".format(tmpcert).encode()

+ +    removed = False

+ +    replaced = False

+ +

+ +    with zipfile.ZipFile(str(pip_wheel), "r") as whlin:

+ +        with zipfile.ZipFile(str(wheeldir / whl_name), "w") as whlout:

+ +            for item in whlin.infolist():

+ +                buff = whlin.read(item.filename)

+ +                if item.filename == cacert:

+ +                    tmpcert.write_bytes(buff)

+ +                    removed = True

+ +                    continue

+ +                if item.filename == certifi:

+ +                    nbuff = buff.replace(oldpath, newpath)

+ +                    assert nbuff != buff

+ +                    buff = nbuff

+ +                    replaced = True

+ +                whlout.writestr(item, buff)

+ +

+ +    assert removed and replaced

+ +

+ +    venvdir = tmp_path / "venv"

+ +    search_dirs = [str(wheeldir), str(support_original)]

+ +    virtualenv.create_environment(str(venvdir), search_dirs=search_dirs)

+ diff --git a/virtualenv.py b/virtualenv.py

+ index c1fe7f1..3837250 100755

+ --- a/virtualenv.py

+ +++ b/virtualenv.py

+ @@ -867,6 +867,8 @@ def install_wheel(project_names, py_executable, search_dirs=None,

+          except ImportError:

+              from pip import main as _main

+              cert_data = pkgutil.get_data("pip._vendor.requests", "cacert.pem")

+ +        except IOError:

+ +            cert_data = None

+  

+          if cert_data is not None:

+              cert_file = tempfile.NamedTemporaryFile(delete=False)

file modified
+8 -1
@@ -1,6 +1,6 @@ 

  Name:           python-virtualenv

  Version:        16.0.0

- Release:        5%{?dist}

+ Release:        6%{?dist}

  Summary:        Tool to create isolated Python environments

  

  Group:          Development/Languages
@@ -23,6 +23,10 @@ 

  # Add /usr/share/python-wheels to file_search_dirs

  Patch2: rpm-wheels.patch

  

+ # Don't fail on missing certifi's cert

+ # https://github.com/pypa/virtualenv/pull/1252

+ Patch3: dont-fail-on-missing-certifi-cert.patch

+ 

  BuildArch:      noarch

  BuildRequires:  git-core

  BuildRequires:  python2-devel
@@ -195,6 +199,9 @@ 

  

  

  %changelog

+ * Thu Dec 13 2018 Miro Hrončok <mhroncok@redhat.com> - 16.0.0-6

+ - Don't fail on missing certifi's cert bundle (#1655253)

+ 

  * Wed Aug 15 2018 Miro Hrončok <mhroncok@redhat.com> - 16.0.0-5

  - Use wheels from RPM packages

  - Put wheels needed for Python 2.6 into a subpackage

$ mock -r fedora-rawhide-x86_64 install virtualenv
...
Installed:
  python3-virtualenv-16.0.0-5.fc30.noarch
...

$ mock -r fedora-rawhide-x86_64 shell 
<mock-chroot> sh-4.4# virtualenv venv
Using base prefix '/usr'
New python executable in /builddir/venv/bin/python3
Also creating executable in /builddir/venv/bin/python
Installing setuptools, pip, wheel...
  Complete output from command /builddir/venv/bin/python3 - setuptools pip wheel:
  Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "/usr/lib64/python3.7/pkgutil.py", line 637, in get_data
    return loader.get_data(resource_name)
OSError: [Errno 0] Error: 'pip/_vendor/certifi/cacert.pem'
----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/bin/virtualenv", line 11, in <module>
    load_entry_point('virtualenv==16.0.0', 'console_scripts', 'virtualenv')()
  File "/usr/lib/python3.7/site-packages/virtualenv.py", line 715, in main
    symlink=options.symlink)
  File "/usr/lib/python3.7/site-packages/virtualenv.py", line 959, in create_environment
    download=download,
  File "/usr/lib/python3.7/site-packages/virtualenv.py", line 907, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python3.7/site-packages/virtualenv.py", line 799, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /builddir/venv/bin/python3 - setuptools pip wheel failed with error code 1
<mock-chroot> sh-4.4# logout
Finish: shell

$ mock -r fedora-rawhide-x86_64 install https://kojipkgs.fedoraproject.org//work/tasks/8109/31438109/python3-virtualenv-16.0.0-6.fc30.noarch.rpm
...
Upgraded:
  python3-virtualenv-16.0.0-6.fc30.noarch
...

$ mock -r fedora-rawhide-x86_64 shell 
<mock-chroot> sh-4.4# rm venv/ -rf
<mock-chroot> sh-4.4# virtualenv venv
Using base prefix '/usr'
New python executable in /builddir/venv/bin/python3
Also creating executable in /builddir/venv/bin/python
Installing setuptools, pip, wheel...done.
<mock-chroot> sh-4.4# logout
Finish: shell

Ready for review.

Tested it and works as expected.

Pull-Request has been merged by cstratak

5 years ago

BTW have you tested this on Python 2? Because I haven't :D