-
Notifications
You must be signed in to change notification settings - Fork 1
/
runfromvenv.py
37 lines (31 loc) · 1.32 KB
/
runfromvenv.py
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
36
37
import venv, sys, inspect
from subprocess import check_call, getoutput
from os.path import realpath, dirname, join, basename, isdir, splitext
from os import environ, execvp
frame = next(reversed(inspect.stack())).frame
project_main_file = realpath(frame.f_globals["__file__"])
project_dir = dirname(project_main_file)
project_name = splitext(basename(project_main_file))[0]
venv_dir = join(project_dir, "." + project_name + ".venv")
if "VIRTUAL_ENV" != venv_dir:
if not isdir(venv_dir):
environ["P"] = "PATH" # random value but reusing already aliased value
# system_site_packages=False, clear=False, symlinks=False, upgrade=False, with_pip=False, prompt=None, upgrade_deps=False
venv.create(venv_dir, with_pip=True)
venv_bin_dir = join(venv_dir, "bin")
e = realpath(sys.executable)
if e != realpath(join(venv_bin_dir, basename(e))):
environ.update(
{
"VIRTUAL_ENV": venv_dir,
"PATH": venv_bin_dir + ":" + environ["PATH"],
}
)
execvp(project_main_file, sys.argv)
deps = frame.f_locals["deps"]
[
check_call(["pip", "install", *args])
for args in (("--upgrade", "pip", "setuptools", "wheel"), deps)
if "P" in environ
]
[getoutput("pip install " + dep) for dep in deps if "P" in environ]