Skip to content

Commit 06904a0

Browse files
Add envsubst.py script (#3425)
## Changes Use the python script instead of the installed `envsubst` binary in acceptance tests. ## Why envsubst is not installed by default on Ubuntu and has to be installed by installing the GNU gettext tools. Since we will start running acceptance tests on DBR soon, it's better to use a python script we own instead since that means one less dependency to install when running tests on DBR. I tried installing envsubst on DBR, but apt installations are not allowed on serverless. It's easier to have a simple python script instead: ``` Failed to install envsubst: Command '['apt-get', 'update']' returned non-zero exit status 100. stdout: Reading package lists... stderr: E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied) E: Unable to lock directory /var/lib/apt/lists/ ``` ## Tests Existing tests pass.
1 parent 5da079b commit 06904a0

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"jupyter.interactiveWindow.cellMarker.codeRegex": "^# COMMAND ----------|^# Databricks notebook source|^(#\\s*%%|#\\s*\\<codecell\\>|#\\s*In\\[\\d*?\\]|#\\s*In\\[ \\])",
2020
"jupyter.interactiveWindow.cellMarker.default": "# COMMAND ----------",
2121
"files.associations": {
22-
"script": "shellscript"
22+
"script": "shellscript",
23+
"script.prepare": "shellscript",
24+
"script.cleanup": "shellscript"
2325
}
2426
}

acceptance/bin/envsubst.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
"""
3+
This script implements functionality equivalent to envsubst. We use a python
4+
script instead because that way we do not need to install it when we run integration
5+
tests on DBR.
6+
7+
Usage: envsubst.py
8+
9+
Substitutes environment variables in shell format strings.
10+
Reads from stdin and writes to stdout.
11+
12+
Examples:
13+
echo 'Hello $USER' | python envsubst.py
14+
echo 'Hello $USER from $HOME' | python envsubst.py
15+
"""
16+
17+
import sys
18+
import os
19+
import re
20+
21+
22+
def substitute_variables(text):
23+
"""
24+
Substitute environment variables in text.
25+
26+
Args:
27+
text: Input text containing variable references
28+
29+
Returns:
30+
Text with variables substituted
31+
"""
32+
33+
def replace_var(match):
34+
var_name = match.group(1) or match.group(2)
35+
36+
# If the environment variable is not set, replace with an empty string.
37+
return os.environ.get(var_name, "")
38+
39+
# Match both $VAR and ${VAR} formats
40+
pattern = r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}|\$([A-Za-z_][A-Za-z0-9_]*)"
41+
return re.sub(pattern, replace_var, text)
42+
43+
44+
def main():
45+
input_text = sys.stdin.read()
46+
output_text = substitute_variables(input_text)
47+
sys.stdout.write(output_text)
48+
49+
50+
if __name__ == "__main__":
51+
main()

acceptance/bundle/validate/empty_resources/script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23

34
SECTIONS = [
45
"jobs",

acceptance/script.prepare

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,10 @@ venv_activate() {
8686
source .venv/bin/activate
8787
fi
8888
}
89+
90+
envsubst() {
91+
# We need to disable MSYS_NO_PATHCONV when running the python script.
92+
# This is because the python interpreter is otherwise unable to find the python script
93+
# when MSYS_NO_PATHCONV is enabled.
94+
env -u MSYS_NO_PATHCONV envsubst.py
95+
}

0 commit comments

Comments
 (0)