Skip to content

Commit

Permalink
Allow restricting of condarc search paths
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoesters committed Sep 16, 2024
1 parent 4ad8f0c commit 6d37cb3
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 15 deletions.
1 change: 1 addition & 0 deletions recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ source:
patches:
- ../src/conda_patches/0001-Rename-and-replace-entrypoint-stub-exe.patch
- ../src/conda_patches/0002-Manipulate-PATH-directly-instead-of-_call_ing-conda.patch
- ../src/conda_patches/0003-Restrict-search-paths.patch

- url: https://github.com/conda/constructor/archive/{{ constructor_version }}.tar.gz # [win]
sha256: 0ea4f6d563a53ebb03475dc6d2d88d3ab01be4e9d291fd276c79315aa92e5114 # [win]
Expand Down
83 changes: 83 additions & 0 deletions src/conda_patches/0003-Restrict-search-paths.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
diff --git a/conda/base/constants.py b/conda/base/constants.py
index d38502a48..dc180c0bf 100644
--- a/conda/base/constants.py
+++ b/conda/base/constants.py
@@ -25,42 +25,47 @@ machine_bits = 8 * struct.calcsize("P")

APP_NAME = "conda"

-if on_win: # pragma: no cover
+if "CONDA_RESTRICT_RC_SEARCH_PATH" in os.environ:
SEARCH_PATH = (
- "C:/ProgramData/conda/.condarc",
- "C:/ProgramData/conda/condarc",
- "C:/ProgramData/conda/condarc.d",
+ "$CONDARC",
)
else:
- SEARCH_PATH = (
- "/etc/conda/.condarc",
- "/etc/conda/condarc",
- "/etc/conda/condarc.d/",
- "/var/lib/conda/.condarc",
- "/var/lib/conda/condarc",
- "/var/lib/conda/condarc.d/",
+ if on_win: # pragma: no cover
+ SEARCH_PATH = (
+ "C:/ProgramData/conda/.condarc",
+ "C:/ProgramData/conda/condarc",
+ "C:/ProgramData/conda/condarc.d",
+ )
+ else:
+ SEARCH_PATH = (
+ "/etc/conda/.condarc",
+ "/etc/conda/condarc",
+ "/etc/conda/condarc.d/",
+ "/var/lib/conda/.condarc",
+ "/var/lib/conda/condarc",
+ "/var/lib/conda/condarc.d/",
+ )
+
+ SEARCH_PATH += (
+ "$CONDA_ROOT/.condarc",
+ "$CONDA_ROOT/condarc",
+ "$CONDA_ROOT/condarc.d/",
+ "$XDG_CONFIG_HOME/conda/.condarc",
+ "$XDG_CONFIG_HOME/conda/condarc",
+ "$XDG_CONFIG_HOME/conda/condarc.d/",
+ "~/.config/conda/.condarc",
+ "~/.config/conda/condarc",
+ "~/.config/conda/condarc.d/",
+ "~/.conda/.condarc",
+ "~/.conda/condarc",
+ "~/.conda/condarc.d/",
+ "~/.condarc",
+ "$CONDA_PREFIX/.condarc",
+ "$CONDA_PREFIX/condarc",
+ "$CONDA_PREFIX/condarc.d/",
+ "$CONDARC",
)

-SEARCH_PATH += (
- "$CONDA_ROOT/.condarc",
- "$CONDA_ROOT/condarc",
- "$CONDA_ROOT/condarc.d/",
- "$XDG_CONFIG_HOME/conda/.condarc",
- "$XDG_CONFIG_HOME/conda/condarc",
- "$XDG_CONFIG_HOME/conda/condarc.d/",
- "~/.config/conda/.condarc",
- "~/.config/conda/condarc",
- "~/.config/conda/condarc.d/",
- "~/.conda/.condarc",
- "~/.conda/condarc",
- "~/.conda/condarc.d/",
- "~/.condarc",
- "$CONDA_PREFIX/.condarc",
- "$CONDA_PREFIX/condarc",
- "$CONDA_PREFIX/condarc.d/",
- "$CONDARC",
-)
-
DEFAULT_CHANNEL_ALIAS = "https://conda.anaconda.org"
CONDA_HOMEPAGE_URL = "https://conda.io"
ERROR_UPLOAD_URL = "https://conda.io/conda-post/unexpected-error"
7 changes: 1 addition & 6 deletions src/entry_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
# See https://github.com/conda/conda-standalone/issues/86
del os.environ["SSLKEYLOGFILE"]

if (
not os.path.samefile(sys.prefix, os.environ.get("CONDA_ROOT"))
and "CONDARC" not in os.environ
):
# Fallback option for when CONDA_ROOT is set externally, e.g.,
# during the test step of conda build
if "CONDARC" not in os.environ:
os.environ["CONDARC"] = os.path.join(sys.prefix, ".condarc")


Expand Down
34 changes: 25 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,29 @@ def test_constructor():
"RECIPE_DIR" not in os.environ,
reason="Requires RECIPE_DIR environment variable.",
)
def test_conda_standalone_config():
@pytest.mark.parametrize("restrict_search_path", (True, False))
def test_conda_standalone_config(restrict_search_path, tmp_path, monkeypatch):
recipe_condarc = Path(os.environ["RECIPE_DIR"], ".condarc")
if not recipe_condarc.exists():
pytest.skip("Recipe does not have a .condarc file.")
expected_configs = {}
yaml = YAML()
with open(recipe_condarc) as crc:
recipe_config = YAML().load(crc)
expected_configs["recipe"] = recipe_config

if restrict_search_path:
monkeypatch.setenv("CONDA_RESTRUCT_RC_SEARCH_PATH", "1")
else:
config_path = str(tmp_path / ".condarc")
expected_configs[config_path] = {
"channels": [
"defaults",
]
}
with open(config_path, "w") as crc:
yaml.dump(expected_configs[config_path], crc)
monkeypatch.setenv("CONDA_ROOT", str(tmp_path))

proc = run_conda(
"config",
Expand All @@ -99,16 +116,15 @@ def test_conda_standalone_config():
capture_output=True,
text=True,
)
tmp_root = Path(proc.stdout).parent
tmp_root = str(Path(proc.stdout).parent)

conda_config = None
conda_configs = {}
for filepath, config in condarcs.items():
if filepath == "cmd_line":
continue
if Path(filepath).parent.parent == tmp_root:
conda_config = config
break
assert recipe_config == conda_config
if filepath.startswith(tmp_root):
conda_configs["recipe"] = config
elif Path(filepath).exists():
conda_configs[filepath] = config
assert expected_configs == conda_configs


def test_extract_conda_pkgs(tmp_path: Path):
Expand Down

0 comments on commit 6d37cb3

Please sign in to comment.