Skip to content

Commit 6d568df

Browse files
author
marwan37
committed
update script to not generate a .env file if it didnt exist, and dont set environment variables directly
1 parent 434f7cb commit 6d568df

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

oncoclear/Dockerfile.sandbox

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ FROM zenmldocker/zenml-sandbox:latest
55
LABEL project_name="oncoclear"
66
LABEL project_version="0.1.0"
77

8-
# Install project-specific dependencies
9-
RUN pip install --no-cache-dir \
8+
# Install dependencies
9+
RUN pip install uv
10+
RUN uv pip install --system \
1011
"zenml[server]>=0.50.0" \
1112
"notebook" \
1213
"scikit-learn" \

scripts/generate_sandbox_dockerfile.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def get_dependencies(project_dir: Path, use_uv: bool) -> str:
121121
else:
122122
lines.append("RUN pip install --no-cache-dir \\")
123123
lines += [f' "{d}" \\' for d in deps[:-1]] + [f' "{deps[-1]}"']
124-
return "\n".join(lines)
124+
return "\n".join(lines), deps
125125

126126

127127
def find_env_keys(project_dir: Path) -> set[str]:
@@ -143,40 +143,48 @@ def find_env_keys(project_dir: Path) -> set[str]:
143143
return keys or {"API_KEY"}
144144

145145

146-
def gen_env_block(project_dir: Path, keys: set[str]) -> str:
146+
def gen_env_block(
147+
project_dir: Path, keys: set[str], installed_deps: list[str]
148+
) -> str:
147149
"""Generate Dockerfile commands to set up .env with detected keys and runtime tweaks.
148150
149-
Copies existing .env or creates a new one, appends missing keys,
150-
and adds ENV lines for Polars and tokenizers settings.
151+
Looks for any .env* files (like .env.example) and uses that for reference.
152+
Does not create a .env file if one doesn't exist.
153+
Adds Polars ENV only if polars-lts-cpu was installed.
151154
"""
152-
has_env = (project_dir / ".env").exists()
153-
if has_env:
154-
existing = {
155-
line.split("=", 1)[0]
156-
for line in (project_dir / ".env").read_text().splitlines()
157-
if "=" in line
158-
}
159-
block = "# Copy existing .env\nCOPY .env /workspace/.env"
155+
lines = []
156+
157+
# Look for any .env* files (.env, .env.example, etc.)
158+
env_files = list(project_dir.glob(".env*"))
159+
160+
if env_files:
161+
# Use the first .env* file found
162+
env_file = env_files[0]
163+
env_file_name = env_file.name
164+
165+
# Parse the existing keys from the file
166+
existing = set()
167+
try:
168+
for line in env_file.read_text(encoding="utf-8").splitlines():
169+
if line and not line.startswith("#") and "=" in line:
170+
existing.add(line.split("=", 1)[0].strip())
171+
except Exception:
172+
existing = set()
173+
174+
# Copy the existing .env* file
175+
lines.append(f"# Copy {env_file_name}")
176+
lines.append(f"COPY {env_file_name} /workspace/.env")
177+
178+
# Add missing keys only if we're copying a template
160179
missing = keys - existing
161-
else:
162-
block = "# Create a template .env file for API keys"
163-
missing = keys
164-
for k in sorted(missing):
165-
val = (
166-
"PATH_TO_YOUR_GOOGLE_CREDENTIALS_FILE"
167-
if k == "GOOGLE_APPLICATION_CREDENTIALS"
168-
else f"YOUR_{k}"
169-
)
170-
block += f'\nRUN echo "{k}={val}" >> /workspace/.env'
171-
# runtime adjustments
172-
if any("polars" in d.lower() for d in keys):
173-
# Add POLARS_SKIP_CPU_CHECK if polars is used - this prevents Polars from
174-
# generating warnings or errors when running in container environments where
175-
# CPU feature detection may not work correctly
176-
block += "\nENV POLARS_SKIP_CPU_CHECK=1"
177-
if any(d.lower().startswith(("transform", "token")) for d in keys):
178-
block += "\nENV TOKENIZERS_PARALLELISM=false"
179-
return block
180+
for k in sorted(missing):
181+
lines.append(f'RUN echo "{k}=YOUR_{k}" >> /workspace/.env')
182+
183+
# Add Polars ENV only if we actually installed polars-lts-cpu
184+
if any("polars-lts-cpu" in dep for dep in installed_deps):
185+
lines.append("ENV POLARS_SKIP_CPU_CHECK=1")
186+
187+
return "\n".join(lines) if lines else ""
180188

181189

182190
def generate_dockerfile(
@@ -193,9 +201,9 @@ def generate_dockerfile(
193201
print(f"Error: {out} not found")
194202
return False
195203
name = Path(project_path).name
196-
deps_block = get_dependencies(out, use_uv)
204+
deps_block, installed_deps = get_dependencies(out, use_uv)
197205
keys = find_env_keys(out)
198-
env_block = gen_env_block(out, keys)
206+
env_block = gen_env_block(out, keys, installed_deps)
199207
content = DOCKER_TEMPLATE.format(
200208
name=name, deps=deps_block, env_block=env_block
201209
)

0 commit comments

Comments
 (0)