Skip to content

Commit 9d0d7d8

Browse files
committed
Change substitution format for env variables
1 parent 9ca6740 commit 9d0d7d8

File tree

2 files changed

+5
-9
lines changed

2 files changed

+5
-9
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ You can use these substitutions in `template`, `dev_template` or `dirty_template
498498
499499
- `{env:SOMEVAR}`: Value of environment variable `SOMEVAR`. Examples:
500500
501-
- You can pass default value using `{env:SOMEVAR:"default"}` syntax. Default value for missing variables is `UNKNOWN`.
501+
- You can pass default value using `{env:SOMEVAR:default}` syntax. Default value for missing variables is `UNKNOWN`.
502502
503-
- It is possible to pass another substitution instead of a default value using `{env:SOMEVAR:subst}` syntax, e.g. `{env:BUILD_NUMBER:ccount}`.
503+
- It is possible to pass another substitution instead of a default value using `{env:SOMEVAR:{subst}}` syntax, e.g. `{env:BUILD_NUMBER:{ccount}}`.
504504
505505
- `{timestamp:format}`: Current timestamp rendered into a specified format. Examples:
506506

setuptools_git_versioning.py

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
DEFAULT_DEV_TEMPLATE = "{tag}.post{ccount}+git.{sha}" # type: str
1616
DEFAULT_DIRTY_TEMPLATE = "{tag}.post{ccount}+git.{sha}.dirty" # type: str
1717
DEFAULT_STARTING_VERSION = "0.0.1"
18-
ENV_VARS_REGEXP = re.compile(r"\{env:([^:}]+):?([^}]+)?\}", re.IGNORECASE | re.UNICODE) # type: Pattern
18+
ENV_VARS_REGEXP = re.compile(r"\{env:([^:}]+):?([^}]+}?)?\}", re.IGNORECASE | re.UNICODE) # type: Pattern
1919
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?([^:}]+)?\}", re.IGNORECASE | re.UNICODE) # type: Pattern
2020

2121

@@ -139,12 +139,8 @@ def read_version_from_file(path): # type: (Union[str, os.PathLike]) -> str
139139
def subst_env_variables(template): # type: (str) -> str
140140
if "{env" in template:
141141
for var, default in ENV_VARS_REGEXP.findall(template):
142-
value = os.environ.get(var)
143-
144-
if value:
145-
template, _ = ENV_VARS_REGEXP.subn(value, template, count=1)
146-
else:
147-
template, _ = ENV_VARS_REGEXP.subn("{" + (default or "UNKNOWN") + "}", template, count=1)
142+
value = os.environ.get(var, default or "UNKNOWN")
143+
template, _ = ENV_VARS_REGEXP.subn(value, template, count=1)
148144

149145
return template
150146

0 commit comments

Comments
 (0)