-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_changes.py
63 lines (53 loc) · 1.81 KB
/
save_changes.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
import pickle
import os
import sys
import shutil
args = sys.argv
dir = os.path.normpath(args[1]) + os.sep
save_dir = os.path.normpath(args[2]) + os.sep
def get_every_file(dir):
files = []
for root, dirs, filenames in os.walk(dir):
for filename in filenames:
files.append(os.path.join(root, filename))
if dir + "state.pkl" in files:
files.remove(dir + "state.pkl")
return files
def last_modified(files):
mapping = {}
for file in files:
mapping[file] = os.path.getmtime(file)
return mapping
def get_changes(mapping, new_mapping):
changes = []
for file in mapping:
if file not in new_mapping:
changes.append(("deleted", file))
elif mapping[file] != new_mapping[file]:
changes.append(("modified", file))
for file in new_mapping:
if file not in mapping:
changes.append(("added", file))
return changes
def commit_changes(changes, save_dir):
for change in changes:
path = change[1].replace(os.path.expanduser(dir), os.path.expanduser(save_dir))
if change[0] == "deleted":
os.remove(path)
elif change[0] == "modified":
with open(change[1], "rb") as f:
data = f.read()
with open(path, "wb") as f:
f.write(data)
elif change[0] == "added":
os.makedirs("/".join(path.split("/")[:-1]), exist_ok=True)
shutil.copy2(change[1], path)
if __name__ == "__main__":
files = get_every_file(dir)
new_mapping = last_modified(files)
with open(dir + "/state.pkl", "rb") as f:
mapping = pickle.load(f)
changes = get_changes(mapping, new_mapping)
commit_changes(changes, save_dir)
with open(dir + "/state.pkl", "wb") as f:
pickle.dump(new_mapping, f)