def configure_virtualenv(config, python_worktree, build_worktree=None, remote_packages=None, site_packages=True): if not remote_packages: remote_packages = list() # create a new virtualenv python_worktree.config = config venv_path = python_worktree.venv_path pip = python_worktree.pip try: virtualenv.create_environment(python_worktree.venv_path, site_packages=site_packages) except: ui.error("Failed to create virtualenv") return # Install all Python projects using pip install -e . python_projects = python_worktree.python_projects for i, project in enumerate(python_projects): ui.info_count(i, len(python_projects), ui.green, "Configuring", ui.reset, ui.blue, project.src) cmd = [pip, "install", "--editable", "."] qisys.command.call(cmd, cwd=project.path) # Write a qi.pth file containing path to C/C++ extensions if build_worktree: handle_extensions(venv_path, python_worktree, build_worktree) # Install the extension in the virtualenv binaries_path = virtualenv.path_locations(venv_path)[-1] pip_binary = os.path.join(binaries_path, "pip") if remote_packages: cmd = [pip_binary, "install"] + remote_packages subprocess.check_call(cmd)
def test_thin_dir(self): ''' Test the thin dir to make sure salt-call can run Run salt call via a python in a new virtual environment to ensure salt-call has all dependencies needed. ''' venv_dir = os.path.join(self.tmpdir, 'venv') virtualenv.create_environment(venv_dir) salt.utils.thin.gen_thin(self.tmpdir) thin_dir = os.path.join(self.tmpdir, 'thin') thin_archive = os.path.join(thin_dir, 'thin.tgz') tar = tarfile.open(thin_archive) tar.extractall(thin_dir) tar.close() bins = 'bin' if sys.platform == 'win32': bins = 'Scripts' cmd = [ os.path.join(venv_dir, bins, 'python'), os.path.join(thin_dir, 'salt-call'), '--version', ] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() proc.wait() assert proc.returncode == 0, (stdout, stderr, proc.returncode)
def execute(dataset, infile, outfile='.'): os.chdir("deepwalk") print('Current directory is', os.getcwd()) # create and activate the virtual environment print('\nRunning in a virtual environment') venv_dir = os.path.join(os.getcwd(), 'project', '.venv') if not os.path.exists(venv_dir): virtualenv.create_environment(venv_dir) execfile(os.path.join(venv_dir, "bin", "activate_this.py"), globals()) # pip install the requirements of deepwalk in the virtual environment print('\nInstalling requirements of deepwalk ...\n') pip.main(['install', '--prefix', venv_dir, '-r', 'requirements.txt']) # Setup deepwalk print('\nSetting up deepwalk ...\n') setup = subprocess.run("python setup.py install", shell=True) print(setup) path = os.path.join('..', '..', 'graphs', dataset) print('\nRunning deepwalk using', dataset, '...\n') command = 'deepwalk --format edgelist ' \ '--input "' + infile + '" ' \ '--max-memory-data-size 0 ' \ '--number-walks 10 ' \ '--representation-size 128 ' \ '--walk-length 40 ' \ '--workers 1 ' \ '--output "' + outfile + '"' run = subprocess.run(command, shell=True) print(run)
def _create(self, clear=False): if clear: self.location.rmtree() if self._template: # On Windows, calling `_virtualenv.path_locations(target)` # will have created the `target` directory... if sys.platform == 'win32' and self.location.exists: self.location.rmdir() # Clone virtual environment from template. self._template.location.copytree(self.location) self._sitecustomize = self._template.sitecustomize self._user_site_packages = self._template.user_site_packages else: # Create a new virtual environment. if self._venv_type == 'virtualenv': _virtualenv.create_environment( self.location, no_pip=True, no_wheel=True, no_setuptools=True, ) self._fix_virtualenv_site_module() elif self._venv_type == 'venv': builder = _venv.EnvBuilder() context = builder.ensure_directories(self.location) builder.create_configuration(context) builder.setup_python(context) self.site.makedirs() self.sitecustomize = self._sitecustomize self.user_site_packages = self._user_site_packages
def create_venv(path): if not os.path.isdir( path): #TODO find a better way to check if the venv is good logging.info('Creating virtual environment: {}'.format(path)) virtualenv.create_environment(path) else: logging.info('Virtual environment exists: {}'.format(path))
def env_create(args): import virtualenv settings = load_settings() ### Create virtualenv virtualenv.create_environment(settings.dir_virtualenv) py_reqs = ['brubeck', 'dictshield', 'ujson'] ### Ask about preferences web_server = ask_webserver(settings) if web_server == ENV_M2: py_reqs.append('pyzmq') concurrency = ask_concurrency(settings) py_reqs.append(concurrency) if concurrency == ENV_GEVENT: py_reqs.append('cython') template_engines = ask_template_engines(settings) py_reqs = py_reqs + template_engines ### Install web server requirements if web_server == ENV_M2: install_mongrel2(settings) ### pip install requirements response = install_with_pip(py_reqs)
def create_project(location, name, executable=False): """ Creates a project folder given a env new_dir """ new_dir = "{}/{}".format(location, name) if os.path.exists(new_dir): error("Folder {} already exists!".format(name)) sys.exit(1) # Make boilerplate files try: os.mkdir(new_dir) os.mkdir(new_dir + "/.carpet/") os.chdir(new_dir) open("__init__.py", "a").close() open("Carpet.toml", "a").close() _repo = git.Repo.init(path=new_dir) config.build_default_config(new_dir, name=name) # If the project is meant to be executable, # add a __main__.py if executable: main = open("__main__.py", "a") main.write("print('Hello World')") main.close() else: config.set_config(new_dir, "package/executable", False) virtualenv.create_environment(new_dir + "/.carpet/env") except Exception as e: shutil.rmtree(new_dir) colour("FAIL", "Failed to create project") echo("Have you checked your permissions?") colour("BLUE", "Created new project!")
def create_if_not_exists(self): python = os.path.join(self.path, 'bin', 'python') if not os.path.exists(python): makedirs(self.path) log.info(style_note('Creating Python virtualenv', self.path)) if hasattr(virtualenv, 'cli_run'): # New API (in which there isn't really any API) virtualenv.cli_run( ['--no-pip', '--no-wheel', '--no-setuptools', self.path]) else: # Old API virtualenv.create_environment(self.path, no_setuptools=True, no_pip=True) if not os.path.exists(python + '-config'): version = get_default_python().version names = ( 'python{}.{}-config'.format(*version), 'python{}-config'.format(*version), 'python-config', ) prefix = getattr(sys, 'real_prefix', sys.prefix) for name in names: old_path = os.path.join(prefix, 'bin', name) if os.path.exists(old_path): for name in names: new_path = os.path.join(self.path, 'bin', name) self.rewrite_shebang_or_link(old_path, new_path) break else: log.warning('Could not find python-config')
def execute(dataset, infile, outfile='.'): os.chdir("struc2vec") print('Current directory is', os.getcwd()) # create and activate the virtual environment print('\nRunning in a virtual environment') venv_dir = os.path.join(os.getcwd(), 'project', '.venv') if not os.path.exists(venv_dir): virtualenv.create_environment(venv_dir) execfile(os.path.join(venv_dir, "bin", "activate_this.py"), globals()) # pip install the requirements of struc2vec in the virtual environment print('\nInstalling requirements of struc2vec ...\n') pip.main(['install', '--prefix', venv_dir, 'figures']) pip.main(['install', '--prefix', venv_dir, 'fastdtw']) pip.main(['install', '--prefix', venv_dir, 'gensim']) pip.main(['install', '--prefix', venv_dir, 'cPickle']) path = os.path.join('..', '..', 'graphs', dataset) print('\nRunning struc2vec using', dataset, '...\n') command = 'python src/main.py ' \ '--input "' + infile + '" ' \ '--num-walks 80 ' \ '--dimensions 128 ' \ '--walk-length 40 ' \ '--OPT1 True ' \ '--OPT2 True ' \ '--OPT3 True ' \ '--output "' + outfile + '"' run = subprocess.run(command, shell=True) print(run)
def prepare_virtualenv(packages=()): """ Prepares a virtual environment. :rtype : VirtualEnvDescription """ vroot = get_vroot() env_key = get_env_key(packages) vdir = os.path.join(vroot, env_key) vbin = os.path.join(vdir, ('bin', 'Scripts')[_windows]) exe_suffix = ("", ".exe")[_windows] vpython = os.path.join(vbin, 'python' + exe_suffix) vpip = os.path.join(vbin, 'pip' + exe_suffix) venv_description = VirtualEnvDescription(home_dir=vdir, bin_dir=vbin, python=vpython, pip=vpip, packages=packages) env = get_clean_system_environment() env['PIP_DOWNLOAD_CACHE'] = os.path.abspath(os.path.join(vroot, "pip-download-cache")) # Cache environment done_flag_file = os.path.join(vdir, "done") if not os.path.exists(done_flag_file): if os.path.exists(vdir): shutil.rmtree(vdir) virtualenv.create_environment(vdir) for package_spec in packages: subprocess.call([vpip, "install", package_spec], env=env) open(done_flag_file, 'a').close() subprocess.call([vpython, "setup.py", "install"], env=env) return venv_description
def install_charm_juju_lint(): create_install_dirs() virtualenv.create_environment(USR_LIB) packages.pip_install("juju", venv=USR_LIB) deploy_scripts() hookenv.status_set("active", "Unit is ready") set_flag("charm-juju-lint.installed")
def run_tests(payload): #payload = get_payload(payload_id) job = get_current_job() # work out the repo_url repo_name = payload['repository']['name'] owner = payload['repository']['owner']['name'] repo_url = "[email protected]:%s/%s.git" % (owner, repo_name) update_progress(job, 'repo url: %s' % repo_url) logger.info("repo: %s" % repo_url) vpath = tempfile.mkdtemp(suffix="ridonkulous") logger.info("cloning repo %s to: %s" % (repo_url, vpath)) update_progress(job, "cloning repo %s to: %s" % (repo_url, vpath)) create_environment(vpath, site_packages=False) os.chdir(vpath) git.Git().clone(repo_url) os.chdir(os.path.join(vpath, repo_name)) pip = "%s/bin/pip" % vpath #python = "%s/bin/python" nose = "%s/bin/nosetests" % vpath ret = subprocess.call(r'%s install -r requirements.txt --use-mirrors' % pip, shell=True) logger.info("running nose") ret = subprocess.call(r'%s' % nose, shell=True) logger.info(ret) update_progress(job, 'done') return 'ok'
def test_pypy_pre_import(tmp_path): """For PyPy, some built-in modules should be pre-imported because some programs expect them to be in sys.modules on startup. """ check_code = inspect.getsource(check_pypy_pre_import) check_code = textwrap.dedent(check_code[check_code.index("\n") + 1:]) if six.PY2: check_code = check_code.decode() check_prog = tmp_path / "check-pre-import.py" check_prog.write_text(check_code) ve_path = str(tmp_path / "venv") virtualenv.create_environment(ve_path) bin_dir = virtualenv.path_locations(ve_path)[-1] try: cmd = [ os.path.join( bin_dir, "{}{}".format(virtualenv.EXPECTED_EXE, ".exe" if virtualenv.IS_WIN else "")), str(check_prog), ] subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as exception: assert not exception.returncode, exception.output
def build(cls, path, key, options, force=False): ''' Build a virtual environment in specified path, according to given key and options. ''' virtualenv.logger = virtualenv.Logger( [(virtualenv.Logger.level_for_integer(2), sys.stderr)]) # a simple check that what we want to remove looks like a ve name :) if force and os.path.exists(path): if len(os.path.basename(path)) == 32: shutil.rmtree(path) else: raise AssertionError("Ah, you probably do not want to delete " "%s, do you?" % path) os.makedirs(path) virtualenv.create_environment(home_dir=path) ve = VirtualEnv(path) # "fix" bug in pip # (see: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=677801) if options.fix_pip: fragments_file = 'local/lib/python2.7/site-packages/' \ 'pip-1.1-py2.7.egg/pip/vcs/__init__.py' ve.execlp("sed", "-ie", "/urlparse.uses_fragment.extend(self.schemes)/d", ve.local_path(fragments_file)) ve.unlink(fragments_file + 'c') key.initialize(ve) ve.mark_as_good() return ve
def _create(self, clear=False): # Create the actual virtual environment _virtualenv.create_environment( self.location, clear=clear, never_download=True, no_pip=True, ) # Install our development version of pip install the virtual # environment cmd = [self.bin.join("python"), "setup.py", "install", "--no-compile"] p = subprocess.Popen( cmd, cwd=self.pip_source_dir, stderr=subprocess.STDOUT, stdout=DEVNULL, ) p.communicate() if p.returncode != 0: raise subprocess.CalledProcessError( p.returncode, cmd, output=p.stdout, )
def install(args): print("Installing HPX...") _check_pip() try: import virtualenv import pip except ImportError: print("Installing virtualenv") import pip pip.main(["install", "virtualenv"]) print("Please re-run bootstrap.py install") sys.exit() if not os.path.exists("env"): print("Setting up virtualenv") virtualenv.create_environment("env") scripts = os.path.join("env", "Scripts") if not os.path.exists(scripts): scripts = os.path.join("env", "bin") _activate_venv() _update_pip(args) print("Finished installing.") if args.run: build(args)
def __setup(venv_dir, requirements=None, verbose=False): """ This creates (if relevant) and activates a virtual environment. It also allows to define requirements to be installed in this environment. :param venv_dir: virtual environment's directory :param requirements: list of required package OR path of the requirements file to be used :param verbose: displayed Pip output while installing packages """ __deactivate() venv_dir = os.path.abspath(venv_dir) if not os.path.exists(venv_dir): # fix to issue: https://github.com/pypa/virtualenv/issues/1585 try: virtualenv.create_environment(venv_dir) except AttributeError: virtualenv.cli_run([venv_dir]) __activate(venv_dir) if isinstance(requirements, string_types): with open(requirements) as f: requirements = [l.strip() for l in f] if isinstance(requirements, (tuple, list, set)): args = ["-v"] if verbose else [] kwargs = {'prefix': venv_dir} for req in requirements: pkg = __install(req, *args, **kwargs) for tl in pkg.top_level: if hasattr(virtualenv, tl): raise TopLevelAlreadyExists("{} ({})".format(tl, pkg.name)) m = import_module(tl) setattr(virtualenv, tl, m)
def test_activate_with_xonsh(tmpdir, monkeypatch): monkeypatch.chdir(tmpdir) home_dir, _, __, bin_dir = virtualenv.path_locations( str(tmpdir.join("env"))) virtualenv.create_environment(home_dir, no_pip=True, no_setuptools=True, no_wheel=True) monkeypatch.chdir(home_dir) activate_script = join(bin_dir, "activate.xsh") cmd = [ XONSH_COMMAND, "-c", "{0}; source r'{1}'; {0}; deactivate; {0}".format( print_python_exe_path(), activate_script), ] print("COMMAND", cmd) print("Executable", sys.executable) env = dict(os.environ) env["XONSH_DEBUG"] = "1" env["XONSH_SHOW_TRACEBACK"] = "True" output = subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT, env=env) content = output.split() assert len(content) == 3, output before_activate, after_activate, after_deactivate = content exe = "{}.exe".format(virtualenv.expected_exe ) if virtualenv.is_win else virtualenv.expected_exe assert normcase(long_path(after_activate)) == normcase( long_path(join(bin_dir, exe))) assert before_activate == after_deactivate
def main(requirements, venvname): print(f"Creating venv from {requirements} in {venvname}") # hash requirements content = open(requirements, "r").read().encode() checksum = hashlib.sha256(content).hexdigest() print(f"Requirements file has checksum: {checksum}") # check cache for hash cache_path = os.path.join(cache_dir, f"{checksum}.tar.gz") if os.path.exists(cache_path): # unzip from cache archive = tarfile.open(cache_path) archive.extractall(path=venvname) archive.close() else: # create venv from scratch # create and activate the virtual environment print(f"Not found in cache, creating") virtualenv.create_environment(venvname) # pip install a package using the venv as a prefix pipmain(["install", "-r", requirements, "--prefix", venvname]) # cache venv tar = tarfile.open(cache_path, "w:gz") for path, a, b in os.walk(venvname): relpath = os.path.relpath(path, venvname) tar.add(path, relpath) tar.close() return 0
def test_create_environment_from_virtual_environment(tmpdir): """Create virtual environment using Python from another virtual environment """ venvdir = str(tmpdir / "venv") home_dir, lib_dir, inc_dir, bin_dir = virtualenv.path_locations(venvdir) virtualenv.create_environment(venvdir) assert not os.path.islink(os.path.join(lib_dir, "distutils"))
def bootstrap(folder): print("+- Loading pip:", end='') try: from pip.req import InstallRequirement except: print(" failed....") raise print(" done") try: print("+- Creating virtual enviroment: ", end='') virtualenv.create_environment(folder, site_packages=True, clear=False) print(" %s" % folder) except Exception as e: print(""" failed.... - Error instanciating the virutal enviroment. This is a catastrophic failure... - Something is really wrong with this Python installation - You could try to manually install virtualenv via: sudo python -m pip install -U virtualenv """) raise else: return finally: pass
def test_relative_symlink(tmpdir): """ Test if a virtualenv works correctly if it was created via a symlink and this symlink is removed """ tmpdir = str(tmpdir) ve_path = os.path.join(tmpdir, "venv") os.mkdir(ve_path) workdir = os.path.join(tmpdir, "work") os.mkdir(workdir) ve_path_linked = os.path.join(workdir, "venv") os.symlink(ve_path, ve_path_linked) lib64 = os.path.join(ve_path, "lib64") virtualenv.create_environment(ve_path_linked, symlink=True) if not os.path.lexists(lib64): # no lib 64 on this platform return assert os.path.exists(lib64) shutil.rmtree(workdir) assert os.path.exists(lib64)
def update_ve(self, full_rebuild, force_update): if not path.exists(self.requirements): print >> sys.stderr, "Could not find requirements: file %s" % self.requirements return 1 update_required = self.virtualenv_needs_update() if not update_required and not force_update: # Nothing to be done print "VirtualEnv does not need to be updated" print "use --force to force an update" return 0 # if we need to create the virtualenv, then we must do that from # outside the virtualenv. The code inside this if statement will only # be run outside the virtualenv. if full_rebuild and path.exists(self.ve_dir): shutil.rmtree(self.ve_dir) if not path.exists(self.ve_dir): import virtualenv virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(self.ve_dir, site_packages=False) # install the pip requirements and exit pip_path = path.join(self.ve_dir, 'bin', 'pip') # use cwd to allow relative path specs in requirements file, e.g. ../tika pip_retcode = subprocess.call( [pip_path, 'install', '--requirement=%s' % self.requirements], cwd=os.path.dirname(self.requirements)) if pip_retcode == 0: self.update_ve_timestamp() return pip_retcode
def run(self): # generate the metadata first self.run_command('egg_info') venv_dir = self.bdist_dir virtualenv.create_environment( venv_dir, never_download=True, ) pip_cmd = [os.path.join(venv_dir, 'bin', 'pip'), 'install', '.'] if self.requirements: pip_cmd.extend(['-r', self.requirements]) subprocess.check_call(pip_cmd) self.copy_file(os.path.join(self.egg_info, 'PKG-INFO'), venv_dir) virtualenv.make_environment_relocatable(venv_dir) log.info("creating '%s' and adding '%s' to it", self.venv_output, venv_dir) mkpath(os.path.dirname(self.venv_output)) with closing(tarfile.open(self.venv_output, 'w:gz')) as tar: tar.add(venv_dir, self.archive_root) self.distribution.dist_files.append(('bdist_venv', get_python_version(), self.venv_output)) if not self.keep_temp: remove_tree(venv_dir)
def main(): ap = argparse.ArgumentParser() ap.add_argument("env") ap.add_argument("-w", "--workdir", dest="workdir") args = ap.parse_args() env_path = os.path.join(ENVS_PATH, args.env) if not os.path.isdir(env_path): if raw_input("%s (for %s) does not exist. Create new virtualenv? [y/n] " % (env_path, args.env)) == "y": virtualenv.logger = virtualenv.Logger([(virtualenv.Logger.NOTIFY, sys.stdout)]) virtualenv.create_environment(env_path, site_packages=True, unzip_setuptools=True) else: print "Abort." sys.exit(1) if args.workdir: workdir = os.path.realpath(args.workdir) if os.path.isdir(workdir): print "Setting environment %s workdir to '%s'." % (args.env, workdir) file(os.path.join(env_path, "workdir"), "wb").write(workdir) activation_script_path = os.environ.get("VW_ACTSCRIPT_PATH") if not activation_script_path: print "VW_ACTSCRIPT_PATH not set, not creating activation script" else: with file(activation_script_path, "wb") as actscript: actscript.write(gen_activation_script(env_path))
def init(config, args): """Create a new WSGI project skeleton and config.""" log = logging.getLogger('kraftwerk.init') # project_root/src_root project_root = os.path.abspath(os.path.join(os.getcwd(), args.title)) src_root = os.path.join(project_root, args.title) if os.path.exists(project_root) and os.listdir(project_root): init.parser.error("Directory exists and isn't empty") elif not os.path.exists(project_root): log.debug('makedirs %s' % project_root) os.makedirs(project_root) elif not os.path.isdir(project_root): init.parser.error("A file with this name already exists here and isn't a directory") log.debug('virtualenv %s/' % args.title) logger = virtualenv.Logger([ (virtualenv.Logger.level_for_integer(2), sys.stdout)]) virtualenv.logger = logger virtualenv.create_environment(args.title, site_packages=True) log.debug('mkdir %s/service/' % project_root) os.makedirs(os.path.join(args.title, 'service')) log.debug('mkdir %s/static/' % project_root) os.makedirs(os.path.join(args.title, 'static')) log.debug('mkdir %s/' % src_root) os.makedirs(src_root) _copy_project(config, args.title, project_root) print "Your new project is at: %s" % project_root
def __init__(self, clone_config): self._tempdir = tempfile.TemporaryDirectory() log.info('New project clone in %s', self._tempdir.name) self._clone_dir = str(Path(self._tempdir.name) / 'repo') if clone_config['method'] == 'git': _clone_with_git(clone_config.get('repo-uri', '.'), self._clone_dir) elif clone_config['method'] == 'copy': _clone_with_copy(os.getcwd(), self._clone_dir) # pylint: disable=fixme # TODO: We should allow user to specify which version of Python to use. # How? The EnvBuilder could be passed a path to a python interpreter # which is used in the call to pip. This path would need to come from # the config. # Install into venv self._venv_path = Path(self._tempdir.name) / 'venv' log.info('Creating virtual environment in %s', self._venv_path) virtualenv.create_environment(str(self._venv_path)) _activate(self._venv_path) _install_sitecustomize(self._venv_path) self._run_commands(clone_config.get('commands', ()))
def clean_python(tmp_path_factory): path = tmp_path_factory.mktemp("activation-test-env") prev_cwd = os.getcwd() try: os.chdir(str(path)) home_dir, _, __, bin_dir = virtualenv.path_locations(str(path / "env")) virtualenv.create_environment(home_dir, no_pip=True, no_setuptools=True, no_wheel=True) site_packages = subprocess.check_output( [ os.path.join(bin_dir, virtualenv.EXPECTED_EXE), "-c", "from distutils.sysconfig import get_python_lib; print(get_python_lib())", ], universal_newlines=True, ).strip() pydoc_test = path.__class__(site_packages) / "pydoc_test.py" pydoc_test.write_text('"""This is pydoc_test.py"""') finally: os.chdir(str(prev_cwd)) yield home_dir, bin_dir, pydoc_test
def test_thin_dir(self): """ Test the thin dir to make sure salt-call can run Run salt call via a python in a new virtual environment to ensure salt-call has all dependencies needed. """ venv_dir = os.path.join(self.tmpdir, "venv") virtualenv.create_environment(venv_dir) salt.utils.thin.gen_thin(self.tmpdir) thin_dir = os.path.join(self.tmpdir, "thin") thin_archive = os.path.join(thin_dir, "thin.tgz") tar = tarfile.open(thin_archive) tar.extractall(thin_dir) tar.close() bins = "bin" if sys.platform == "win32": bins = "Scripts" cmd = [ os.path.join(venv_dir, bins, "python"), os.path.join(thin_dir, "salt-call"), "--version", ] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() proc.wait() assert proc.returncode == 0, (stdout, stderr, proc.returncode)
def make_venv(self, mode): if not os.path.exists(self.subpath("venvs")): os.mkdir(self.subpath("venvs")) venv_dir = self.subpath("venvs/%s" % mode) # python3 on OS-X uses a funky two-part executable and an environment # variable to communicate between them. If this variable is still set # by the time a virtualenv's 'pip' or 'python' is run, and if that # command spawns another sys.executable underneath it, that second # child may use the wrong python, and can install things into the # real system library instead of the virtualenv. Invoking # virtualenv.create_environment() clears this as a side-effect, but # to make things safe I'll just clear this now. See # https://github.com/pypa/virtualenv/issues/322 and # https://bugs.python.org/issue22490 for some hints. I tried # switching to 'venv' on py3, but only py3.4 includes pip, and even # then it's an ancient version. os.environ.pop("__PYVENV_LAUNCHER__", None) virtualenv.logger = virtualenv.Logger([]) # hush # virtualenv causes DeprecationWarning/ResourceWarning on py3 with warnings.catch_warnings(): warnings.simplefilter("ignore") virtualenv.create_environment(venv_dir) self.run_in_venv(venv_dir, venv_dir, 'pip', 'install', '-U', 'pip', 'wheel', 'packaging') return venv_dir
def main(): if not requirements_modified_time > environment_modified_time: print 'Environment already up-to-date' return import virtualenv import subprocess if path.exists(VE_ROOT): shutil.rmtree(VE_ROOT) virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(VE_ROOT, site_packages=False) # check requirements if not path.exists(PIP_CACHE_ROOT): os.mkdir(PIP_CACHE_ROOT) PIP_BASE = [VE_PIP, 'install', '-q', '-i', 'https://simple.crate.io/', '--extra-index-url', 'http://e.pypi.python.org/simple', '--download-cache=%s' % (PIP_CACHE_ROOT,),] for req in REQUIREMENTS: subprocess.call(PIP_BASE + ['-r', req]) file(VE_TIMESTAMP, 'w').close()
def create_virtualenv(venv, version=VENV_VERSION, base_url=VENV_URL, srcdir=None): venv = expand(venv) venv_tgz = 'virtualenv-{}.tar.gz'.format(version) src_is_temp = False if path.exists(path.join(venv, 'bin', 'activate')): log.info('virtualenv {} already exists'.format(venv)) else: try: # raise ImportError import virtualenv if LooseVersion(virtualenv.__version__) < LooseVersion(version): raise ImportError log.info('using system version of virtualenv') except ImportError: log.info('downloading and extracting virtualenv source to {}'.format(srcdir)) src_is_temp = not srcdir srcdir = srcdir or tempfile.mkdtemp() archive = fetch(path.join(base_url, venv_tgz), dest_dir=srcdir) with tarfile.open(archive, 'r') as tfile: tfile.extractall(srcdir) virtualenv = imp.load_source( 'virtualenv', path.join(archive.replace('.tar.gz', ''), 'virtualenv.py')) log.info('creating virtualenv {}'.format(venv)) virtualenv.create_environment(venv) if src_is_temp: shutil.rmtree(srcdir)
def setup_virtualenv(): """Set up virtualenv with flake8-diff.""" virtualenv.create_environment(VENV_PATH) check_call([ PIP_PATH, "install", "flake8=={0}".format(FLAKE8_VERSION), "flake8-diff=={0}".format(FLAKE8_DIFF_VERSION) ])
def auto_create(self): assert not self._in_virtual_env # Create the virtual environment if the folder is missing or empty if not os.path.isfile(self._venv_python): sys.stdout.write('Creating virtual environment...\n') if venv: ctx = venv.EnvBuilder(with_pip=True) ctx.create(self._venv_dir) else: virtualenv.create_environment(self._venv_dir) # Skip if the requirements and ezvirtualenv files are unchanged. cache_key = self._get_cache_key() if os.path.isfile(self._venv_cache_path): with open(self._venv_cache_path) as f: if cache_key == f.read().strip(): return self._refresh_requirements() self._copy_ezvirtualenv() # Update the cache with open(self._venv_cache_path, 'w') as f: f.write(cache_key)
def test_dist_missing_data(testdir): venv_path = os.path.join(str(testdir.tmpdir), 'venv') virtualenv.create_environment(venv_path) if sys.platform == 'win32': exe = os.path.join(venv_path, 'Scripts', 'python.exe') else: exe = os.path.join(venv_path, 'bin', 'python') subprocess.check_call([ exe, '-mpip' if sys.version_info >= (2, 7) else '-mpip.__main__', 'install', 'py==%s' % py.__version__, 'pytest==%s' % pytest.__version__ ]) script = testdir.makepyfile(SCRIPT) result = testdir.runpytest('-v', '--cov=%s' % script.dirpath(), '--cov-report=term-missing', '--dist=load', '--tx=popen//python=%s' % exe, '--max-slave-restart=0', script) result.stdout.fnmatch_lines([ '*- coverage: failed slaves -*' ]) assert result.ret == 0
def test_pypy_pre_import(tmp_path): """For PyPy, some built-in modules should be pre-imported because some programs expect them to be in sys.modules on startup. """ check_code = inspect.getsource(check_pypy_pre_import) check_code = textwrap.dedent(check_code[check_code.index("\n") + 1 :]) if six.PY2: check_code = check_code.decode() check_prog = tmp_path / "check-pre-import.py" check_prog.write_text(check_code) ve_path = str(tmp_path / "venv") virtualenv.create_environment(ve_path) bin_dir = virtualenv.path_locations(ve_path)[-1] try: cmd = [ os.path.join(bin_dir, "{}{}".format(virtualenv.EXPECTED_EXE, ".exe" if virtualenv.IS_WIN else "")), str(check_prog), ] subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as exception: assert not exception.returncode, exception.output
def install(self): # Create a new virtualenv in the temp install location import virtualenv virtualenv.create_environment( self.install_dir, site_packages=False ) # chdir into the pecan source os.chdir(pkg_resources.get_distribution('pecan').location) py_exe = os.path.join(self.install_dir, 'bin', 'python') pecan_exe = os.path.join(self.install_dir, 'bin', 'pecan') # env/bin/python setup.py develop (pecan) subprocess.check_call([ py_exe, 'setup.py', 'develop' ]) # create the templated project os.chdir(self.install_dir) subprocess.check_call([pecan_exe, 'create', 'Testing123']) # move into the new project directory and install os.chdir('Testing123') subprocess.check_call([ py_exe, 'setup.py', 'develop' ])
def create_if_not_exists(self): python = os.path.join(self.path, 'bin', 'python') if not os.path.exists(python): makedirs(self.path) print style('Creating Python virtualenv', 'blue', bold=True), style(self.path, bold=True) virtualenv.create_environment(self.path, no_setuptools=True, no_pip=True) if not os.path.exists(python + '-config'): names = ( 'python%d.%d-config' % sys.version_info[:2], 'python%d-config' % sys.version_info[0], 'python-config', ) prefix = getattr(sys, 'real_prefix', sys.prefix) for name in names: old_path = os.path.join(prefix, 'bin', name) if os.path.exists(old_path): for name in names: new_path = os.path.join(self.path, 'bin', name) self.rewrite_shebang_or_link(old_path, new_path) break else: log.warning('Could not find python-config')
def __init__(self, binPath, profilePath, password=None): self.venvDir = tempfile.mkdtemp() self.leafName = os.path.basename(__file__) self.binDir = os.path.join(self.venvDir, 'bin') self.profilePath = profilePath self.password = password self.subproc = None # create the virtualenv virtualenv.create_environment(self.venvDir, site_packages=True, never_download=True, no_pip=True, no_setuptools=True ) # copy libraries if sys.platform == "linux2": dllfiles = "*.so" elif sys.platform == "darwin": dllfiles = "*.dylib" elif sys.platform == "win32": dllfiles = "*.dll" files = glob.glob(os.path.join(binPath, dllfiles)) if not len(files): raise Exception("Could not find libraries in " + binPath) for filename in files: shutil.copy(filename, self.binDir) # copy our script shutil.copy(__file__, self.binDir)
def _create(self, clear=False): if clear: self.location.rmtree() if self._template: # On Windows, calling `_virtualenv.path_locations(target)` # will have created the `target` directory... if sys.platform == 'win32' and self.location.exists(): self.location.rmdir() # Clone virtual environment from template. self._template.location.copytree(self.location) self._sitecustomize = self._template.sitecustomize self._user_site_packages = self._template.user_site_packages else: # Create a new virtual environment. if self._venv_type == 'virtualenv': _virtualenv.create_environment( self.location, no_pip=True, no_wheel=True, no_setuptools=True, ) self._fix_virtualenv_site_module() elif self._venv_type == 'venv': builder = _venv.EnvBuilder() context = builder.ensure_directories(self.location) builder.create_configuration(context) builder.setup_python(context) self.site.makedirs() self.sitecustomize = self._sitecustomize self.user_site_packages = self._user_site_packages
def main(): ap = argparse.ArgumentParser() ap.add_argument("env") ap.add_argument("-w", "--workdir", dest="workdir") args = ap.parse_args() env_path = os.path.join(ENVS_PATH, args.env) if not os.path.isdir(env_path): if raw_input( "%s (for %s) does not exist. Create new virtualenv? [y/n] " % (env_path, args.env)) == "y": virtualenv.logger = virtualenv.Logger([(virtualenv.Logger.NOTIFY, sys.stdout)]) virtualenv.create_environment(env_path, site_packages=True, unzip_setuptools=True) else: print "Abort." sys.exit(1) if args.workdir: workdir = os.path.realpath(args.workdir) if os.path.isdir(workdir): print "Setting environment %s workdir to '%s'." % (args.env, workdir) file(os.path.join(env_path, "workdir"), "wb").write(workdir) activation_script_path = os.environ.get("VW_ACTSCRIPT_PATH") if not activation_script_path: print "VW_ACTSCRIPT_PATH not set, not creating activation script" else: with file(activation_script_path, "wb") as actscript: actscript.write(gen_activation_script(env_path))
def test_activate_with_powershell(tmpdir, monkeypatch): monkeypatch.chdir(tmpdir) home_dir, _, __, bin_dir = virtualenv.path_locations( str(tmpdir.join("env"))) virtualenv.create_environment(home_dir, no_pip=True, no_setuptools=True, no_wheel=True) monkeypatch.chdir(home_dir) activate_script = join(bin_dir, "activate.ps1") cmd = [ POWER_SHELL, "-Command", "{0}; {1}; {0}; deactivate; {0}".format(print_python_exe_path(), activate_script) ] output = subprocess.check_output(cmd, universal_newlines=True, stderr=subprocess.STDOUT) content = output.split() assert len(content) == 3, output before_activate, after_activate, after_deactivate = content exe = "{}.exe".format(virtualenv.expected_exe ) if virtualenv.is_win else virtualenv.expected_exe assert normcase(long_path(after_activate)) == normcase( long_path(join(bin_dir, exe))) assert before_activate == after_deactivate
def install(package): if not os.path.exists('.env'): print('Creating virtualenv') create_environment('.env', never_download=True) print('Installing %s' % package) pip.main(['install', package, '-t', os.environ['PWD'] + '/.env/lib/python2.7/site-packages/','--log-file', '.env/ppm.log'])
def create_virtual_env(project_path, install_path): # remove existing virtual env if exists ve_path = os.path.join(project_path, cfg.VIRTUAL_ENV_PATH) if os.path.exists(ve_path): shutil.rmtree(ve_path) try: logger.info('creating virtual env') virtualenv.create_environment(ve_path) except Exception: logger.exception('failed to create virtualenv: ') raise Exception('failed to create virtualenv!') try: logger.info('installing requirements for virtualenv') # update pip to latest version run_command(['{}/ve/bin/pip'.format(project_path), 'install', '-U', 'pip']) if not os.path.exists('{}/requirements.txt'.format(project_path)): logger.warning('requirements.txt not found') return ve_path run_command(['{}/ve/bin/pip'.format(project_path), 'install', '-r', '{}/requirements.txt'.format(project_path)]) virtualenv.make_environment_relocatable(ve_path) fixup_scripts(install_path, ve_path) except Exception: logger.exception('failed to install requirements! error message:') raise Exception('fail to install requirements.') return ve_path
def virtualenv(request): """ Create a virtualenv and ``chdir`` into it. Remove it and ``chdir`` into the previous working directory again when the test has been run. """ import virtualenv from virtualenv import Logger # create a temp directory cwd = os.getcwd() virtualenv_directory = mkdtemp() # install a virtualenv logger = Logger([(Logger.level_for_integer(2), sys.stdout)]) virtualenv.logger = logger virtualenv.create_environment( virtualenv_directory, site_packages=False, clear=True, unzip_setuptools=True) # chdir into the virtualenv directory os.chdir(virtualenv_directory) # update setuptools in the virtualenv subprocess.check_call([ os.path.join('bin', 'pip'), 'install', '-U', 'setuptools>=17.1']) # install requirements.txt into the virtualenv subprocess.check_call([ os.path.join('bin', 'pip'), 'install', '-r', os.path.join(cwd, 'requirements.txt')]) # also install psycopg2 and oursql subprocess.check_call([ os.path.join('bin', 'pip'), 'install', 'psycopg2', 'oursql']) # setuptools-git is required to be able to call setup.py install # sucessfully. subprocess.check_call([ os.path.join('bin', 'pip'), 'install', 'setuptools-git']) shutil.copytree(cwd, os.path.join(virtualenv_directory, 'kotti')) # install Kotti into the virtualenv os.chdir('kotti') subprocess.check_call([ os.path.join('..', 'bin', 'python'), 'setup.py', 'develop']) os.chdir('..') def delete_virtualenv(): shutil.rmtree(virtualenv_directory) os.chdir(cwd) request.addfinalizer(delete_virtualenv)
def main(): p = argparse.ArgumentParser(usage=__doc__.strip()) args = p.parse_args() try: import virtualenv except ImportError: print("ERROR: You need to install Virtualenv (https://pypi.python.org/pypi/virtualenv) first") sys.exit(0) try: find_python() except RuntimeError: print("\n-- Creating virtualenv in deploy/env") print("$ virtualenv %s" % VENV_DIR) virtualenv.create_environment(VENV_DIR) python_bin, easy_install_bin = find_python() os.chdir(ROOT_DIR) print("\n-- Installing required modules") with open("requirements.txt", "r") as f: requirements = [x.strip() for x in f.read().splitlines() if x.strip()] if sys.platform.startswith("win"): # Download the windows binary requirements = [MERCURIAL_WIN_BINARY if x.lower().startswith("mercurial") else x for x in requirements] run_cmd([easy_install_bin] + requirements) print("\n-- Setting up development database") print("$ cd deploy") os.chdir(DEPLOY_DIR) run_cmd([python_bin, "manage.py", "syncdb"]) run_cmd([python_bin, "manage.py", "migrate"]) run_cmd([python_bin, "manage.py", "loaddata", "base"]) run_cmd([python_bin, "manage.py", "loaddata", "sample"]) run_cmd([python_bin, "manage.py", "rebuild_index"]) print( r""" -- All done! Now you can do: * Linux & OSX: cd deploy source env/bin/activate python manage.py runserver * Windows: cd deploy env\Scripts\activate python manage.py runserver """ )
def install_virtualenv(install_dir): _ensure_distutils_config(install_dir) if is_virtualenv(install_dir): return create_environment(install_dir, no_setuptools=False, no_pip=True, site_packages=False, symlink=False)
def virtualenv(): "Prepares a checked out directory for development" if not os.path.exists(os.path.join('bin', 'pip')): sys.path.insert(0, os.path.join('deps', 'virtualenv.zip')) import virtualenv virtualenv.create_environment('.') else: print('Virtualenv already set up')
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 setup(): global testenv_dir ip = get_ipython() #creating a testing virtualenv testenv_dir = TemporaryDirectory() os.environ['WORKON_HOME'] = testenv_dir.name virtualenv.create_environment(os.path.join(testenv_dir.name,'testenv')) ip.extension_manager.load_extension('virtualenvmagic')
def venv(self, reason): path = self.useFixture(fixtures.TempDir()).path virtualenv.create_environment(path, clear=True) python = os.path.join(path, 'bin', 'python') self.useFixture(base.CapturedSubprocess( 'mkvenv-' + reason, [python] + PIP_CMD + [ '-U', PIPVERSION, 'wheel', PBRVERSION])) return path, python
def setup(self): BaseFixture.setup(self) with VIRTUALENV_LOCK: virtualenv.create_environment(self.env_dir) self._debian_fix() self.env_install(['pip==8.0.2'])
def create(cls, path=None, create_if_not_exist=False): if path is None: path = tempfile.mkdtemp() if create_if_not_exist and not os.path.exists(path): os.mkdirs(path) ve = cls(path) virtualenv.create_environment(path) return ve