Skip to content

Commit cb5d1a2

Browse files
authored
Merge pull request #18 from mithro/master
Improve isolation from system Python.
2 parents 12caaa3 + d4f9d23 commit cb5d1a2

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

.github/workflows/test.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ jobs:
3636
with:
3737
fetch-depth: 0
3838

39-
- name: Make env
39+
- name: make env
4040
run: |
4141
cd test
4242
make env
4343
make env-info
44+
45+
- name: example
46+
run: |
47+
cd test
48+
make example-command
49+
50+
- name: test
51+
run: |
52+
cd test
53+
make test-command

Makefile.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ ENVIRONMENT_FILE := environment.yml
2020
include third_party/make-env/conda.mk
2121

2222
# Example make target which runs commands inside the conda environment.
23-
test-command: | $(CONDA_ENV_PYTHON)
23+
example-command: | $(CONDA_ENV_PYTHON)
2424
$(IN_CONDA_ENV) echo "Python is $$(which python)"
2525
$(IN_CONDA_ENV) python --version
26+
27+
# Check that no system packages are found in the environment.
28+
test-command: | $(CONDA_ENV_PYTHON)
29+
$(IN_CONDA_ENV) python check.py

conda.mk

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ DOWNLOADS_DIR := $(ENV_DIR)$(SEP)downloads
6464

6565
CONDA_INSTALLER := Miniconda3-latest-$(OS_TYPE)-$(CPU_TYPE).$(OS_EXT)
6666
CONDA_PYTHON := $(CONDA_DIR)$(SEP)$(PYTHON_BIN)
67+
CONDA_PYVENV := $(CONDA_DIR)$(SEP)pyvenv.cfg
6768
CONDA_PKGS_DIR := $(DOWNLOADS_DIR)$(SEP)conda-pkgs
6869
CONDA_PKGS_DEP := $(CONDA_PKGS_DIR)$(SEP)urls.txt
6970

@@ -77,6 +78,10 @@ CONDA_INSTALLER_DOWNLOAD := $(DOWNLOADS_DIR)$(SEP)$(CONDA_INSTALLER)
7778
CONDA_ALWAYS_YES := 1
7879
export CONDA_ALWAYS_YES
7980

81+
# Force ignoring a user's Python site.py config.
82+
PYTHONNOUSERSITE := 1
83+
export PYTHONNOUSERSITE
84+
8085
# Check spaces are not found in important locations
8186
NULL_STRING :=
8287
SPACE := $(NULL_STRING) $(NULL_STRING)
@@ -118,11 +123,19 @@ $(CONDA_PYTHON): $(CONDA_INSTALLER_DOWNLOAD)
118123
$(TOUCH) "$(CONDA_PYTHON)"
119124
endif
120125

126+
# FIXME: Why does this break on Windows?
127+
ifeq ($(OS_TYPE),Windows)
128+
CONDA_PYVENV := $(CONDA_PYTHON)
129+
else
130+
$(CONDA_PYVENV): $(CONDA_PYTHON) $(MAKE_DIR)/conda.mk
131+
echo "include-system-site-packages=false" >> $(CONDA_PYVENV)
132+
endif
133+
121134
$(CONDA_ENVS_DIR): $(CONDA_PYTHON)
122135
$(IN_CONDA_ENV_BASE) conda config --system --add envs_dirs $(CONDA_ENVS_DIR)
123136
$(MKDIR) "$(CONDA_ENVS_DIR)"
124137

125-
$(CONDA_ENV_PYTHON): $(ENVIRONMENT_FILE) $(REQUIREMENTS_FILE) | $(CONDA_PYTHON) $(CONDA_PKGS_DEP) $(CONDA_ENVS_DIR)
138+
$(CONDA_ENV_PYTHON): $(ENVIRONMENT_FILE) $(REQUIREMENTS_FILE) | $(CONDA_PYTHON) $(CONDA_PKGS_DEP) $(CONDA_ENVS_DIR) $(CONDA_PYVENV)
126139
$(IN_CONDA_ENV_BASE) conda env update --name $(CONDA_ENV_NAME) --file $(ENVIRONMENT_FILE)
127140
$(TOUCH) "$(CONDA_ENV_PYTHON)"
128141

test/check.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
3+
# Check that no system packages are found in the environment.
4+
import sys
5+
assert all('/usr' not in p for p in sys.path), sys.path
6+
print(sys.path)

0 commit comments

Comments
 (0)