-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
65 lines (57 loc) · 1.84 KB
/
Copy pathencode.py
File metadata and controls
65 lines (57 loc) · 1.84 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 python3
from pathlib import Path
import sys
import base64
import subprocess
import zlib
import shlex
file = Path(sys.argv[1]) if len(sys.argv) > 1 else None
if not file:
print("No file path specified!")
exit(1)
if not file.exists():
print(f"The file '{file}' was not found in the current directory.")
exit(1)
def evaluate(comamnd):
return subprocess.run(
shlex.split(comamnd), stdout=subprocess.PIPE, stderr=subprocess.PIPE
).stdout.decode("utf-8")
def chunk_data(file, chunk_size=2048):
data = file.read_bytes()
filename = file.name
compressed = zlib.compress(data)
encoded = base64.b64encode(compressed).decode("utf-8")
res = []
for i in range(0, len(encoded), chunk_size):
res.append(encoded[i : i + chunk_size])
return {"filename": filename, "chunks": res}
if Path(".git").exists():
print("warning! existing git repository found!")
print("proceed? (y/n):", end=" ")
if input().lower() != "y":
print("aborting...")
exit(0)
else:
print("no git repository found, initializing new git repository...")
evaluate("git init")
print("git repository initialized")
print()
print("initial commit message (default: where am i?):", end=" ")
msg = input()
if msg.strip() == "":
msg = "where am i?"
evaluate(f'git commit --allow-empty -m "{msg}"')
print("created initial commit")
print(
"caution! running the script will pollute your tags, only run in a test repository!"
)
print()
res = chunk_data(file, 125)
filename = res.get("filename").replace(" ", "_")
data = res.get("chunks")
print(f"encoded data into {len(data)} chunks")
for i, chunk in enumerate(data):
tag_name = f"{filename}_{i:04}"
print(f"creating tag: {tag_name} with size {len(chunk)}")
evaluate(f'git tag -a {tag_name} -m "{chunk}"')
print("done!")