-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.py
More file actions
159 lines (134 loc) · 5.77 KB
/
export.py
File metadata and controls
159 lines (134 loc) · 5.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""ddday Export — One-command migration bundle"""
import json, os, shutil, subprocess, datetime, tarfile, tempfile, glob
HOME = os.path.expanduser("~")
DDDAY_HOME = os.environ.get("DDDAY_HOME", os.path.dirname(os.path.abspath(__file__)))
TODAY = datetime.date.today().isoformat()
USERNAME = os.path.basename(HOME)
# ── Directories to migrate (customize these) ───────────────────
MIGRATE_DIRS = {
"ddday": DDDAY_HOME,
# Add your own report directories here, e.g.:
# "reports": os.path.join(HOME, ".myreports"),
}
MIGRATE_FILES = {
# Add credential files or configs here, e.g.:
# "credentials/my-service-account.json": os.path.join(HOME, "credentials/key.json"),
}
# Claude memory
MEMORY_GLOB = os.path.join(HOME, ".claude/projects/*/memory")
# ── Collect info ────────────────────────────────────────────────
print(f"🔍 Scanning environment ({USERNAME}@{os.uname().nodename})...")
crontab = ""
try:
crontab = subprocess.check_output(["crontab", "-l"], stderr=subprocess.DEVNULL).decode()
except:
pass
pip_freeze = ""
try:
pip_freeze = subprocess.check_output(
["python3", "-m", "pip", "freeze", "--local"], stderr=subprocess.DEVNULL).decode()
except:
pass
ws_file = os.path.join(DDDAY_HOME, "workspace.json")
with open(ws_file, "r") as f:
workspace = json.load(f)
symlinks = []
skills_dir = os.path.join(HOME, ".claude/skills")
if os.path.isdir(skills_dir):
for item in os.listdir(skills_dir):
full = os.path.join(skills_dir, item)
if os.path.islink(full):
symlinks.append({
"link": full.replace(HOME, "$HOME"),
"target": os.readlink(full).replace(HOME, "$HOME"),
"name": item
})
# ── Manifest ────────────────────────────────────────────────────
manifest = {
"version": "1.0.0",
"exported_at": TODAY,
"exported_from": {
"hostname": os.uname().nodename,
"username": USERNAME,
"home": HOME,
"platform": os.uname().sysname,
"arch": os.uname().machine,
},
"path_mapping": {
"old_home": HOME,
"old_username": USERNAME,
},
"cron_jobs": crontab.strip().split("\n") if crontab.strip() else [],
"symlinks": symlinks,
"python_deps": [l for l in pip_freeze.split("\n") if l.strip()],
"projects": workspace.get("projects", []),
"restore_steps": [
"1. Extract the migration bundle",
"2. Run: bash setup.sh",
"3. Run: python3 doctor.py",
"4. Start working: /ddday"
]
}
# ── Generate work snapshot first ────────────────────────────────
print(f"\n📸 Generating work snapshot...")
snapshot_script = os.path.join(DDDAY_HOME, "work_snapshot.py")
if os.path.isfile(snapshot_script):
subprocess.run(["python3", snapshot_script], cwd=DDDAY_HOME, capture_output=False, timeout=30)
# ── Pack ────────────────────────────────────────────────────────
staging = tempfile.mkdtemp(prefix="ddday-migration-")
print(f"\n📦 Packing...")
for name, src in MIGRATE_DIRS.items():
dst = os.path.join(staging, name)
if os.path.isdir(src):
shutil.copytree(src, dst, symlinks=False,
ignore=shutil.ignore_patterns("node_modules", ".git", "__pycache__", "*.pyc", ".DS_Store"))
print(f" ✅ {name}/")
else:
print(f" ⚠️ {name}/ not found, skipping")
for rel_path, src in MIGRATE_FILES.items():
dst = os.path.join(staging, rel_path)
os.makedirs(os.path.dirname(dst), exist_ok=True)
if os.path.isfile(src):
shutil.copy2(src, dst)
print(f" ✅ {rel_path}")
memory_dirs = glob.glob(MEMORY_GLOB)
if memory_dirs:
mem_dst = os.path.join(staging, "claude-memory")
os.makedirs(mem_dst, exist_ok=True)
for md in memory_dirs:
parent_name = os.path.basename(os.path.dirname(md))
shutil.copytree(md, os.path.join(mem_dst, parent_name),
ignore=shutil.ignore_patterns(".DS_Store"))
print(f" ✅ claude-memory/ ({len(memory_dirs)} projects)")
with open(os.path.join(staging, "manifest.json"), "w") as f:
json.dump(manifest, f, indent=2, ensure_ascii=False)
print(f" ✅ manifest.json")
if crontab:
with open(os.path.join(staging, "crontab.bak"), "w") as f:
f.write(crontab)
print(f" ✅ crontab.bak")
if pip_freeze:
with open(os.path.join(staging, "requirements.txt"), "w") as f:
f.write(pip_freeze)
print(f" ✅ requirements.txt")
for fname in ["setup.sh", "doctor.py", "work_snapshot.py"]:
src = os.path.join(DDDAY_HOME, fname)
if os.path.isfile(src):
shutil.copy2(src, os.path.join(staging, fname))
print(f" ✅ {fname}")
snapshot_src = os.path.join(DDDAY_HOME, "context", "LATEST-SNAPSHOT.md")
if os.path.isfile(snapshot_src):
shutil.copy2(snapshot_src, os.path.join(staging, "READ-ME-FIRST.md"))
print(f" ✅ READ-ME-FIRST.md (work snapshot)")
output_path = os.path.join(HOME, "Desktop", f"ddday-migration-{TODAY}.tar.gz")
with tarfile.open(output_path, "w:gz") as tar:
tar.add(staging, arcname="ddday-migration")
size_mb = os.path.getsize(output_path) / (1024 * 1024)
print(f"\n📦 Migration bundle: {output_path}")
print(f" Size: {size_mb:.1f} MB")
shutil.rmtree(staging)
print(f"\n🚀 Restore on new machine:")
print(f" 1. tar xzf {os.path.basename(output_path)}")
print(f" 2. cd ddday-migration && bash setup.sh")
print(f" 3. python3 doctor.py")