Repository navigation
python: restore pip user installs #506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+139
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| import zipfile | ||
|
|
||
|
|
||
| PIP, PIP_LIB, CONFIG, SITECUSTOMIZE = sys.argv[1:] | ||
| sys.argv = sys.argv[:1] | ||
|
|
||
|
|
||
| class UserSiteTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.temp = tempfile.TemporaryDirectory() | ||
| self.addCleanup(self.temp.cleanup) | ||
| self.home = Path(self.temp.name) | ||
| self.userbase = self.home / ".pythonlibs" | ||
| self.env = { | ||
| key: value | ||
| for key, value in os.environ.items() | ||
| if not key.startswith(("PIP_", "PYTHON", "REPLIT_PYTHON")) | ||
| } | ||
| self.env.update( | ||
| HOME=str(self.home), | ||
| REPL_HOME=str(self.home), | ||
| PYTHONUSERBASE=str(self.userbase), | ||
| PYTHONPATH=os.pathsep.join([SITECUSTOMIZE, PIP_LIB]), | ||
| PIP_CONFIG_FILE=CONFIG, | ||
| ) | ||
| self.empty = self.home / "empty.txt" | ||
| self.empty.touch() | ||
| self.wheel = self.home / "user_site_probe-0.0.1-py3-none-any.whl" | ||
| info = "user_site_probe-0.0.1.dist-info" | ||
| with zipfile.ZipFile(self.wheel, "w") as wheel: | ||
| wheel.writestr("user_site_probe.py", 'value = "installed"\n') | ||
| wheel.writestr( | ||
| f"{info}/METADATA", | ||
| "Metadata-Version: 2.1\nName: user-site-probe\nVersion: 0.0.1\n", | ||
| ) | ||
| wheel.writestr( | ||
| f"{info}/WHEEL", | ||
| "Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n", | ||
| ) | ||
| wheel.writestr(f"{info}/RECORD", "") | ||
| self.requirements = self.home / "requirements.txt" | ||
| self.requirements.write_text(f"{self.wheel}\n") | ||
|
|
||
| def run_command(self, args, *, env=None, success=True): | ||
| result = subprocess.run( | ||
| args, | ||
| env=self.env if env is None else env, | ||
| text=True, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.STDOUT, | ||
| ) | ||
| if success: | ||
| self.assertEqual(result.returncode, 0, result.stdout) | ||
| else: | ||
| self.assertNotEqual(result.returncode, 0, result.stdout) | ||
| return result.stdout | ||
|
|
||
| def assert_import(self, python, base, env=None): | ||
| self.run_command( | ||
| [ | ||
| python, | ||
| "-c", | ||
| "import pathlib, user_site_probe; " | ||
| "assert user_site_probe.value == 'installed'; " | ||
| f"assert pathlib.Path(user_site_probe.__file__).is_relative_to({str(base)!r})", | ||
| ], | ||
| env=env, | ||
| ) | ||
|
|
||
| def test_requirements_install_into_userbase(self): | ||
| for requirements in (self.empty, self.requirements): | ||
| self.run_command([PIP, "install", "--no-index", "-r", str(requirements)]) | ||
| self.assert_import(sys.executable, self.userbase) | ||
|
|
||
| def test_explicitly_disabled_user_site(self): | ||
| env = dict(self.env, PYTHONNOUSERSITE="1") | ||
| output = self.run_command( | ||
| [PIP, "install", "--no-index", "-r", str(self.empty)], | ||
| env=env, | ||
| success=False, | ||
| ) | ||
| self.assertIn("User site-packages are disabled for this Python", output) | ||
| output = self.run_command( | ||
| [sys.executable, "-s", "-m", "pip", "install", "--no-index", "-r", str(self.empty)], | ||
| success=False, | ||
| ) | ||
| self.assertIn("User site-packages are disabled for this Python", output) | ||
|
|
||
| def test_virtualenv(self): | ||
| self.run_command( | ||
| [sys.executable, "-m", "venv", "--without-pip", str(self.userbase)] | ||
| ) | ||
| python = str(self.userbase / "bin" / "python") | ||
| self.run_command( | ||
| [python, "-m", "pip", "install", "--no-index", "-r", str(self.requirements)] | ||
| ) | ||
| self.assert_import(python, self.userbase) | ||
| output = self.run_command( | ||
| [python, "-m", "pip", "install", "--user", "--no-index", "-r", str(self.empty)], | ||
| success=False, | ||
| ) | ||
| self.assertIn("User site-packages are not visible in this virtualenv", output) | ||
|
|
||
|
|
||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { pkgs }: | ||
| let | ||
| sitecustomize = pkgs.callPackage ../modules/python/sitecustomize.nix { }; | ||
| mkTest = kind: python: | ||
| let | ||
| legacy = import ../modules/python/pip.nix (pkgs // { pypkgs = python.pkgs; }); | ||
| pip = if kind == "legacy" then legacy.pip else python.pkgs.callPackage ./. { }; | ||
| name = "pip-user-site-${kind}-${python.pythonVersion}"; | ||
| in | ||
| pkgs.lib.nameValuePair name (pkgs.runCommand name { } '' | ||
| export HOME="$TMPDIR/home" | ||
| mkdir -p "$HOME" | ||
| ${python.interpreter} ${./test_user_site.py} \ | ||
| ${pip}/bin/pip ${pip}/${python.sitePackages} \ | ||
| ${legacy.config} ${sitecustomize} | ||
| touch "$out" | ||
| ''); | ||
| in | ||
| pkgs.lib.listToAttrs ( | ||
| map (mkTest "legacy") [ pkgs.python310 pkgs.python311 pkgs.python312 ] | ||
| ++ map (mkTest "base") [ pkgs.python311 pkgs.python312 pkgs.python313 ] | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.