|
|
99a46d |
From fc8e412fa8fe524ab3f112f02e865aa42608388b Mon Sep 17 00:00:00 2001
|
|
|
99a46d |
From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= <Kulikjak@gmail.com>
|
|
|
99a46d |
Date: Thu, 27 Apr 2023 18:52:38 +0200
|
|
|
99a46d |
Subject: [PATCH] prevent PermissionError when using venv creator on some
|
|
|
99a46d |
systems (#2543)
|
|
|
99a46d |
|
|
|
99a46d |
Cherry-picked from 0597a2f8ab32705b86a25dbd1d42fd30c3033061
|
|
|
99a46d |
---
|
|
|
99a46d |
docs/changelog/2543.bugfix.rst | 2 ++
|
|
|
99a46d |
src/virtualenv/activation/via_template.py | 4 ++++
|
|
|
99a46d |
tests/unit/create/test_creator.py | 22 ++++++++++++++++++++++
|
|
|
99a46d |
3 files changed, 28 insertions(+)
|
|
|
99a46d |
create mode 100644 docs/changelog/2543.bugfix.rst
|
|
|
99a46d |
|
|
|
99a46d |
diff --git a/docs/changelog/2543.bugfix.rst b/docs/changelog/2543.bugfix.rst
|
|
|
99a46d |
new file mode 100644
|
|
|
99a46d |
index 0000000..5f0d6ca
|
|
|
99a46d |
--- /dev/null
|
|
|
99a46d |
+++ b/docs/changelog/2543.bugfix.rst
|
|
|
99a46d |
@@ -0,0 +1,2 @@
|
|
|
99a46d |
+Prevent ``PermissionError`` when using venv creator on systems that deliver files without user write
|
|
|
99a46d |
+permission - by :user:`kulikjak`.
|
|
|
99a46d |
diff --git a/src/virtualenv/activation/via_template.py b/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
index 069d52e..cc9dbda 100644
|
|
|
99a46d |
--- a/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
+++ b/src/virtualenv/activation/via_template.py
|
|
|
99a46d |
@@ -41,6 +41,10 @@ class ViaTemplateActivator(Activator, metaclass=ABCMeta):
|
|
|
99a46d |
for template in templates:
|
|
|
99a46d |
text = self.instantiate_template(replacements, template, creator)
|
|
|
99a46d |
dest = to_folder / self.as_name(template)
|
|
|
99a46d |
+ # remove the file if it already exists - this prevents permission
|
|
|
99a46d |
+ # errors when the dest is not writable
|
|
|
99a46d |
+ if dest.exists():
|
|
|
99a46d |
+ dest.unlink()
|
|
|
99a46d |
# use write_bytes to avoid platform specific line normalization (\n -> \r\n)
|
|
|
99a46d |
dest.write_bytes(text.encode("utf-8"))
|
|
|
99a46d |
generated.append(dest)
|
|
|
99a46d |
diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py
|
|
|
99a46d |
index ea61ed0..0ec6d62 100644
|
|
|
99a46d |
--- a/tests/unit/create/test_creator.py
|
|
|
99a46d |
+++ b/tests/unit/create/test_creator.py
|
|
|
99a46d |
@@ -690,3 +690,25 @@ def test_py_pyc_missing(tmp_path, mocker, py, pyc):
|
|
|
99a46d |
|
|
|
99a46d |
pyc_at = Python2.from_stdlib(Python2.mappings(CURRENT), "osc.py")[1](result.creator, Path("os.pyc"))
|
|
|
99a46d |
assert pyc_at.exists() is pyc
|
|
|
99a46d |
+
|
|
|
99a46d |
+
|
|
|
99a46d |
+# Make sure that the venv creator works on systems where vendor-delivered files
|
|
|
99a46d |
+# (specifically venv scripts delivered with Python itself) are not writable.
|
|
|
99a46d |
+#
|
|
|
99a46d |
+# https://github.com/pypa/virtualenv/issues/2419
|
|
|
99a46d |
+@pytest.mark.skipif("venv" not in CURRENT_CREATORS, reason="test needs venv creator")
|
|
|
99a46d |
+def test_venv_creator_without_write_perms(tmp_path, mocker):
|
|
|
99a46d |
+ from virtualenv.run.session import Session
|
|
|
99a46d |
+
|
|
|
99a46d |
+ prev = Session._create
|
|
|
99a46d |
+
|
|
|
99a46d |
+ def func(self):
|
|
|
99a46d |
+ prev(self)
|
|
|
99a46d |
+ scripts_dir = self.creator.dest / "bin"
|
|
|
99a46d |
+ for script in scripts_dir.glob("*ctivate*"):
|
|
|
99a46d |
+ script.chmod(stat.S_IREAD | stat.S_IRGRP | stat.S_IROTH)
|
|
|
99a46d |
+
|
|
|
99a46d |
+ mocker.patch("virtualenv.run.session.Session._create", side_effect=func, autospec=True)
|
|
|
99a46d |
+
|
|
|
99a46d |
+ cmd = [str(tmp_path), "--seeder", "app-data", "--without-pip", "--creator", "venv"]
|
|
|
99a46d |
+ cli_run(cmd)
|
|
|
99a46d |
--
|
|
|
99a46d |
2.40.0
|
|
|
99a46d |
|