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 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 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 check_ve(project_root, argv): VE_ROOT = path.join(project_root, '.ve') VE_TIMESTAMP = path.join(VE_ROOT, 'timestamp') REQUIREMENTS = path.join(project_root, 'requirements.pip') envtime = path.exists(VE_ROOT) and path.getmtime(VE_ROOT) or 0 envreqs = path.exists(VE_TIMESTAMP) and path.getmtime(VE_TIMESTAMP) or 0 envspec = path.getmtime(REQUIREMENTS) def go_to_ve(ve_root): # going into ve if not ve_root in sys.prefix: retcode = 3 while retcode == 3: env = os.environ if sys.platform == 'win32': python = path.join(VE_ROOT, 'Scripts', 'python.exe') elif sys.platform == 'darwin': # temporary fix for broken virtualenv in macports import airy env["PYTHONPATH"] = "%s:" % path.join( path.abspath(path.dirname(airy.__file__)), '..') + env.get('PYTHONPATH', '') python = path.join(VE_ROOT, 'bin', 'python') else: python = path.join(VE_ROOT, 'bin', 'python') try: retcode = subprocess.call( [python, path.join(project_root, 'manage.py')] + argv[1:]) except KeyboardInterrupt: retcode = 1 sys.exit(retcode) update_ve = 'update_ve' in argv if update_ve or envtime < envspec or envreqs < envspec: if update_ve: # install ve if envtime < envspec: if path.exists(VE_ROOT): shutil.rmtree(VE_ROOT) virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(VE_ROOT, site_packages=True) go_to_ve(VE_ROOT) # check requirements if update_ve or envreqs < envspec: import pip pip.main( initial_args=['install', '-r', REQUIREMENTS, '--upgrade']) file(VE_TIMESTAMP, 'w').close() sys.exit(0) else: print "VirtualEnv needs to be updated" print "Run 'python manage.py update_ve'" sys.exit(1) go_to_ve(VE_ROOT)
def setup_virtualenv(self, home_dir=os.curdir, bootstrap=None, **opts): """ Set up a virtualenv in the `directory` with options. If a `bootstrap` file is provided or the `virtualenv_script` exists, it is run as a script with positional `args` inserted into `sys.argv`. Otherwise, `virtualenv` is imported and `create_environment()` is called with any kwargs. Following the run of this command, dependencies can automatically be installed with the develop command. """ if bootstrap: virtualenv_globals = dict(__file__=bootstrap) execfile(bootstrap, virtualenv_globals) argv = [bootstrap] if self.verbose == 0: argv.append('--quiet') elif self.verbose == 2: argv.append('--verbose') for option, value in opts.iteritems(): argv.extend(['--' + option, value]) argv.append(home_dir) self.logger.info( 'Setting up a isolated Python with bootstrap script: {0}'. format(' '.join(argv))) orig_argv = sys.argv[:] try: sys.argv[:] = argv virtualenv_globals['main']() finally: sys.argv[:] = orig_argv else: try: import virtualenv except ImportError: raise errors.DistutilsModuleError( 'The virtualenv module must be available if no virtualenv ' 'bootstrap script is given: {0}'.format(bootstrap)) self.logger.info('Setting up a isolated Python with module: ' '{0}.create_environment({1} {2})'.format( virtualenv, repr(home_dir), ' '.join('{0}={1}'.format(item) for item in opts.items()))) virtualenv.logger = virtualenv.Logger([ (virtualenv.Logger.level_for_integer(2 - self.verbose), sys.stdout) ]) virtualenv.create_environment(home_dir, **opts) return os.path.join( sysconfig.get_path('scripts', vars=dict(base=home_dir)), 'python' + sysconfig.get_config_var('EXE'))
def reset_python_environment(self): """ Reset the python environment. """ path = self.root_path if not exists(path): return False virtualenv.logger = virtualenv.Logger([ (virtualenv.Logger.level_for_integer(2), sys.stdout) ]) virtualenv.create_environment(path, site_packages=False) return True
def ensure_virtualenv_exists(self, full_rebuild): # if we need to create the virtualenv, then we must do that from # outside the virtualenv. The code inside this if statement should 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): if not self.check_current_python_version(): print "Running wrong version of python for virtualenv creation" return 1 import virtualenv virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(self.ve_dir, site_packages=False) return 0
def install_environment(root): """Install our virtual environment; removing the old one if it exists""" sys.stdout.write('Installing virtualenv into %s \n' % root) try: import virtualenv except ImportError: sys.stdout.write('Installing virtualenv into global interpreter \n') subprocess.call([VE_GLOBAL_SCRIPT, PROJECT_ROOT]) import virtualenv if path.exists(root): shutil.rmtree(root) virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(root, site_packages=False) ret_code = subprocess.call([VE_SCRIPT, PROJECT_ROOT, root]) sys.exit(ret_code)
def install_virtualenv_p2(root, python_version): """Install virtual environment for Python 2.7+; removing the old one if it exists""" try: import virtualenv except ImportError: sys.stdout.write('Installing virtualenv into global interpreter \n') ret_code = subprocess.call([VE_GLOBAL_SCRIPT, PROJECT_ROOT]) sys.stdout.write('Installation finished with code {0}. Re-run ./launch.py install \n'.format(ret_code)) sys.exit(ret_code) if path.exists(root): shutil.rmtree(root) virtualenv.logger = virtualenv.Logger(consumers=[]) virtualenv.create_environment(root, site_packages=False) ret_code = subprocess.call([VE_SCRIPT, PROJECT_ROOT, root, python_version]) sys.exit(ret_code)
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) if self.pypi_cache_url is not None: pypi_cache_args = ['-i', self.pypi_cache_url] else: pypi_cache_args = [] # install the pip requirements and exit pip_path = path.join(self.ve_dir, 'bin', 'pip') # first ensure we have an up to date version of distribute command = [pip_path, 'install', '-U', 'distribute'] + pypi_cache_args try: pip_retcode = subprocess.call(command) except OSError, e: print "command failed: %s: %s" % (" ".join(command), e) return 1
def create_environment(): dest_dir = os.path.join(DIRNAME, CONFIG['virtualenv']['dest_dir']) dest_dir = os.path.abspath(dest_dir) # Create new virtual environment print('Step 1. Create new virtual environment') if not os.path.isdir(dest_dir) or CONFIG['virtualenv']['clear']: kwargs = copy.copy(CONFIG['virtualenv']) kwargs['home_dir'] = kwargs['dest_dir'] verbosity = int(kwargs['verbose']) - int(kwargs['quiet']) logger = virtualenv.Logger([ (virtualenv.Logger.level_for_integer(2 - verbosity), sys.stdout), ]) del kwargs['dest_dir'], kwargs['quiet'], kwargs['verbose'] virtualenv.logger = logger virtualenv.create_environment(**kwargs) else: print('Virtual environment %r already exists.' % \ CONFIG['virtualenv']['dest_dir'])
def create_environment(path, config_ori): """Create environment structure. """ # Archivo de configuracion destino config_dst = join(path, 'etc', config_filename) if not exists(path) or not listdir(path): # Crea el ambiente python virtualenv.logger = virtualenv.Logger([ (virtualenv.Logger.level_for_integer(2), sys.stdout) ]) virtualenv.create_environment(path, site_packages=False) # Crea el directorio donde va el archivo de configuracion makedirs(dirname(config_dst)) # Descarga el archivo de configuracion environment.yml urlretrieve(config_ori, config_dst) # Prepara el ambiente Odoo env = OdooEnvironment(path) env.setup() return env
import os from argparse import ArgumentParser from pin import registry, event, hook, PROJECT_FOLDER from pin.event import eventhook from pin.util import * import virtualenv virtualenv.logger = virtualenv.Logger(consumers=[]) VENV_FOLDER = 'env' class VirtualEnvPinHook(hook.PinHook): '''Add new arguments to core init command for creating virtualenvs''' name = "venv" def __init__(self): self.options = None def write_env_path_file(self, root, venv): path_file = os.path.join(root, '.pin', 'venv.pth') with open(path_file, 'w') as file: file.write(venv) @eventhook('init-post-parser') def init_post_parser(self, parser): parser.add_argument( '--mkenv', nargs="?", default=False,
class Runner(object): """Cmdtool runner. >>> runner = Runner() >>> class Mock(object): ... def __init__(self, **kw): ... self.__dict__.update(kw) >>> class MockSecurity(object): ... def update_user(self, *args, **kwargs): pass ... def update_roles(self, *args, **kwargs): pass >>> runner.pypi_factory = lambda x, y: Mock( ... security_manager=MockSecurity()) >>> runner.main([], []) Usage: setup.py <cmd> [<arg1>, <arg2>...] <BLANKLINE> Where <cmd> can be: setupindex <distro_id> <indexname> <eggreq1> [<eggreq2> ...] updateuser <username> <password> <email> [<role1> <role2> ...] updategroup <groupname> [<role1> <role2> ...] updateusersgroups <username> <group1> [<group2> ...] addfile <distro_id> <filename> addindexentry <distro_id> <indexname> <target_distro_id> <target_distro_version> delindexentry <distro_id> <indexname> <target_distro_id> <BLANKLINE> >>> runner.main(['updateuser', 'foo', 'bar', 'abc', 'role1']) """ pypi_factory = staticmethod(InsecurePyPi) def split_roles(self, *args): roles = {} for arg in args: s = arg.split(':') distro_id = '' if len(s) == 2: distro_id = s[0] role = s[1] else: role = s[0] l = roles.get(distro_id, None) if l is None: l = set() roles[distro_id] = l l.add(role) return roles def main(self, args=None, extraargs=None): logging.basicConfig() usage = """%prog <cmd> [<arg1>, <arg2>...] Where <cmd> can be: setupindex <distro_id> <indexname> <eggreq1> [<eggreq2> ...] updateuser <username> <password> <email> [<role1> <role2> ...] updategroup <groupname> [<role1> <role2> ...] updateusersgroups <username> <group1> [<group2> ...] addfile <distro_id> <filename> addindexentry <distro_id> <indexname> <target_distro_id> <target_distro_version> delindexentry <distro_id> <indexname> <target_distro_id>""" parser = optparse.OptionParser(usage=usage) if args is None: args = [] if extraargs is None: extraargs = sys.argv[1:] allargs = args + extraargs if len(allargs) < 1: parser.print_usage() return cmd = allargs[0] params = allargs[1:] pypi = self.pypi_factory('files', 'sqlite:///cluerelmgr.db') if cmd == 'updateuser': roledict = self.split_roles(*params[3:]) pypi.security_manager.update_user(params[0], params[1], params[2], roledict.get('', [])) for distro_id, roles in roledict.items(): pypi.security_manager.update_roles(distro_id=distro_id, username=params[0], roles=roles) elif cmd == 'updategroup': roledict = self.split_roles(*params[1:]) pypi.security_manager.update_group(params[0], roledict.get('', [])) for distro_id, roles in roledict.items(): pypi.security_manager.update_roles(distro_id=distro_id, groupname=params[0], roles=roles) elif cmd == 'updateusersgroups': username = params[0] pypi.security_manager.update_users_groups(params[0], params[1:]) elif cmd == 'addfile': def content(v): if v.startswith('http:') or v.startswith('https:'): return utils.UrlContent(v) return utils.FileContent(v) distro_id = params[0] files = [content(x) for x in params[1:]] pypi.upload_files(distro_id, files) elif cmd == 'addindexentry': distro_id = params[0] indexname = params[1] target_distro_id = params[2] target_version = params[3] pypi.index_manager.add_index_item(distro_id, indexname, target_distro_id, target_version) elif cmd == 'delindexentry': distro_id = params[0] indexname = params[1] target_distro_id = params[2] pypi.index_manager.del_index_item(distro_id, indexname, target_distro_id) elif cmd == 'setupindex': parser = optparse.OptionParser() parser.add_option('-f', '--overwrite', dest='overwrite', action='store_true', help='Force overwrite', default=False) options, args = parser.parse_args(params) self.setupindex(pypi, args[0], args[1], args[2:], overwrite=options.overwrite) else: print "No such command: %s" % cmd def setupindex(self, pypi, distro_id, indexname, reqs, overwrite=False): if len(reqs) == 0: raise ValueError('Please specify one or more requirements') if not overwrite and pypi.index_manager.has_index( distro_id, indexname): raise ValueError('Index by the name of "%s" for distro "%s" ' 'already exists, use --overwrite to overwrite' % (indexname, distro_id)) try: d = pkg_resources.get_distribution('virtualenv') except pkg_resources.DistributionNotFound, e: raise EnvironmentError('Please install "virtualenv"') import virtualenv path = tempfile.mkdtemp() print 'Using "%s" for temporary virtualenv' % path if overwrite: print 'Will overwrite index it if already exists' virtualenv.logger = virtualenv.Logger([(logging.ERROR, sys.stdout)]) virtualenv.create_environment(path) easy_install_path = os.path.join(path, 'bin', 'easy_install') subprocess.call([easy_install_path, 'pip']) pip_path = os.path.join(path, 'bin', 'pip') subprocess.call([pip_path, 'install'] + list(reqs)) froze = subprocess.Popen([pip_path, 'freeze'], stdout=subprocess.PIPE).communicate()[0] print if overwrite and pypi.index_manager.has_index(distro_id, indexname): pypi.index_manager.remove_index(distro_id, indexname) print 'Removed existing index' index_reqs = [x.strip() for x in froze.split('\n') if x.strip()] for x in index_reqs: req = pkg_resources.Requirement.parse(x) pypi.index_manager.add_index_item(distro_id, indexname, req.project_name, req.specs[0][1]) print 'Created index "%s" for distro "%s" with the ' \ 'following requirements' % (distro_id, indexname) for x in index_reqs: print ' ', x.strip()
# under the License. """Installer for Bloodhound - depends on the supplied requirements.txt file to determine the installation packages""" import os from optparse import OptionParser import subprocess import platform import sys from getpass import getpass import virtualenv from createdigest import htdigest_create if not hasattr(virtualenv, 'logger'): virtualenv.logger = virtualenv.Logger([(virtualenv.Logger.LEVELS[-1], sys.stdout)]) DEFAULT_DB_USER = '******' DEFAULT_DB_NAME = 'bloodhound' DEFAULT_ADMIN_USER = '******' BASE_CONFIG = """ [components] bhtheme.* = enabled bhdashboard.* = enabled multiproduct.* = enabled permredirect.* = enabled themeengine.* = enabled trac.ticket.web_ui.ticketmodule = disabled trac.ticket.report.reportmodule = disabled