def test_envs(): with temp_chdir(): runner = CliRunner() env_name1, env_name2 = get_new_venv_name(count=2) venv_dir1 = os.path.join(VENV_DIR, env_name1) venv_dir2 = os.path.join(VENV_DIR, env_name2) create_venv(venv_dir1) try: result = runner.invoke(hatch, [ 'init', '-ne', '--basic', 'ok', '-e', '{}/{}'.format( env_name1, env_name2) ]) wait_until(is_venv, venv_dir2) with venv(venv_dir1): assert 'ok' in get_editable_packages() with venv(venv_dir2): assert 'ok' in get_editable_packages() finally: remove_path(venv_dir1) remove_path(venv_dir2) assert result.exit_code == 0 assert 'Creating virtual env `{}`... complete!'.format( env_name1) not in result.output assert 'Creating virtual env `{}`... complete!'.format( env_name2) in result.output assert 'Installing locally in virtual env `{}`... complete!'.format( env_name1) in result.output assert 'Installing locally in virtual env `{}`... complete!'.format( env_name2) in result.output
def test_restore_success(): with temp_chdir() as d: runner = CliRunner() env_name, fake_name = get_new_venv_name(count=2) venv_origin = os.path.join(d, env_name) venv_dir = os.path.join(get_venv_dir(), env_name) fake_venv = os.path.join(get_venv_dir(), fake_name) create_venv(venv_origin) copy_path(venv_origin, get_venv_dir()) os.makedirs(fake_venv) try: runner.invoke(hatch, ['env', env_name]) wait_until(is_venv, venv_dir) result = runner.invoke(hatch, ['env', '-r']) with venv(venv_dir): install_packages(['six']) installed_packages = get_installed_packages() finally: remove_path(venv_dir) remove_path(fake_venv) assert result.exit_code == 0 assert 'Successfully restored all available virtual envs.' in result.output assert 'six' in installed_packages
def test_env_multiple(): with temp_chdir(): runner = CliRunner() env_name1, env_name2 = get_new_venv_name(count=2) venv_dir1 = os.path.join(get_venv_dir(), env_name1) venv_dir2 = os.path.join(get_venv_dir(), env_name2) try: runner.invoke(hatch, ['env', env_name1]) wait_until(is_venv, venv_dir1) assert os.path.exists(venv_dir1) runner.invoke(hatch, ['env', env_name2]) wait_until(is_venv, venv_dir2) assert os.path.exists(venv_dir2) result = runner.invoke(hatch, ['shed', '-e', '{}/{}'.format(env_name1, env_name2)]) assert not os.path.exists(venv_dir1) assert not os.path.exists(venv_dir2) finally: remove_path(venv_dir1) remove_path(venv_dir2) assert result.exit_code == 0 assert 'Successfully removed virtual env named `{}`.'.format(env_name1) in result.output assert 'Successfully removed virtual env named `{}`.'.format(env_name2) in result.output
def test_clone_success(): with temp_chdir(): runner = CliRunner() origin = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, origin)): # no cov origin = os.urandom(10).hex() origin_dir = os.path.join(VENV_DIR, origin) clone_dir = origin_dir try: runner.invoke(hatch, ['env', origin]) with venv(origin_dir): install_packages(['requests']) clone = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, clone)): # no cov clone = os.urandom(10).hex() clone_dir = os.path.join(VENV_DIR, clone) result = runner.invoke(hatch, ['env', clone, '-c', origin]) with venv(clone_dir): install_packages(['six']) installed_packages = get_installed_packages() finally: remove_path(origin_dir) remove_path(clone_dir) assert result.exit_code == 0 assert 'Successfully cloned virtual env `{}` from `{}` to {}.'.format( clone, origin, clone_dir) in result.output assert 'requests' in installed_packages assert 'six' in installed_packages
def test_list_success(): with temp_chdir(): runner = CliRunner() env_name1 = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name1)): # no cov env_name1 = os.urandom(10).hex() env_name2 = '' try: runner.invoke(hatch, ['env', env_name1]) env_name2 = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name2)): # no cov env_name2 = os.urandom(10).hex() os.makedirs(os.path.join(VENV_DIR, env_name2)) result = runner.invoke(hatch, ['env', '-l']) finally: remove_path(os.path.join(VENV_DIR, env_name1)) remove_path(os.path.join(VENV_DIR, env_name2)) assert result.exit_code == 0 assert ('{} ->\n' ' Version: {}\n' ' Implementation: {}'.format( env_name1, get_python_version(), get_python_implementation())) in result.output
def test_env_multiple(): with temp_chdir(): runner = CliRunner() env_name1 = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name1)): # no cov env_name1 = os.urandom(10).hex() venv_dir1 = os.path.join(VENV_DIR, env_name1) env_name2 = '' venv_dir2 = '' try: runner.invoke(hatch, ['env', env_name1]) assert os.path.exists(venv_dir1) env_name2 = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name2)): # no cov env_name2 = os.urandom(10).hex() venv_dir2 = os.path.join(VENV_DIR, env_name2) runner.invoke(hatch, ['env', env_name2]) assert os.path.exists(venv_dir2) result = runner.invoke(hatch, ['shed', '-e', '{}/{}'.format(env_name1, env_name2)]) assert not os.path.exists(venv_dir1) assert not os.path.exists(venv_dir2) finally: remove_path(venv_dir1) remove_path(venv_dir2) assert result.exit_code == 0 assert 'Successfully removed virtual env named `{}`.'.format(env_name1) in result.output assert 'Successfully removed virtual env named `{}`.'.format(env_name2) in result.output
def test_clone_success(): with temp_chdir(): runner = CliRunner() origin, clone = get_new_venv_name(count=2) origin_dir = os.path.join(get_venv_dir(), origin) clone_dir = os.path.join(get_venv_dir(), clone) try: runner.invoke(hatch, ['env', origin]) wait_until(is_venv, origin_dir) with venv(origin_dir): install_packages(['requests']) result = runner.invoke(hatch, ['env', '-c', origin, clone]) wait_until(is_venv, clone_dir) with venv(clone_dir): install_packages(['six']) installed_packages = get_installed_packages() finally: remove_path(origin_dir) remove_path(clone_dir) assert result.exit_code == 0 assert 'Successfully cloned virtual env `{}` from `{}` to `{}`.'.format( clone, origin, clone_dir) in result.output assert 'requests' in installed_packages assert 'six' in installed_packages
def test_pyname_and_env(): with temp_chdir() as d: runner = CliRunner() env_name = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name)): # no cov env_name = os.urandom(10).hex() venv_dir = os.path.join(VENV_DIR, env_name) try: runner.invoke(hatch, ['env', env_name]) assert os.path.exists(venv_dir) with temp_move_path(SETTINGS_FILE, d): settings = copy_default_settings() settings['pypaths']['pyname'] = 'pypath' save_settings(settings) result = runner.invoke( hatch, ['shed', '-p', 'pyname', '-e', env_name]) assert load_settings()['pypaths'] == {} assert not os.path.exists(venv_dir) finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Successfully removed Python path named `pyname`.' in result.output assert 'Successfully removed virtual env named `{}`.'.format( env_name) in result.output
def shed(ctx, pyname, env_name): """Removes named Python paths or virtual environments. \b $ hatch pypath -l py2 -> /usr/bin/python py3 -> /usr/bin/python3 invalid -> :\/: $ hatch env -l Virtual environments found in /home/ofek/.local/share/hatch/venvs: \b duplicate -> Version: 3.5.2 Implementation: CPython fast -> Version: 3.5.3 Implementation: PyPy my-app -> Version: 3.5.2 Implementation: CPython old -> Version: 2.7.12 Implementation: CPython $ hatch shed -p invalid -e duplicate,old Successfully removed Python path named `invalid`. Successfully removed virtual env named `duplicate`. Successfully removed virtual env named `old`. """ if not (pyname or env_name): click.echo(ctx.get_help()) return if pyname: try: settings = load_settings() except FileNotFoundError: click.echo('Unable to locate config file. Try `hatch config --restore`.') sys.exit(1) for pyname in pyname.split(','): pypath = settings.get('pypaths', {}).pop(pyname, None) if pypath is not None: save_settings(settings) click.echo('Successfully removed Python path named `{}`.'.format(pyname)) else: click.echo('Python path named `{}` already does not exist.'.format(pyname)) if env_name: for env_name in env_name.split(','): venv_dir = os.path.join(VENV_DIR, env_name) if os.path.exists(venv_dir): remove_path(venv_dir) click.echo('Successfully removed virtual env named `{}`.'.format(env_name)) else: click.echo('Virtual env named `{}` already does not exist.'.format(env_name))
def conda(location, force): # no cov if not force and conda_available(): echo_warning(('Conda is already in PATH! If you are sure you want ' 'to proceed, try again with the -f/--force flag.')) sys.exit(2) else: if ON_WINDOWS: installer_name = 'installer.exe' if is_os_64bit(): url = 'https://repo.continuum.io/miniconda/Miniconda3-latest-Windows-x86_64.exe' else: url = 'https://repo.continuum.io/miniconda/Miniconda3-latest-Windows-x86.exe' else: installer_name = 'installer.sh' if ON_MACOS: if is_os_64bit(): url = 'https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh' else: echo_failure( 'Conda is not available for 32-bit macOS, sorry!') sys.exit(1) else: if is_os_64bit(): url = 'https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh' else: url = 'https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86.sh' with temp_chdir() as d: fname = os.path.join(d, installer_name) download_file(url, fname) location = os.path.abspath(location) if os.path.exists(location): if force: remove_path(location) else: echo_warning(( '`{}` already exists! If you are sure you want to proceed, ' 'try again with the -f/--force flag.'.format(location) )) sys.exit(2) if ON_WINDOWS: command = [ 'start', '/wait', '', fname, '/S', '/InstallationType=JustMe', '/AddToPath=0', '/RegisterPython=0', '/D={}'.format(location) ] else: command = ['bash', fname, '-b', '-p', location] result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL)
def remove_compiled_scripts(d): removed = set() for root, _, files in os.walk(d): for file in files: if file.endswith('.pyc'): removed.add(join(root, file)) removed = sorted(removed) for p in reversed(removed): remove_path(p) return removed
def remove_compiled_scripts(d, detect_project=True): removed = set() for root, _, files in generate_walker(d, detect_project): for file in files: if file.endswith('.pyc'): removed.add(join(root, file)) removed = sorted(removed) for p in reversed(removed): remove_path(p) return removed
def test_existing_venv(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() try: runner.invoke(hatch, ['env', env_name]) result = runner.invoke(hatch, ['env', env_name]) finally: remove_path(os.path.join(VENV_DIR, env_name)) assert result.exit_code == 1 assert ('Virtual env `{name}` already exists. To remove ' 'it do `hatch shed -e {name}`.'.format( name=env_name)) in result.output
def test_success(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) try: result = runner.invoke(hatch, ['env', env_name]) assert os.path.exists(venv_dir) finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Successfully saved virtual env `{}` to `{}`.'.format( env_name, venv_dir) in result.output
def test_new_env(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) try: result = runner.invoke(hatch, ['init', '--basic', 'ok', env_name]) with venv(venv_dir): assert 'ok' in get_editable_packages() finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Creating virtual env `{}`...'.format(env_name) in result.output assert 'Installing locally in virtual env `{}`...'.format(env_name) in result.output
def test_new_env_exists(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) os.makedirs(venv_dir) try: result = runner.invoke(hatch, ['init', '--basic', 'ok', env_name]) finally: remove_path(venv_dir) assert result.exit_code == 1 assert ( 'Virtual env `{name}` already exists. To remove ' 'it do `hatch shed -e {name}`.'.format(name=env_name) ) in result.output
def test_env(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) create_venv(venv_dir) try: with venv(venv_dir): assert 'six' not in get_installed_packages() result = runner.invoke(hatch, ['install', '-e', env_name, 'six']) with venv(venv_dir): assert 'six' in get_installed_packages() finally: remove_path(venv_dir) assert result.exit_code == 0
def test_env(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(get_venv_dir(), env_name) try: runner.invoke(hatch, ['env', env_name]) wait_until(is_venv, venv_dir) assert os.path.exists(venv_dir) result = runner.invoke(hatch, ['shed', '-e', env_name]) assert not os.path.exists(venv_dir) finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Successfully removed virtual env named `{}`.'.format(env_name) in result.output
def test_list_success_2(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() try: runner.invoke(hatch, ['env', env_name]) result = runner.invoke(hatch, ['env', '-ll']) finally: remove_path(os.path.join(VENV_DIR, env_name)) assert result.exit_code == 0 assert ('{} ->\n' ' Version: {}\n' ' Implementation: {}'.format( env_name, get_python_version(), get_python_implementation())) in result.output
def test_env(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) create_venv(venv_dir) try: with venv(venv_dir): install_packages(['requests==2.17.3']) initial_version = get_version_as_bytes('requests') result = runner.invoke(hatch, ['update', '-e', env_name, '--all']) with venv(venv_dir): final_version = get_version_as_bytes('requests') finally: remove_path(venv_dir) assert result.exit_code == 0 assert initial_version < final_version
def test_list_success_1(): with temp_chdir(): runner = CliRunner() env_name1, env_name2 = get_new_venv_name(count=2) venv_dir1 = os.path.join(VENV_DIR, env_name1) venv_dir2 = os.path.join(VENV_DIR, env_name2) try: runner.invoke(hatch, ['env', env_name1]) os.makedirs(venv_dir2) result = runner.invoke(hatch, ['env', '-l']) finally: remove_path(venv_dir1) remove_path(venv_dir2) assert result.exit_code == 0 assert ('{} ->\n' ' Version: {}'.format(env_name1, get_python_version())) in result.output
def test_env(): with temp_chdir(): runner = CliRunner() env_name = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name)): # no cov env_name = os.urandom(10).hex() venv_dir = os.path.join(VENV_DIR, env_name) try: runner.invoke(hatch, ['env', env_name]) assert os.path.exists(venv_dir) result = runner.invoke(hatch, ['shed', '-e', env_name]) assert not os.path.exists(venv_dir) finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Successfully removed virtual env named `{}`.'.format(env_name) in result.output
def test_infra_env(): with temp_chdir(): runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(get_venv_dir(), env_name) create_venv(venv_dir) try: with venv(venv_dir): install_packages(['setuptools==36.0.1']) initial_version = get_version_as_bytes('setuptools') result = runner.invoke(hatch, ['update', '-e', env_name, '--infra']) with venv(venv_dir): final_version = get_version_as_bytes('setuptools') finally: remove_path(venv_dir) assert result.exit_code == 0 assert initial_version < final_version
def clean_package(d, editable=False): removed = set() patterns = ALL_PATTERNS.copy() if editable: patterns.remove('*.egg-info') root = Path(d) for pattern in patterns: for p in root.glob(pattern): removed.add(str(p)) if p.is_dir(): find_globs(str(p), DELETE_EVERYWHERE, removed) find_globs(d, DELETE_EVERYWHERE, removed) removed = sorted(removed) for p in reversed(removed): remove_path(p) return removed
def test_pyname(): with temp_chdir() as d: runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) try: with temp_move_path(SETTINGS_FILE, d): settings = copy_default_settings() settings['pypaths']['python'] = sys.executable save_settings(settings) result = runner.invoke(hatch, ['env', env_name, '-py', 'python']) assert os.path.exists(venv_dir) finally: remove_path(venv_dir) assert result.exit_code == 0 assert 'Successfully saved virtual env `{}` to `{}`.'.format( env_name, venv_dir) in result.output
def test_pyname(): with temp_chdir() as d: runner = CliRunner() env_name = get_new_venv_name() venv_dir = os.path.join(VENV_DIR, env_name) try: with temp_move_path(SETTINGS_FILE, d): settings = copy_default_settings() settings['pypaths']['python'] = sys.executable save_settings(settings) result = runner.invoke(hatch, ['init', 'ok', '-py', 'python']) venv_dir = os.path.join(d, 'venv') global_version = get_python_version() wait_until(is_venv, venv_dir) with venv(venv_dir): assert get_python_version() == global_version finally: remove_path(venv_dir) assert result.exit_code == 0
def clean_package(d, editable=False, detect_project=True): removed = set() patterns = ALL_PATTERNS.copy() if editable: patterns.remove('*.egg-info') root = Path(d) for pattern in patterns: for p in root.glob(pattern): full_path = str(p) removed.add(full_path) if p.is_dir(): find_globs(os.walk(full_path), DELETE_EVERYWHERE, removed) find_globs(generate_walker(d, detect_project), DELETE_EVERYWHERE, removed) removed = sorted(removed) for p in reversed(removed): remove_path(p) return removed
def test_env(): with temp_chdir(): runner = CliRunner() env_name = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name)): # no cov env_name = os.urandom(10).hex() venv_dir = os.path.join(VENV_DIR, env_name) create_venv(venv_dir) try: runner.invoke(hatch, ['install', '-e', env_name, 'six']) with venv(venv_dir): assert 'six' in get_installed_packages() result = runner.invoke(hatch, ['uninstall', '-y', '-e', env_name, 'six']) with venv(venv_dir): assert 'six' not in get_installed_packages() finally: remove_path(venv_dir) assert result.exit_code == 0
def test_list_success_3(): with temp_chdir(): runner = CliRunner() runner.invoke(hatch, ['init', 'ok']) env_name = os.urandom(10).hex() while os.path.exists(os.path.join(VENV_DIR, env_name)): # no cov env_name = os.urandom(10).hex() try: runner.invoke(hatch, ['env', env_name]) runner.invoke(hatch, ['install', '-l', '-e', env_name]) result = runner.invoke(hatch, ['env', '-lll']) finally: remove_path(os.path.join(VENV_DIR, env_name)) assert result.exit_code == 0 assert ('{} ->\n' ' Version: {}\n' ' Implementation: {}\n' ' Local packages: {}'.format(env_name, get_python_version(), get_python_implementation(), 'ok')) in result.output
def test_list_success_3(): with temp_chdir(): runner = CliRunner() runner.invoke(hatch, ['init', 'ok', '-ne']) env_name = get_new_venv_name() venv_dir = os.path.join(get_venv_dir(), env_name) try: runner.invoke(hatch, ['env', env_name]) wait_until(is_venv, venv_dir) runner.invoke(hatch, ['install', '-l', '-e', env_name]) result = runner.invoke(hatch, ['env', '-lll']) finally: remove_path(os.path.join(get_venv_dir(), env_name)) assert result.exit_code == 0 assert ('{} ->\n' ' Version: {}\n' ' Implementation: {}\n' ' Local packages: {}'.format(env_name, get_python_version(), get_python_implementation(), 'ok')) in result.output