forked from IVainqueur/auto-push
-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.py
206 lines (165 loc) · 5.79 KB
/
methods.py
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
import re
import subprocess
import sys
import signal
import os
from threading import Timer
from uuid import uuid4
from functools import partial
from platform import platform
ispaused = False
_dir = os.getcwd()
_branch = "main"
class PauseException(Exception):
pass
COLORS_WITH_CODES = {
"black": "\x1B[30m",
"bg-black": "\x1B[40m",
"red": "\x1B[31m",
"bg-red": "\x1B[41m",
"green": "\x1B[32m",
"bg-green": "\x1B[42m",
"yellow": "\x1B[33m",
"bg-yellow": "\x1B[43m",
"blue": "\x1B[34m",
"bg-blue": "\x1B[44m",
"white": "\x1B[37m",
"bg-white": "\x1B[47m",
"clear": "\x1B[0m ",
"none": ""
}
def customexit():
if "windows" in platform().lower():
os._exit(0)
else:
os.kill(os.getpid(), signal.SIGINT)
def clear():
if "windows" in platform().lower():
os.system('cls')
else:
os.system('clear')
def printcommands():
print(colorcode("q - quit\tp - pause or resume\tcb - change branch", color='yellow'))
def pause_or_play():
global ispaused
ispaused = not ispaused
if ispaused:
print("===> PAUSED")
else:
print("===> RESUMING...")
def change_branch(newbranch = _branch, *args):
global ispaused
global _dir
global _branch
if newbranch == "":
return
print(f"Changing branch to {newbranch}")
force_pause = not ispaused
if force_pause:
pause_or_play()
setbranch(_dir, newbranch)
if force_pause:
pause_or_play()
KEYS_WITH_ACTIONS = {
"'q'": customexit,
"'p'": pause_or_play
}
OTHER_CMDS = {
"cb": change_branch
}
def param_dict(arr):
classified = {}
for el in arr:
try:
destructered = el.rsplit('=')
if len(destructered) == 1:
classified[destructered[0]] = True
else:
classified[destructered[0]] = destructered[1]
except Exception as e:
print(e)
return classified
def colorcode(text, color='', bg=''):
os.system('')
color = COLORS_WITH_CODES.get(color, "")
bg = COLORS_WITH_CODES.get(bg, "")
# if color == 'none' and bg == 'none':
# return f'{text}'
# if bg == 'none':
# return f'{COLORS_WITH_CODES[color]}{text}{COLORS_WITH_CODES["clear"]}'
return f'{color}{bg}{text}{COLORS_WITH_CODES["clear"]}'
def setbranch(dir, branch):
global _dir
global _branch
_dir = dir
_branch = branch
try:
subprocess.call(["git", "-C", dir, "branch", "-M", branch])
print("--> Set Branch to {br}".format(br=colorcode(branch, "green")))
except Exception as e:
print("{error}".format(error=colorcode(repr(e), "white", "bg-red")))
customexit()
def commit_message(template):
if re.search("#num#", template):
template = re.sub("#num#", uuid4().hex, template)
else:
template = f'{template}-{uuid4().hex}'
return template
def push(ct, dir, branch, interval, beforemethod=None):
clear()
global ispaused
global _branch
branch = _branch
try:
global ispaused
if ispaused:
raise PauseException
if beforemethod:
beforemethod()
print("\n--> Pushing to {br}".format(br=colorcode(branch, "green")))
subprocess.call(["git", "-C", dir, "add", "."], stdout=subprocess.DEVNULL)
# print("--> Set Branch to {br}".format(br=colorcode(branch, "green")))
subprocess.call(["git", "-C", dir, "commit", "-m", "{m}".format(m=commit_message(ct))], stdout=subprocess.DEVNULL)
# print("--> Set Branch to {br}".format(br=colorcode(branch, "green")))
subprocess.call(["git", "-C", dir, "push", "origin", branch], stdout=subprocess.DEVNULL)
print("--> Pushed to {br}".format(br=colorcode(branch, "green")))
printcommands()
except PauseException:
pass
except Exception as e:
print("{error}".format(error=colorcode(repr(e), "white", "bg-red")))
customexit()
finally:
Timer(interval*60, partial(push, ct, dir, branch, interval, beforemethod)).start()
def test_push(ct, dir, branch, interval):
print(f"got these {ct}, {dir}, {branch}, {interval}")
Timer(interval*60, partial(test_push, ct, dir, branch, interval)).start()
def beforeexit():
print('\n\n\n=================================\n THANKS FOR USING AUTO-PUSH\n=================================\n\n')
def help():
print("===================================")
print(" AUTO-PUSH")
print("===================================")
print("Usage: python3 main.py [--dir] [--branch] [--commit] [--interval]")
print("\n--dir\tis the path to the directory you want to push. Defaults to CURRENT DIRECTORY")
print("\n--branch\tis the branch to which you want to push. Default is the result of the 'git branch' command.")
print("\n--commit\tis a template for the commit message. Default is 'auto-commit-[uuid]")
print("\tFor example: if --commit='auto-commit' then all the commit message will be 'auto-commit-[uuid]'.")
print("\tNote: You can also put the uuid anywhere else in the string like so: --commit='commit-#num#-automatic'")
print("The --commit above will be turned into 'custom-[uuid]-automatic'")
print("\n--interval\tis the interval between pushes in minutes. Default is 5 minutes\n")
print("Note: You can click q anytime to quit\n\n")
def listenForKeys(key):
action = KEYS_WITH_ACTIONS.get(repr(key), None)
if action:
action()
return
if len(key.rsplit(" ")) <= 1:
print(colorcode("Unknown command", bg="bg-red"))
return
# If the key is not a single key command
key_parsed = key.rsplit(" ")
command = OTHER_CMDS.get(key_parsed[0], None)
if command:
print(f"Calling {command} with parameters {key_parsed[1:]}")
command(*key_parsed[1:])