Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
python-poetry
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
python-poetry
Commits
dd483c8c
Unverified
Commit
dd483c8c
authored
Apr 22, 2023
by
David Hotham
Committed by
GitHub
Apr 22, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use existing .venv even without explicit in-project (#7792)
parent
6eccf926
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
22 deletions
+71
-22
src/poetry/utils/env.py
+24
-21
tests/utils/test_env.py
+47
-1
No files found.
src/poetry/utils/env.py
View file @
dd483c8c
...
...
@@ -588,6 +588,11 @@ class EnvManager:
return
Version
.
parse
(
version
)
@property
def
in_project_venv
(
self
)
->
Path
:
venv
:
Path
=
self
.
_poetry
.
file
.
parent
/
".venv"
return
venv
def
activate
(
self
,
python
:
str
)
->
Env
:
venv_path
=
self
.
_poetry
.
config
.
virtualenvs_path
cwd
=
self
.
_poetry
.
file
.
parent
...
...
@@ -621,12 +626,11 @@ class EnvManager:
patch
=
python_version
.
text
create
=
False
is_root_venv
=
self
.
_poetry
.
config
.
get
(
"virtualenvs.in-project"
)
# If we are required to create the virtual environment in the root folder,
# If we are required to create the virtual environment in the project directory,
# create or recreate it if needed
if
is_root_venv
:
if
self
.
use_in_project_venv
()
:
create
=
False
venv
=
self
.
_poetry
.
file
.
parent
/
".venv"
venv
=
self
.
in_project_venv
if
venv
.
exists
():
# We need to check if the patch version is correct
_venv
=
VirtualEnv
(
venv
)
...
...
@@ -730,12 +734,8 @@ class EnvManager:
if
not
in_venv
or
env
is
not
None
:
# Checking if a local virtualenv exists
if
(
self
.
_poetry
.
config
.
get
(
"virtualenvs.in-project"
)
is
not
False
and
(
cwd
/
".venv"
)
.
exists
()
and
(
cwd
/
".venv"
)
.
is_dir
()
):
venv
=
cwd
/
".venv"
if
self
.
use_in_project_venv
():
venv
=
self
.
in_project_venv
return
VirtualEnv
(
venv
)
...
...
@@ -772,12 +772,8 @@ class EnvManager:
venv_path
=
self
.
_poetry
.
config
.
virtualenvs_path
env_list
=
[
VirtualEnv
(
p
)
for
p
in
sorted
(
venv_path
.
glob
(
f
"{venv_name}-py*"
))]
venv
=
self
.
_poetry
.
file
.
parent
/
".venv"
if
(
self
.
_poetry
.
config
.
get
(
"virtualenvs.in-project"
)
is
not
False
and
venv
.
exists
()
and
venv
.
is_dir
()
):
if
self
.
use_in_project_venv
():
venv
=
self
.
in_project_venv
env_list
.
insert
(
0
,
VirtualEnv
(
venv
))
return
env_list
...
...
@@ -891,6 +887,12 @@ class EnvManager:
return
VirtualEnv
(
venv_path
,
venv_path
)
def
use_in_project_venv
(
self
)
->
bool
:
in_project
:
bool
|
None
=
self
.
_poetry
.
config
.
get
(
"virtualenvs.in-project"
)
if
in_project
is
not
None
:
return
in_project
return
self
.
in_project_venv
.
is_dir
()
def
create_venv
(
self
,
name
:
str
|
None
=
None
,
...
...
@@ -918,7 +920,7 @@ class EnvManager:
return
env
create_venv
=
self
.
_poetry
.
config
.
get
(
"virtualenvs.create"
)
root_venv
=
self
.
_poetry
.
config
.
get
(
"virtualenvs.in-project"
)
in_project_venv
=
self
.
use_in_project_venv
(
)
prefer_active_python
=
self
.
_poetry
.
config
.
get
(
"virtualenvs.prefer-active-python"
)
...
...
@@ -927,12 +929,13 @@ class EnvManager:
if
not
executable
and
prefer_active_python
:
executable
=
self
.
_detect_active_python
()
venv_path
:
Path
=
(
cwd
/
".venv"
if
root_venv
else
self
.
_poetry
.
config
.
virtualenvs_path
venv_path
=
(
self
.
in_project_venv
if
in_project_venv
else
self
.
_poetry
.
config
.
virtualenvs_path
)
if
not
name
:
name
=
self
.
_poetry
.
package
.
name
assert
name
is
not
None
python_patch
=
"."
.
join
([
str
(
v
)
for
v
in
sys
.
version_info
[:
3
]])
python_minor
=
"."
.
join
([
str
(
v
)
for
v
in
sys
.
version_info
[:
2
]])
...
...
@@ -1007,7 +1010,7 @@ class EnvManager:
self
.
_poetry
.
package
.
python_versions
)
if
roo
t_venv
:
if
in_projec
t_venv
:
venv
=
venv_path
else
:
name
=
self
.
generate_env_name
(
name
,
str
(
cwd
))
...
...
tests/utils/test_env.py
View file @
dd483c8c
...
...
@@ -208,6 +208,52 @@ def check_output_wrapper(
return
check_output
def
test_activate_in_project_venv_no_explicit_config
(
tmp_path
:
Path
,
manager
:
EnvManager
,
poetry
:
Poetry
,
mocker
:
MockerFixture
,
venv_name
:
str
,
in_project_venv_dir
:
Path
,
)
->
None
:
mocker
.
patch
(
"shutil.which"
,
side_effect
=
lambda
py
:
f
"/usr/bin/{py}"
)
mocker
.
patch
(
"subprocess.check_output"
,
side_effect
=
check_output_wrapper
(),
)
mocker
.
patch
(
"subprocess.Popen.communicate"
,
side_effect
=
[
(
"/prefix"
,
None
),
(
'{"version_info": [3, 7, 0]}'
,
None
),
(
"/prefix"
,
None
),
(
"/prefix"
,
None
),
(
"/prefix"
,
None
),
],
)
m
=
mocker
.
patch
(
"poetry.utils.env.EnvManager.build_venv"
,
side_effect
=
build_venv
)
env
=
manager
.
activate
(
"python3.7"
)
assert
env
.
path
==
tmp_path
/
"poetry-fixture-simple"
/
".venv"
assert
env
.
base
==
Path
(
"/prefix"
)
m
.
assert_called_with
(
tmp_path
/
"poetry-fixture-simple"
/
".venv"
,
executable
=
Path
(
"/usr/bin/python3.7"
),
flags
=
{
"always-copy"
:
False
,
"system-site-packages"
:
False
,
"no-pip"
:
False
,
"no-setuptools"
:
False
,
},
prompt
=
"simple-project-py3.7"
,
)
envs_file
=
TOMLFile
(
tmp_path
/
"envs.toml"
)
assert
not
envs_file
.
exists
()
def
test_activate_activates_non_existing_virtualenv_no_envs_file
(
tmp_path
:
Path
,
manager
:
EnvManager
,
...
...
@@ -1382,7 +1428,7 @@ def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
)
mocker
.
patch
(
"subprocess.Popen.communicate"
,
side_effect
=
[(
"/prefix"
,
None
),
(
"/prefix"
,
None
)],
side_effect
=
[(
"/prefix"
,
None
),
(
"/prefix"
,
None
)
,
(
"/prefix"
,
None
)
],
)
m
=
mocker
.
patch
(
"poetry.utils.env.EnvManager.build_venv"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment