Esempio n. 1
0
def install_fonts():
    if sys.platform not in font_locations:
        log.error('No font location defined for %s' % sys.platform)
        return True

    font_location = os.path.expanduser(font_locations[sys.platform])
    no_error = True

    try:
        os.makedirs(font_location)
    except:
        pass

    for font in fonts:
        font_name = urllib.parse.unquote(os.path.basename(font))
        font_path = os.path.expanduser(os.path.join(font_location, font_name))

        if not os.path.exists(font_path):
            print('[*] Downloading %s...' % log.color('1;33', font_name))

            res = requests.get(font, stream=True)
            if res.status_code != 200:
                log.error('Got status code %s for %s' %
                          (res.status_code, font))
                no_error = False

            with open(font_path, 'wb') as f:
                for chunk in res.iter_content(1024):
                    f.write(chunk)

    return no_error
Esempio n. 2
0
def fix_venv(path, python, python_path, force):
    # Default to local venv path in current directory
    if not path:
        path = LOCAL_VIRTUALENV_PATH

    path = os.path.abspath(path)

    # Make sure virtualenv path exists
    if not os.path.exists(path):
        log.error('Virtualenv path does not exist: %s' % path)
        return False

    # Validate either Python interpreter or Python path
    if not python and not python_path:
        log.error(
            'Please specify either a Python interpreter (with --python) or Python path (with --python-path) to recreate the virtualenv with.'
        )
        return False

    if python:
        data = virtualenvs.get_virtualenvs()
        python_path = virtualenvs.resolve_python_paths(python, data)
        if not python_path:
            log.error('No valid interpreter path found for %s.' % python)
            log.info('Valid interpreters: %s' % ', '.join(data.get('pythonPaths').keys()))
            return False

    # Confirmation prompt
    yes = input('Are you sure you want to fix the virtualenv at %s? [y/n] ' % path).upper() == 'Y'
    if not yes:
        log.info('Aborting.')
        return False

    # Remove all symlinks in the virtualenv
    log.info('Removing broken symlinks in virtualenv...')
    num_broken = 0

    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            fullpath = os.path.join(dirpath, filename)

            # Find broken symlinks, or any symlink if -f is passed
            if os.path.islink(fullpath) and (force or not os.path.exists(fullpath)):
                log.debug('Removing symlink %s' % fullpath)
                os.unlink(fullpath)
                num_broken += 1

    if num_broken == 0:
        log.error('No broken symlinks found. Aborting.')
        return False

    # Recreate virtualenv
    log.info('Recreating virtualenv using %s...' % python_path)
    env = VirtualEnvironment(path, python=python_path)
    env._create()

    log.success('Done!')
Esempio n. 3
0
def install_venv(name, path, requirements, python_path):
    print('[*] Setting up %s virtualenv...' % log.color('1;34', name))

    try:
        # Convert target path to absolute path.
        path = os.path.abspath(path)

        # Make sure that base path exists.
        if not os.path.exists(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))

        # Create new virtualenv if it doesn't exist.
        if not os.path.exists(path):
            env = VirtualEnvironment(path, python=python_path)
            env.open_or_create()

        # Reopen virtualenv to use the correct Python path
        env = VirtualEnvironment(path)
        assert env.path == os.path.abspath(path)

        # Install requirements for the virtualenv.
        # TODO: Shebang lines in binaries installed this way
        #       use the path in python_path instead of this virtualenv's,
        #       which I haven't been able to solve yet. A workaround
        #       is just to pip uninstall and then install it back.
        requirements = get_requirements(requirements)
        for requirement in requirements:
            requirement = requirement.split(' ')
            package, options = requirement[0], requirement[1:]

            if not env.is_installed(package):
                print('    Installing %s...' % log.color('1;33', package))
                env.install(package, options=options)
    except AssertionError as e:
        raise e
    except VirtualenvCreationException as e:
        # Could not create virtualenv, which executes `virtualenv` as a subprocess under the hood.
        log.error('Exception occurred while creating virtualenv: %s' % e)

        # Check if we can find the virtualenv bin.
        which = shutil.which('virtualenv')
        if which is None:
            log.error(
                'Most probable error: Could not locate `virtualenv` in $PATH.')
            return False

        log.error('Possible errors:\n' + \
                  '1. Check the shebang of %s' % which)

        return False
    except Exception as e:
        log.error('Exception (%s) occurred while setting up %s: %s' %
                  (e.__class__, name, e))
        traceback.print_exc()
        return False

    return True
Esempio n. 4
0
def setup_from(venv_name, target_path, python_path=None):
    data = virtualenvs.get_virtualenvs()
    venv_from = data.get('venvs').get(venv_name)
    if not venv_from:
        log.error('No virtualenv config named "%s" found.' % venv_name)
        return False

    requirements = venv_from.get('requirements')

    # Use interpreter python path if not specified
    if not python_path:
        python_path = virtualenvs.resolve_python_paths(
            venv_from.get('interpreter'), data)

    if not python_path:
        log.error('No valid interpreter path found.')
        return False

    log.debug('Using Python interpreter %s.' % python_path)
    return install_venv(target_path, target_path, requirements, python_path)
Esempio n. 5
0
def setup_venv_from(venv_name,
                    target_path=LOCAL_VIRTUALENV_PATH,
                    python_path=None):
    fullpath = os.path.abspath(target_path)
    if os.path.exists(fullpath):
        log.error('Virtualenv already exists at %s.' % fullpath)
        return False

    if not venv_name:
        log.error('Please specify venv name.')
        return False

    log.info('Installing virtualenv from %s...' % venv_name)

    if not setup_from(venv_name, target_path, python_path=python_path):
        return False

    print(log.color('0;32', 'Virtualenv is created! Now run'),
          log.color('1;34', 'venv'),
          log.color('0;32', 'in your shell to activate.'))
    return True
Esempio n. 6
0
def setup_venvs_from_config():
    log.info('Installing virtualenvs using config...')

    data = virtualenvs.get_virtualenvs()
    venvs = data.get('venvs')

    for name, venv in venvs.items():
        requirements = venv.get('requirements')
        python_path = virtualenvs.resolve_python_paths(venv.get('interpreter'),
                                                       data)
        if not python_path:
            log.error(
                'Cannot setup virtualenv for %s, no valid interpreter path found.'
                % name)
            continue

        path = os.path.join(GLOBAL_VIRTUALENV_ROOT, name)
        if not install_venv(name, path, requirements, python_path):
            return False

    log.success('All virtualenvs have been set up!')
    return True