-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsync_to_workspace.py
More file actions
65 lines (54 loc) · 2.11 KB
/
Copy pathsync_to_workspace.py
File metadata and controls
65 lines (54 loc) · 2.11 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
#!/usr/bin/env python
"""Sync a project directory to Databricks Workspace."""
import sys
import subprocess
from pathlib import Path
try:
from databricks.sdk import WorkspaceClient
except ImportError:
error_log = Path.home() / ".sync-errors.log"
with open(error_log, "a") as f:
f.write(f"databricks-sdk not installed for {sys.executable}\n")
print("⚠ databricks-sdk not available", file=sys.stderr)
sys.exit(0)
def sync_project(project_path: Path):
"""Sync project to user's Workspace."""
project_path = project_path.resolve()
projects_dir = Path.home() / "projects"
try:
project_path.relative_to(projects_dir)
except ValueError:
print(f"⚠ SKIP: {project_path} is outside {projects_dir}", file=sys.stderr)
return
try:
from utils import workspace_sync_auth, workspace_sync_dest
workspace_dest = workspace_sync_dest(project_path.name)
# Validates auth (PAT profile, else the SP-broker profile) + inits
# telemetry, and returns the env the CLI must run with.
sync_env, _ = workspace_sync_auth()
result = subprocess.run(
["databricks", "sync", str(project_path), workspace_dest, "--watch=false"],
capture_output=True,
text=True,
env=sync_env,
)
if result.returncode == 0:
print(f"✓ Synced to {workspace_dest}")
# Telemetry: track workspace sync events
try:
from telemetry import log_telemetry
log_telemetry("event", "workspace_sync")
except Exception:
pass # Telemetry must never break sync
else:
print(f"⚠ Sync warning: {result.stderr}", file=sys.stderr)
except Exception as e:
error_log = Path.home() / ".sync-errors.log"
with open(error_log, "a") as f:
f.write(f"{project_path}: {e}\n")
print(f"⚠ Sync failed (logged to ~/.sync-errors.log)", file=sys.stderr)
if __name__ == "__main__":
if len(sys.argv) > 1:
sync_project(Path(sys.argv[1]))
else:
sync_project(Path.cwd())