def setUp(self): tempdirdir = gettempdir() dirname = str(uuid4())[:8] self.prefix = join(tempdirdir, dirname) mkdir_p(self.prefix) assert isdir(self.prefix) mkdir_p(join(self.prefix, 'conda-meta')) activate_env_vars = join(self.prefix, PREFIX_STATE_FILE) with open(activate_env_vars, 'w') as f: f.write(ENV_VARS_FILE) self.pd = PrefixData(self.prefix)
def test_rewrite_environments_txt_file(self): mkdir_p(join(self.prefix, 'conda-meta')) touch(join(self.prefix, 'conda-meta', 'history')) doesnt_exist = join(self.prefix, 'blarg') environments_txt_path = join(self.prefix, 'environments.txt') with open(environments_txt_path, 'w') as fh: fh.write(self.prefix + '\n') fh.write(doesnt_exist + '\n') cleaned_1 = _clean_environments_txt(environments_txt_path) assert cleaned_1 == (self.prefix, ) with patch('conda.core.envs_manager._rewrite_environments_txt' ) as _rewrite_patch: cleaned_2 = _clean_environments_txt(environments_txt_path) assert cleaned_2 == (self.prefix, ) assert _rewrite_patch.call_count == 0
def test_rewrite_environments_txt_file(self): mkdir_p(join(self.prefix, 'conda-meta')) touch(join(self.prefix, 'conda-meta', 'history')) doesnt_exist = join(self.prefix, 'blarg') environments_txt_path = join(self.prefix, 'environments.txt') with open(environments_txt_path, 'w') as fh: fh.write(self.prefix + '\n') fh.write(doesnt_exist + '\n') cleaned_1 = _clean_environments_txt(environments_txt_path) assert cleaned_1 == (self.prefix,) with patch('conda.core.envs_manager._rewrite_environments_txt') as _rewrite_patch: cleaned_2 = _clean_environments_txt(environments_txt_path) assert cleaned_2 == (self.prefix,) assert _rewrite_patch.call_count == 0
def test_minimum_conda_version_error(): with tempdir() as prefix: assert not isfile(join(prefix, 'conda-meta', 'history')) mkdir_p(join(prefix, 'conda-meta')) copy2(join(dirname(__file__), 'conda-meta', 'history'), join(prefix, 'conda-meta', 'history')) with open(join(prefix, 'conda-meta', 'history'), 'a') as fh: fh.write("==> 2018-07-09 11:18:09 <==\n") fh.write("# cmd: blarg\n") fh.write("# conda version: 42.42.4242\n") h = History(prefix) with pytest.raises(CondaUpgradeError) as exc: h.get_user_requests() exception_string = repr(exc.value) print(exception_string) assert "minimum conda version: 42.42" in exception_string assert "$ conda install -p" in exception_string
def test_name_cli_flag(self): envs_dirs = (join(self.prefix, 'first-envs-dir'), join(self.prefix, 'seconds-envs-dir')) with env_var('CONDA_ENVS_DIRS', os.pathsep.join(envs_dirs), reset_context): # with both dirs writable, choose first reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') # with first dir read-only, choose second EnvsDirectory(envs_dirs[0])._is_writable = False reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[1], 'blarg') # if first dir is read-only but environment exists, choose first EnvsDirectory._cache_.pop(envs_dirs[0]) mkdir_p(join(envs_dirs[0], 'blarg')) touch(join(envs_dirs[0], 'blarg', 'history')) reset_context((), argparse_args=AttrDict(name='blarg')) assert context.target_prefix == join(envs_dirs[0], 'blarg') EnvsDirectory._cache_ = {}
def setUp(self): tempdirdir = gettempdir() dirname = str(uuid4())[:8] self.prefix = join(tempdirdir, dirname) mkdir_p(self.prefix) assert isdir(self.prefix)
def _load(self): try: mtime = getmtime(self.cache_path_json) except (IOError, OSError): log.debug("No local cache found for %s at %s", self.url_w_subdir, self.cache_path_json) if context.use_index_cache or ( context.offline and not self.url_w_subdir.startswith('file://')): log.debug( "Using cached data for %s at %s forced. Returning empty repodata.", self.url_w_subdir, self.cache_path_json) return { '_package_records': (), '_names_index': defaultdict(list), '_track_features_index': defaultdict(list), } else: mod_etag_headers = {} else: mod_etag_headers = read_mod_and_etag(self.cache_path_json) if context.use_index_cache: log.debug( "Using cached repodata for %s at %s because use_cache=True", self.url_w_subdir, self.cache_path_json) _internal_state = self._read_local_repdata( mod_etag_headers.get('_etag'), mod_etag_headers.get('_mod')) return _internal_state if context.local_repodata_ttl > 1: max_age = context.local_repodata_ttl elif context.local_repodata_ttl == 1: max_age = get_cache_control_max_age( mod_etag_headers.get('_cache_control', '')) else: max_age = 0 timeout = mtime + max_age - time() if (timeout > 0 or context.offline ) and not self.url_w_subdir.startswith('file://'): log.debug( "Using cached repodata for %s at %s. Timeout in %d sec", self.url_w_subdir, self.cache_path_json, timeout) _internal_state = self._read_local_repdata( mod_etag_headers.get('_etag'), mod_etag_headers.get('_mod')) return _internal_state log.debug("Local cache timed out for %s at %s", self.url_w_subdir, self.cache_path_json) try: raw_repodata_str = fetch_repodata_remote_request( self.url_w_credentials, mod_etag_headers.get('_etag'), mod_etag_headers.get('_mod')) except Response304ContentUnchanged: log.debug( "304 NOT MODIFIED for '%s'. Updating mtime and loading from disk", self.url_w_subdir) touch(self.cache_path_json) _internal_state = self._read_local_repdata( mod_etag_headers.get('_etag'), mod_etag_headers.get('_mod')) return _internal_state else: if not isdir(dirname(self.cache_path_json)): mkdir_p(dirname(self.cache_path_json)) try: with io_open(self.cache_path_json, 'w') as fh: fh.write(raw_repodata_str or '{}') except (IOError, OSError) as e: if e.errno in (EACCES, EPERM): raise NotWritableError(self.cache_path_json, e.errno, caused_by=e) else: raise _internal_state = self._process_raw_repodata_str(raw_repodata_str) self._internal_state = _internal_state return _internal_state