Skip to content

Commit

Permalink
feature(virtualenv): add 'system-site-packages' option
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad0337187 committed Feb 20, 2021
1 parent 6eaa298 commit 3c1f245
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 16 deletions.
6 changes: 6 additions & 0 deletions docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ cache-dir = "/path/to/cache/directory"
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = true
virtualenvs.options.system-site-packages = false
virtualenvs.path = "{cache-dir}/virtualenvs" # /path/to/cache/directory/virtualenvs
```

Expand Down Expand Up @@ -143,6 +144,11 @@ Defaults to `{cache-dir}/virtualenvs` (`{cache-dir}\virtualenvs` on Windows).
If set to `true` the `--always-copy` parameter is passed to `virtualenv` on creation of the venv. Thus all needed files are copied into the venv instead of symlinked.
Defaults to `false`.

### `virtualenvs.options.system-site-packages`: boolean

Give the virtual environment access to the system site-packages directory.
Applies on virtualenv creation.
Defaults to `false`.

### `repositories.<name>`: string

Expand Down
3 changes: 2 additions & 1 deletion poetry/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Config(object):
"create": True,
"in-project": None,
"path": os.path.join("{cache-dir}", "virtualenvs"),
"options": {"always-copy": False},
"options": {"always-copy": False, "system-site-packages": False},
},
"experimental": {"new-installer": True},
"installer": {"parallel": True},
Expand Down Expand Up @@ -140,6 +140,7 @@ def _get_normalizer(self, name: str) -> Callable:
"virtualenvs.create",
"virtualenvs.in-project",
"virtualenvs.options.always-copy",
"virtualenvs.options.system-site-packages",
"installer.parallel",
}:
return boolean_normalizer
Expand Down
15 changes: 10 additions & 5 deletions poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ def unique_config_values(self) -> Dict[str, Tuple[Any, Any, Any]]:
),
"virtualenvs.create": (boolean_validator, boolean_normalizer, True),
"virtualenvs.in-project": (boolean_validator, boolean_normalizer, False),
"virtualenvs.options.always-copy": (
boolean_validator,
boolean_normalizer,
False,
),
"virtualenvs.options.system-site-packages": (
boolean_validator,
boolean_normalizer,
False,
),
"virtualenvs.path": (
str,
lambda val: str(Path(val)),
Expand All @@ -76,11 +86,6 @@ def unique_config_values(self) -> Dict[str, Tuple[Any, Any, Any]]:
boolean_normalizer,
True,
),
"virtualenvs.options.always-copy": (
boolean_validator,
boolean_normalizer,
False,
),
"installer.parallel": (
boolean_validator,
boolean_normalizer,
Expand Down
2 changes: 1 addition & 1 deletion poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ def create_venv(

create_venv = self._poetry.config.get("virtualenvs.create")
root_venv = self._poetry.config.get("virtualenvs.in-project")

venv_path = self._poetry.config.get("virtualenvs.path")

if root_venv:
venv_path = cwd / ".venv"
elif venv_path is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/console/commands/env/test_use.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(

venv_py37 = venv_cache / "{}-py3.7".format(venv_name)
mock_build_env.assert_called_with(
venv_py37, executable="python3.7", flags={"always-copy": False}
venv_py37, executable="python3.7", flags={"always-copy": False, "system-site-packages": False}
)

envs_file = TOMLFile(venv_cache / "envs.toml")
Expand Down
3 changes: 3 additions & 0 deletions tests/console/commands/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def test_list_displays_default_value_if_not_set(tester, config, config_cache_dir
virtualenvs.create = true
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {path} # {virtualenvs}
""".format(
cache=json.dumps(str(config_cache_dir)),
Expand All @@ -53,6 +54,7 @@ def test_list_displays_set_get_setting(tester, config, config_cache_dir):
virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {path} # {virtualenvs}
""".format(
cache=json.dumps(str(config_cache_dir)),
Expand Down Expand Up @@ -96,6 +98,7 @@ def test_list_displays_set_get_local_setting(tester, config, config_cache_dir):
virtualenvs.create = false
virtualenvs.in-project = null
virtualenvs.options.always-copy = false
virtualenvs.options.system-site-packages = false
virtualenvs.path = {path} # {virtualenvs}
""".format(
cache=json.dumps(str(config_cache_dir)),
Expand Down
2 changes: 2 additions & 0 deletions tests/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,5 @@ def test_create_poetry_with_local_config(fixture_dir):

assert not poetry.config.get("virtualenvs.in-project")
assert not poetry.config.get("virtualenvs.create")
assert not poetry.config.get("virtualenvs.options.always-copy")
assert not poetry.config.get("virtualenvs.options.system-site-packages")
30 changes: 22 additions & 8 deletions tests/utils/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_activate_activates_non_existing_virtualenv_no_envs_file(
m.assert_called_with(
Path(tmp_dir) / "{}-py3.7".format(venv_name),
executable="python3.7",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)

envs_file = TOMLFile(Path(tmp_dir) / "envs.toml")
Expand Down Expand Up @@ -279,7 +279,7 @@ def test_activate_activates_different_virtualenv_with_envs_file(
m.assert_called_with(
Path(tmp_dir) / "{}-py3.6".format(venv_name),
executable="python3.6",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)

assert envs_file.exists()
Expand Down Expand Up @@ -333,7 +333,7 @@ def test_activate_activates_recreates_for_different_patch(
build_venv_m.assert_called_with(
Path(tmp_dir) / "{}-py3.7".format(venv_name),
executable="python3.7",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)
remove_venv_m.assert_called_with(Path(tmp_dir) / "{}-py3.7".format(venv_name))

Expand Down Expand Up @@ -661,7 +661,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_generic_
m.assert_called_with(
config_virtualenvs_path / "{}-py3.7".format(venv_name),
executable="python3",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)


Expand All @@ -685,7 +685,7 @@ def test_create_venv_tries_to_find_a_compatible_python_executable_using_specific
m.assert_called_with(
config_virtualenvs_path / "{}-py3.9".format(venv_name),
executable="python3.9",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)


Expand Down Expand Up @@ -769,7 +769,7 @@ def test_create_venv_uses_patch_version_to_detect_compatibility(
config_virtualenvs_path
/ "{}-py{}.{}".format(venv_name, version.major, version.minor),
executable=None,
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)


Expand Down Expand Up @@ -804,7 +804,7 @@ def test_create_venv_uses_patch_version_to_detect_compatibility_with_executable(
config_virtualenvs_path
/ "{}-py{}.{}".format(venv_name, version.major, version.minor - 1),
executable="python{}.{}".format(version.major, version.minor - 1),
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)


Expand Down Expand Up @@ -838,7 +838,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
m.assert_called_with(
poetry.file.parent / ".venv",
executable="python3.7",
flags={"always-copy": False},
flags={"always-copy": False, "system-site-packages": False},
)

envs_file = TOMLFile(Path(tmp_dir) / "virtualenvs" / "envs.toml")
Expand Down Expand Up @@ -875,3 +875,17 @@ def test_venv_has_correct_paths(tmp_venv):
assert paths.get("platlib") is not None
assert paths.get("scripts") is not None
assert tmp_venv.site_packages.path == Path(paths["purelib"])


def test_env_system_packages(tmp_path, config):
venv_path = tmp_path / "venv"
pyvenv_cfg = venv_path / "pyvenv.cfg"

EnvManager(config).build_venv(path=venv_path, flags={"system-site-packages": True})

if sys.version_info >= (3, 3):
assert "include-system-site-packages = true" in pyvenv_cfg.read_text()
elif (2, 6) < sys.version_info < (3, 0):
assert not venv_path.joinpath(
"lib", "python2.7", "no-global-site-packages.txt"
).exists()

0 comments on commit 3c1f245

Please sign in to comment.