-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_push.py
More file actions
391 lines (343 loc) · 16.5 KB
/
auto_push.py
File metadata and controls
391 lines (343 loc) · 16.5 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!/usr/bin/env python3
import os
import subprocess
import datetime
import requests
import time
import sys
import math
import shutil
try:
from github import Github
HAS_PYGITHUB = True
except Exception:
HAS_PYGITHUB = False
# Load env file
if os.path.exists(".env"):
print("🔍 Loading environment variables from .env ...")
with open(".env") as f:
for line in f:
if line.strip() and not line.strip().startswith("#"):
key, _, value = line.strip().partition("=")
os.environ[key.strip()] = value.strip()
# Translation
LANGUAGES = {
"en": {
"welcome_header": "Auto Push Tool",
"welcome_by": "Developer: Hirokixd",
"select_language": "Select language:",
"select_mode": "Select mode:",
"mode_auto_push": "1. Auto Push (push local repos listed in repos file)",
"mode_delete_branch": "2. Delete Branch (delete branch on repos listed in repos file)",
"start_auto_push": "=== Auto Push Started ===",
"end_auto_push": "=== Auto Push Finished ===",
"repo_not_found_file": "❌ Repository list file '{file}' not found.",
"folder_not_found": "❌ Folder not found: {path}",
"not_git_repo": "❌ Not a git repository, skipped.",
"repo_create": "📦 Repository not found on GitHub, creating...",
"repo_created": "✅ Repository {repo} successfully created on GitHub.",
"repo_create_failed": "❌ Failed to create new repository on GitHub.",
"branch_created": "✅ Branch '{branch}' created.",
"branch_exists": "ℹ️ Branch '{branch}' already exists, continuing.",
"remote_set": "🔗 Remote origin set to {org}/{repo}",
"remote_ok": "ℹ️ Remote origin is already correct.",
"no_changes": "ℹ️ No changes to commit.",
"commit_created": "✅ Commit created: {msg}",
"push_done": "✅ Push {repo} completed.",
"framework_filter": "⚙️ Filtering files larger than 100MB...",
"framework_push": "📤 {repo} pushing in {batch} batch(es)...",
"framework_final_push": "🔁 Performing final push for all changes...",
"framework_done": "✅ {repo} fully pushed!",
"process_done": "✅ Process finished! Log saved to '{log}'",
"delete_start": "=== Delete Branch Started ===",
"delete_end": "=== Delete Branch Finished ===",
"delete_prompt_branch": "Enter branch name to delete: ",
"delete_empty_branch": "❌ Branch name cannot be empty.",
"delete_processing": "🔍 Processing repo: {repo}",
"delete_deleted": "✅ Branch '{branch}' deleted from {repo}.",
"delete_not_found": "⚠️ Branch '{branch}' not found in {repo}. Skipped.",
"delete_error": "❌ Error in {repo}: {error}",
"missing_env_token": "❌ ERROR: GITHUB_TOKEN must be set in environment variables.",
"pygithub_missing": "❌ ERROR: PyGithub not installed. Install with: pip install PyGithub",
},
"id": {
"welcome_header": "Auto Push Tool",
"welcome_by": "Developer: Hirokixd",
"select_language": "Pilih bahasa:",
"select_mode": "Pilih mode:",
"mode_auto_push": "1. Auto Push (push repo lokal yang ada di daftar repos)",
"mode_delete_branch": "2. Delete Branch (hapus branch pada repo yang terdaftar)",
"start_auto_push": "=== Proses Auto Push Dimulai ===",
"end_auto_push": "=== Proses Auto Push Selesai ===",
"repo_not_found_file": "❌ File daftar repo '{file}' tidak ditemukan.",
"folder_not_found": "❌ Folder tidak ditemukan: {path}",
"not_git_repo": "❌ Bukan repository git, dilewati.",
"repo_create": "📦 Repository belum ada di GitHub, membuat...",
"repo_created": "✅ Repository {repo} berhasil dibuat di GitHub.",
"repo_create_failed": "❌ Gagal membuat repository baru di GitHub.",
"branch_created": "✅ Branch '{branch}' dibuat.",
"branch_exists": "ℹ️ Branch '{branch}' sudah ada, lanjut.",
"remote_set": "🔗 Remote origin diatur ke {org}/{repo}",
"remote_ok": "ℹ️ Remote origin sudah benar.",
"no_changes": "ℹ️ Tidak ada perubahan untuk di-commit.",
"commit_created": "✅ Commit dibuat: {msg}",
"push_done": "✅ Push {repo} selesai.",
"framework_filter": "⚙️ Memfilter file berukuran lebih dari 100MB...",
"framework_push": "📤 {repo} sedang dipush dalam {batch} batch...",
"framework_final_push": "🔁 Melakukan push akhir untuk semua perubahan...",
"framework_done": "✅ {repo} berhasil di-push seluruhnya!",
"process_done": "✅ Proses selesai! Log tersimpan di '{log}'",
"delete_start": "=== Proses Hapus Branch Dimulai ===",
"delete_end": "=== Proses Hapus Branch Selesai ===",
"delete_prompt_branch": "Masukkan nama branch yang ingin dihapus: ",
"delete_empty_branch": "❌ Nama branch tidak boleh kosong.",
"delete_processing": "🔍 Memproses repo: {repo}",
"delete_deleted": "✅ Branch '{branch}' berhasil dihapus dari {repo}.",
"delete_not_found": "⚠️ Branch '{branch}' tidak ditemukan di {repo}. Dilewati.",
"delete_error": "❌ Error di {repo}: {error}",
"missing_env_token": "❌ ERROR: GITHUB_TOKEN harus diset di environment variables.",
"pygithub_missing": "❌ ERROR: PyGithub belum terpasang. Install dengan: pip install PyGithub",
}
}
# Welcome message
def welcome_message(T):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("===========================================")
print(f"🔹 {T['welcome_header']}")
print(f"🔹 {T['welcome_by']}")
print(f"🔹 Started at: {now}")
print("===========================================\n")
# Load env
BASE_DIR = os.getenv("BASE_DIR", os.path.expanduser("~/orion"))
REPO_LIST_FILE = os.getenv("REPO_LIST_FILE", "repos.txt")
BRANCH = os.getenv("BRANCH", "")
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME", "Wrdn28")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "")
GITHUB_ORG = os.getenv("GITHUB_ORG", "")
LOG_FILE = os.getenv("LOG_FILE", "push_log.txt")
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "50000"))
# lang selection
print("Select language / Pilih bahasa:")
print("1. English")
print("2. Bahasa Indonesia")
choice = input(">> ").strip()
LANG = "en" if choice == "1" else "id"
T = LANGUAGES[LANG]
welcome_message(T)
# Mode selection
print(T["select_mode"])
print(T["mode_auto_push"])
print(T["mode_delete_branch"])
mode_choice = input(">> ").strip()
def print_log(msg, log_file):
print(msg)
try:
with open(log_file, "a") as f:
f.write(msg + "\n")
except Exception:
pass
def run_command(command, cwd=None, log_file=None):
"""Run shell command and optionally log output."""
try:
result = subprocess.run(
command, cwd=cwd, shell=True, check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
)
out = result.stdout.strip()
if log_file and out:
print_log(out, log_file)
return out
except subprocess.CalledProcessError as e:
err = e.stderr.strip() or e.stdout.strip()
if log_file:
print_log(f"❌ ERROR: {err}", log_file)
return f"ERROR: {err}"
def repo_exists_on_github(repo_name):
org_or_user = GITHUB_ORG if GITHUB_ORG else GITHUB_USERNAME
url = f"https://api.github.com/repos/{org_or_user}/{repo_name}"
headers = {"Authorization": f"token {GITHUB_TOKEN}"} if GITHUB_TOKEN else {}
try:
return requests.get(url, headers=headers, timeout=15).status_code == 200
except requests.RequestException:
return False
def create_repo_on_github(repo_name):
if not GITHUB_TOKEN:
return False
if GITHUB_ORG:
url = f"https://api.github.com/orgs/{GITHUB_ORG}/repos"
else:
url = "https://api.github.com/user/repos"
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
data = {"name": repo_name, "private": False}
try:
return requests.post(url, json=data, headers=headers, timeout=15).status_code == 201
except requests.RequestException:
return False
def ensure_branch_exists(repo_path, branch, log_file):
branches = run_command("git branch -a", cwd=repo_path)
if branch not in branches:
run_command(f"git checkout -b {branch}", cwd=repo_path, log_file=log_file)
return T["branch_created"].format(branch=branch)
else:
run_command(f"git checkout {branch}", cwd=repo_path, log_file=log_file)
return T["branch_exists"].format(branch=branch)
def setup_remote_if_needed(repo_path, repo_name, log_file):
remote_url = run_command("git remote get-url origin", cwd=repo_path)
org_or_user = GITHUB_ORG if GITHUB_ORG else GITHUB_USERNAME
https_url = f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{org_or_user}/{repo_name}.git"
if "ERROR" in remote_url or not remote_url or org_or_user not in remote_url:
run_command("git remote remove origin", cwd=repo_path, log_file=log_file)
run_command(f"git remote add origin {https_url}", cwd=repo_path, log_file=log_file)
return T["remote_set"].format(org=org_or_user, repo=repo_name)
return T["remote_ok"]
# frameworks_base handler
def handle_frameworks_base(repo_path, repo_name, log_file):
print_log(T["framework_filter"], log_file)
if not shutil.which("git-filter-repo"):
subprocess.run("wget -q https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo", shell=True)
subprocess.run("chmod +x git-filter-repo", shell=True)
subprocess.run("sudo mv -f git-filter-repo /usr/local/bin/git-filter-repo", shell=True)
run_command("git filter-repo --strip-blobs-bigger-than 100M --force", cwd=repo_path)
branches = subprocess.getoutput("git branch --format='%(refname:short)'").split()
if BRANCH not in branches:
print_log(f"📦 {T['branch_created'].format(branch=BRANCH)}", log_file)
run_command(f"git checkout -b {BRANCH}", cwd=repo_path)
else:
run_command(f"git checkout {BRANCH}", cwd=repo_path)
remotes = subprocess.getoutput("git remote").split()
if "origin" not in remotes:
run_command(f"git remote add origin https://{GITHUB_USERNAME}:{GITHUB_TOKEN}@github.com/{GITHUB_ORG}/{repo_name}.git", cwd=repo_path)
try:
total_commits = int(subprocess.check_output("git rev-list --count HEAD", shell=True, cwd=repo_path).strip())
except Exception:
total_commits = 1000000
total_batches = math.ceil(total_commits / BATCH_SIZE)
print_log(T["framework_push"].format(repo=repo_name, batch=total_batches), log_file)
for i in range(total_batches, 0, -1):
start_commit = i * BATCH_SIZE
cmd = f"git push -u origin HEAD~{start_commit}:refs/heads/{BRANCH}"
progress = int(((total_batches - i + 1) / total_batches) * 100)
bar = "#" * (progress // 4) + "-" * (25 - (progress // 4))
sys.stdout.write(f"\r[{bar}] {progress}% Push batch {total_batches - i + 1}/{total_batches}...")
sys.stdout.flush()
run_command(cmd, cwd=repo_path)
time.sleep(0.3)
sys.stdout.write("\n")
print_log(T["framework_final_push"], log_file)
run_command(f"git push origin {BRANCH}", cwd=repo_path, log_file=log_file)
print_log(T["framework_done"].format(repo=repo_name), log_file)
# normal repo
def auto_push(repo_path, repo_name, log_file):
print_log(f"\n[{datetime.datetime.now()}] 🚀 Repository: {repo_name}", log_file)
if not os.path.isdir(os.path.join(repo_path, ".git")):
print_log(T["not_git_repo"], log_file)
return
if repo_name in ["frameworks_base", "base"]:
handle_frameworks_base(repo_path, repo_name, log_file)
return
if not repo_exists_on_github(repo_name):
print_log(T["repo_create"], log_file)
if create_repo_on_github(repo_name):
print_log(T["repo_created"].format(repo=repo_name), log_file)
else:
print_log(T["repo_create_failed"], log_file)
return
print_log(setup_remote_if_needed(repo_path, repo_name, log_file), log_file)
print_log(ensure_branch_exists(repo_path, BRANCH, log_file), log_file)
run_command("git add .", cwd=repo_path, log_file=log_file)
status = run_command("git status --porcelain", cwd=repo_path)
if status:
commit_msg = f"Auto commit on {datetime.datetime.now():%Y-%m-%d %H:%M:%S}"
run_command(f'git commit -m "{commit_msg}"', cwd=repo_path, log_file=log_file)
print_log(T["commit_created"].format(msg=commit_msg), log_file)
else:
print_log(T["no_changes"], log_file)
run_command(f"git push -u origin {BRANCH}", cwd=repo_path, log_file=log_file)
print_log(T["push_done"].format(repo=repo_name), log_file)
# delete branch mode
def delete_branch_on_repos(repo_names_list, branch_to_delete, log_file):
if not GITHUB_TOKEN:
print(T["missing_env_token"])
return
if not HAS_PYGITHUB:
print(T["pygithub_missing"])
return
g = Github(GITHUB_TOKEN)
owner = GITHUB_ORG if GITHUB_ORG else GITHUB_USERNAME
print_log(f"\n=== {T['delete_start']} {datetime.datetime.now()} ===", log_file)
for repo_name in repo_names_list:
print_log(f"[{datetime.datetime.now().strftime('%H:%M:%S')}] " + T["delete_processing"].format(repo=repo_name), log_file)
sys.stdout.flush()
time.sleep(0.15)
try:
full_name = f"{owner}/{repo_name}"
repo = g.get_repo(full_name)
ref_path = f"heads/{branch_to_delete}"
ref = repo.get_git_ref(ref_path)
ref.delete()
print_log(T["delete_deleted"].format(branch=branch_to_delete, repo=repo_name), log_file)
except Exception as e:
s = str(e)
if "Reference does not exist" in s or "Not Found" in s:
print_log(T["delete_not_found"].format(branch=branch_to_delete, repo=repo_name), log_file)
else:
print_log(T["delete_error"].format(repo=repo_name, error=s), log_file)
time.sleep(0.1)
print_log(f"=== {T['delete_end']} {datetime.datetime.now()} ===", log_file)
def parse_repo_entry(repo_entry):
"""
Returns tuple (repo_path_relative, github_repo_name)
If entry contains ":" i.e. source:custom_name -> use custom_name as github repo name
Otherwise treat entry as folder path with underscores converted to slashes for path,
and GitHub repo name as entry with slashes replaced by underscores.
"""
if ":" in repo_entry:
source, custom_name = repo_entry.split(":", 1)
repo_path = os.path.join(BASE_DIR, source.replace("_", "/"))
repo_name = custom_name.strip()
else:
repo_path = os.path.join(BASE_DIR, repo_entry.replace("_", "/"))
repo_name = repo_entry.replace("/", "_")
return repo_path, repo_name
# auto push main function
def main_auto_push_mode():
if not os.path.exists(REPO_LIST_FILE):
print(T["repo_not_found_file"].format(file=REPO_LIST_FILE))
return
with open(REPO_LIST_FILE, "r") as f:
raw_repos = [line.strip() for line in f.readlines() if line.strip()]
with open(LOG_FILE, "a") as log:
log.write(f"\n=== {T['start_auto_push']} {datetime.datetime.now()} ===\n")
for repo_entry in raw_repos:
repo_path, repo_name = parse_repo_entry(repo_entry)
if not os.path.exists(repo_path):
print_log(T["folder_not_found"].format(path=repo_path), LOG_FILE)
continue
auto_push(repo_path, repo_name, LOG_FILE)
print_log(f"{T['end_auto_push']} {datetime.datetime.now()}", LOG_FILE)
print(T["process_done"].format(log=LOG_FILE))
# delete branch main function
def main_delete_branch_mode():
if not os.path.exists(REPO_LIST_FILE):
print(T["repo_not_found_file"].format(file=REPO_LIST_FILE))
return
with open(REPO_LIST_FILE, "r") as f:
raw_repos = [line.strip() for line in f.readlines() if line.strip()]
repo_names = []
for entry in raw_repos:
_, repo_name = parse_repo_entry(entry)
repo_names.append(repo_name)
branch_to_delete = input(T["delete_prompt_branch"]).strip()
if not branch_to_delete:
print(T["delete_empty_branch"])
return
with open(LOG_FILE, "a") as log:
log.write(f"\n=== {T['delete_start']} {datetime.datetime.now()} ===\n")
delete_branch_on_repos(repo_names, branch_to_delete, LOG_FILE)
# Route based on mode_choice
if mode_choice == "1":
main_auto_push_mode()
elif mode_choice == "2":
main_delete_branch_mode()
else:
print("Invalid choice. Exiting.")