-
Notifications
You must be signed in to change notification settings - Fork 51
/
bundle.py
executable file
·69 lines (52 loc) · 2.25 KB
/
bundle.py
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
#!/usr/bin/env python3
import re
from pathlib import Path
from typing import Dict, Union
import os
def replace_env_macro() -> bool:
"""
Replace content between environment macro markers with formatted environment variables.
Args:
provider_type (str): The type of provider (e.g., 'databricks')
host (str): The host URL
model (str): The model name
Returns:
bool: True if successful, False otherwise
"""
file_path = './src/main.ts'
try:
# Read the file content
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Format the environment variables
formatted_vars = [
f" process.env.GOOSE_PROVIDER__TYPE = '{os.getenv("GOOSE_BUNDLE_TYPE")}';",
f" process.env.GOOSE_PROVIDER__HOST = '{os.getenv("GOOSE_BUNDLE_HOST")}';",
f" process.env.GOOSE_PROVIDER__MODEL = '{os.getenv("GOOSE_BUNDLE_MODEL")}';"
]
replacement_content = "\n".join(formatted_vars)
replacement_content += "\n return true;"
# Define the pattern to match content between markers
pattern = r'//{env-macro-start}//.*?//{env-macro-end}//'
flags = re.DOTALL # Allow matching across multiple lines
# Create the replacement string with the markers and new content
replacement = f"//{{env-macro-start}}//\n{replacement_content}\n//{{env-macro-end}}//"
# Perform the replacement
new_content, count = re.subn(pattern, replacement, content, flags=flags)
if count == 0:
print(f"Error: Could not find macro markers in {file_path}")
return False
# Write the modified content back to the file
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Successfully updated {file_path}")
return True
except Exception as e:
print(f"Error processing file {file_path}: {str(e)}")
return False
# Example usage
if __name__ == '__main__':
success = replace_env_macro()
if not success:
print("Failed to update environment variables")
exit(1)