-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
133 lines (100 loc) · 2.99 KB
/
main.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
import os
import hashlib
import subprocess
import argparse
import time
from colorama import Fore, Style
import colorama
def get_parser():
parser = argparse.ArgumentParser(description="Sync Overleaf with Github")
parser.add_argument("--path", type=str, required=True, help="Path to the repo")
parser.add_argument(
"--local",
type=str,
required=True,
help="Name of the local branch which was used during the initialisation",
)
parser.add_argument(
"--refresh",
type=float,
default=10,
help="Refresh time in seconds",
)
return parser
def hash_folder(folder_path):
hash_value = hashlib.md5()
for root, dirs, files in os.walk(folder_path):
if ".git" in dirs:
dirs.remove(".git")
if "README.md" in files:
files.remove("README.md")
for file in sorted(files):
file_path = os.path.join(root, file)
with open(file_path, "rb") as f:
content = f.read()
hash_value.update(content)
return hash_value.hexdigest()
def checkout_to_origin():
result = subprocess.run(
["git", "checkout", "master"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(result.stdout.decode("utf-8"))
def pull_from_origin():
result = subprocess.run(
["git", "pull", "origin", "master"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(result.stdout.decode("utf-8"))
def checkout_to_local(local_branch_name):
result = subprocess.run(
["git", "checkout", local_branch_name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(result.stdout.decode("utf-8"))
def merge_OL_to_GH():
result = subprocess.run(
["git", "merge", "origin/master", "--allow-unrelated-histories"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(result.stdout.decode("utf-8"))
def push_to_GH():
result = subprocess.run(
["git", "push", "github", "HEAD:main"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
)
print(result.stdout.decode("utf-8"))
parser = get_parser()
args = parser.parse_args()
repo_directory = args.path
colorama.init()
while True:
old_hash = hash_folder(repo_directory)
os.chdir(repo_directory)
pull_from_origin()
new_hash = hash_folder(repo_directory)
if old_hash != new_hash:
print(
f"\n{Fore.RED}***********Changes found on Overleaf... Merging them to the"
" Github repository***********"
)
checkout_to_local(args.local)
merge_OL_to_GH()
print(Style.RESET_ALL)
push_to_GH()
print(Fore.GREEN)
checkout_to_origin()
print(Style.RESET_ALL)
else:
print(f"{Fore.GREEN}No changes found...{Style.RESET_ALL}\n")
time.sleep(args.refresh)