Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/config-v3-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,13 @@ upon errors is inherently unreliable.
<!-- md:version 2.1.0 -->
<!-- md:type string -->
<!-- md:default-empty -->
<!-- md:flag experimental -->

Sets an environment variable when running this program.
Sets an environment variable when running this program. You can use `${...}` interpolation for substituting environment variables.

For example `env_DEBUG=true` sets `DEBUG` to have value `true`.
??? example "`env_{KEY}` usage"
For example `env_DEBUG=true` sets `DEBUG` to have value `true`.

When running pisek with `TASK` set to `aplusb`, `env_DATASET=../{TASK}` produces `DATASET=../aplusb`. (However, the program will have no access to `TASK` by default.)

## [build]
Sections describing how to build a program.
Expand Down
7 changes: 7 additions & 0 deletions fixtures/max/config
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ tests=@all
[solution_solve_java_complex]
tests=@all

[solution_solve_env]
tests=@all

[run_solution]
time_limit=0.2

Expand All @@ -84,6 +87,10 @@ exec=min
build=solve_rs_complex
exec=max

[run_solution:solve_env]
env_TASK=max
env_DBG=${DEBUG}_${DEBUG}

[build_gen:gen]
sources=gen/gen gen/random

Expand Down
8 changes: 8 additions & 0 deletions fixtures/max/solve_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python3
import os

assert os.environ["TASK"] == "max"
assert os.environ["DBG"] == "false_false"

input()
print(max(map(int, input().split())))
61 changes: 55 additions & 6 deletions pisek/config/task_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,53 @@ def load_dict(
def validate_name(cls, value: str) -> str:
return _validate_program_name("program name", value)

@field_validator("env", mode="before")
def interpolate_env(cls, dictionary: dict[str, str]) -> dict[str, str]:
def raise_err(error_type: str, message: str, key: str):
raise PydanticCustomError(
error_type, message, {"_loc": key, f"env_{key}": dictionary[key]}
)

def interpolate(key: str, val: str) -> str:
state: list[str] = [""]
i = 0
while i < len(val):
char = val[i]
if char == "\\":
if i == len(val) - 1:
raise_err("env_wrong_escape", "Unterminated escape", key)
i += 1
state[-1] += val[i]
elif val[i : i + 2] == "${":
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do character escapes use an intermediate state while this looks two characters ahead?

state.append("")
i += 1
elif char == "}" and len(state) > 1:
var_name = state.pop()
if var_name not in os.environ:
raise_err(
"unknown_variable",
f"No environment variable '{var_name}'",
key,
)
state[-1] += os.environ[var_name]
elif char in "${}":
raise_err("unescaped_char", f"Unescaped character '{char}'", key)
else:
state[-1] += char

i += 1

if len(state) > 1:
raise_err(
"env_unterminated_interpolation", "Unterminated interpolation", key
)

return state[0]

for key in dictionary:
dictionary[key] = interpolate(key, dictionary[key])
return dictionary


class BuildSection(BaseEnv):
program_names: ClassVar[dict[str, str]] = {}
Expand Down Expand Up @@ -902,7 +949,9 @@ def _format_message(err: ErrorDetails) -> str:
if ctx == {}:
return f"{err['msg']}."
return f"{err['msg']}:\n" + tab(
"\n".join(f"{key}={val}" for key, val in ctx.items())
"\n".join(
f"{key}={val}" for key, val in ctx.items() if not key.startswith("_")
)
)
return f"{err['msg']}: '{inp}'"

Expand All @@ -914,12 +963,12 @@ def _convert_errors(e: ValidationError, config_values: ConfigValuesDict) -> list
for loc in error["loc"]:
value = value[loc]

if not isinstance(value, ConfigValue):
location = (
value["_section"].location() if "_section" in value else "global config"
)
else:
if isinstance(value, ConfigValue):
location = value.location()
elif "_section" in value:
location = value["_section"].location()
else:
location = value[error["ctx"]["_loc"]].location()

error_msgs.append(f"In {location}:\n" + tab(_format_message(error)))
return error_msgs
Expand Down
4 changes: 4 additions & 0 deletions tests/test_max.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import unittest

from util import TestFixtureVariant
Expand All @@ -8,6 +9,9 @@ class TestMax(TestFixtureVariant):
def fixture_path(self) -> str:
return "fixtures/max/"

def set_env(self):
os.environ["DEBUG"] = "false"


if __name__ == "__main__":
unittest.main(verbosity=2)
6 changes: 6 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class TestFixture(unittest.TestCase):
def fixture_path(self) -> str | None:
return None

def set_env(self) -> None:
pass

def setUp(self) -> None:
os.environ["LOG_LEVEL"] = "debug"

Expand Down Expand Up @@ -49,6 +52,8 @@ def setUp(self) -> None:
os.path.join(self.fixtures_dir, "pisek"),
)

self.set_env()

assert_task_dir(
self.task_dir,
os.environ.get("PISEK_DIRECTORY"),
Expand Down Expand Up @@ -123,6 +128,7 @@ def runTest(self) -> None:
if not self.fixture_path:
return

self.set_env()
self.modify_task()
self.log_files()

Expand Down