-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_env.py
More file actions
70 lines (59 loc) · 1.77 KB
/
Copy pathupdate_env.py
File metadata and controls
70 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import re
import os
LOCAL_ENV = ".env.local"
README = "README.md"
TEST_PATH = ".env.test"
START_POINT = "```"
END_POINT = "```"
def get_env_names(ENV_PATH):
env_names = []
try:
with open(ENV_PATH, 'r') as f:
for line in f:
line.strip()
if not line:
continue
if '=' in line:
name, _ = line.split('=', 1)
name = name.strip()
env_names.append(name)
if '#' in line:
comment = line.strip()
env_names.append(comment)
except FileNotFoundError:
print(f"Error : {ENV_PATH} not found.")
exit(1)
return env_names
def write_to_readme(README_PATH, env_names):
env_block_lines = [START_POINT]
for name in env_names:
env_block_lines.append(name)
env_block_lines.append(END_POINT)
env_block = "\n".join(env_block_lines)
try:
with open(README_PATH, "r") as f:
readme_contents = f.read()
except FileNotFoundError:
print(f"Error: {README_PATH} not found.")
exit(1)
# Define a regex pattern to match the block between the markers.
pattern = re.compile(
rf'({re.escape(START_POINT)}\n)(.*?)(\n{re.escape(END_POINT)})',
re.DOTALL
)
if pattern.search(readme_contents):
# Replace the existing block with the new env block.
new_env_docs = pattern.sub(env_block, readme_contents)
else:
# If the markers are not found, append the block to the end.
new_env_docs = readme_contents + env_block
# Write the updated contents back to the spec file.
with open(README_PATH, 'w') as f:
f.write(new_env_docs)
print(f"Updated {README_PATH} with {len(env_names)} environment variables.")
def main():
env_names = get_env_names(LOCAL_ENV)
print(env_names)
write_to_readme(README, env_names)
if __name__ == '__main__':
main()