-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure distutils configuration values do not escape virtual environme…
…nt (#1657) * Ensure distutils configuration values do not escape virtual environment Distutils has some configuration files where the user may alter paths to point outside of the virtual environment. Defend against this by installing a pth file that resets this to their expected path. Signed-off-by: Bernat Gabor <[email protected]> * fix CI failure due to #pypa/pip/issues/7778 Signed-off-by: Bernat Gabor <[email protected]>
- Loading branch information
1 parent
ef711b7
commit 9201422
Showing
11 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
49 changes: 49 additions & 0 deletions
49
src/virtualenv/create/via_global_ref/_distutils_patch_virtualenv.py
This file contains 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,49 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Distutils allows user to configure some arguments via a configuration file: | ||
https://docs.python.org/3/install/index.html#distutils-configuration-files | ||
Some of this arguments though don't make sense in context of the virtual environment files, let's fix them up. | ||
""" | ||
import os | ||
import sys | ||
|
||
VIRTUALENV_PATCH_FILE = os.path.join(__file__) | ||
|
||
|
||
def patch(dist_of): | ||
# we cannot allow the prefix override as that would get packages installed outside of the virtual environment | ||
old_parse_config_files = dist_of.Distribution.parse_config_files | ||
|
||
def parse_config_files(self, *args, **kwargs): | ||
result = old_parse_config_files(self, *args, **kwargs) | ||
install_dict = self.get_option_dict("install") | ||
|
||
if "prefix" in install_dict: # the prefix governs where to install the libraries | ||
install_dict["prefix"] = VIRTUALENV_PATCH_FILE, os.path.abspath(sys.prefix) | ||
|
||
if "install_scripts" in install_dict: # the install_scripts governs where to generate console scripts | ||
script_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "__SCRIPT_DIR__")) | ||
install_dict["install_scripts"] = VIRTUALENV_PATCH_FILE, script_path | ||
|
||
return result | ||
|
||
dist_of.Distribution.parse_config_files = parse_config_files | ||
|
||
|
||
def run(): | ||
# patch distutils | ||
from distutils import dist | ||
|
||
patch(dist) | ||
|
||
# patch setuptools (that has it's own copy of the dist package) | ||
try: | ||
from setuptools import dist | ||
except ImportError: | ||
pass # if setuptools is not around that's alright, just don't patch | ||
else: | ||
patch(dist) | ||
|
||
|
||
run() |
This file contains 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 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 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 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,6 @@ | ||
def run(): | ||
print("magic") | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains 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,6 @@ | ||
def run(): | ||
print("magic") | ||
|
||
|
||
if __name__ == "__main__": | ||
run() |
This file contains 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,15 @@ | ||
[metadata] | ||
name = demo | ||
version = 1.0.0 | ||
description = magic package | ||
|
||
[options] | ||
packages = find: | ||
install_requires = | ||
|
||
[options.entry_points] | ||
console_scripts = | ||
magic=demo.__main__:run | ||
|
||
[bdist_wheel] | ||
universal = true |
This file contains 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,3 @@ | ||
from setuptools import setup | ||
|
||
setup() |
This file contains 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 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