-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync_repo.py
408 lines (333 loc) · 12.7 KB
/
sync_repo.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#%%
import subprocess
import threading
import time
from inputimeout import inputimeout, TimeoutOccurred
#%% Script metadata
__author__ = "muralivnv"
__version__ = "1.0.9"
#%% util_functions
min_to_sec = lambda x: x*60.0
#%% global setting
## configuration
global_config = {}
global_config['git'] = "git"
global_config['sync_time_sec'] = min_to_sec(10.0)
global_config['local_commit_wait_time_sec'] = min_to_sec(2.0)
## variables
global_vars = {}
global_vars['kill_script'] = False
global_vars['is_pulling'] = False
global_vars['is_pushing'] = False
global_vars['is_committing'] = False
global_vars['disp_user_controller'] = False
global_vars['is_stdout_released'] = True
git_toplevel_dir = ""
## Constants
CURSOR_UP = '\033[F'
ERASE_LINE = '\033[K'
#%% setup logging
import logging
class CustomFormatter(logging.Formatter):
def format(self, record):
global global_vars
global_vars['disp_user_controller'] = False
while ( (not global_vars['is_stdout_released']) and (not global_vars['kill_script']) ):
time.sleep(0.5)
# remove last user input prompt from stdout
print(ERASE_LINE + CURSOR_UP)
message = super(CustomFormatter, self).format(record)
global_vars['disp_user_controller'] = True
return message
logging.basicConfig(level=logging.INFO,
format='%(asctime)s | %(message)s')
logger = logging.getLogger(__name__)
for handler in logger.root.handlers:
handler.setFormatter(CustomFormatter(handler.formatter._fmt))
#%% unused functions
def init_ssh_agent(sys_os: str)->None:
ssh_agent_cmd = {}
ssh_agent_cmd["Linux"] = ["eval", "$(ssh-agent", "-s)"]
ssh_agent_cmd["Windows"] = ["start-ssh-agent.cmd"]
if (sys_os in ssh_agent_cmd):
_ = subprocess.run(ssh_agent_cmd[sys_os], shell=True)
def add_ssh_key(sys_os: str, ssh_key: str)->None:
ssh_add_cmd = "ssh-add"
if (sys_os == "Windows"):
# get path where ssh-add is stored
git_path = subprocess.check_output(["where", "git"])
git_path = git_path.strip()
git_path = git_path.decode('utf-8')
if (any(git_path)):
path_list = git_path.split('\\')
ssh_add_path = []
for path in path_list:
ssh_add_path.append(path)
if ("git" in path.lower()):
break
ssh_add_path = ssh_add_path + ["usr", "bin"]
ssh_add_cmd = '/'.join(ssh_add_path + [ssh_add_cmd])
_ = subprocess.run([ssh_add_cmd, ssh_key], shell=True)
def git_establish_ssh(ssh_key: str)->None:
import platform
sys_os = platform.system()
if ((sys_os == "Linux") or (sys_os == "Windows")):
init_ssh_agent(sys_os)
add_ssh_key(sys_os, ssh_key)
else:
logging.warn("SSH Key not added, OS is unfamiliar")
#%% git utility functions
def log_subprocess_output(out_pipe, err_pipe)->None:
out_str = b''.join( [line for line in out_pipe] ).strip()
err_str = b''.join( [line for line in err_pipe] ).strip()
if (any(out_str)):
logging.info(out_str.decode('utf-8'))
if (any(err_str)):
logging.error(err_str.decode('utf-8'))
def get_git_top_level_dir()->str:
proc = subprocess.Popen([global_config['git'], "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE)
dir_name = ""
for line in proc.stdout:
line = line.strip()
if (any(line)):
dir_name = line.decode('utf-8')
break
return dir_name
def fetch()->None:
proc = subprocess.Popen([global_config['git'], "fetch", "--all"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def pull()->None:
proc = subprocess.Popen([global_config['git'], "pull", "origin", "main"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def push()->None:
proc = subprocess.Popen([global_config['git'], "push", "origin", "main"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def stage_files(files: list)->None:
# prepend and append " in case file names have spaces in them
# files = [f"\"{file}\"" for file in files]
# main implementation
add_command = [global_config['git'], "add"] + files
proc = subprocess.Popen(add_command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def commit(commit_msg:str)->None:
proc = subprocess.Popen([global_config['git'], "commit", "-m", commit_msg],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def reset_to_commit(commit_id: str)->None:
proc = subprocess.Popen([global_config['git'], "reset", "--soft", commit_id],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def get_dirty_files()->list:
dirty_files = []
proc = subprocess.Popen([global_config['git'], "status", "--porcelain"],
stdout=subprocess.PIPE, cwd=git_toplevel_dir)
for line in proc.stdout:
line = line.strip()
if(any(line)):
dirty_file = line[line.find(b' '):]
dirty_file = dirty_file.decode('utf-8').strip()
dirty_file = dirty_file.replace('"', '')
dirty_files.append( dirty_file )
return dirty_files
def stash_save(files_to_save: list)->None:
save_command = [global_config['git'], "stash", "push", "-u"] + files_to_save
proc = subprocess.Popen(save_command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def stash_apply(stash_name: str) -> None:
proc = subprocess.Popen([global_config['git'], "stash", "apply", stash_name],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=git_toplevel_dir)
log_subprocess_output(proc.stdout, proc.stderr)
proc.wait()
def compute_git_hash()->str:
staged_changes_proc = subprocess.Popen([global_config['git'], "status", "-v"], stdout=subprocess.PIPE, cwd=git_toplevel_dir)
hash_proc = subprocess.Popen([global_config['git'], "hash-object", "--stdin"], stdin=staged_changes_proc.stdout,
stdout=subprocess.PIPE, cwd=git_toplevel_dir)
for line in hash_proc.stdout:
line = line.strip()
if (any(line)):
return line.decode('utf-8')
return ""
def get_local_commits() -> (bool, list):
commits_info = []
has_commits = False
proc = subprocess.Popen([global_config['git'], "log", "origin/main..HEAD", "--oneline"], stdout=subprocess.PIPE, cwd=git_toplevel_dir)
for line in proc.stdout:
line = line.strip()
if (any(line)):
line = line.decode('utf-8')
commits_info.append(line)
has_commits = True
return has_commits, commits_info
def is_pull_req()->bool:
fetch()
proc = subprocess.Popen([global_config['git'], "log", "HEAD..origin/main", "--oneline"],
stdout=subprocess.PIPE)
remote_has_commits = False
for line in proc.stdout:
line = line.strip()
if (any(line)):
line = line.decode('utf-8')
remote_has_commits = True
break
return remote_has_commits
def squash_commits(commits_info: list)->None:
if (len(commits_info) > 1):
local_commits_hash = []
for commit_info in commits_info:
tmp = commit_info.split(' ')
hash_str = tmp[0]
local_commits_hash.append( hash_str )
if (any(local_commits_hash)):
logging.info(f"squashed commits {','.join(local_commits_hash)} onto HEAD~{len(local_commits_hash)}")
reset_to_commit(f"HEAD~{len(local_commits_hash)}")
commit_hash = compute_git_hash()
commit(f"sync -- #{commit_hash}")
#%% Git Pull/Push thread utils
class CustomTimer:
last_update_time_ = 0.0
timer_limit_ = 0.0
def __init__(self, timespan_limit):
self.timer_limit_ = timespan_limit
self.last_update_time_ = time.time()
def has_exceeded_limit(self)->bool:
time_now = time.time()
if ( (time_now - self.last_update_time_) > self.timer_limit_ ):
return True
return False
def reset(self)->None:
self.last_update_time_ = time.time()
def pull_helper():
global global_vars
if (is_pull_req() == True):
global_vars['is_pulling'] = True
logging.info("pulling from origin")
dirty_files = get_dirty_files()
if (any(dirty_files)):
logging.info("local repo is dirty stashing temporarily under stash@{0}")
stash_save(dirty_files)
pull()
if (any(dirty_files)):
logging.info("adding back local changes from stash@{0}")
stash_apply("stash@{0}")
global_vars['is_pulling'] = False
def push_helper():
global global_vars
has_local_commits, commits_info = get_local_commits()
if (has_local_commits == True):
global_vars['is_pushing'] = True
squash_commits(commits_info)
logging.info("pushing to origin")
push()
global_vars['is_pushing'] = False
def commit_helper():
global global_vars
dirty_files = get_dirty_files()
if (any(dirty_files)):
global_vars['is_committing'] = True
stage_files(dirty_files)
commit_hash = compute_git_hash()
logging.info(f"committing with message - \"sync -- #{commit_hash}\"")
commit(f"sync -- #{commit_hash}")
global_vars['is_committing'] = False
def pull_from_origin()->None:
timer = CustomTimer(global_config['sync_time_sec'])
pull_helper()
while(global_vars['kill_script'] == False):
if ( (timer.has_exceeded_limit()) and
(global_vars['is_committing'] == False) and
(global_vars['is_pushing'] == False) ):
pull_helper()
timer.reset()
time.sleep(5.0)
def push_to_origin()->None:
timer = CustomTimer(global_config['sync_time_sec'])
while(global_vars['kill_script'] == False):
if ( ( timer.has_exceeded_limit() ) and
(global_vars['is_pulling'] == False) and
(global_vars['is_committing'] == False)):
push_helper()
timer.reset()
time.sleep(5.0)
def commit_if_req()->None:
try:
while( (global_vars['is_pulling'] == True) or (global_vars['is_pushing'] == True) ):
time.sleep(1.0)
if (global_vars['kill_script'] == True):
return
except:
return
commit_helper()
def handle_user_input(user_input: str)->None:
if (user_input == '1'):
push_helper()
elif (user_input == '2'):
pull_helper()
elif (user_input == '3'):
commit_helper()
else:
print("unknown command, check input")
def user_control()->None:
global global_vars
while(global_vars['kill_script'] == False):
if (global_vars['disp_user_controller'] == True):
global_vars['is_stdout_released'] = False
try:
user_input = inputimeout(prompt="1[push], 2[pull], 3[commit] >> ", timeout=5)
global_vars['is_stdout_released'] = True
handle_user_input(user_input)
except TimeoutOccurred:
# remove last user input prompt from stdout
print(CURSOR_UP + ERASE_LINE + CURSOR_UP)
except:
pass
else:
global_vars['is_stdout_released'] = True
time.sleep(0.1)
#%% Main
def main():
global global_vars, git_toplevel_dir
pull_thread = threading.Thread(target=pull_from_origin)
push_thread = threading.Thread(target=push_to_origin)
user_control_thread = threading.Thread(target=user_control)
git_toplevel_dir = get_git_top_level_dir()
logging.info("initiated sync")
try:
pull_thread.start()
time.sleep(5.0)
push_thread.start()
commit_if_req()
user_control_thread.start()
commit_timer = CustomTimer(global_config['local_commit_wait_time_sec'])
while(global_vars['kill_script'] == False):
if (commit_timer.has_exceeded_limit()):
commit_if_req()
commit_timer.reset()
time.sleep(10.0)
pull_thread.join()
push_thread.join()
except KeyboardInterrupt:
global_vars['kill_script'] = True
pull_thread.join()
push_thread.join()
user_control_thread.join()
except:
global_vars['kill_script'] = True
pull_thread.join()
push_thread.join()
user_control_thread.join()
logging.exception("exception occured")
logging.info("closing sync, goodbye!")
if __name__ == '__main__':
main()