Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_virtualenv(where, distribute=False):
import virtualenv
if sys.version_info[0] > 2:
distribute = True
virtualenv.create_environment(
where, use_distribute=distribute, unzip_setuptools=True)
return virtualenv.path_locations(where)
def version_exe(venv, exe_name):
_, _, _, bin_dir = virtualenv.path_locations(str(venv))
exe = os.path.join(bin_dir, exe_name)
script = "import sys; import json; print(json.dumps(dict(v=list(sys.version_info), e=sys.executable)))"
cmd = [exe, "-c", script]
out = json.loads(subprocess.check_output(cmd, universal_newlines=True))
return out["v"], out["e"]
def create_virtualenv(where, distribute=False):
save_argv = sys.argv
try:
import virtualenv
distribute_opt = distribute and ['--distribute'] or []
sys.argv = ['virtualenv', '--quiet'] + distribute_opt + ['--no-site-packages', '--unzip-setuptools', where]
virtualenv.main()
finally:
sys.argv = save_argv
return virtualenv.path_locations(where)
def test_commandline_basic(tmpdir):
"""Simple command line usage should work and files should be generated"""
home_dir, lib_dir, inc_dir, bin_dir = virtualenv.path_locations(str(tmpdir.join("venv")))
subprocess.check_call([sys.executable, "-m", "virtualenv", "-vvv", home_dir, "--no-download"])
assert os.path.exists(home_dir)
assert os.path.exists(bin_dir)
assert os.path.exists(os.path.join(bin_dir, "activate"))
assert os.path.exists(os.path.join(bin_dir, "activate_this.py"))
assert os.path.exists(os.path.join(bin_dir, "activate.ps1"))
exe = os.path.join(bin_dir, os.path.basename(sys.executable))
assert os.path.exists(exe)
def _check_no_warnings(module):
subprocess.check_call((exe, "-Werror", "-c", "import {}".format(module)))
_check_no_warnings("distutils")
def build(app_path):
'''
Installs the dependencies in requirements.txt into a virtualenv in the app directory
Returns the path to the python executable that should be used to run the app.
'''
import virtualenv
wheelhouse = os.path.join(AppDirs('Tingbot', 'Tingbot').user_cache_dir, 'Wheelhouse')
if not os.path.exists(wheelhouse):
os.makedirs(wheelhouse)
requirements_txt_path = os.path.join(app_path, 'requirements.txt')
venv_path = os.path.join(app_path, 'venv')
_, venv_lib_dir, _, venv_bin_dir = virtualenv.path_locations(venv_path)
venv_python_path = os.path.join(venv_bin_dir, 'python')
venv_previous_requirements_path = os.path.join(venv_path, 'requirements.txt')
if not os.path.isfile(requirements_txt_path):
# delete the venv if it's there and use the system python
clean(app_path)
return sys.executable
# check that there's a virtualenv there and that it's got a working
# version of python
try:
subprocess.check_call([
venv_python_path,
'-c', 'import sys; sys.exit(0)'
])
except (OSError, subprocess.CalledProcessError):
def _get_env_bin_path(env_path):
"""Returns the bin path for a virtualenv
This provides a fallback for a race condition in which you're trying
to use the script and create a virtualenv from within
a virtualenv in which virtualenv isn't installed and so
is not importable.
"""
try:
import virtualenv
return virtualenv.path_locations(env_path)[3]
except ImportError:
return os.path.join(env_path, 'scripts' if IS_WIN else 'bin')
def do(args):
""" Main Entry Point """
build_worktree = qibuild.parsers.get_build_worktree(args)
env = build_worktree.get_env()
build_config = qibuild.parsers.get_build_config(build_worktree, args)
worktree = build_worktree.worktree
cmd = args.command
venvs_path = os.path.join(worktree.dot_qi,
"venvs")
name = build_config.build_directory("py")
venv_root = os.path.join(venvs_path, name)
if not os.path.exists(venv_root):
err = "No Virtualenv found in %s\n" % (venv_root)
err += "Please run `qipy bootstrap`"
raise Exception(err)
binaries_path = virtualenv.path_locations(venv_root)[-1]
if args.dolist:
for f in sorted(os.listdir(binaries_path)):
if os.path.isfile(os.path.join(binaries_path, f)):
print(f)
return
if not cmd:
cmd = ["ipython"]
if os.path.exists(cmd[0]):
# Assume it is a script we want to run
python_path = os.path.join(binaries_path, "python")
cmd.insert(0, python_path)
else:
script_path = qipy.venv.find_script(venv_root, cmd[0])
if script_path:
cmd[0] = script_path
lib_path = virtualenv.path_locations(venv_root)[1]