-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassgit.py
More file actions
334 lines (286 loc) · 14.9 KB
/
classgit.py
File metadata and controls
334 lines (286 loc) · 14.9 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python3
"""
- Default path: ~/ClassGit
- Encrypts course files before push (without touching originals)
- Decrypts pulled files automatically into ~/ClassGit/courses
- Never pushes the key
- GitHub repo URL and public key stored locally during setup
- Incremental pushes: only new or modified files are encrypted and pushed
- Generates a README.md during push with system info
"""
import os
import subprocess
from pathlib import Path
import shutil
import tempfile
# -----
# logo intro
# -----
print(r"""
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░▒░░░░░░░░░░░░░░░▓░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░█▓▓█▓▓█▓▓▓█░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░▓▓▓▓░░░░░▓▓█▓█░░░░░░░░░░░░░░░░░░░░
░░░░░░▒░░░░░░░░░░░░░█▓▓░░░░░░░░░██▓░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░█▓▓░░░░░░░░░▓█▓▒░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░▓▓▓░░░░░░░░░▓█▓▒░░░░░░▓██░░░░░░░░░░
░░░░░░░░░░░░░░░▒░██▓▓██▓█▓▓████▓███▓█▓░░░░▓▓█░░░░░░░░░░
░░░░░░░░░░░░░░░░░██▓▓▓▓██▓█░▓███▓▓▓▓░░░░█▓░░░░░░░░▒░░░░
░░░░░░░░░░░░░░░░░█▓▓█▓▓▓██░░░▓██▓█▓░██▓▓░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░█████▒██▓▓░▓██▓█▓█▓░█▓▓▓░░▓▓░░░░░░░░░░
░░░░░░░░░░░░░███▓░░▓█▓█▓█▓░░░██▓▓▓███▓░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░▓░░░░░█▓▓░░█▓▓▓▓████▓▓▓▓██▓▓░░█░░░░░░░░░░░░
░░░░░░░░░░░█░░▓░░░▒█░░█▓░▓█▓█▓▓▓░█▓░░░▓█░░░▓░░░░░░░░░░░
░░░░░░░░░░█▒░░░░░░▓▓░░▓░░▓░█▓░▓█░░█░░░█▓█░░░▓░░░░░░░░░░
░░░░░░░░░█▓█▓█▓▓█░░░░░█░█░░▓▓░░█░░░░░░▒█▓▓▓█▓▓░░░░░░░░░
░░░░░░░░░░█▓█▓███▓▓██▓░░▓░░██░░░░▓██▓▓▓▓▓▓█▓▓░░░░░░░░░░
░░░░░░░░░░░░░░░░▒█▓█▓▓▓█▓██▓▓██▓██▓█▓▓█░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░███░░░░░▓▓█░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░▓██▓█░██░░░░█▓▓▒░░▓██▓▓░▓█▓█▓░▓███▓░█▓░█▓█▓██░░░░░
░░░░░█▓░▓█░▓▓░░░░▓██▓░░▓▒░░░░█▓░░░░▓▓░░░░█▓░░░▓█░░░░░░░
░░░░░▓█░░░░▓▓░░░██░▓▓░░▓██▒░░▓▓██░░▓▓░▓▓░██░░░▓█░░░░░░░
░░░░░█▓░▓▓░▓▓░░░▓▓▓▓▓░░░░░▓█░░░░█▓░█▓░▓█░▓█░░░▓█░░░░░░░
░░░░░▓▓▓▓█░▓█▓▓░▓█░░▓▓░▓█▓▓▓░▓▓▓█▓░▓▓▓██░▓█░░░▓█░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
""")
# -----------------------------
# Configuration
# -----------------------------
LOCAL_DIR = Path.home() / "ClassGit"
CONFIG_DIR = LOCAL_DIR / "config"
COURSES_DIR = LOCAL_DIR / "courses"
AGE_KEY_PATH = CONFIG_DIR / "age_key.txt"
REPO_FILE = CONFIG_DIR / "repo_url.txt"
PUBLIC_KEY_FILE = CONFIG_DIR / "public_key.txt"
# -----------------------------
# Utility Functions
# -----------------------------
def run(cmd, cwd=None):
result = subprocess.run(cmd, shell=True, cwd=cwd)
if result.returncode != 0:
print(f"Error running: {cmd}")
exit(1)
def encrypt_file(file_path, recipient, output_path):
subprocess.run([
"age",
"-r", recipient,
"-o", str(output_path),
str(file_path)
], check=True)
def decrypt_file(file_path, key_path, output_path):
subprocess.run([
"age",
"-d",
"-i", str(key_path),
"-o", str(output_path),
str(file_path)
], check=True)
# -----------------------------
# Setup Functions
# -----------------------------
def configure_repo():
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
COURSES_DIR.mkdir(exist_ok=True)
if not AGE_KEY_PATH.exists():
run(f"age-keygen -o {AGE_KEY_PATH}")
print(f"Encryption key created at {AGE_KEY_PATH}")
if REPO_FILE.exists():
repo_url = REPO_FILE.read_text().strip()
else:
repo_url = input("Enter your GitHub repository HTTPS URL: ").strip()
REPO_FILE.write_text(repo_url)
if not PUBLIC_KEY_FILE.exists():
key = input("Enter the Age public key to encrypt courses (this will be saved): ").strip()
PUBLIC_KEY_FILE.write_text(key)
if not (LOCAL_DIR / ".git").exists():
run("git init", cwd=LOCAL_DIR)
run(f"git remote add origin {repo_url}", cwd=LOCAL_DIR)
run("git branch -M main", cwd=LOCAL_DIR)
with open(LOCAL_DIR / ".gitignore", "w") as f:
f.write("config/age_key.txt\n")
run("git add .gitignore", cwd=LOCAL_DIR)
run('git commit -m "Initial commit with gitignore"', cwd=LOCAL_DIR)
return repo_url
def get_public_key():
return PUBLIC_KEY_FILE.read_text().strip()
# -----------------------------
# Core Functions
# -----------------------------
def generate_readme(tmpdir):
"""Create or update README.md with system info."""
readme_path = tmpdir / "README.md"
content = f"""# ClassGit
ClassGit is a system for secure synchronization of your course files using Git and age encryption.
**Default local folder:** `{LOCAL_DIR}`
**Encrypted files:** Stored on GitHub only, local files remain unencrypted.
**Key location:** `{AGE_KEY_PATH}` (never pushed)
**Public key:** stored locally for encryption
**Do not remove** the `{LOCAL_DIR}/.git` folder.
## Usage
1. **Push courses:** Encrypts all files in `{COURSES_DIR}` and pushes them to the remote repo.
2. **Pull courses:** Downloads encrypted files from GitHub and decrypts them automatically into `{COURSES_DIR}`.
3. **Add a new device:** Copy your age key to another device to access the encrypted files.
4. **Git status:** Check the repository status locally.
## License & Disclaimer
ClassGit is provided **as-is**. The author is not responsible for any data loss, misuse, or damage caused while using the system.
"""
readme_path.write_text(content)
def push_courses(repo_url):
recipient = get_public_key()
if not any(COURSES_DIR.iterdir()):
print("No course files found to push.")
return
encrypted_dir = LOCAL_DIR / "encrypted"
encrypted_dir.mkdir(exist_ok=True)
print("🔒 Synchronizing encrypted directory...")
# --- Encrypt or update changed files ---
for root, dirs, files in os.walk(COURSES_DIR):
rel_path = Path(root).relative_to(COURSES_DIR)
for d in dirs:
(encrypted_dir / rel_path / d).mkdir(parents=True, exist_ok=True)
for f in files:
src = Path(root) / f
dst = encrypted_dir / rel_path / (f + ".age")
if not dst.exists() or src.stat().st_mtime > dst.stat().st_mtime:
print(f"🔒 Encrypting {src} → {dst}")
encrypt_file(src, recipient, dst)
# --- Remove orphan encrypted files ---
for root, dirs, files in os.walk(encrypted_dir):
for f in files:
if not f.endswith(".age"):
continue
enc_path = Path(root) / f
try:
rel = enc_path.relative_to(encrypted_dir)
except Exception:
continue
orig = COURSES_DIR / rel.with_suffix('') # remove .age
if not orig.exists():
print(f"🗑️ Removing orphan encrypted file: {enc_path}")
try:
enc_path.unlink()
except Exception as e:
print(f"❌ Failed to remove {enc_path}: {e}")
# --- Remove empty directories in encrypted_dir ---
for root, dirs, files in os.walk(encrypted_dir, topdown=False):
rootp = Path(root)
if rootp == encrypted_dir:
continue
if not any(rootp.iterdir()):
try:
print(f"🗑️ Removing empty directory: {rootp}")
rootp.rmdir()
except Exception as e:
print(f"❌ Failed to remove dir {rootp}: {e}")
# --- Generate README.md ---
generate_readme(LOCAL_DIR)
# --- Update .gitignore ---
gitignore_path = LOCAL_DIR / ".gitignore"
with open(gitignore_path, "w") as f:
f.write("# Local sensitive / plaintext\n")
f.write("config/\n")
f.write("courses/\n")
f.write("\n# Keep encrypted files (in encrypted/ or repo root) and README\n")
f.write("!.gitignore\n")
f.write("!README.md\n")
# --- Git snapshot (single commit) ---
print("🧹 Creating single-commit snapshot to avoid large history...")
subprocess.run(["git", "checkout", "--orphan", "temp-branch"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "add", "-A"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "commit", "-m", "Snapshot: update courses and README"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "branch", "-D", "main"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "branch", "-m", "main"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "reflog", "expire", "--expire=now", "--all"], cwd=LOCAL_DIR, check=True)
subprocess.run(["git", "gc", "--prune=now", "--aggressive"], cwd=LOCAL_DIR, check=True)
# --- Push forced to remote ---
subprocess.run(["git", "push", "--force", "origin", "main"], cwd=LOCAL_DIR, check=True)
print("✅ Courses encrypted and pushed (history reset).")
def pull_courses():
print("⬇️ Pulling latest encrypted files from remote...")
# --- fetch and force align with origin/main if divergence ---
fetch = subprocess.run(["git", "fetch", "origin"], cwd=LOCAL_DIR)
if fetch.returncode != 0:
print("❌ Failed to fetch from remote.")
return
# check divergence
status = subprocess.run(["git", "status", "--porcelain=2", "--branch"],
cwd=LOCAL_DIR, capture_output=True, text=True)
if "branch.ab" in status.stdout and "+" in status.stdout:
print("⚠️ Local branch diverged from remote. Resetting to origin/main...")
reset = subprocess.run(["git", "reset", "--hard", "origin/main"], cwd=LOCAL_DIR)
if reset.returncode != 0:
print("❌ Failed to reset to remote branch.")
return
else:
# normal fast-forward pull
pull = subprocess.run(["git", "pull", "--ff-only"], cwd=LOCAL_DIR)
if pull.returncode != 0:
print("⚠️ Pull failed, forcing reset to remote state...")
subprocess.run(["git", "reset", "--hard", "origin/main"], cwd=LOCAL_DIR)
encrypted_dir = LOCAL_DIR / "encrypted"
decrypted_dir = Path.home() / "ClassGit" / "courses"
decrypted_dir.mkdir(parents=True, exist_ok=True)
if not encrypted_dir.exists():
print("❌ No encrypted/ directory found after sync. Check remote repo contents.")
return
# Iterate through all .age files and decrypt them
for root, _, files in os.walk(encrypted_dir):
for file in files:
if file.endswith(".age"):
src = Path(root) / file
relative = src.relative_to(encrypted_dir)
dst = decrypted_dir / relative.with_suffix("")
dst.parent.mkdir(parents=True, exist_ok=True)
cmd = [
"age", "-d",
"-i", str(AGE_KEY_PATH),
"-o", str(dst),
str(src)
]
print(f"🔓 Decrypting {src} → {dst}")
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
print(f"❌ Failed to decrypt {src}")
def add_device():
new_path = Path(input("Enter full path to copy your age key for a new device: ").strip())
new_path.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(AGE_KEY_PATH, new_path)
print(f"🔑 Key copied to {new_path}")
def status():
run("git status", cwd=LOCAL_DIR)
# -----------------------------
# Menu
# -----------------------------
def menu():
repo_url = configure_repo()
while True:
print("""
ClassGit Menu
1. ⬆️ Push courses
2. ⬇️ Pull courses
3. 💻 Add a new device
4. 📋 Show Git status
5. 🚪 Quit
""")
choice = input("Select an option: ").strip()
if choice == "1":
push_courses(repo_url)
elif choice == "2":
pull_courses()
elif choice == "3":
add_device()
elif choice == "4":
status()
elif choice == "5":
exit()
else:
print("❓ Invalid option, try again.")
# -----------------------------
# Main
# -----------------------------
if __name__ == "__main__":
menu()