initialize#

Backend logic for conda init.

Sections in this module are

  1. top-level functions

  2. plan creators

  3. plan runners

  4. individual operations

  5. helper functions

The top-level functions compose and execute full plans.

A plan is created by composing various individual operations. The plan data structure is a list of dicts, where each dict represents an individual operation. The dict contains two keys--function and kwargs--where function is the name of the individual operation function within this module.

Each individual operation must

  1. return a Result (i.e. NEEDS_SUDO, MODIFIED, or NO_CHANGE)

  2. have no side effects if context.dry_run is True

  3. be verbose and descriptive about the changes being made or proposed is context.verbose

The plan runner functions take the plan (list of dicts) as an argument, and then coordinate the execution of each individual operation. The docstring for run_plan_elevated() has details on how that strategy is implemented.

Classes#

Result

Functions#

install(conda_prefix)

initialize(conda_prefix, shells, for_user, for_system, ...)

initialize_dev(shell[, dev_env_prefix, conda_source_root])

_initialize_dev_bash(prefix, env_vars, unset_env_vars)

_initialize_dev_cmdexe(prefix, env_vars, unset_env_vars)

make_install_plan(conda_prefix)

make_initialize_plan(conda_prefix, shells, for_user, ...)

Creates a plan for initializing conda in shells.

run_plan(plan)

run_plan_elevated(plan)

The strategy of this function differs between unix and Windows. Both strategies use a

run_plan_from_stdin()

run_plan_from_temp_file(temp_path)

print_plan_results(plan[, stream])

make_entry_point(target_path, conda_prefix, module, func)

make_entry_point_exe(target_path, conda_prefix)

install_anaconda_prompt(target_path, conda_prefix, reverse)

_install_file(target_path, file_content)

install_conda_sh(target_path, conda_prefix)

install_Scripts_activate_bat(target_path, conda_prefix)

install_activate_bat(target_path, conda_prefix)

install_deactivate_bat(target_path, conda_prefix)

install_activate(target_path, conda_prefix)

install_deactivate(target_path, conda_prefix)

install_condabin_conda_bat(target_path, conda_prefix)

install_library_bin_conda_bat(target_path, conda_prefix)

install_condabin_conda_activate_bat(target_path, ...)

install_condabin_rename_tmp_bat(target_path, conda_prefix)

install_condabin_conda_auto_activate_bat(target_path, ...)

install_condabin_hook_bat(target_path, conda_prefix)

install_conda_fish(target_path, conda_prefix)

install_conda_psm1(target_path, conda_prefix)

install_conda_hook_ps1(target_path, conda_prefix)

install_conda_xsh(target_path, conda_prefix)

install_conda_csh(target_path, conda_prefix)

_config_fish_content(conda_prefix)

init_fish_user(target_path, conda_prefix, reverse)

_config_xonsh_content(conda_prefix)

init_xonsh_user(target_path, conda_prefix, reverse)

_bashrc_content(conda_prefix, shell)

init_sh_user(target_path, conda_prefix, shell[, reverse])

init_sh_system(target_path, conda_prefix[, reverse])

_read_windows_registry(target_path)

_write_windows_registry(target_path, value_value, ...)

init_cmd_exe_registry(target_path, conda_prefix[, reverse])

init_long_path(target_path)

_powershell_profile_content(conda_prefix)

init_powershell_user(target_path, conda_prefix, reverse)

remove_conda_in_sp_dir(target_path)

make_conda_egg_link(target_path, conda_source_root)

modify_easy_install_pth(target_path, conda_source_root)

make_dev_egg_info_file(target_path)

make_diff(old, new)

_get_python_info(prefix)

Attributes#

CONDA_INITIALIZE_RE_BLOCK

CONDA_INITIALIZE_PS_RE_BLOCK

temp_path

CONDA_INITIALIZE_RE_BLOCK = '^# >>> conda initialize >>>(?:\\n|\\r\\n)([\\s\\S]*?)# <<< conda initialize <<<(?:\\n|\\r\\n)?'#
CONDA_INITIALIZE_PS_RE_BLOCK = '^#region conda initialize(?:\\n|\\r\\n)([\\s\\S]*?)#endregion(?:\\n|\\r\\n)?'#
class Result#
NEEDS_SUDO = 'needs sudo'#
MODIFIED = 'modified'#
NO_CHANGE = 'no change'#
install(conda_prefix)#
initialize(conda_prefix, shells, for_user, for_system, anaconda_prompt, reverse=False)#
initialize_dev(shell, dev_env_prefix=None, conda_source_root=None)#
_initialize_dev_bash(prefix, env_vars, unset_env_vars)#
_initialize_dev_cmdexe(prefix, env_vars, unset_env_vars)#
make_install_plan(conda_prefix)#
make_initialize_plan(conda_prefix, shells, for_user, for_system, anaconda_prompt, reverse=False)#

Creates a plan for initializing conda in shells.

Bash: On Linux, when opening the terminal, .bashrc is sourced (because it is an interactive shell). On macOS on the other hand, the .bash_profile gets sourced by default when executing it in Terminal.app. Some other programs do the same on macOS so that's why we're initializing conda in .bash_profile. On Windows, there are multiple ways to open bash depending on how it was installed. Git Bash, Cygwin, and MSYS2 all use .bash_profile by default.

PowerShell: There's several places PowerShell can store its path, depending on if it's Windows PowerShell, PowerShell Core on Windows, or PowerShell Core on macOS/Linux. The easiest way to resolve it is to just ask different possible installations of PowerShell where their profiles are.

run_plan(plan)#
run_plan_elevated(plan)#

The strategy of this function differs between unix and Windows. Both strategies use a subprocess call, where the subprocess is run with elevated privileges. The executable invoked with the subprocess is python -m conda.core.initialize, so see the if __name__ == "__main__" at the bottom of this module.

For unix platforms, we convert the plan list to json, and then call this module with sudo python -m conda.core.initialize while piping the plan json to stdin. We collect json from stdout for the results of the plan execution with elevated privileges.

For Windows, we create a temporary file that holds the json content of the plan. The subprocess reads the content of the file, modifies the content of the file with updated execution status, and then closes the file. This process then reads the content of that file for the individual operation execution results, and then deletes the file.

run_plan_from_stdin()#
run_plan_from_temp_file(temp_path)#
print_plan_results(plan, stream=None)#
make_entry_point(target_path, conda_prefix, module, func)#
make_entry_point_exe(target_path, conda_prefix)#
install_anaconda_prompt(target_path, conda_prefix, reverse)#
_install_file(target_path, file_content)#
install_conda_sh(target_path, conda_prefix)#
install_Scripts_activate_bat(target_path, conda_prefix)#
install_activate_bat(target_path, conda_prefix)#
install_deactivate_bat(target_path, conda_prefix)#
install_activate(target_path, conda_prefix)#
install_deactivate(target_path, conda_prefix)#
install_condabin_conda_bat(target_path, conda_prefix)#
install_library_bin_conda_bat(target_path, conda_prefix)#
install_condabin_conda_activate_bat(target_path, conda_prefix)#
install_condabin_rename_tmp_bat(target_path, conda_prefix)#
install_condabin_conda_auto_activate_bat(target_path, conda_prefix)#
install_condabin_hook_bat(target_path, conda_prefix)#
install_conda_fish(target_path, conda_prefix)#
install_conda_psm1(target_path, conda_prefix)#
install_conda_hook_ps1(target_path, conda_prefix)#
install_conda_xsh(target_path, conda_prefix)#
install_conda_csh(target_path, conda_prefix)#
_config_fish_content(conda_prefix)#
init_fish_user(target_path, conda_prefix, reverse)#
_config_xonsh_content(conda_prefix)#
init_xonsh_user(target_path, conda_prefix, reverse)#
_bashrc_content(conda_prefix, shell)#
init_sh_user(target_path, conda_prefix, shell, reverse=False)#
init_sh_system(target_path, conda_prefix, reverse=False)#
_read_windows_registry(target_path)#
_write_windows_registry(target_path, value_value, value_type)#
init_cmd_exe_registry(target_path, conda_prefix, reverse=False)#
init_long_path(target_path)#
_powershell_profile_content(conda_prefix)#
init_powershell_user(target_path, conda_prefix, reverse)#
remove_conda_in_sp_dir(target_path)#
modify_easy_install_pth(target_path, conda_source_root)#
make_dev_egg_info_file(target_path)#
make_diff(old, new)#
_get_python_info(prefix)#
temp_path#