8000 create_virtualenv now delegates to Virtualenv.create and Virtualenv.p… · pythonanywhere/helper_scripts@7a60e54 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a60e54

Browse files
committed
create_virtualenv now delegates to Virtualenv.create and Virtualenv.pip_install
1 parent dd84b1a commit 7a60e54

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

pythonanywhere/virtualenvs.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@ def __init__(self, domain, python_version):
15< 8000 /td>15
def __eq__(self, other):
1616
return self.domain == other.domain and self.python_version == other.python_version
1717

18+
1819
def create(self, nuke):
19-
pass
20+
command = f'mkvirtualenv --python=/usr/bin/python{self.python_version} {self.domain}'
21+
if nuke:
22+
command = f'rmvirtualenv {self.domain} && {command}'
23+
subprocess.check_call(['bash', '-c', f'source virtualenvwrapper.sh && {command}'])
2024

2125

26+
def pip_install(self, packages):
27+
subprocess.check_call([
28+
str(self.path / 'bin/pip'),
29+
'install',
30+
packages
31+
])
2232

23-
def virtualenv_path(domain):
24-
return Path(os.environ['WORKON_HOME']) / domain
2533

2634

2735
def create_virtualenv(name, python_version, packages, nuke):
2836
print(snakesay(f'Creating virtualenv with Python{python_version} and installing {packages}'))
29-
command = f'mkvirtualenv --python=/usr/bin/python{python_version} {name} && pip install {packages}'
30-
if nuke:
31-
command = f'rmvirtualenv {name} && {command}'
32-
subprocess.check_call(['bash', '-c', f'source virtualenvwrapper.sh && {command}'])
33-
return virtualenv_path(name)
37+
v = Virtualenv(name, python_version)
38+
v.create(nuke=nuke)
39+
v.pip_install(packages)
40+
return v.path
3441

tests/test_virtualenvs.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from pythonanywhere.virtualenvs import create_virtualenv, Virtualenv
2+
from pythonanywhere.virtualenvs import Virtualenv
33

44

55
class TestVirtualenv:
@@ -10,32 +10,40 @@ def test_path(self, virtualenvs_folder):
1010

1111

1212
def test_create_uses_bash_and_sources_virtualenvwrapper(self, mock_subprocess):
13-
create_virtualenv('domain.com', '2.7', 'django', nuke=False)
13+
v = Virtualenv('domain.com', '2.7')
14+
v.create(nuke=False)
1415
args, kwargs = mock_subprocess.check_call.call_args
1516
command_list = args[0]
1617
assert command_list[:2] == ['bash', '-c']
1718
assert command_list[2].startswith('source virtualenvwrapper.sh && mkvirtualenv')
1819

1920

2021
def test_create_calls_mkvirtualenv_with_python_version_and_domain(self, mock_subprocess):
21-
create_virtualenv('domain.com', '2.7', 'django', nuke=False)
22+
v = Virtualenv('domain.com', '2.7')
23+
v.create(nuke=False)
2224
args, kwargs = mock_subprocess.check_call.call_args
2325
command_list = args[0]
2426
bash_command = command_list[2]
2527
assert 'mkvirtualenv --python=/usr/bin/python2.7 domain.com' in bash_command
2628

2729

28-
def test_create_pip_installs_packages(self, mock_subprocess):
29-
create_virtualenv('domain.com', '2.7', 'package1 package2==1.1.2', nuke=False)
30+
def test_nuke_option_deletes_virtualenv(self, mock_subprocess, virtualenvs_folder):
31+
v = Virtualenv('domain.com', '2.7')
32+
v.create(nuke=True)
3033
args, kwargs = mock_subprocess.check_call.call_args
3134
command_list = args[0]
32-
assert command_list[2].endswith('pip install package1 package2==1.1.2')
35+
assert command_list[:2] == ['bash', '-c']
36+
assert command_list[2].startswith('source virtualenvwrapper.sh && rmvirtualenv domain.com')
3337

3438

35-
def test_create_nuke_option_deletes_virtualenv_first(self, mock_subprocess, virtualenvs_folder):
36-
create_virtualenv('domain.com', '2.7', 'django', nuke=True)
37-
args, kwargs = mock_subprocess.check_call.call_args
39+
def test_install_pip_installs_packages(self, mock_subprocess):
40+
packages = 'package1 package2==1.1.2'
41+
v = Virtualenv('domain.com', '2.7')
42+
v.create(nuke=False)
43+
v.pip_install(packages)
44+
args, kwargs = mock_subprocess.check_call.call_args_list[-1]
3845
command_list = args[0]
39-
assert command_list[:2] == ['bash', '-c']
40-
assert command_list[2].startswith('source virtualenvwrapper.sh && rmvirtualenv domain.com && mkvirtualenv')
46+
pip_path = str(v.path / 'bin/pip')
47+
assert command_list == [pip_path, 'install', packages]
48+
4149

0 commit comments

Comments
 (0)
0