-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.py
executable file
·78 lines (59 loc) · 2.07 KB
/
diff.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
#!/usr/bin/python3
import filecmp
import os
import shutil
from sys import argv
def check_valid_dir(directory):
return os.path.exists(directory) and os.path.isdir(directory)
def copy_files_to(files, src, dest):
for current in files:
dir_name = os.path.join(dest, os.path.dirname(current))
if not os.path.exists(dir_name):
os.makedirs(dir_name)
shutil.copy(os.path.join(src, current), os.path.join(dest, current))
if len(argv) != 4:
print("Invalid number of parameters")
print("Usage:")
print(argv[0], "<old_src> <new_src> <dest_dir>")
exit(1)
_, old, new, dst = argv
if not check_valid_dir(old) or not check_valid_dir(new) or not check_valid_dir(dst):
print(f'All parameters must exist and be directories')
exit(1)
new_files = []
old_files = []
to_delete = []
to_replace_files = []
to_add_files = []
for dirname, dirnames, filenames in os.walk(old):
path = os.path.relpath(dirname, old)
for old_filename in filenames:
old_files.append(os.path.join(path, old_filename))
for dirname, dirnames, filenames in os.walk(new):
path = os.path.relpath(dirname, new)
for new_filename in filenames:
new_files.append(os.path.join(path, new_filename))
for old_file in old_files:
if old_file not in new_files:
to_delete.append(old_file)
for new_file in new_files:
if new_file not in old_files:
to_add_files.append(new_file)
for to_delete_file in to_delete:
old_files.remove(to_delete_file)
for found_file in old_files:
file_old = os.path.join(old, found_file)
file_new = os.path.join(new, found_file)
equals = filecmp.cmp(file_old, file_new, False)
if not equals:
to_replace_files.append(found_file)
copy_files_to(to_replace_files, new, dst)
copy_files_to(to_add_files, new, dst)
if len(to_delete) > 0:
to_delete_file = os.path.join(dst, "to_delete.txt")
f_delete = open(to_delete_file, "w")
for dfile in to_delete:
f_delete.write(dfile + "\n")
f_delete.close()
print(f"Files to be deleted were listed in {to_delete_file}")
print("Done")