Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
111 lines (89 loc) · 3.1 KB
/
Copy pathsetup.py
File metadata and controls
111 lines (89 loc) · 3.1 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python3
"""Bootstrap the sample app: sync deps (including OpenTelemetry) and create/update .env.
Usage:
uv run setup.py
uv run setup.py <LD_SDK_KEY> <CONFIG_KEY>
"""
from __future__ import annotations
import re
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
ENV_EXAMPLE = ROOT / ".env.example"
ENV_FILE = ROOT / ".env"
def upsert_env(content: str, key: str, value: str) -> str:
pattern = re.compile(rf"^{re.escape(key)}=.*$", re.MULTILINE)
line = f"{key}={value}"
if pattern.search(content):
return pattern.sub(line, content, count=1)
return content.rstrip("\n") + f"\n{line}\n"
def ensure_env(sdk_key: str | None, config_key: str | None) -> None:
if not ENV_FILE.exists():
if not ENV_EXAMPLE.exists():
raise FileNotFoundError(f"Missing {ENV_EXAMPLE}")
shutil.copy(ENV_EXAMPLE, ENV_FILE)
print(f"Created {ENV_FILE} from .env.example")
else:
print(f"Using existing {ENV_FILE}")
if not sdk_key and not config_key:
print("Fill in CONFIG_KEY, LD_SDK_KEY, and your provider API keys.")
return
content = ENV_FILE.read_text(encoding="utf-8")
if sdk_key:
content = upsert_env(content, "LD_SDK_KEY", sdk_key)
print("Set LD_SDK_KEY in .env")
if config_key:
content = upsert_env(content, "CONFIG_KEY", config_key)
print("Set CONFIG_KEY in .env")
ENV_FILE.write_text(content, encoding="utf-8")
def sync_dependencies() -> int:
print("Syncing dependencies with uv, including OpenTelemetry...")
result = subprocess.run(["uv", "sync"], cwd=ROOT)
if result.returncode != 0:
print("uv sync failed.", file=sys.stderr)
return result.returncode
check = subprocess.run(
[
"uv",
"run",
"python",
"-c",
"import opentelemetry.sdk, opentelemetry.exporter.otlp.proto.http",
],
cwd=ROOT,
)
if check.returncode != 0:
print(
"OpenTelemetry is not installed. "
'Expected opentelemetry-sdk and opentelemetry-exporter-otlp-proto-http '
'from launchdarkly-ai-python[otel].',
file=sys.stderr,
)
return check.returncode or 1
print("OpenTelemetry is installed.")
return 0
def main() -> int:
sdk_key = sys.argv[1].strip() if len(sys.argv) > 1 else None
config_key = sys.argv[2].strip() if len(sys.argv) > 2 else None
if len(sys.argv) == 2:
print(
"Usage: uv run setup.py [<LD_SDK_KEY> <CONFIG_KEY>]",
file=sys.stderr,
)
print("Provide both keys, or neither.", file=sys.stderr)
return 1
sync_result = sync_dependencies()
if sync_result != 0:
return sync_result
try:
ensure_env(sdk_key or None, config_key or None)
except FileNotFoundError as exc:
print(exc, file=sys.stderr)
return 1
print("\nSetup complete. Run:")
print(' uv run main.py "Your question here"')
return 0
if __name__ == "__main__":
raise SystemExit(main())