-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
179 lines (123 loc) · 3.59 KB
/
Copy pathmain.py
File metadata and controls
179 lines (123 loc) · 3.59 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
import os
import json
import subprocess
import threading
from datetime import datetime
import tkinter as tk
from tkinter import simpledialog
from PIL import Image, ImageDraw
import pystray
REPO_PATH = r"C:\AutoCommitter"
CONFIG_PATH = os.path.join(REPO_PATH, "config.json")
LOG_FILE = "log.txt"
os.makedirs(REPO_PATH, exist_ok=True)
def load_config():
if os.path.exists(CONFIG_PATH):
with open(CONFIG_PATH, "r") as f:
return json.load(f)
return {}
def save_config(cfg):
with open(CONFIG_PATH, "w") as f:
json.dump(cfg, f, indent=4)
cfg = load_config()
git = None
class GitManager:
def __init__(self, repo_url):
self.repo_url = repo_url
self.path = REPO_PATH
self.log_file = os.path.join(REPO_PATH, LOG_FILE)
def run(self, cmd):
subprocess.run(cmd, cwd=self.path, shell=True)
def init(self):
os.makedirs(self.path, exist_ok=True)
if not os.path.exists(os.path.join(self.path, ".git")):
self.run("git init")
self.run("git branch -M main")
self.run(f'git remote add origin {self.repo_url}')
if not os.path.exists(self.log_file):
with open(self.log_file, "w") as f:
f.write("AutoCommitter log\n")
def commit(self):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open(self.log_file, "a") as f:
f.write(f"Commit at {now}\n")
self.run("git add .")
status = subprocess.run(
"git status --porcelain",
cwd=self.path,
shell=True,
capture_output=True,
text=True
)
if not status.stdout.strip():
return "No changes"
self.run(f'git commit -m "Auto commit {now}"')
self.run("git push origin main")
return "Committed"
def ensure_git():
global git, cfg
if cfg.get("repo_url"):
git = GitManager(cfg["repo_url"])
git.init()
def do_startup_commit():
if git:
try:
git.commit()
except Exception as e:
print("Commit error:", e)
def ask_repo_url():
root = tk.Tk()
root.withdraw()
url = simpledialog.askstring(
"AutoCommitter",
"Enter GitHub Repo URL (HTTPS or SSH):"
)
root.destroy()
return url
def set_uri(icon, item):
def worker():
url = ask_repo_url()
if not url:
return
cfg["repo_url"] = url.strip()
cfg["repo_path"] = REPO_PATH
save_config(cfg)
ensure_git()
do_startup_commit()
threading.Thread(target=worker).start()
def commit_now(icon, item):
def worker():
if git:
print(git.commit())
else:
print("No repo set")
threading.Thread(target=worker).start()
def exit_app(icon, item):
icon.stop()
def create_image():
image = Image.new("RGB", (64, 64), "black")
dc = ImageDraw.Draw(image)
dc.rectangle((16, 16, 48, 48), fill="white")
return image
def build_menu():
items = []
if cfg.get("repo_url"):
items.append(pystray.MenuItem("Change Repo URL", set_uri))
items.append(pystray.MenuItem("Commit now", commit_now))
else:
items.append(pystray.MenuItem("Set Repo URL", set_uri))
items.append(pystray.MenuItem("Exit", exit_app))
return pystray.Menu(*items)
def run_tray():
icon = pystray.Icon(
"AutoCommitter",
create_image(),
"AutoCommitter",
build_menu()
)
icon.run()
if __name__ == "__main__":
ensure_git()
if git:
do_startup_commit()
run_tray()