-
Notifications
You must be signed in to change notification settings - Fork 14
/
sync_combined_py_files.py
134 lines (97 loc) · 3.84 KB
/
sync_combined_py_files.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
import re
import os
# Regex key:
# ' *' means any number of spaces
# '-{5}' means -----
# '.*' means any group of characters ('.' means any character)
# '-*' means any number of consequtive dashes
# '--*' means one dash followed by any number of consequtive dashes
# '$' means the end of the string
# (else re.match will only matches the beginning of the string)
# '|' means "or" (i.e. can match on either version)
# all other characters, incuding spaces, are literal
begin = re.compile(' *##-{5} Begin .* -*##$')
begin_parts = re.compile('##-{5} Begin | --*##')
end = re.compile(' *##-{5} End .* -*##$')
end_parts = re.compile(' *##-{5} End | --*##')
def get_file_contents(file_path):
with open(file_path, 'r') as f:
file_contents = f.readlines()
if file_contents and (not file_contents[-1] or file_contents[-1][-1] != '\n'):
file_contents[-1] = file_contents[-1] + '\n'
return file_contents
def get_code_blocks(combined_file):
cnt = 0
while True:
for line in combined_file:
cnt += 1
if begin.match(line):
indent_spaces, file, _ = begin_parts.split(line)
indent = len(indent_spaces)
break
else:
return None # end of file, no more code_blocks
start = cnt
block = []
for line in combined_file:
cnt += 1
if end.match(line) and end_parts.split(line)[1] == file:
break
else:
block.append(line[indent:]) # contains newlines
else:
return None # no End to match the Begin, error
finish = cnt - 1
if block and block[-1] == '\n':
del block[-1]
yield start, finish, indent, file, block
def get_all_blocks(combined_file):
with open(combined_file, 'r') as f:
file_blocks = list(get_code_blocks(f))
return file_blocks
def file_block_synced(file_path, textlines):
file_contents = get_file_contents(file_path)
if file_contents != textlines:
print("file:", file_contents)
print("block", textlines)
return file_contents == textlines
def update_file(file_path, textlines):
with open(file_path, 'w') as f:
f.writelines(textlines)
def update_block_in_combined_file(combined_file, start, end, indent, textlines):
with open(combined_file, 'r') as f:
contents = f.readlines()
indented_textlines = [' '*indent + line for line in textlines]
contents = contents[:start] + indented_textlines + contents[end:]
with open(combined_file, 'w') as f:
f.writelines(contents)
def sync(directory, combined_file):
combined_modified = os.path.getmtime(combined_file)
# reverse the order to prevent the start/end values from changing
for start, end, indent, file, textlines in reversed(get_all_blocks(combined_file)):
file_path = directory + file
file_contents = get_file_contents(file_path)
if file_contents != textlines:
file_modified = os.path.getmtime(file_path)
if combined_modified > file_modified:
update_file(file_path, textlines)
print("Syncing", file, "to match", combined_file)
else:
update_block_in_combined_file(combined_file, start, end, indent, file_contents)
print("Syncing", combined_file, "to match", file)
if __name__ == '__main__':
import sys # replace with argparser
import time
directory = sys.argv[1]
combined_file = sys.argv[2]
print("Running...")
try:
while True:
try:
time.sleep(.1)
finally:
start_time = time.perf_counter()
sync(directory, combined_file)
#print(time.perf_counter() - start_time)
except KeyboardInterrupt:
print("\nGOOD BYE!")