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_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_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_package(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--cli', '-ne']) package_dir = os.path.join(d, 'ok') files = find_all_files(package_dir) test_file = os.path.join(package_dir, 'test.pyc') create_file(test_file) assert os.path.exists(test_file) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): os.chdir(package_dir) install_packages(['-e', '.']) os.chdir(d) result = runner.invoke(hatch, ['clean', 'ok']) assert result.exit_code == 0 assert 'Cleaned!' in result.output assert not os.path.exists(test_file) assert os.path.exists(os.path.join(package_dir, 'ok.egg-info')) assert_files_exist(files)
def test_get_editable_package_location(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'foo', '--basic', '-ne']) runner.invoke(hatch, ['new', 'bar', '--basic', '-ne']) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['-e', os.path.join(d, 'foo')]) install_packages(['-e', os.path.join(d, 'bar')]) assert get_editable_package_location('foo') == os.path.join(d, 'foo')
def test_get_installed_packages_no_editable(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['init', 'ok', '--basic', '-ne']) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['six']) install_packages(['-e', '.']) packages = get_installed_packages(editable=False) assert 'six' in packages assert 'ok' not in packages
def test_infra(): with temp_chdir() as d: runner = CliRunner() venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['setuptools==36.0.1']) initial_version = get_version_as_bytes('setuptools') result = runner.invoke(hatch, ['update', '-nd', '--infra']) final_version = get_version_as_bytes('setuptools') assert result.exit_code == 0 assert initial_version < final_version
def test_all_packages(): with temp_chdir() as d: venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['requests==2.17.3']) initial_version = get_version_as_bytes('requests') runner = CliRunner() result = runner.invoke(hatch, ['update', '-nd', '--all']) final_version = get_version_as_bytes('requests') assert result.exit_code == 0 assert initial_version < final_version
def test_local(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', PACKAGE_NAME, '--basic', '-ne']) runner.invoke(hatch, ['build', '-p', PACKAGE_NAME]) package_dir = os.path.join(d, PACKAGE_NAME) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir, evars=ENV_VARS): install_packages(['-e', package_dir]) result = runner.invoke(hatch, ['release', '-l', '-u', USERNAME, '-t']) assert result.exit_code == 0
def test_repository_env_vars(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', PACKAGE_NAME, '--basic', '-ne']) runner.invoke(hatch, ['build', '-p', PACKAGE_NAME]) package_dir = os.path.join(d, PACKAGE_NAME) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) extra_env_vars = {'TWINE_REPOSITORY': TEST_REPOSITORY, 'TWINE_REPOSITORY_URL': TEST_REPOSITORY, **ENV_VARS} with venv(venv_dir, evars=extra_env_vars): install_packages(['-e', package_dir]) result = runner.invoke(hatch, ['release', '-l', '-u', USERNAME]) assert result.exit_code == 0
def test_requirements_dev(): with temp_chdir() as d: with open(os.path.join(d, 'requirements-dev.txt'), 'w') as f: f.write('requests==2.18.1\n') venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['requests==2.17.3']) initial_version = get_version_as_bytes('requests') runner = CliRunner() result = runner.invoke(hatch, ['update', '-nd']) final_version = get_version_as_bytes('requests') assert result.exit_code == 0 assert initial_version < final_version
def test_packages(): with temp_chdir() as d: runner = CliRunner() venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['requests==2.17.3', 'six==1.9.0']) initial_version_requests = get_version_as_bytes('requests') initial_version_six = get_version_as_bytes('six') result = runner.invoke(hatch, ['update', 'six']) final_version_requests = get_version_as_bytes('requests') final_version_six = get_version_as_bytes('six') assert result.exit_code == 0 assert initial_version_requests == final_version_requests assert initial_version_six < final_version_six
def test_local(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic', '-ne']) package_dir = os.path.join(d, 'ok') create_test_passing(package_dir) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['-e', package_dir]) result = runner.invoke(hatch, ['test', '-nd', '-l', '-g']) assert result.exit_code == 0 assert 'Package `ok` has been selected.' in result.output assert '1 passed' in result.output
def test_local_multiple(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic', '-ne']) runner.invoke(hatch, ['new', 'ko', '--basic', '-ne']) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['-e', os.path.join(d, 'ok')]) install_packages(['-e', os.path.join(d, 'ko')]) result = runner.invoke(hatch, ['clean', '-l']) assert result.exit_code == 1 assert ('There are multiple local packages available. ' 'Select one with the optional argument.') in result.output
def test_project_existing_venv(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['init', 'ok', '--basic']) venv_dir = os.path.join(d, 'venv') wait_until(is_venv, venv_dir) with venv(venv_dir): install_packages(['pytest', 'coverage']) installed_packages = get_installed_packages(editable=False) assert 'pytest' in installed_packages assert 'coverage' in installed_packages create_test_passing(d) with env_vars({'_IGNORE_VENV_': '1'}): result = runner.invoke(hatch, ['test']) assert result.exit_code == 0 assert '1 passed' in result.output
def test_package(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic', '-ne']) package_dir = os.path.join(d, 'ok') create_test_passing(package_dir) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): os.chdir(package_dir) install_packages(['-e', '.']) os.chdir(d) result = runner.invoke(hatch, ['test', '-nd', 'ok', '-g']) assert result.exit_code == 0 assert '1 passed' in result.output
def test_requirements_includes_hatch(): with temp_chdir() as d: runner = CliRunner() with open(os.path.join(d, 'requirements.txt'), 'w') as f: f.write('requests==2.18.1\nhatch>=0.0.1\n') venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['requests==2.17.3']) initial_version = get_version_as_bytes('requests') result = runner.invoke(hatch, ['update']) final_version = get_version_as_bytes('requests') installed_packages = get_installed_packages() assert result.exit_code == 0 assert initial_version < final_version assert 'hatch' not in installed_packages
def test_repository_local(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', PACKAGE_NAME, '--basic', '-ne']) runner.invoke(hatch, ['build', '-p', PACKAGE_NAME]) package_dir = os.path.join(d, PACKAGE_NAME) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) # Make sure there's no configuration with temp_move_path(os.path.expanduser("~/.pypirc"), d): with venv(venv_dir, evars=ENV_VARS): install_packages(['-e', package_dir]) # Will error, since there's no configuration parameter for # this URL result = runner.invoke(hatch, ['release', '-l', '-u', USERNAME, '-r', TEST_REPOSITORY]) assert result.exit_code == 1
def test_package(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic']) package_dir = os.path.join(d, 'ok') venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): os.chdir(package_dir) install_packages(['-e', '.']) os.chdir(d) result = runner.invoke(hatch, ['build', 'ok']) files = os.listdir(os.path.join(package_dir, 'dist')) assert result.exit_code == 0 assert matching_file(r'.*\.whl$', files) assert len(files) == 2
def test_local(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic', '-ne']) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['-e', os.path.join(d, 'ok')]) result = runner.invoke(hatch, ['grow', 'fix', '-l']) init_file = os.path.join(d, 'ok', 'ok', '__init__.py') contents = read_file(init_file) assert result.exit_code == 0 assert contents == "__version__ = '0.0.2'\n" assert 'Package `ok` has been selected.' in result.output assert 'Updated {}'.format(init_file) in result.output assert '0.0.1 -> 0.0.2' 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_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 test_package(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['egg', 'ok', '--basic']) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): os.chdir(os.path.join(d, 'ok')) install_packages(['-e', '.']) os.chdir(d) result = runner.invoke(hatch, ['grow', 'fix', 'ok']) init_file = os.path.join(d, 'ok', 'ok', '__init__.py') contents = read_file(init_file) assert result.exit_code == 0 assert contents == "__version__ = '0.0.2'\n" assert 'Updated {}'.format(init_file) in result.output assert '0.0.1 -> 0.0.2' in result.output
def test_local(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', 'ok', '--basic', '-ne']) package_dir = os.path.join(d, 'ok') build_dir = os.path.join(package_dir, 'dist') venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir): install_packages(['-e', package_dir]) result = runner.invoke(hatch, ['build', '-l']) files = os.listdir(build_dir) assert result.exit_code == 0 assert matching_file(r'.*\.whl$', files) assert len(files) == 2 assert ('Files found in `{}`:\n\n'.format(build_dir) + format_files(build_dir)) in result.output
def test_repository_and_test(): with temp_chdir() as d: runner = CliRunner() runner.invoke(hatch, ['new', PACKAGE_NAME, '--basic', '-ne']) runner.invoke(hatch, ['build', '-p', PACKAGE_NAME]) package_dir = os.path.join(d, PACKAGE_NAME) venv_dir = os.path.join(d, 'venv') create_venv(venv_dir) with venv(venv_dir, evars=ENV_VARS): install_packages(['-e', package_dir]) result = runner.invoke( hatch, ['release', '-l', '-u', USERNAME, '-r', TEST_REPOSITORY, '-t']) assert result.exit_code == 1 assert "Cannot specify both --test and --repo." in result.output with venv(venv_dir, evars=ENV_VARS): result = runner.invoke(hatch, [ 'release', '-l', '-u', USERNAME, '--repo-url', TEST_REPOSITORY, '-t' ]) assert result.exit_code == 1 assert "Cannot specify both --test and --repo-url." in result.output with venv(venv_dir, evars=ENV_VARS): result = runner.invoke(hatch, [ 'release', '-l', '-u', USERNAME, '-r', TEST_REPOSITORY, '-ru', TEST_REPOSITORY, '-t' ]) assert result.exit_code == 1 assert "Cannot specify both --test and --repo." in result.output assert "Cannot specify both --test and --repo-url." in result.output
def test_restore_success(): 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_origin = os.path.join(d, env_name) create_venv(venv_origin) venv_dir = os.path.join(VENV_DIR, env_name) copy_path(venv_origin, VENV_DIR) fake_venv = '' try: runner.invoke(hatch, ['env', env_name]) 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() fake_venv = os.path.join(VENV_DIR, env_name) os.makedirs(fake_venv) 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(package, local, path, cov, merge, test_args, cov_args, global_exe, no_detect): """Runs tests using `pytest`, optionally checking coverage. The path is derived in the following order: \b 1. The optional argument, which should be the name of a package that was installed via `hatch install -l` or `pip install -e`. 2. The --local flag. 3. The option --path, which can be a relative or absolute path. 4. The current directory. If the path points to a package, it should have a `tests` directory. If a project is detected but there is no dedicated virtual env, it will be created and any dev requirements will be installed in it. \b $ git clone https://github.com/ofek/privy && cd privy $ hatch test -c ========================= test session starts ========================== platform linux -- Python 3.5.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0 rootdir: /home/ofek/privy, inifile: plugins: xdist-1.20.0, mock-1.6.2, httpbin-0.0.7, forked-0.2, cov-2.5.1 collected 10 items \b tests/test_privy.py .......... \b ====================== 10 passed in 4.34 seconds ======================= \b Tests completed, checking coverage... \b Name Stmts Miss Branch BrPart Cover Missing ----------------------------------------------------------------- privy/__init__.py 1 0 0 0 100% privy/core.py 30 0 0 0 100% privy/utils.py 13 0 4 0 100% tests/__init__.py 0 0 0 0 100% tests/test_privy.py 57 0 0 0 100% ----------------------------------------------------------------- TOTAL 101 0 4 0 100% """ if package: echo_waiting('Locating package...') path = get_editable_package_location(package) if not path: echo_failure('`{}` is not an editable package.'.format(package)) sys.exit(1) elif local: echo_waiting('Locating package...') name, path = get_editable_package_location() if not name: if path is None: echo_failure('There are no local packages available.') sys.exit(1) else: echo_failure( 'There are multiple local packages available. Select ' 'one with the optional argument.') sys.exit(1) echo_info('Package `{}` has been selected.'.format(name)) elif path: possible_path = resolve_path(path) if not possible_path: echo_failure('Directory `{}` does not exist.'.format(path)) sys.exit(1) path = possible_path else: path = os.getcwd() python_cmd = [sys.executable if global_exe else get_proper_python(), '-m'] command = python_cmd.copy() if cov: command.extend(['coverage', 'run']) command.extend(cov_args.split() if cov_args is not None else ( ['--parallel-mode'] if merge else [])) command.append('-m') command.append('pytest') command.extend(test_args.split()) try: # no cov sys.stdout.fileno() testing = False except io.UnsupportedOperation: # no cov testing = True # For testing we need to pipe because Click changes stdio streams. stdout = sys.stdout if not testing else subprocess.PIPE stderr = sys.stderr if not testing else subprocess.PIPE venv_dir = None if not (package or local) and not venv_active() and not no_detect and is_project(): venv_dir = os.path.join(path, get_venv_folder()) if not is_venv(venv_dir): echo_info('A project has been detected!') echo_waiting('Creating a dedicated virtual env... ', nl=False) create_venv(venv_dir) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing this project in the virtual env...') install_packages(['-e', '.']) click.echo() echo_waiting('Ensuring pytest and coverage are available...') install_packages(['pytest', 'coverage']) click.echo() dev_requirements = get_requirements_file(path, dev=True) if dev_requirements: echo_waiting( 'Installing test dependencies in the virtual env...') install_packages(['-r', dev_requirements]) click.echo() with chdir(path): echo_waiting('Testing...') output = b'' if venv_dir: with venv(venv_dir): test_result = subprocess.run(command, stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: test_result = subprocess.run(command, stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += test_result.stdout or b'' output += test_result.stderr or b'' if cov: echo_waiting('\nTests completed, checking coverage...\n') if merge: if venv_dir: with venv(venv_dir): result = subprocess.run( python_cmd + ['coverage', 'combine', '--append'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: result = subprocess.run( python_cmd + ['coverage', 'combine', '--append'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += result.stdout or b'' output += result.stderr or b'' if venv_dir: with venv(venv_dir): result = subprocess.run( python_cmd + ['coverage', 'report', '--show-missing'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) else: result = subprocess.run( python_cmd + ['coverage', 'report', '--show-missing'], stdout=stdout, stderr=stderr, shell=NEED_SUBPROCESS_SHELL) output += result.stdout or b'' output += result.stderr or b'' if testing: # no cov click.echo(output.decode()) sys.exit(test_result.returncode)
def install(packages, no_detect, env_name, editable, global_install, admin, quiet): """If the option --env is supplied, the install will be applied using that named virtual env. Unless the option --global is selected, the install will only affect the current user. Of course, this will have no effect if a virtual env is in use. The desired name of the admin user can be set with the `_DEFAULT_ADMIN_` environment variable. With no packages selected, this will install using a `setup.py` in the current directory. If no --env is chosen, this will attempt to detect a project and use its virtual env before resorting to the default pip. No project detection will occur if a virtual env is active. """ packages = packages or ['.'] # Windows' `runas` allows only a single argument for the # command so we catch this case and turn our command into # a string later. windows_admin_command = None if editable: packages = ['-e', *packages] if env_name: venv_dir = os.path.join(get_venv_dir(), env_name) if not os.path.exists(venv_dir): echo_failure( 'Virtual env named `{}` does not exist.'.format(env_name)) sys.exit(1) with venv(venv_dir): command = [get_proper_pip(), 'install', *packages ] + (['-q'] if quiet else []) echo_waiting('Installing in virtual env `{}`...'.format(env_name)) result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL) elif not venv_active() and not no_detect and is_project(): venv_dir = os.path.join(os.getcwd(), 'venv') if not is_venv(venv_dir): echo_info('A project has been detected!') echo_waiting('Creating a dedicated virtual env... ', nl=False) create_venv(venv_dir) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing this project in the virtual env... ', nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!') with venv(venv_dir): command = [get_proper_pip(), 'install', *packages ] + (['-q'] if quiet else []) echo_waiting('Installing for this project...') result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL) else: command = [get_proper_pip(), 'install'] + (['-q'] if quiet else []) if not venv_active(): # no cov if global_install: if not admin: if ON_WINDOWS: windows_admin_command = get_admin_command() else: command = get_admin_command() + command else: command.append('--user') command.extend(packages) if windows_admin_command: # no cov command = windows_admin_command + [' '.join(command)] echo_waiting('Installing...') result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL) sys.exit(result.returncode)
def init(name, no_env, pyname, pypath, global_packages, env_name, basic, cli, licenses): """Creates a new Python project in the current directory. Values from your config file such as `name` and `pyversions` will be used to help populate fields. You can also specify things like the readme format and which CI service files to create. All options override the config file. By default a virtual env will be created in the project directory and will install the project locally so any edits will auto-update the installation. You can also locally install the created project in other virtual envs using the --env option. Here is an example using an unmodified config file: \b $ hatch init my-app Created project `my-app` here $ tree --dirsfirst . . ├── my_app │ └── __init__.py ├── tests │ └── __init__.py ├── LICENSE-APACHE ├── LICENSE-MIT ├── MANIFEST.in ├── README.rst ├── requirements.txt ├── setup.py └── tox.ini 2 directories, 8 files """ try: settings = load_settings() except FileNotFoundError: settings = {} echo_warning( 'Unable to locate config file; try `hatch config --restore`. ' 'The default project structure will be used.') if basic: settings['basic'] = True if licenses: settings['licenses'] = licenses.split(',') settings['cli'] = cli d = os.getcwd() create_package(d, name, settings) echo_success('Created project `{}` here'.format(name)) venvs = env_name.split('/') if env_name else [] if (venvs or not no_env) and pyname: try: settings = load_settings() except FileNotFoundError: # no cov echo_failure( 'Unable to locate config file. Try `hatch config --restore`.') sys.exit(1) pypath = settings.get('pypaths', {}).get(pyname, None) if not pypath: echo_failure( 'Unable to find a Python path named `{}`.'.format(pyname)) sys.exit(1) if not no_env: venv_dir = os.path.join(d, 'venv') echo_waiting('Creating its own virtual env... ', nl=False) create_venv(venv_dir, pypath=pypath, use_global=global_packages) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing locally in the virtual env... ', nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!') for vname in venvs: venv_dir = os.path.join(VENV_DIR, vname) if not os.path.exists(venv_dir): echo_waiting('Creating virtual env `{}`... '.format(vname), nl=False) create_venv(venv_dir, pypath=pypath, use_global=global_packages) echo_success('complete!') with venv(venv_dir): echo_waiting( 'Installing locally in virtual env `{}`... '.format(vname), nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!')
def shell(env_name, command, shell_name, temp_env, pyname, pypath, global_packages): # no cov """Activates or sends a command to a virtual environment. A default shell name (or command) can be specified in the config file entry `shell` or the environment variable `SHELL`. If there is no entry, env var, nor shell option provided, a system default will be used: `cmd` on Windows, `bash` otherwise. Any arguments provided after the first will be sent to the virtual env as a command without activating it. If there is only the env without args, it will be activated similarly to how you are accustomed. The name of the virtual env to use must be omitted if using the --temp env option. If no env is chosen, this will attempt to detect a project and activate its virtual env. To run a command in a project's virtual env, use `.` as the env name. Activation will not do anything to your current shell, but will rather spawn a subprocess to avoid any unwanted strangeness occurring in your current environment. If you would like to learn more about the benefits of this approach, be sure to read https://gist.github.com/datagrok/2199506. To leave a virtual env, type `exit`, or you can do `Ctrl+D` on non-Windows machines. `use` is an alias for this command. \b Activation: $ hatch env -ll Virtual environments found in `/home/ofek/.virtualenvs`: \b fast -> Version: 3.5.3 Implementation: PyPy my-app -> Version: 3.5.2 Implementation: CPython old -> Version: 2.7.12 Implementation: CPython $ which python /usr/bin/python $ hatch shell my-app (my-app) $ which python /home/ofek/.virtualenvs/my-app/bin/python \b Commands: $ hatch shell my-app pip list --format=columns Package Version ---------- ------- pip 9.0.1 setuptools 36.3.0 wheel 0.29.0 $ hatch shell my-app hatch install -q requests six $ hatch shell my-app pip list --format=columns Package Version ---------- ----------- certifi 2017.7.27.1 chardet 3.0.4 idna 2.6 pip 9.0.1 requests 2.18.4 setuptools 36.3.0 six 1.10.0 urllib3 1.22 wheel 0.29.0 \b Temporary env: $ hatch shell -t Already using interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /tmp/tmpzg73untp/Ihqd/bin/python3 Also creating executable in /tmp/tmpzg73untp/Ihqd/bin/python Installing setuptools, pip, wheel...done. $ which python /tmp/tmpzg73untp/Ihqd/bin/python """ venv_dir = None if resolve_path(env_name) == os.getcwd(): env_name = '' if not (env_name or temp_env): if is_project(): venv_dir = os.path.join(os.getcwd(), 'venv') if not is_venv(venv_dir): echo_info('A project has been detected!') echo_waiting('Creating a dedicated virtual env... ', nl=False) create_venv(venv_dir, use_global=global_packages) echo_success('complete!') with venv(venv_dir): echo_waiting('Installing this project in the virtual env... ', nl=False) install_packages(['-q', '-e', '.']) echo_success('complete!') else: echo_failure('No project found.') sys.exit(1) if env_name and temp_env: echo_failure('Cannot use more than one virtual env at a time!') sys.exit(1) if not command and '_HATCHING_' in os.environ: echo_failure( 'Virtual environments cannot be nested, sorry! To leave ' 'the current one type `exit` or press `Ctrl+D`.' ) sys.exit(1) if temp_env: if pyname: try: settings = load_settings() except FileNotFoundError: echo_failure('Unable to locate config file. Try `hatch config --restore`.') sys.exit(1) pypath = settings.get('pypaths', {}).get(pyname, None) if not pypath: echo_failure('Unable to find a Python path named `{}`.'.format(pyname)) sys.exit(1) temp_dir = TemporaryDirectory() env_name = get_random_venv_name() venv_dir = os.path.join(temp_dir.name, env_name) echo_waiting('Creating a temporary virtual env named `{}`...'.format(env_name)) create_venv(venv_dir, pypath=pypath, use_global=global_packages, verbose=True) else: temp_dir = None venv_dir = venv_dir or os.path.join(get_venv_dir(), env_name) if not os.path.exists(venv_dir): echo_failure('Virtual env named `{}` does not exist.'.format(env_name)) sys.exit(1) result = None try: if command: with venv(venv_dir): echo_waiting('Running `{}` in {}...'.format( ' '.join(c if len(c.split()) == 1 else '"{}"'.format(c) for c in command), '`{}`'.format(env_name) if env_name else "this project's env" )) result = subprocess.run(command, shell=NEED_SUBPROCESS_SHELL).returncode else: with venv(venv_dir) as exe_dir: result = run_shell(exe_dir, shell_name) finally: result = 1 if result is None else result if temp_dir is not None: temp_dir.cleanup() sys.exit(result)