Esempio n. 1
0
 def install(self):
     create_virtualenv(self.directory.install_directory(self.feature_name),
                       search_dirs=file_search_dirs(),
                       symlink=True)
     self.__install_eggs(self.target)
     self.__add_paths(self.target)
     return FormulaBase.install(self)
Esempio n. 2
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'
Esempio n. 3
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'
Esempio n. 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'
Esempio n. 5
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'
Esempio n. 6
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'
Esempio n. 7
0
 def install(self):
     create_virtualenv(self.directory.install_directory(self.feature_name),
                       search_dirs=file_search_dirs(), symlink=True)
     self.__install_eggs(self.target)
     self.__add_paths(self.target)
     return FormulaBase.install(self)
Esempio n. 8
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'
Esempio n. 9
0
def file_search_dirs():
    dirs = []
    for d in virtualenv.file_search_dirs():
        if "vootstrap" not in d:
            dirs.append(d)
    return dirs