Exemple #1
0
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
Exemple #2
0
    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)
Exemple #3
0
    def install(self, name, version=None, develop=False, upgrade=False, install_options=None):
        """
        install is used when installing a python package into the environment.

        if version is set, the specified version of the package will be installed.
        The specified version should be a full `PEP 440`_ version specifier (i.e. "==1.2.0")

        .. _`PEP 440`: https://www.python.org/dev/peps/pep-0440/

        if develop is set to True, the package will be installed as editable: the source
        in the directory passed will be used when using that package.

        if install_options is provided, it should be a list of options, like
        ["--prefix=/opt/srv", "--install-lib=/opt/srv/lib"]
        """
        if self._is_package_already_installed(name, version):
            return
        p_assert(
            not (develop and version),
            "unable to set both version and develop flags when installing packages"
        )
        if name in self.versions:
            if version is None:
                version = self.versions[name]
            del self.versions[name]
        req_set = install(
            name, upgrade=upgrade, develop=develop, version=version,
            index_urls=self.index_urls, constraint_dict=self.versions,
            packages_config=self.config,
            install_options=install_options
        )
        if req_set:
            for req in req_set.requirements.values():
                # Don't examine packages that weren't modified
                if not req.install_succeeded:
                    continue
                # Retrieving a package's installed version is time consuming.
                # Prefer the calculated package specifier if available, and use
                # the installed version if not.
                new_constraint = str(req.specifier) if req.specifier else None
                if not new_constraint:
                    installed_version = req.installed_version
                    if installed_version:
                        new_constraint = '=={}'.format(installed_version)
                if new_constraint:
                    self.versions[req.name] = new_constraint
        # if virtualenv dir is set, we should make the environment relocatable.
        # this will fix issues with commands not being usable by the
        # uranium via build.executables.run
        if self._virtualenv_dir:
            virtualenv.make_environment_relocatable(self._virtualenv_dir)
        # there's a caveat that requires the site packages to be reloaded,
        # if the package is a develop package. This is to enable
        # immediately consuming the package after install.
        self._reimport_site_packages()
        # invalidate the finder's cache, to ensure new modules are
        # picked up
        invalidate_caches()
Exemple #4
0
def activate_env(env, deps, quiet=False):
    if hasattr(sys, 'real_prefix'):
        LOGGER.error('Already activated environment!')
        return

    if not quiet:
        print 'Activating environment: %r' % env
    assert isinstance(deps, dict)

    manifest_path = os.path.join(env, 'manifest.pyl')
    cur_deps = read_deps(manifest_path)
    if cur_deps != deps:
        if not quiet:
            print '  Removing old environment: %r' % cur_deps
        shutil.rmtree(env, ignore_errors=True)
        cur_deps = None

    if cur_deps is None:
        check_pydistutils()

        if not quiet:
            print '  Building new environment'
        # Add in bundled virtualenv lib
        sys.path.insert(0, os.path.join(os.path.dirname(__file__),
                                        'virtualenv'))
        import virtualenv  # pylint: disable=F0401
        virtualenv.create_environment(
            env, search_dirs=virtualenv.file_search_dirs())

    if not quiet:
        print '  Activating environment'
    # Ensure hermeticity during activation.
    os.environ.pop('PYTHONPATH', None)
    bin_dir = 'Scripts' if sys.platform.startswith('win') else 'bin'
    activate_this = os.path.join(env, bin_dir, 'activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))

    if cur_deps is None:
        if not quiet:
            print '  Installing deps'
            print_deps(deps, indent=2, with_implicit=False)
        install(deps)
        virtualenv.make_environment_relocatable(env)
        with open(manifest_path, 'wb') as f:
            f.write(repr(deps) + '\n')

    # Create bin\python.bat on Windows to unify path where Python is found.
    if sys.platform.startswith('win'):
        bin_path = os.path.join(env, 'bin')
        if not os.path.isdir(bin_path):
            os.makedirs(bin_path)
        python_bat_path = os.path.join(bin_path, 'python.bat')
        if not os.path.isfile(python_bat_path):
            with open(python_bat_path, 'w') as python_bat_file:
                python_bat_file.write(PYTHON_BAT_WIN)

    if not quiet:
        print 'Done creating environment'
Exemple #5
0
def activate_env(env, deps, quiet=False):
  if hasattr(sys, 'real_prefix'):
    LOGGER.error('Already activated environment!')
    return

  if not quiet:
    print 'Activating environment: %r' % env
  assert isinstance(deps, dict)

  manifest_path = os.path.join(env, 'manifest.pyl')
  cur_deps = read_deps(manifest_path)
  if cur_deps != deps:
    if not quiet:
      print '  Removing old environment: %r' % cur_deps
    shutil.rmtree(env, ignore_errors=True)
    cur_deps = None

  if cur_deps is None:
    check_pydistutils()

    if not quiet:
      print '  Building new environment'
    # Add in bundled virtualenv lib
    sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'virtualenv'))
    import virtualenv  # pylint: disable=F0401
    virtualenv.create_environment(
        env, search_dirs=virtualenv.file_search_dirs())

  if not quiet:
    print '  Activating environment'
  # Ensure hermeticity during activation.
  os.environ.pop('PYTHONPATH', None)
  bin_dir = 'Scripts' if sys.platform.startswith('win') else 'bin'
  activate_this = os.path.join(env, bin_dir, 'activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))

  if cur_deps is None:
    if not quiet:
      print '  Installing deps'
      print_deps(deps, indent=2, with_implicit=False)
    install(deps)
    virtualenv.make_environment_relocatable(env)
    with open(manifest_path, 'wb') as f:
      f.write(repr(deps) + '\n')

  # Create bin\python.bat on Windows to unify path where Python is found.
  if sys.platform.startswith('win'):
    bin_path = os.path.join(env, 'bin')
    if not os.path.isdir(bin_path):
      os.makedirs(bin_path)
    python_bat_path = os.path.join(bin_path, 'python.bat')
    if not os.path.isfile(python_bat_path):
      with open(python_bat_path, 'w') as python_bat_file:
        python_bat_file.write(PYTHON_BAT_WIN)

  if not quiet:
    print 'Done creating environment'
Exemple #6
0
 def _finalize(self):
     virtualenv.make_environment_relocatable(self._root)
     activate_content = ""
     activate_content += self.envvars.generate_activate_content()
     write_activate_this(self._root, additional_content=activate_content)
     self.history[HISTORY_KEY] = {
         "python_version": "{0}.{1}".format(*sys.version_info[:2])
     }
     self.history.save()
Exemple #7
0
def create_virtualenv(bundle_path, requirements_path):
    import compileall
    import pip
    import virtualenv
    scripts_path = os.path.join(bundle_path, 'Contents', 'Scripts')
    virtualenv.create_environment(scripts_path, site_packages=True)
    virtualenv.make_environment_relocatable(scripts_path)
    pip.main(['install', '--prefix', scripts_path, '-r', requirements_path])
    compileall.compile_dir(scripts_path, maxlevels=0)
Exemple #8
0
def activate_env(env, manifest, quiet=False):
    if not quiet:
        print 'Activating environment: %r' % env
    assert isinstance(manifest, dict)

    manifest_path = os.path.join(env, 'manifest.pyl')
    cur_manifest = read_python_literal(manifest_path)
    if cur_manifest != manifest:
        if not quiet:
            print '  Removing old environment: %r' % cur_manifest
        shutil.rmtree(env, ignore_errors=True)
        cur_manifest = None

    if cur_manifest is None:
        check_pydistutils()

        if not quiet:
            print '  Building new environment'
        # Add in bundled virtualenv lib
        sys.path.insert(
            0, os.path.join(os.path.dirname(__file__), 'virtualenv-ext'))
        import virtualenv  # pylint: disable=F0401
        virtualenv.create_environment(
            env, search_dirs=virtualenv.file_search_dirs())

    if not quiet:
        print '  Activating environment'
    # Ensure hermeticity during activation.
    os.environ.pop('PYTHONPATH', None)
    bin_dir = 'Scripts' if sys.platform.startswith('win') else 'bin'
    activate_this = os.path.join(env, bin_dir, 'activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))

    if cur_manifest is None:
        deps = manifest.get('deps', {})
        if not quiet:
            print '  Installing deps'
            print_deps(deps, indent=2, with_implicit=False)
        install(deps)
        virtualenv.make_environment_relocatable(env)

        # Write the original deps (including metadata) as manifest.
        with open(manifest_path, 'wb') as f:
            f.write(repr(manifest) + '\n')

    # Create bin\python.bat on Windows to unify path where Python is found.
    if sys.platform.startswith('win'):
        bin_path = os.path.join(env, 'bin')
        if not os.path.isdir(bin_path):
            os.makedirs(bin_path)
        python_bat_path = os.path.join(bin_path, 'python.bat')
        if not os.path.isfile(python_bat_path):
            with open(python_bat_path, 'w') as python_bat_file:
                python_bat_file.write(PYTHON_BAT_WIN)

    if not quiet:
        print 'Done creating environment'
Exemple #9
0
 def _finalize(self):
     virtualenv.make_environment_relocatable(self._root)
     activate_content = ""
     activate_content += self.envvars.generate_activate_content()
     write_activate_this(self._root, additional_content=activate_content)
     self.history[HISTORY_KEY] = {
         "python_version": "{0}.{1}".format(*sys.version_info[:2])
     }
     self.history.save()
def install_virtualenv(install_dir):
    """
    install a virtualenv to the desired directory,
    and returns the path to the executable.
    """
    if is_virtualenv(install_dir):
        return

    create_environment(install_dir, no_setuptools=False,
                       no_pip=False, site_packages=False,
                       symlink=False)
    make_environment_relocatable(install_dir)
    return os.path.exists(install_dir, "bin")
Exemple #11
0
def relocateable_ve(ve_dir):
    log.debug('Making virtualenv relocatable')
    virtualenv.make_environment_relocatable(ve_dir)

    # Make activate relocatable, using approach taken in
    # https://github.com/pypa/virtualenv/pull/236
    activate = []
    with open(os.path.join(ve_dir, 'bin', 'activate')) as f:
        for line in f:
            line = line.strip()
            if line == 'deactivate () {':
                activate += ['ACTIVATE_PATH_FALLBACK="$_"', '']
            if line.startswith('VIRTUAL_ENV='):
                activate += [
                    '# attempt to determine VIRTUAL_ENV in relocatable way',
                    'if [ ! -z "${BASH_SOURCE:-}" ]; then',
                    '    # bash',
                    '    ACTIVATE_PATH="${BASH_SOURCE}"',
                    'elif [ ! -z "${DASH_SOURCE:-}" ]; then',
                    '    # dash',
                    '    ACTIVATE_PATH="${DASH_SOURCE}"',
                    'elif [ ! -z "${ZSH_VERSION:-}" ]; then',
                    '    # zsh',
                    '    ACTIVATE_PATH="$0"',
                    'elif [ ! -z "${KSH_VERSION:-}" ] '
                         '|| [ ! -z "${.sh.version:}" ]; then',
                    '    # ksh - we have to use history, '
                                'and unescape spaces before quoting',
                    '    ACTIVATE_PATH="$(history -r -l -n | head -1 | sed -e '
                        '\'s/^[\\t ]*\\(\\.\\|source\\) *//;s/\\\\ / /g\')"',
                    'elif [ "$(basename "$ACTIVATE_PATH_FALLBACK")" == '
                           '"activate.sh" ]; then',
                    '    ACTIVATE_PATH="${ACTIVATE_PATH_FALLBACK}"',
                    'else',
                    '    ACTIVATE_PATH=""',
                    'fi',
                    '',
                    '# default to non-relocatable path',
                ]
            if line == 'export VIRTUAL_ENV':
                activate += [
                    'if [ ! -z "${ACTIVATE_PATH:-}" ]; then',
                    '    VIRTUAL_ENV="$(cd '
                        '"$(dirname "${ACTIVATE_PATH}")/.."; pwd)"',
                    'fi',
                    'unset ACTIVATE_PATH',
                    'unset ACTIVATE_PATH_FALLBACK',
                ]
            activate.append(line)
    with open(os.path.join(ve_dir, 'bin', 'activate'), 'w') as f:
        f.write('\n'.join(activate))
Exemple #12
0
def install_virtualenv(install_dir):
    """
    install a virtualenv to the desired directory,
    and returns the path to the executable.
    """
    if is_virtualenv(install_dir):
        return

    create_environment(install_dir,
                       no_setuptools=False,
                       no_pip=False,
                       site_packages=False,
                       symlink=False)
    make_environment_relocatable(install_dir)
    return os.path.exists(install_dir, "bin")
Exemple #13
0
 def _create(self, clear=False, pip_source_dir=None, relocatable=False):
     # Create the actual virtual environment
     _virtualenv.create_environment(
         self.location,
         clear=clear,
         download=False,
         no_pip=True,
         no_wheel=True,
     )
     _virtualenv.install_wheel([pip_source_dir or '.'],
                               self.bin.join("python"))
     if relocatable:
         _virtualenv.make_environment_relocatable(self.location)
     # FIXME: some tests rely on 'easy-install.pth' being already present.
     site_package = distutils.sysconfig.get_python_lib(prefix=self.location)
     Path(site_package).join('easy-install.pth').touch()
Exemple #14
0
 def _create(self, clear=False, pip_source_dir=None, relocatable=False):
     # Create the actual virtual environment
     _virtualenv.create_environment(
         self.location,
         clear=clear,
         download=False,
         no_pip=True,
         no_wheel=True,
     )
     _virtualenv.install_wheel([pip_source_dir or '.'],
                               self.bin.join("python"))
     if relocatable:
         _virtualenv.make_environment_relocatable(self.location)
     # FIXME: some tests rely on 'easy-install.pth' being already present.
     site_package = distutils.sysconfig.get_python_lib(prefix=self.location)
     Path(site_package).join('easy-install.pth').touch()
Exemple #15
0
    def install(self,
                name,
                version=None,
                develop=False,
                upgrade=False,
                install_options=None):
        """
        install is used when installing a python package into the environment.

        if version is set, the specified version of the package will be installed.
        The specified version should be a full `PEP 440`_ version specifier (i.e. "==1.2.0")

        .. _`PEP 440`: https://www.python.org/dev/peps/pep-0440/

        if develop is set to True, the package will be installed as editable: the source
        in the directory passed will be used when using that package.

        if install_options is provided, it should be a list of options, like
        ["--prefix=/opt/srv", "--install-lib=/opt/srv/lib"]
        """
        if self._is_package_already_installed(name, version):
            return
        p_assert(
            not (develop and version),
            "unable to set both version and develop flags when installing packages"
        )
        if name in self.versions:
            if version is None:
                version = self.versions[name]
            del self.versions[name]
        req_set = install(name,
                          upgrade=upgrade,
                          develop=develop,
                          version=version,
                          index_urls=self.index_urls,
                          constraint_dict=self.versions,
                          install_options=install_options)
        if req_set:
            for req in req_set.requirements.values():
                if req.installed_version:
                    self.versions[req.name] = ("==" + req.installed_version)
        # if virtualenv dir is set, we should make the environment relocatable.
        # this will fix issues with commands not being usable by the
        # uranium via build.executables.run
        if self._virtualenv_dir:
            virtualenv.make_environment_relocatable(self._virtualenv_dir)
Exemple #16
0
def create_virtual_env(project_path, install_path, ve_args):
    # 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')
        sys.argv = ['']
        if '--no-wheel' not in ve_args:
            sys.argv.append('--no-wheel')

        sys.argv.extend(ve_args)
        sys.argv.append(ve_path)
        try:
            virtualenv.main()
        except SystemExit as sysext:
            if sysext.code != 0:
                raise SystemExit(sysext)
    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(['{}/{}/bin/pip'.format(project_path,
                                            cfg.VIRTUAL_ENV_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(['{}/{}/bin/pip'.format(project_path,
                                            cfg.VIRTUAL_ENV_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
Exemple #17
0
def setup_virtualenv(env_path, relocatable=False):
  """Create a virtualenv in specified location.

  The virtualenv contains a standard Python installation, plus setuptools, pip
  and wheel.

  Args:
    env_path (str): where to create the virtual environment.

  """
  if os.path.exists(os.path.join(os.path.expanduser('~'), '.pydistutils.cfg')):
    raise GlycoSetupError('\n'.join([
      '',
      'You have a ~/.pydistutils.cfg file, which interferes with the ',
      'infra virtualenv environment. Please move it to the side and bootstrap ',
      'again. Once infra has bootstrapped, you may move it back.',
      '',
      'Upstream bug: https://github.com/pypa/virtualenv/issues/88/',
      ''
    ]))

  print 'Creating environment: %r' % env_path

  if os.path.exists(env_path):
    print '  Removing existing one...'
    shutil.rmtree(env_path, ignore_errors=True)

  print '  Building new environment...'
  # Import bundled virtualenv lib
  import virtualenv  # pylint: disable=F0401
  virtualenv.create_environment(
    env_path, search_dirs=virtualenv.file_search_dirs())

  if relocatable:
    print '  Make environment relocatable'
    virtualenv.make_environment_relocatable(env_path)

  print 'Done creating environment'
Exemple #18
0
def setup_virtualenv(env_path, relocatable=False):
    """Create a virtualenv in specified location.

  The virtualenv contains a standard Python installation, plus setuptools, pip
  and wheel.

  Args:
    env_path (str): where to create the virtual environment.

  """
    if os.path.exists(os.path.join(os.path.expanduser('~'),
                                   '.pydistutils.cfg')):
        raise GlycoSetupError('\n'.join([
            '',
            'You have a ~/.pydistutils.cfg file, which interferes with the ',
            'infra virtualenv environment. Please move it to the side and bootstrap ',
            'again. Once infra has bootstrapped, you may move it back.', '',
            'Upstream bug: https://github.com/pypa/virtualenv/issues/88/', ''
        ]))

    print 'Creating environment: %r' % env_path

    if os.path.exists(env_path):
        print '  Removing existing one...'
        shutil.rmtree(env_path, ignore_errors=True)

    print '  Building new environment...'
    # Import bundled virtualenv lib
    import virtualenv  # pylint: disable=F0401
    virtualenv.create_environment(env_path,
                                  search_dirs=virtualenv.file_search_dirs())

    if relocatable:
        print '  Make environment relocatable'
        virtualenv.make_environment_relocatable(env_path)

    print 'Done creating environment'
Exemple #19
0
def create_virtualenv(bundle_path, requirements_path):
    scripts_path = os.path.join(bundle_path, 'Contents', 'Scripts')
    virtualenv.create_environment(scripts_path, site_packages=True)
    virtualenv.make_environment_relocatable(scripts_path)
    pip(['install', '--prefix', scripts_path, '-r', requirements_path])
    compileall.compile_dir(scripts_path, maxlevels=0)
Exemple #20
0
def relocateable_ve(ve_dir, python_version):
    # We can only call into the virtualenv module for operating on a
    # Python 2 virtualenv, in Python 2.
    if sys.version_info.major == 3 and python_version == '2.7':
        cmd = [
            os.path.join(ve_dir, 'bin', 'python'), '-c',
            'import sys; sys.path.append("%(ve_path)s"); '
            'sys.path.append("%(my_path)s"); '
            'from yodeploy import virtualenv; '
            'virtualenv.relocateable_ve("%(ve_dir)s", "%(python_version)s")'
            % {
                've_path': os.path.dirname(virtualenv.__file__),
                'my_path': os.path.join(os.path.dirname(__file__), '..'),
                've_dir': ve_dir,
                'python_version': python_version,
            }]
        subprocess.check_call(cmd)
        return

    log.debug('Making virtualenv relocatable')

    # Python 3 venv virtualenvs don't have these problems
    if python_version == '2.7':
        virtualenv.make_environment_relocatable(ve_dir)
        fix_local_symlinks(ve_dir)
        remove_fragile_symlinks(ve_dir)

    # Make activate relocatable, using approach taken in
    # https://github.com/pypa/virtualenv/pull/236
    activate = []
    with open(os.path.join(ve_dir, 'bin', 'activate')) as f:
        for line in f:
            line = line.strip()
            if line == 'deactivate () {':
                activate += ['ACTIVATE_PATH_FALLBACK="$_"', '']
            if line.startswith('VIRTUAL_ENV='):
                activate += [
                    '# attempt to determine VIRTUAL_ENV in relocatable way',
                    'if [ ! -z "${BASH_SOURCE:-}" ]; then',
                    '    # bash',
                    '    ACTIVATE_PATH="${BASH_SOURCE}"',
                    'elif [ ! -z "${DASH_SOURCE:-}" ]; then',
                    '    # dash',
                    '    ACTIVATE_PATH="${DASH_SOURCE}"',
                    'elif [ ! -z "${ZSH_VERSION:-}" ]; then',
                    '    # zsh',
                    '    ACTIVATE_PATH="$0"',
                    'elif [ ! -z "${KSH_VERSION:-}" ] '
                         '|| [ ! -z "${.sh.version:}" ]; then',
                    '    # ksh - we have to use history, '
                                'and unescape spaces before quoting',
                    '    ACTIVATE_PATH="$(history -r -l -n | head -1 | sed -e '
                        '\'s/^[\\t ]*\\(\\.\\|source\\) *//;s/\\\\ / /g\')"',
                    'elif [ "$(basename "$ACTIVATE_PATH_FALLBACK")" == '
                           '"activate.sh" ]; then',
                    '    ACTIVATE_PATH="${ACTIVATE_PATH_FALLBACK}"',
                    'else',
                    '    ACTIVATE_PATH=""',
                    'fi',
                    '',
                    '# default to non-relocatable path',
                ]
            if line == 'export VIRTUAL_ENV':
                activate += [
                    'if [ ! -z "${ACTIVATE_PATH:-}" ]; then',
                    '    VIRTUAL_ENV="$(cd '
                        '"$(dirname "${ACTIVATE_PATH}")/.."; pwd)"',
                    'fi',
                    'unset ACTIVATE_PATH',
                    'unset ACTIVATE_PATH_FALLBACK',
                ]
            activate.append(line)
    with open(os.path.join(ve_dir, 'bin', 'activate'), 'w') as f:
        f.write('\n'.join(activate))
Exemple #21
0
def activate_env(env, deps, quiet=False, run_within_virtualenv=False):
    if hasattr(sys, 'real_prefix'):
        if not run_within_virtualenv:
            LOGGER.error('Already activated environment!')
            return
        LOGGER.info('Discarding current VirtualEnv (--run-within-virtualenv)')
        sys.prefix = sys.real_prefix

    if not quiet:
        print 'Activating environment: %r' % env
    assert isinstance(deps, dict)

    manifest_path = os.path.join(env, 'manifest.pyl')
    cur_deps = read_deps(manifest_path)
    if cur_deps != deps:
        if not quiet:
            print '  Removing old environment: %r' % cur_deps
        shutil.rmtree(env, ignore_errors=True)
        cur_deps = None

    if cur_deps is None:
        check_pydistutils()

        if not quiet:
            print '  Building new environment'
        # Add in bundled virtualenv lib
        sys.path.insert(0, os.path.join(os.path.dirname(__file__),
                                        'virtualenv'))
        import virtualenv  # pylint: disable=F0401
        virtualenv.create_environment(
            env, search_dirs=virtualenv.file_search_dirs())

        # Hack: On windows orig-prefix.txt contains the hardcoded path
        # "E:\b\depot_tools\python276_bin", but some systems have depot_tools
        # installed on C:\ instead, so fiddle site.py to try loading it from there
        # as well.
        if sys.platform.startswith('win'):
            site_py_path = os.path.join(env, 'Lib\\site.py')
            with open(site_py_path) as fh:
                site_py = fh.read()

            m = re.search(r'( +)sys\.real_prefix = .*', site_py)
            replacement = (
                '%(indent)sif (sys.real_prefix.startswith("E:\\\\") and\n'
                '%(indent)s    not os.path.exists(sys.real_prefix)):\n'
                '%(indent)s  cand = "C:\\\\setup" + sys.real_prefix[4:]\n'
                '%(indent)s  if os.path.exists(cand):\n'
                '%(indent)s    sys.real_prefix = cand\n'
                '%(indent)s  else:\n'
                '%(indent)s    sys.real_prefix = "C" + sys.real_prefix'
                '[1:]\n' % {
                    'indent': m.group(1)
                })

            site_py = site_py[:m.
                              end(0)] + '\n' + replacement + site_py[m.end(0):]
            with open(site_py_path, 'w') as fh:
                fh.write(site_py)

    if not quiet:
        print '  Activating environment'
    # Ensure hermeticity during activation.
    os.environ.pop('PYTHONPATH', None)
    bin_dir = 'Scripts' if sys.platform.startswith('win') else 'bin'
    activate_this = os.path.join(env, bin_dir, 'activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))

    if cur_deps is None:
        if not quiet:
            print '  Installing deps'
            print_deps(deps, indent=2, with_implicit=False)
        install(deps)
        virtualenv.make_environment_relocatable(env)
        with open(manifest_path, 'wb') as f:
            f.write(repr(deps) + '\n')

    # Create bin\python.bat on Windows to unify path where Python is found.
    if sys.platform.startswith('win'):
        bin_path = os.path.join(env, 'bin')
        if not os.path.isdir(bin_path):
            os.makedirs(bin_path)
        python_bat_path = os.path.join(bin_path, 'python.bat')
        if not os.path.isfile(python_bat_path):
            with open(python_bat_path, 'w') as python_bat_file:
                python_bat_file.write(PYTHON_BAT_WIN)

    if not quiet:
        print 'Done creating environment'
Exemple #22
0
 def _finalize(self):
     virtualenv.make_environment_relocatable(self._root)
     activate_content = ""
     activate_content += self.envvars.generate_activate_content()
     write_activate_this(self._root, additional_content=activate_content)
     self.history.save()
Exemple #23
0
    def run(self):
        # create it
        venv_dir = self.bdist_dir
        if self.location_dir:
            venv_dir = os.path.join(venv_dir, self.location_dir)
        log.info('creating virtualenv "%s"', venv_dir)
        virtualenv.create_environment(venv_dir, clear=True)
         
        # install to
        log.info('installing to virtualenv "%s"', venv_dir)
        pip = os.path.join(venv_dir, 'bin', 'pip')
        install_cmd = [pip, 'install', '.']
        self.spawn(install_cmd)
        if self.extras:
            log.info('installing extras to virtualenv "%s"', venv_dir)
            install_cmd = [pip, 'install']
            for extra in self.extras:
                extras_require = self.distribution.extras_require[extra]
                if isinstance(extras_require, basestring):
                    extras_require = [extras_require] 
                install_cmd.extend(extras_require)
            self.spawn(install_cmd)
        
        # location
        if self.location_dir:
            log.info('making virtualenv "%s" installable to "%s"', venv_dir, self.location_dir)
            self.fixup_shebangs(venv_dir)
            self.fixup_virtual_envs(venv_dir)
            self.fixup_links(venv_dir)
            if self.keep_compiled:
                self.fixup_compiled(venv_dir)
            else:
                self.remove_compiled(venv_dir)
        else:
            log.info('making virtualenv "%s" relocatable', venv_dir)
            virtualenv.make_environment_relocatable(venv_dir)
            self.remove_compiled(venv_dir)

        # distribution        
        dist_root = os.path.join(self.dist_dir, self.dist_name) 
        if self.format:
            # as archive
            archive = self.make_archive(
                dist_root,
                self.format,
                root_dir=self.bdist_dir,
                owner=self.owner,
                group=self.group,
            )
            self.distribution.dist_files.append(
                ('bdist_venv', get_python_version(), archive)
            )
        else:
            # as directory
            copy_tree(self.bdist_dir, dist_root, preserve_symlinks=1)
            self.distribution.dist_files.append(
                ('bdist_venv', get_python_version(), dist_root)
            )

        # cleanup
        if not self.keep_temp:
            remove_tree(self.bdist_dir, dry_run=self.dry_run)