def test_register_unregister_location_env(self): user_environments_txt_file = get_user_environments_txt_file() if (not os.path.exists(user_environments_txt_file) or user_environments_txt_file == os.devnull): pytest.skip('user environments.txt file {} does not exist'.format( user_environments_txt_file)) gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(user_environments_txt_file, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(user_environments_txt_file) assert len( tuple(x for x in yield_lines(user_environments_txt_file) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len( tuple(x for x in yield_lines(user_environments_txt_file) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def get_conda_root(self): try: from conda.core.envs_manager import list_all_known_prefixes prefixes = list_all_known_prefixes() return prefixes[0], os.path.join(prefixes[0], "envs") except: try: conda_info = subprocess.check_output(["conda", "info"]).decode("utf-8") info = {} for line in conda_info.split("\n")[1:]: try: key, value = line.strip().split(" : ") info[key] = value except ValueError: pass envs_dname = info["envs directories"] try: root_dname = (info["root environment"].replace( "(writable)", "").strip()) except KeyError: root_dname = (info["base environment"].replace( "(writable)", "").strip()) return str(root_dname), str(envs_dname) except: return "", ""
def get_conda_env(prefix=conda_root()): """Method used to get the current conda environment get_conda_env ============= This method is used to get the the name and prefix path for a specified conda environment. Used to access ggd environment variables created for this specific environment. Returns: ++++++++ 1) The conda environment name 2) The path to the conda environent """ ## Get environment list from conda.core.envs_manager import list_all_known_prefixes prefix = prefix.rstrip("/") env_var_paths = dict() ## Key = env_path, value = env_name for env_path in list_all_known_prefixes(): name = os.path.basename(env_path.rstrip("/")) env_var_paths[env_path.rstrip("/")] = name prefix_path = get_conda_prefix_path(prefix) return (env_var_paths[prefix_path], prefix_path) print( ":ggd:conda-env: Error in checking conda environment. Verify that conda is working and try again.", file=sys.stderr, ) sys.exit(1)
def test_register_unregister_location_env(self): gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(USER_ENVIRONMENTS_TXT_FILE, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(USER_ENVIRONMENTS_TXT_FILE) assert len( tuple(x for x in yield_lines(USER_ENVIRONMENTS_TXT_FILE) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len( tuple(x for x in yield_lines(USER_ENVIRONMENTS_TXT_FILE) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def test_register_unregister_location_env(self): user_environments_txt_file = get_user_environments_txt_file() if (not os.path.exists(user_environments_txt_file) or user_environments_txt_file == os.devnull): pytest.skip('user environments.txt file {} does not exist'.format(user_environments_txt_file)) gascon_location = join(self.prefix, 'gascon') touch(join(gascon_location, PREFIX_MAGIC_FILE), mkdir=True) assert gascon_location not in list_all_known_prefixes() touch(user_environments_txt_file, mkdir=True, sudo_safe=True) register_env(gascon_location) assert gascon_location in yield_lines(user_environments_txt_file) assert len(tuple(x for x in yield_lines(user_environments_txt_file) if paths_equal(gascon_location, x))) == 1 register_env(gascon_location) # should be completely idempotent assert len(tuple(x for x in yield_lines(user_environments_txt_file) if x == gascon_location)) == 1 unregister_env(gascon_location) assert gascon_location not in list_all_known_prefixes() unregister_env(gascon_location) # should be idempotent assert gascon_location not in list_all_known_prefixes()
def prefix_in_conda(prefix): """Method to check if a perfix is a conda environment or not prefix_in_conda =============== This method is used to check if a full file path is a conda environment or not. If it is True is returned. If it is not, the CondaEnvironmentNotFound error is raised Parameters: ----------- 1) prefix: The conda enviroment full file path/prefix Returns: ++++++++ 1) True if prefix is a conda environment, raises an error otherwise """ ## Get environment list from conda.core.envs_manager import list_all_known_prefixes env_var_names = dict() ## Key = env_name, value = env_path env_var_paths = dict() ## Key - env_path, value = env_name for env_path in list_all_known_prefixes(): name = os.path.basename(env_path.rstrip("/")) env_var_names[name] = env_path.rstrip("/") env_var_paths[env_path.rstrip("/")] = name ## Get the base/first conda environment cbase = min(env_var_paths.keys()) prefix = prefix.rstrip("/") ## Check that the file is in the enviroment lists if prefix not in env_var_names.keys() and prefix not in env_var_paths.keys(): raise CondaEnvironmentNotFound(prefix) ## Get the prefix path dd prefix_path = get_conda_prefix_path(prefix) ## Check that the file path includes the conda base directory if cbase not in prefix_path: raise CondaEnvironmentNotFound(prefix) ## Check that the prefix is an existing directory if not os.path.isdir(prefix_path): raise CondaEnvironmentNotFound(prefix) return True
def env_is_created(env_name): """ Assert an environment is created Args: env_name: the environment name Returns: True if created False otherwise """ from os.path import basename for prefix in list_all_known_prefixes(): name = (ROOT_ENV_NAME if prefix == context.root_dir else basename(prefix)) if name == env_name: return True return False
def env_is_created(env_name): """ Assert an environment is created Args: env_name: the environment name Returns: True if created False otherwise """ from os.path import basename for prefix in list_all_known_prefixes(): name = (ROOT_ENV_NAME if prefix == context.root_dir else basename(prefix)) if name == env_name: return True return False
def test_list_all_known_prefixes_with_permission_error(mock_clean_env, mock_get_user_env, mock_context, tmp_path): # Mock context myenv_dir = tmp_path / "envs" myenv_dir.mkdir() mock_context.envs_dirs = str(myenv_dir) mock_context.root_prefix = "root_prefix" # Mock get_user_environments_txt_file to return a file env_txt_file = tmp_path / "environment.txt" touch(env_txt_file) mock_get_user_env.return_value = env_txt_file # Mock _clean_environments_txt to raise PermissionError mock_clean_env.side_effect = PermissionError() all_env_paths = list_all_known_prefixes() # On Windows, all_env_paths can contain more paths (like '\\Miniconda') assert "root_prefix" in all_env_paths
def get_conda_prefix_path(prefix): """Method to get the conda environment/prefix path from either the actual path or name get_conda_prefix_path ===================== This method is used to get the acutal prefix path from a file path or conda environment name Parameters: ----------- 1) prefix: The file path or conda environment name to get the prefix path for Returns: ++++++++ 1) The prefix path """ ## Get environment list from conda.core.envs_manager import list_all_known_prefixes env_var_names = dict() ## Key = env_name, value = env_path env_var_paths = dict() ## Key - env_path, value = env_name for env_path in list_all_known_prefixes(): name = os.path.basename(env_path.rstrip("/")) env_var_names[name] = env_path.rstrip("/") env_var_paths[env_path.rstrip("/")] = name prefix = prefix.rstrip("/") ## Check that the file is in the enviroment lists if prefix not in env_var_names.keys() and prefix not in env_var_paths.keys(): raise CondaEnvironmentNotFound(prefix) ## Check that the prefix is an existing directory prefix_path = prefix if prefix in env_var_names.keys(): prefix_path = env_var_names[prefix] return prefix_path
def execute(args, parser): info_dict = {'envs': list_all_known_prefixes()} common.print_envs_list(info_dict['envs'], not args.json) if args.json: common.stdout_json(info_dict)
[ "Not open source", "MIT license", "BSD license", "ISC license", "Apache Software License 2.0", "GNU General Public License v3", ], ) project_short_description = st.text_area("project_short_description", "description...") st.subheader("Extra Options") command_line_interface = st.checkbox("Command Line Interface") st.subheader("Choose Virtual Envs") conda_env_paths = list_all_known_prefixes() use_new = st.checkbox("Use New Virtual Environment", False) if use_new: col41, col42 = st.beta_columns(2) with col41: new_env_name = st.text_input("Env Name") with col42: python_version = st.selectbox("Python Version", ["2.7", "3.6", "3.7", "3.8"], index=1) conda_env_path = str(Path(context.envs_dirs[0]) / new_env_name) else: conda_env_path = st.selectbox("Conda Envs", conda_env_paths) python_version = get_python_version_from_env_path(conda_env_path) st.subheader("Preview")