Add get_environment_variable utility function#5148
Add get_environment_variable utility function#5148mvanhorn wants to merge 2 commits intoeasybuilders:developfrom
get_environment_variable utility function#5148Conversation
Provides safe environment variable access with required and empty checks. Returns empty string for undefined variables by default, raises EasyBuildError when required=True and the variable is missing or (with empty=False) empty. Fixes easybuilders#5059
ocaisa
left a comment
There was a problem hiding this comment.
You will need to add tests for this as well.
easybuild/tools/environment.py
Outdated
| return result | ||
|
|
||
|
|
||
| def get_environment_variable(name, required=False, empty=True): |
There was a problem hiding this comment.
With empty I think what you really mean is allow_empty...and then if we don't allow_empty then it should be a straight error, not dependent on required.
easybuild/tools/environment.py
Outdated
| _log.debug("Environment variable '%s' is not defined, returning empty string", name) | ||
| return '' | ||
|
|
||
| if not empty and value == '': |
There was a problem hiding this comment.
Am empty string is falsey so you can just use value.strip() (which also removes whitespace)
| if not empty and value == '': | |
| # Check if we allow empty values (and we consider whitespace an empty value) | |
| if not empty and value.strip(): |
easybuild/tools/environment.py
Outdated
| if required: | ||
| raise EasyBuildError("Required environment variable '%s' is defined but empty", name) |
There was a problem hiding this comment.
| if required: | |
| raise EasyBuildError("Required environment variable '%s' is defined but empty", name) | |
| raise EasyBuildError("Required environment variable '%s' is defined but empty", name) |
easybuild/tools/environment.py
Outdated
| if required: | ||
| raise EasyBuildError("Required environment variable '%s' is not defined", name) | ||
| _log.debug("Environment variable '%s' is not defined, returning empty string", name) | ||
| return '' |
There was a problem hiding this comment.
Don't use inline returns
| return '' | |
| value = '' |
easybuild/tools/environment.py
Outdated
| if required: | ||
| raise EasyBuildError("Required environment variable '%s' is defined but empty", name) | ||
| _log.debug("Environment variable '%s' is empty, returning empty string", name) | ||
| return '' |
There was a problem hiding this comment.
| return '' | |
| value = '' |
|
Ha, you were working directly off of #5059 but now I think about it, I don't think supporting |
|
I also want to see the adopted, both in framework itself, and in one or more easyblocks, to show the usage of this... |
get_environment_variable utility function
- Rename `empty` param to `allow_empty` for clarity - Simplify empty check using `value.strip()` (falsey) - Remove allow_empty=False + required=False combo (always error on empty) - Replace inline returns with `value = ''` assignment - Add test_get_environment_variable covering all modes Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Addressed all feedback:
Still need to add an adoption example in an easyblock per @boegel's request - will follow up on that. |
Summary
Adds
get_environment_variable()toeasybuild/tools/environment.pyfor safe environment variable access with optionalrequiredandemptyvalidation.Why this matters
Careless use of
os.getenv()without defaults led toNonebeing injected into build commands (#5059, referenced from easyblocks#4003). This function provides a single point of control for environment variable access with clear error messages.Changes
easybuild/tools/environment.py: Addedget_environment_variable(name, required=False, empty=True)with four modes:required=True: raisesEasyBuildErrorif undefinedempty=False: treats empty strings as missingEasyBuildErrorif undefined OR emptyTesting
read_environment()andEasyBuildErrorpatternsFixes #5059
This contribution was developed with AI assistance (Claude Code).