-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.py
executable file
·61 lines (47 loc) · 1.8 KB
/
update.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
#!/usr/bin/python3
import os
from utils import (
diff_contents,
input_bool,
load_actual_file,
load_stored_files,
load_file_dirs,
stored_file_path,
)
def write_stored_file(filename, new_contents):
with open(stored_file_path(filename), 'w') as f:
f.write(new_contents)
def update_stored_file(filename, directory, stored_contents):
new_contents = load_actual_file(filename, directory)
path = os.path.join(directory, filename)
if stored_contents != new_contents:
print('Config file %s differs from the stored version:' % path)
print(diff_contents(stored_contents, new_contents, fromfile=path, tofile=filename))
if input_bool('Update stored config file %s?' % path):
write_stored_file(filename, new_contents)
def add_stored_file(filename, directory):
new_contents = load_actual_file(filename, directory)
path = os.path.join(directory, filename)
print('Config file %s is new:' % path)
print(diff_contents('', new_contents, tofile=filename))
if input_bool('Store config file %s?' % path):
write_stored_file(filename, new_contents)
def remove_stored_file(filename, contents):
diff = diff_contents(contents, '', fromfile=filename)
print('Stored config file %s is no longer mapped to a directory:' % filename)
print(diff)
if input_bool('Remove stored config file %s?' % filename):
os.remove(stored_file_path(filename))
def main():
file_dirs = load_file_dirs()
stored_files = load_stored_files()
for filename, directory in file_dirs.items():
if filename in stored_files:
update_stored_file(filename, directory, stored_files[filename])
else:
add_stored_file(filename, directory)
for filename, contents in stored_files.items():
if filename not in file_dirs:
remove_stored_file(filename, contents)
if __name__ == '__main__':
main()