def FORCE_temp_prefix(name=None, use_restricted_unicode=False): _temp_prefix = _get_temp_prefix( name=name, use_restricted_unicode=use_restricted_unicode) rm_rf(_temp_prefix) os.makedirs(_temp_prefix) assert isdir(_temp_prefix) return _temp_prefix
def test_multi_channel_explicit(self): """ When try to import from txt every package should come from same channel """ with make_temp_env("python=3") as prefix: assert exists(join(prefix, PYTHON_BINARY)) assert_package_is_installed(prefix, 'python-3') run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge") assert_package_is_installed(prefix, "six") output, error = run_command(Commands.LIST, prefix, "--explicit") self.assertIn("conda-forge", output) try: with tempfile.NamedTemporaryFile(mode="w", suffix="txt", delete=False) as env_txt: env_txt.write(output) env_txt.close() prefix2 = make_temp_prefix() run_command(Commands.CREATE, prefix2, "--file " + env_txt.name) assert_package_is_installed(prefix2, "python") assert_package_is_installed(prefix2, "six") output2, _ = run_command(Commands.LIST, prefix2, "--explicit") self.assertEqual(output, output2) finally: rm_rf(env_txt.name)
def make_temp_env(*packages, **kwargs): name = kwargs.pop("name", None) use_restricted_unicode = kwargs.pop("use_restricted_unicode", False) prefix = kwargs.pop("prefix", None) or _get_temp_prefix( name=name, use_restricted_unicode=use_restricted_unicode) clean_prefix = kwargs.pop("clean_prefix", None) if clean_prefix: if os.path.exists(prefix): rm_rf(prefix) if not isdir(prefix): make_temp_prefix(name, use_restricted_unicode, prefix) with disable_logger("fetch"), disable_logger("dotupdate"): try: # try to clear any config that's been set by other tests # CAUTION :: This does not partake in the context stack management code # of env_{var,vars,unmodified} and, when used in conjunction # with that code, this *must* be called first. reset_context([os.path.join(prefix + os.sep, "condarc")]) run_command(Commands.CREATE, prefix, *packages, **kwargs) yield prefix finally: if "CONDA_TEST_SAVE_TEMPS" not in os.environ: rmtree(prefix, ignore_errors=True) else: log.warning( "CONDA_TEST_SAVE_TEMPS :: retaining make_temp_env {}". format(prefix))
def test_multi_channel_explicit(self): """ When try to import from txt every package should come from same channel """ with make_temp_env("python=3.5") as prefix: assert exists(join(prefix, PYTHON_BINARY)) assert_package_is_installed(prefix, 'python-3') run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge") assert_package_is_installed(prefix, "six") output, error = run_command(Commands.LIST, prefix, "--explicit") self.assertIn("conda-forge", output) try: with tempfile.NamedTemporaryFile(mode="w", suffix="txt", delete=False) as env_txt: env_txt.write(output) env_txt.close() prefix2 = make_temp_prefix() run_command(Commands.CREATE, prefix2, "--file " + env_txt.name) assert_package_is_installed(prefix2, "python") assert_package_is_installed(prefix2, "six") output2, _ = run_command(Commands.LIST, prefix2, "--explicit") self.assertEqual(output, output2) finally: rm_rf(env_txt.name)
def test_multi_channel_explicit(self): """ When try to import from txt every package should come from same channel """ with make_temp_env("python=3.5") as prefix: assert exists(join(prefix, PYTHON_BINARY)) assert_package_is_installed(prefix, 'python-3') run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge") assert package_is_installed(prefix, "conda-forge::six") output, error = run_command(Commands.LIST, prefix, "--explicit") assert not error assert "conda-forge" in output urls1 = set(url for url in output.split() if url.startswith("http")) try: with tempfile.NamedTemporaryFile(mode="w", suffix="txt", delete=False) as env_txt: env_txt.write(output) env_txt.close() prefix2 = make_temp_prefix() run_command(Commands.CREATE, prefix2, "--file " + env_txt.name) assert package_is_installed(prefix2, "python") assert package_is_installed(prefix2, "six") output2, error2 = run_command(Commands.LIST, prefix2, "--explicit") assert not error2 urls2 = set(url for url in output2.split() if url.startswith("http")) assert urls1 == urls2 finally: rm_rf(env_txt.name)
def test_local_channel(self): conda_bld_path = join(gettempdir(), 'conda-bld') mkdir_p(conda_bld_path) try: with env_var('CONDA_CROOT', conda_bld_path, reset_context): Channel._reset_state() channel = Channel('local') assert channel._channels[0].name.rsplit('/', 1)[-1] == 'conda-bld' assert channel.channel_name == "local" assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme is None assert channel.canonical_name == "local" local_channel_first_subchannel = channel._channels[0].name channel = Channel(local_channel_first_subchannel) assert channel.channel_name == local_channel_first_subchannel assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme == "file" assert channel.canonical_name == "local" assert channel.urls() == Channel(local_channel_first_subchannel).urls() assert channel.urls()[0].startswith('file:///') finally: rm_rf(conda_bld_path)
def test_multi_channel_explicit(self): """ When try to import from txt every package should come from same channel """ with make_temp_env("python=3.5") as prefix: assert exists(join(prefix, PYTHON_BINARY)) assert package_is_installed(prefix, 'python=3') run_command(Commands.INSTALL, prefix, "six", "-c", "conda-forge") assert package_is_installed(prefix, "conda-forge::six") output, error = run_command(Commands.LIST, prefix, "--explicit") assert not error assert "conda-forge" in output urls1 = set(url for url in output.split() if url.startswith("http")) try: with tempfile.NamedTemporaryFile(mode="w", suffix="txt", delete=False) as env_txt: env_txt.write(output) env_txt.close() prefix2 = make_temp_prefix() run_command(Commands.CREATE, prefix2, "--file " + env_txt.name) assert package_is_installed(prefix2, "python") assert package_is_installed(prefix2, "six") output2, error2 = run_command(Commands.LIST, prefix2, "--explicit") assert not error2 urls2 = set(url for url in output2.split() if url.startswith("http")) assert urls1 == urls2 finally: rm_rf(env_txt.name)
def tearDown(self): rm_rf("environment.yml") run_env_command(Commands.ENV_REMOVE, test_env_name_1) run_env_command(Commands.ENV_REMOVE, test_env_name_42) run_env_command(Commands.ENV_REMOVE, test_env_name_pip) for env_nb in range(1, 6): run_env_command(Commands.ENV_REMOVE, "envjson-{0}".format(env_nb))
def test_clean_tarballs_and_packages(self): pkgs_dir = PackageCache.first_writable().pkgs_dir mkdir_p(pkgs_dir) pkgs_dir_hold = pkgs_dir + '_hold' try: shutil.move(pkgs_dir, pkgs_dir_hold) with make_temp_env("flask") as prefix: pkgs_dir_contents = [join(pkgs_dir, d) for d in os.listdir(pkgs_dir)] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] pkgs_dir_tarballs = [f for f in pkgs_dir_contents if f.endswith('.tar.bz2')] assert any(basename(d).startswith('flask-') for d in pkgs_dir_dirs) assert any(basename(f).startswith('flask-') for f in pkgs_dir_tarballs) run_command(Commands.CLEAN, prefix, "--packages --yes") run_command(Commands.CLEAN, prefix, "--tarballs --yes") pkgs_dir_contents = [join(pkgs_dir, d) for d in os.listdir(pkgs_dir)] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] pkgs_dir_tarballs = [f for f in pkgs_dir_contents if f.endswith('.tar.bz2')] assert any(basename(d).startswith('flask-') for d in pkgs_dir_dirs) assert not any(basename(f).startswith('flask-') for f in pkgs_dir_tarballs) run_command(Commands.CLEAN, prefix, "--packages --yes") pkgs_dir_contents = [join(pkgs_dir, d) for d in os.listdir(pkgs_dir)] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] assert not any(basename(d).startswith('flask-') for d in pkgs_dir_dirs) finally: rm_rf(pkgs_dir) shutil.move(pkgs_dir_hold, pkgs_dir) PackageCache.clear()
def prefix(tmpdir): prefix = tmpdir.mkdir("cli_install_prefix") test_env = tmpdir.mkdir("cli_install_test_env") run_command(Commands.CREATE, str(prefix), 'python=3.7') yield str(prefix), str(test_env) rm_rf(prefix) rm_rf(test_env)
def execute(args, parser): from conda.base.context import context name = args.remote_definition or args.name try: spec = specs.detect(name=name, filename=get_filename(args.file), directory=os.getcwd()) env = spec.environment # FIXME conda code currently requires args to have a name or prefix # don't overwrite name if it's given. gh-254 if args.prefix is None and args.name is None: args.name = env.name except exceptions.SpecNotFound: raise prefix = get_prefix(args, search=False) if args.force and prefix != context.root_prefix and os.path.exists(prefix): rm_rf(prefix) cli_install.check_prefix(prefix, json=args.json) # TODO, add capability # common.ensure_override_channels_requires_channel(args) # channel_urls = args.channel or () result = {"conda": None, "pip": None} if len(env.dependencies.items()) == 0: installer_type = "conda" pkg_specs = [] installer = get_installer(installer_type) result[installer_type] = installer.install(prefix, pkg_specs, args, env) else: for installer_type, pkg_specs in env.dependencies.items(): try: installer = get_installer(installer_type) result[installer_type] = installer.install( prefix, pkg_specs, args, env) except InvalidInstaller: sys.stderr.write( textwrap.dedent(""" Unable to install package for {0}. Please double check and ensure your dependencies file has the correct spelling. You might also try installing the conda-env-{0} package to see if provides the required installer. """).lstrip().format(installer_type)) return -1 if env.variables: pd = PrefixData(prefix) pd.set_environment_env_vars(env.variables) touch_nonadmin(prefix) print_result(args, prefix, result)
def tempdir(): prefix = create_temp_location() try: os.makedirs(prefix) yield prefix finally: if lexists(prefix): rm_rf(prefix)
def test_CompilePycAction_noarch_python(self): if not softlink_supported(__file__, self.prefix) and on_win: pytest.skip("softlink not supported") target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), ] axns = CompilePycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] assert axn.source_full_path == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert axn.target_full_path == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(axn.source_full_path)) with open(axn.source_full_path, 'w') as fh: fh.write("value = 42\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(axn.target_full_path) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(axn.source_full_path) assert not isfile(axn.source_full_path) if (3,) > sys.version_info >= (3, 5): # we're probably dropping py34 support soon enough anyway imported_pyc_file = load_python_file(axn.target_full_path) assert imported_pyc_file.value == 42 axn.reverse() assert not isfile(axn.target_full_path)
def tearDown(self): rm_rf(self.prefix) if not (on_win and PY2): # this assertion fails for the Softlink action windows tests # line 141 in backoff_rmdir # exp_backoff_fn(rmtree, path, onerror=retry, max_tries=max_tries) # leaves a directory self.prefix\\Scripts that cannot be accessed or removed assert not lexists(self.prefix) rm_rf(self.pkgs_dir) assert not lexists(self.pkgs_dir)
def tempdir(): tempdirdir = gettempdir() dirname = str(uuid4())[:8] prefix = join(tempdirdir, dirname) try: os.makedirs(prefix) yield prefix finally: if lexists(prefix): rm_rf(prefix)
def execute(args, parser): from conda.base.context import context name = args.remote_definition or args.name try: spec = specs.detect(name=name, filename=expand(args.file), directory=os.getcwd()) env = spec.environment # FIXME conda code currently requires args to have a name or prefix # don't overwrite name if it's given. gh-254 if args.prefix is None and args.name is None: args.name = env.name except exceptions.SpecNotFound: raise prefix = get_prefix(args, search=False) if args.force and prefix != context.root_prefix and os.path.exists(prefix): rm_rf(prefix) cli_install.check_prefix(prefix, json=args.json) # TODO, add capability # common.ensure_override_channels_requires_channel(args) # channel_urls = args.channel or () # # special case for empty environment # if not env.dependencies: # from conda.install import symlink_conda # symlink_conda(prefix, context.root_dir) for installer_type, pkg_specs in env.dependencies.items(): try: installer = get_installer(installer_type) installer.install(prefix, pkg_specs, args, env) except InvalidInstaller: sys.stderr.write( textwrap.dedent(""" Unable to install package for {0}. Please double check and ensure your dependencies file has the correct spelling. You might also try installing the conda-env-{0} package to see if provides the required installer. """).lstrip().format(installer_type)) return -1 touch_nonadmin(prefix) delete_trash() if not args.json: cli_install.print_activate(args.name if args.name else prefix)
def make_temp_condarc(value=None): try: tempfile = NamedTemporaryFile(suffix='.yml', delete=False) tempfile.close() temp_path = tempfile.name if value: with open(temp_path, 'w') as f: f.write(value) reset_context([temp_path]) yield temp_path finally: rm_rf(temp_path)
def execute(args, parser): from conda.base.context import context name = args.remote_definition or args.name try: spec = specs.detect(name=name, filename=expand(args.file), directory=os.getcwd()) env = spec.environment # FIXME conda code currently requires args to have a name or prefix # don't overwrite name if it's given. gh-254 if args.prefix is None and args.name is None: args.name = env.name except exceptions.SpecNotFound: raise prefix = get_prefix(args, search=False) if args.force and prefix != context.root_prefix and os.path.exists(prefix): rm_rf(prefix) cli_install.check_prefix(prefix, json=args.json) # TODO, add capability # common.ensure_override_channels_requires_channel(args) # channel_urls = args.channel or () # # special case for empty environment # if not env.dependencies: # from conda.install import symlink_conda # symlink_conda(prefix, context.root_dir) for installer_type, pkg_specs in env.dependencies.items(): try: installer = get_installer(installer_type) installer.install(prefix, pkg_specs, args, env) except InvalidInstaller: sys.stderr.write(textwrap.dedent(""" Unable to install package for {0}. Please double check and ensure your dependencies file has the correct spelling. You might also try installing the conda-env-{0} package to see if provides the required installer. """).lstrip().format(installer_type) ) return -1 touch_nonadmin(prefix) delete_trash() if not args.json: cli_install.print_activate(args.name if args.name else prefix)
def test_CompilePycAction_noarch_python(self): target_python_version = '%d.%d' % sys.version_info[:2] sp_dir = get_python_site_packages_short_path(target_python_version) transaction_context = { 'target_python_version': target_python_version, 'target_site_packages_short_path': sp_dir, } package_info = AttrDict(package_metadata=AttrDict(noarch=AttrDict(type=NoarchType.python))) file_link_actions = [ AttrDict( source_short_path='site-packages/something.py', target_short_path=get_python_noarch_target_path('site-packages/something.py', sp_dir), ), AttrDict( # this one shouldn't get compiled source_short_path='something.py', target_short_path=get_python_noarch_target_path('something.py', sp_dir), ), ] axns = CompilePycAction.create_actions(transaction_context, package_info, self.prefix, None, file_link_actions) assert len(axns) == 1 axn = axns[0] assert axn.source_full_path == join(self.prefix, win_path_ok(get_python_noarch_target_path('site-packages/something.py', sp_dir))) assert axn.target_full_path == join(self.prefix, win_path_ok(pyc_path(get_python_noarch_target_path('site-packages/something.py', sp_dir), target_python_version))) # make .py file in prefix that will be compiled mkdir_p(dirname(axn.source_full_path)) with open(axn.source_full_path, 'w') as fh: fh.write("value = 42\n") # symlink the current python python_full_path = join(self.prefix, get_python_short_path(target_python_version)) mkdir_p(dirname(python_full_path)) create_link(sys.executable, python_full_path, LinkType.softlink) axn.execute() assert isfile(axn.target_full_path) # remove the source .py file so we're sure we're importing the pyc file below rm_rf(axn.source_full_path) assert not isfile(axn.source_full_path) if (3, ) > sys.version_info >= (3, 5): # we're probably dropping py34 support soon enough anyway imported_pyc_file = load_python_file(axn.target_full_path) assert imported_pyc_file.value == 42 axn.reverse() assert not isfile(axn.target_full_path)
def test_remove_dir(): with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) _try_open(test_path) assert isfile(test_path) assert isdir(td) assert not islink(test_path) assert rm_rf(td) assert rm_rf(test_path) assert not isdir(td) assert not isfile(test_path) assert not lexists(test_path)
def _make_single_record(self, package_filename): if not package_filename.endswith(CONDA_TARBALL_EXTENSION): package_filename += CONDA_TARBALL_EXTENSION package_tarball_full_path = join(self.pkgs_dir, package_filename) log.trace("adding to package cache %s", package_tarball_full_path) extracted_package_dir = package_tarball_full_path[:-len( CONDA_TARBALL_EXTENSION)] # try reading info/repodata_record.json try: repodata_record = read_repodata_json(extracted_package_dir) package_cache_record = PackageCacheRecord.from_objects( repodata_record, package_tarball_full_path=package_tarball_full_path, extracted_package_dir=extracted_package_dir, ) except (IOError, OSError): try: index_json_record = read_index_json(extracted_package_dir) except (IOError, OSError): try: if self.is_writable: extract_tarball(package_tarball_full_path, extracted_package_dir) index_json_record = read_index_json( extracted_package_dir) else: index_json_record = read_index_json_from_tarball( package_tarball_full_path) except (EOFError, ReadError): # EOFError: Compressed file ended before the end-of-stream marker was reached # tarfile.ReadError: file could not be opened successfully rm_rf(package_tarball_full_path) return None if isfile(package_tarball_full_path): md5 = compute_md5sum(package_tarball_full_path) else: md5 = None url = first(self._urls_data, lambda x: basename(x) == package_filename) package_cache_record = PackageCacheRecord.from_objects( index_json_record, url=url, md5=md5, package_tarball_full_path=package_tarball_full_path, extracted_package_dir=extracted_package_dir, ) return package_cache_record
def _pip_install_via_requirements(prefix, specs, args, *_, **kwargs): """ Installs the pip dependencies in specs using a temporary pip requirements file. Args ---- prefix: string The path to the python and pip executables. specs: iterable of strings Each element should be a valid pip dependency. See: https://pip.pypa.io/en/stable/user_guide/#requirements-files https://pip.pypa.io/en/stable/reference/pip_install/#requirements-file-format """ try: pip_workdir = op.dirname(op.abspath(args.file)) except AttributeError: pip_workdir = None requirements = None try: # Generate the temporary requirements file requirements = tempfile.NamedTemporaryFile(mode='w', prefix='condaenv.', suffix='.requirements.txt', dir=pip_workdir, delete=False) requirements.write('\n'.join(specs)) requirements.close() # pip command line... args, pip_version = pip_args(prefix) if args is None: return script_caller = None pip_cmd = args + ['install', '-r', requirements.name] cmd = " ".join(pip_cmd) script_caller, command_args = wrap_subprocess_call( on_win, context.root_prefix, prefix, cmd) # ...run it process = subprocess.Popen(command_args, cwd=pip_workdir, universal_newlines=True) process.communicate() if process.returncode != 0: raise CondaValueError("pip returned an error") finally: # Win/Appveyor does not like it if we use context manager + delete=True. # So we delete the temporary file in a finally block. if requirements is not None and op.isfile(requirements.name): os.remove(requirements.name) if script_caller is not None: rm_rf(script_caller)
def pip_subprocess(args, prefix, env=None, cwd=None): script_caller, command_args = wrap_subprocess_call(on_win, context.root_prefix, prefix, ' '.join(['pip'] + args)) process = subprocess.Popen(command_args, cwd=cwd or prefix, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) stdout, stderr = process.communicate() if script_caller is not None: rm_rf(script_caller) if process.returncode != 0: raise CondaEnvException("Pip subcommand failed with \n" "output: {}\nerror: {}".format(stdout, stderr)) return stdout, stderr
def test_install_mkdir(self): try: prefix = make_temp_prefix() assert isdir(prefix) run_command(Commands.INSTALL, prefix, "python=3.5", "--mkdir") assert_package_is_installed(prefix, "python-3.5") rm_rf(prefix) assert not isdir(prefix) run_command(Commands.INSTALL, prefix, "python=3.5", "--mkdir") assert_package_is_installed(prefix, "python-3.5") finally: rmtree(prefix, ignore_errors=True)
def test_clean_tarballs_and_packages(self): pkgs_dir = PackageCache.first_writable().pkgs_dir mkdir_p(pkgs_dir) pkgs_dir_hold = pkgs_dir + '_hold' try: shutil.move(pkgs_dir, pkgs_dir_hold) with make_temp_env("flask") as prefix: pkgs_dir_contents = [ join(pkgs_dir, d) for d in os.listdir(pkgs_dir) ] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] pkgs_dir_tarballs = [ f for f in pkgs_dir_contents if f.endswith('.tar.bz2') ] assert any( basename(d).startswith('flask-') for d in pkgs_dir_dirs) assert any( basename(f).startswith('flask-') for f in pkgs_dir_tarballs) run_command(Commands.CLEAN, prefix, "--packages --yes") run_command(Commands.CLEAN, prefix, "--tarballs --yes") pkgs_dir_contents = [ join(pkgs_dir, d) for d in os.listdir(pkgs_dir) ] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] pkgs_dir_tarballs = [ f for f in pkgs_dir_contents if f.endswith('.tar.bz2') ] assert any( basename(d).startswith('flask-') for d in pkgs_dir_dirs) assert not any( basename(f).startswith('flask-') for f in pkgs_dir_tarballs) run_command(Commands.CLEAN, prefix, "--packages --yes") pkgs_dir_contents = [ join(pkgs_dir, d) for d in os.listdir(pkgs_dir) ] pkgs_dir_dirs = [d for d in pkgs_dir_contents if isdir(d)] assert not any( basename(d).startswith('flask-') for d in pkgs_dir_dirs) finally: rm_rf(pkgs_dir) shutil.move(pkgs_dir_hold, pkgs_dir) PackageCache.clear()
def test_remove_link_to_dir(): with tempdir() as td: dst_link = join(td, "test_link") src_dir = join(td, "test_dir") _write_file(src_dir, "welcome to the ministry of silly walks") symlink(src_dir, dst_link) assert not islink(src_dir) assert islink(dst_link) assert rm_rf(dst_link) assert not isdir(dst_link) assert not islink(dst_link) assert rm_rf(src_dir) assert not isdir(src_dir) assert not islink(src_dir) assert not lexists(dst_link)
def test_remove_link_to_file(): with tempdir() as td: dst_link = join(td, "test_link") src_file = join(td, "test_file") _write_file(src_file, "welcome to the ministry of silly walks") os.symlink(src_file, dst_link) assert isfile(src_file) assert not islink(src_file) assert islink(dst_link) assert rm_rf(dst_link) assert isfile(src_file) assert rm_rf(src_file) assert not isfile(src_file) assert not islink(dst_link) assert not lexists(dst_link)
def test_local_file_adapter_200(self): test_path = None try: with NamedTemporaryFile(delete=False) as fh: test_path = fh.name fh.write(ensure_binary('{"content": "file content"}')) test_url = path_to_url(test_path) session = CondaSession() r = session.get(test_url) r.raise_for_status() assert r.status_code == 200 assert r.json()['content'] == "file content" finally: if test_path is not None: rm_rf(test_path)
def test_conda_bld_path(self): conda_bld_path = join(gettempdir(), 'conda-bld') conda_bld_url = path_to_url(conda_bld_path) try: mkdir_p(conda_bld_path) with env_var('CONDA_BLD_PATH', conda_bld_path, stack_callback=conda_tests_ctxt_mgmt_def_pol): assert len(context.conda_build_local_paths) >= 1 assert context.conda_build_local_paths[0] == conda_bld_path channel = Channel('local') assert channel.channel_name == "local" assert channel.channel_location is None assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme is None assert channel.canonical_name == "local" assert channel.url() is None urls = list( concat(( join_url(url, context.subdir), join_url(url, 'noarch'), ) for url in context.conda_build_local_urls)) assert channel.urls() == urls channel = Channel(conda_bld_url) assert channel.canonical_name == "local" assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme == "file" assert channel.urls() == [ join_url(conda_bld_url, context.subdir), join_url(conda_bld_url, 'noarch'), ] assert channel.url() == join_url(conda_bld_url, context.subdir) assert channel.channel_name.lower() == win_path_backout( conda_bld_path).lstrip('/').lower() assert channel.channel_location == '' # location really is an empty string; all path information is in channel_name assert channel.canonical_name == "local" finally: rm_rf(conda_bld_path)
def create(args, parser): if is_conda_environment(context.target_prefix): if paths_equal(context.target_prefix, context.root_prefix): raise CondaValueError("The target prefix is the base prefix. Aborting.") confirm_yn("WARNING: A conda environment already exists at '%s'\n" "Remove existing environment" % context.target_prefix, default='no', dry_run=False) log.info("Removing existing environment %s", context.target_prefix) rm_rf(context.target_prefix) elif isdir(context.target_prefix): confirm_yn("WARNING: A directory already exists at the target location '%s'\n" "but it is not a conda environment.\n" "Continue creating environment" % context.target_prefix, default='no', dry_run=False) install(args, parser, 'create')
def test_rm_rf_does_not_follow_symlinks(): with TemporaryDirectory() as tmp: # make a file in some temp folder real_file = os.path.join(tmp, 'testfile') with open(real_file, 'w') as f: f.write('weee') # make a subfolder subdir = os.path.join(tmp, 'subfolder') os.makedirs(subdir) # link to the file in the subfolder link_path = join(subdir, 'file_link') create_link(real_file, link_path, link_type=LinkType.softlink) assert islink(link_path) # rm_rf the subfolder rm_rf(subdir) # assert that the file still exists in the root folder assert os.path.isfile(real_file)
def test_remove_link_to_file(): with tempdir() as td: dst_link = join(td, "test_link") src_file = join(td, "test_file") _write_file(src_file, "welcome to the ministry of silly walks") if not softlink_supported(src_file, td) and on_win: pytest.skip("softlink not supported") symlink(src_file, dst_link) assert isfile(src_file) assert not islink(src_file) assert islink(dst_link) assert rm_rf(dst_link) assert isfile(src_file) assert rm_rf(src_file) assert not isfile(src_file) assert not islink(dst_link) assert not lexists(dst_link)
def test_remove_dir(tmpdir): test_dir = "test" tmpdir.mkdir(test_dir) path = join(text_type(tmpdir), test_dir) assert isdir(path) assert not islink(path) assert rm_rf(path) is True assert not isdir(path) assert not lexists(path)
def test_remove_file_to_trash(): with tempdir() as td: test_path = join(td, 'test_path') touch(test_path) assert isfile(test_path) _try_open(test_path) _make_read_only(test_path) pytest.raises((IOError, OSError), _try_open, test_path) assert rm_rf(test_path) assert not isfile(test_path)
def test_conda_bld_path(self): conda_bld_path = join(gettempdir(), 'conda-bld') conda_bld_url = path_to_url(conda_bld_path) try: mkdir_p(conda_bld_path) with env_var('CONDA_BLD_PATH', conda_bld_path, reset_context): assert len(context.conda_build_local_paths) >= 1 assert context.conda_build_local_paths[0] == conda_bld_path channel = Channel('local') assert channel.channel_name == "local" assert channel.channel_location is None assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme is None assert channel.canonical_name == "local" assert channel.url() is None urls = list(concat(( join_url(url, context.subdir), join_url(url, 'noarch'), ) for url in context.conda_build_local_urls)) assert channel.urls() == urls channel = Channel(conda_bld_url) assert channel.canonical_name == "local" assert channel.platform is None assert channel.package_filename is None assert channel.auth is None assert channel.token is None assert channel.scheme == "file" assert channel.urls() == [ join_url(conda_bld_url, context.subdir), join_url(conda_bld_url, 'noarch'), ] assert channel.url() == join_url(conda_bld_url, context.subdir) assert channel.channel_name.lower() == win_path_backout(conda_bld_path).lstrip('/').lower() assert channel.channel_location == '' # location really is an empty string; all path information is in channel_name assert channel.canonical_name == "local" finally: rm_rf(conda_bld_path)
def test_remove_link_to_dir(tmpdir): dst_link = join(text_type(tmpdir), "test_link") src_dir = join(text_type(tmpdir), "test_dir") tmpdir.mkdir("test_dir") os.symlink(src_dir, dst_link) assert isdir(src_dir) assert not islink(src_dir) assert islink(dst_link) assert rm_rf(dst_link) is True assert isdir(src_dir) # make sure the directory is still there assert not isdir(dst_link) assert not lexists(dst_link)
def test_remove_link_to_dir(): with tempdir() as td: dst_link = join(td, "test_link") src_dir = join(td, "test_dir") test_file = join(td, "test_file") mkdir_p(src_dir) touch(test_file) assert isdir(src_dir) assert not islink(src_dir) assert not islink(dst_link) if not softlink_supported(test_file, td) and on_win: pytest.skip("softlink not supported") symlink(src_dir, dst_link) assert islink(dst_link) assert rm_rf(dst_link) assert not isdir(dst_link) assert not islink(dst_link) assert not lexists(dst_link) assert isdir(src_dir) assert rm_rf(src_dir) assert not isdir(src_dir) assert not islink(src_dir)
def test_remove_link_to_file(tmpdir): dst_link = join(text_type(tmpdir), "test_link") src_file = join(text_type(tmpdir), "test_file") _write_file(src_file, "welcome to the ministry of silly walks") os.symlink(src_file, dst_link) assert rm_rf(dst_link) is True
def test_remove_dir(tmpdir): test_dir = "test" tmpdir.mkdir(test_dir) path = join(text_type(tmpdir), test_dir) assert rm_rf(path) is True
def test_remove_file_to_trash(tmpdir): test_file = "test.txt" path = join(text_type(tmpdir), test_file) _write_file(path, "welcome to the ministry of silly walks") assert rm_rf(path) is True
def make_temp_envs_dir(): envs_dir = mkdtemp() try: yield envs_dir finally: rm_rf(envs_dir)
def test_dist_with_channel_url(self): # standard named channel url = "https://repo.anaconda.com/pkgs/free/win-64/spyder-app-2.3.8-py27_0.tar.bz2" d = Dist(url) assert d.channel == 'defaults' assert d.name == 'spyder-app' assert d.version == '2.3.8' assert d.build_string == 'py27_0' assert d.to_url() == url assert d.is_channel is True # standard url channel url = "https://not.real.continuum.io/pkgs/free/win-64/spyder-app-2.3.8-py27_0.tar.bz2" d = Dist(url) assert d.channel == 'defaults' # because pkgs/anaconda is in defaults assert d.name == 'spyder-app' assert d.version == '2.3.8' assert d.build_string == 'py27_0' assert d.to_url() == url assert d.is_channel is True # another standard url channel url = "https://not.real.continuum.io/not/anaconda/win-64/spyder-app-2.3.8-py27_0.tar.bz2" d = Dist(url) assert d.channel == 'https://not.real.continuum.io/not/anaconda' assert d.name == 'spyder-app' assert d.version == '2.3.8' assert d.build_string == 'py27_0' assert d.to_url() == url assert d.is_channel is True # local file url that is a named channel conda_bld_path = join(gettempdir(), 'conda-bld') try: mkdir_p(conda_bld_path) with env_var('CONDA_BLD_PATH', conda_bld_path, stack_callback=conda_tests_ctxt_mgmt_def_pol): url = path_to_url(join_url(context.croot, 'osx-64', 'bcrypt-3.1.1-py35_2.tar.bz2')) d = Dist(url) assert d.channel == 'local' assert d.name == 'bcrypt' assert d.version == '3.1.1' assert d.build_string == 'py35_2' assert d.to_url() == url assert d.is_channel is True finally: rm_rf(conda_bld_path) # local file url that is not a named channel url = join_url('file:///some/location/on/disk', 'osx-64', 'bcrypt-3.1.1-py35_2.tar.bz2') d = Dist(url) assert d.channel == 'file:///some/location/on/disk' assert d.name == 'bcrypt' assert d.version == '3.1.1' assert d.build_string == 'py35_2' assert d.to_url() == url assert d.is_channel is True
def tearDown(self): rm_rf(self.test_dir) assert not lexists(self.test_dir)
def execute(args, parser): import conda.plan as plan import conda.instructions as inst from conda.gateways.disk.delete import rm_rf from conda.core.linked_data import linked_data if not (args.all or args.package_names): raise CondaValueError('no package names supplied,\n' ' try "conda remove -h" for more details') prefix = context.prefix_w_legacy_search if args.all and prefix == context.default_prefix: msg = "cannot remove current environment. deactivate and run conda remove again" raise CondaEnvironmentError(msg) check_write('remove', prefix, json=context.json) ensure_use_local(args) ensure_override_channels_requires_channel(args) if not args.features and args.all: index = linked_data(prefix) index = {dist: info for dist, info in iteritems(index)} else: index = get_index(channel_urls=context.channels, prepend=not args.override_channels, use_local=args.use_local, use_cache=args.use_index_cache, prefix=prefix) specs = None if args.features: specs = ['@' + f for f in set(args.package_names)] actions = plan.remove_actions(prefix, specs, index, pinned=args.pinned) action_groups = actions, elif args.all: if plan.is_root_prefix(prefix): raise CondaEnvironmentError('cannot remove root environment,\n' ' add -n NAME or -p PREFIX option') actions = {inst.PREFIX: prefix} for dist in sorted(iterkeys(index)): plan.add_unlink(actions, dist) action_groups = actions, else: specs = specs_from_args(args.package_names) r = Resolve(index) prefix_spec_map = create_prefix_spec_map_with_deps(r, specs, prefix) if (context.conda_in_root and plan.is_root_prefix(prefix) and names_in_specs( ROOT_NO_RM, specs) and not args.force): raise CondaEnvironmentError('cannot remove %s from root environment' % ', '.join(ROOT_NO_RM)) actions = [] for prfx, spcs in iteritems(prefix_spec_map): index = linked_data(prfx) index = {dist: info for dist, info in iteritems(index)} actions.append(plan.remove_actions(prfx, list(spcs), index=index, force=args.force, pinned=args.pinned)) action_groups = tuple(actions) delete_trash() if any(plan.nothing_to_do(actions) for actions in action_groups): if args.all: print("\nRemove all packages in environment %s:\n" % prefix, file=sys.stderr) if not context.json: confirm_yn(args) rm_rf(prefix) if context.json: stdout_json({ 'success': True, 'actions': action_groups }) return raise PackageNotFoundError('', 'no packages found to remove from ' 'environment: %s' % prefix) for action in action_groups: if not context.json: print() print("Package plan for package removal in environment %s:" % action["PREFIX"]) plan.display_actions(action, index) if context.json and args.dry_run: stdout_json({ 'success': True, 'dry_run': True, 'actions': action_groups }) return if not context.json: confirm_yn(args) for actions in action_groups: if context.json and not context.quiet: with json_progress_bars(): plan.execute_actions(actions, index, verbose=not context.quiet) else: plan.execute_actions(actions, index, verbose=not context.quiet) if specs: try: with open(join(prefix, 'conda-meta', 'history'), 'a') as f: f.write('# remove specs: %s\n' % ','.join(specs)) except IOError as e: if e.errno == errno.EACCES: log.debug("Can't write the history file") else: raise target_prefix = actions["PREFIX"] if (is_private_env(prefix_to_env_name(target_prefix, context.root_prefix)) and linked_data(target_prefix) == {}): rm_rf(target_prefix) if args.all: rm_rf(prefix) if context.json: stdout_json({ 'success': True, 'actions': actions })
def tearDown(self): rm_rf('tempfile.rc')
def test_remove_link_to_dir(tmpdir): dst_link = join(text_type(tmpdir), "test_link") src_dir = join(text_type(tmpdir), "test_dir") tmpdir.mkdir("test_dir") os.symlink(src_dir, dst_link) assert rm_rf(dst_link) is True
def tearDown(self): rm_rf(self.prefix) assert not lexists(self.prefix) rm_rf(self.pkgs_dir) assert not lexists(self.pkgs_dir)