Ejemplo n.º 1
0
def main_pip(start_time, use_lock):
    """
    Install pip-based project (uses venv), looks for requirements.txt files

    Parameters
    ----------
    start_time : datetime
        The initial runtime of the function.
    use_lock : bool
        If True Uses requirements.txt and requirements.dev.lock.txt files
    """
    reqs_txt = _REQS_LOCK_TXT if use_lock else _REQS_TXT
    reqs_dev_txt = ('requirements.dev.lock.txt'
                    if use_lock else 'requirements.dev.txt')

    cmdr = Commander()

    # TODO: modify readme to add how to activate env? probably also in conda
    name = Path('.').resolve().name

    venv_dir = f'venv-{name}'
    cmdr.run('python', '-m', 'venv', venv_dir, description='Creating venv')

    # add venv_dir to .gitignore if it doesn't exist
    if Path('.gitignore').exists():
        with open('.gitignore') as f:
            if venv_dir not in f.read():
                cmdr.append_inline(venv_dir, '.gitignore')
    else:
        cmdr.append_inline(venv_dir, '.gitignore')

    folder, bin_name = _get_pip_folder_and_bin_name()
    pip = str(Path(venv_dir, folder, bin_name))

    if Path(_SETUP_PY).exists():
        _pip_install_setup_py_pip(cmdr, pip)

    _pip_install(cmdr, pip, lock=not use_lock, requirements=reqs_txt)

    if Path(reqs_dev_txt).exists():
        _pip_install(cmdr, pip, lock=not use_lock, requirements=reqs_dev_txt)

    if os.name == 'nt':
        cmd_activate = f'{venv_dir}\\Scripts\\Activate.ps1'
    else:
        cmd_activate = f'source {venv_dir}/bin/activate'

    _next_steps(cmdr, cmd_activate, start_time)
Ejemplo n.º 2
0
def main_pip():
    if not Path('requirements.txt').exists():
        raise exceptions.ClickException(
            '"ploomber install" requires a pip '
            'requirements.txt file. Use "ploomber scaffold" to create one '
            'from a template or create one manually')

    cmdr = Commander()

    # TODO: modify readme to add how to activate env? probably also in conda
    # TODO: add to gitignore, create if it doesn't exist
    name = Path('.').resolve().name

    venv_dir = f'venv-{name}'
    cmdr.run('python', '-m', 'venv', venv_dir, description='Creating venv')
    cmdr.append_inline(venv_dir, '.gitignore')

    folder = 'Scripts' if os.name == 'nt' else 'bin'
    bin_name = 'pip.EXE' if os.name == 'nt' else 'pip'
    pip = str(Path(venv_dir, folder, bin_name))

    _try_pip_install_setup_py(cmdr, pip)

    _pip_install_and_lock(cmdr, pip, requirements='requirements.txt')

    if Path('requirements.dev.txt').exists():
        _pip_install_and_lock(cmdr, pip, requirements='requirements.dev.txt')

    if os.name == 'nt':
        cmd_activate = (
            f'\nIf using cmd.exe: {venv_dir}\\Scripts\\activate.bat'
            f'\nIf using PowerShell: {venv_dir}\\Scripts\\Activate.ps1')
    else:
        cmd_activate = f'source {venv_dir}/bin/activate'

    _next_steps(cmdr, cmd_activate)
Ejemplo n.º 3
0
def main_conda():
    if not Path('environment.yml').exists():
        raise exceptions.ClickException(
            '"ploomber install" requires a conda '
            'environment.yml file. Use "ploomber scaffold" to create one '
            'from a template or create one manually')

    # TODO: ensure ploomber-scaffold includes dependency file (including
    # lock files in MANIFEST.in
    cmdr = Commander()

    # TODO: provide helpful error messages on each command

    with open('environment.yml') as f:
        env_name = yaml.safe_load(f)['name']

    current_env = Path(shutil.which('python')).parents[1].name

    if env_name == current_env:
        raise RuntimeError('environment.yaml will create an environment '
                           f'named {env_name!r}, which is the current active '
                           'environment. Move to a different one and try '
                           'again (e.g., "conda activate base")')

    # get current installed envs
    envs = cmdr.run('conda', 'env', 'list', '--json', capture_output=True)
    already_installed = any([
        env for env in json.loads(envs)['envs']
        # only check in the envs folder, ignore envs in other locations
        if 'envs' in env and env_name in env
    ])

    # if already installed and running on windows, ask to delete first,
    # otherwise it might lead to an intermitent error (permission denied
    # on vcruntime140.dll)
    if already_installed and os.name == 'nt':
        raise ValueError(f'Environemnt {env_name!r} already exists, '
                         f'delete it and try again '
                         f'(conda env remove --name {env_name})')

    pkg_manager = 'mamba' if shutil.which('mamba') else 'conda'
    cmdr.run(pkg_manager,
             'env',
             'create',
             '--file',
             'environment.yml',
             '--force',
             description='Creating env')

    pip = _locate_pip_inside_conda(env_name)
    _try_pip_install_setup_py(cmdr, pip)

    env_lock = cmdr.run('conda',
                        'env',
                        'export',
                        '--no-build',
                        '--name',
                        env_name,
                        description='Locking dependencies',
                        capture_output=True)
    Path('environment.lock.yml').write_text(env_lock)

    _try_conda_install_and_lock_dev(cmdr, pkg_manager, env_name)

    cmd_activate = f'conda activate {env_name}'
    _next_steps(cmdr, cmd_activate)
Ejemplo n.º 4
0
def main_conda(start_time, use_lock):
    """
    Install conda-based project, looks for environment.yml files

    Parameters
    ----------
    start_time : datetime
        The initial runtime of the function.
    use_lock : bool
        If True Uses environment.lock.yml and environment.dev.lock.yml files
    """
    env_yml = _ENV_LOCK_YML if use_lock else _ENV_YML

    # TODO: ensure ploomber-scaffold includes dependency file (including
    # lock files in MANIFEST.in
    cmdr = Commander()

    # TODO: provide helpful error messages on each command

    with open(env_yml) as f:
        env_name = yaml.safe_load(f)['name']

    current_env = Path(shutil.which('python')).parents[1].name

    if env_name == current_env:
        err = (f'{env_yml} will create an environment '
               f'named {env_name!r}, which is the current active '
               'environment. Move to a different one and try '
               'again (e.g., "conda activate base")')
        telemetry.log_api("install-error",
                          metadata={
                              'type': 'env_running_conflict',
                              'exception': err
                          })
        raise RuntimeError(err)

    # get current installed envs
    conda = shutil.which('conda')
    mamba = shutil.which('mamba')

    # if already installed and running on windows, ask to delete first,
    # otherwise it might lead to an intermittent error (permission denied
    # on vcruntime140.dll)
    if os.name == 'nt':
        envs = cmdr.run(conda, 'env', 'list', '--json', capture_output=True)
        already_installed = any([
            env for env in json.loads(envs)['envs']
            # only check in the envs folder, ignore envs in other locations
            if 'envs' in env and env_name in env
        ])

        if already_installed:
            err = (f'Environment {env_name!r} already exists, '
                   f'delete it and try again '
                   f'(conda env remove --name {env_name})')
            telemetry.log_api("install-error",
                              metadata={
                                  'type': 'duplicate_env',
                                  'exception': err
                              })
            raise ValueError(err)

    pkg_manager = mamba if mamba else conda
    cmdr.run(pkg_manager,
             'env',
             'create',
             '--file',
             env_yml,
             '--force',
             description='Creating env')

    if Path(_SETUP_PY).exists():
        _pip_install_setup_py_conda(cmdr, env_name)

    if not use_lock:
        env_lock = cmdr.run(conda,
                            'env',
                            'export',
                            '--no-build',
                            '--name',
                            env_name,
                            description='Locking dependencies',
                            capture_output=True)
        Path(_ENV_LOCK_YML).write_text(env_lock)

    _try_conda_install_and_lock_dev(cmdr,
                                    pkg_manager,
                                    env_name,
                                    use_lock=use_lock)

    cmd_activate = f'conda activate {env_name}'
    _next_steps(cmdr, cmd_activate, start_time)