-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvirtualenvs.py
More file actions
35 lines (28 loc) · 1.31 KB
/
virtualenvs.py
File metadata and controls
35 lines (28 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import subprocess
from pathlib import Path
from snakesay import snakesay
class Virtualenv:
def __init__(self, domain, python_version):
self.domain = domain
self.python_version = python_version
self.path = Path(os.environ["WORKON_HOME"]) / domain
def __eq__(self, other):
return self.domain == other.domain and self.python_version == other.python_version
def create(self, nuke):
print(snakesay(f"Creating virtualenv with Python{self.python_version}"))
command = f"mkvirtualenv --python=python{self.python_version} {self.domain}"
if nuke:
command = f"rmvirtualenv {self.domain} && {command}"
subprocess.check_call(["bash", "-c", f"source virtualenvwrapper.sh && {command}"])
return self
def pip_install(self, packages):
print(snakesay(f"Pip installing {packages} (this may take a couple of minutes)"))
commands = [str(self.path / "bin/pip"), "install"] + packages.split()
subprocess.check_call(commands)
def get_version(self, package_name):
commands = [str(self.path / "bin/pip"), "show", package_name]
output = subprocess.check_output(commands).decode()
for line in output.splitlines():
if line.startswith("Version: "):
return line.split()[1]