1
+ import os , hashlib
2
+ from os import path
3
+
4
+ #Collects the location of the folder to overlook and the location for the log file
5
+ current_location = path .dirname (__file__ )
6
+ file_to_overlook = path .join (current_location , "./" + "YOUR-FILENAME" )
7
+ log_file = str (current_location ) + "/" + "YOUR-FILENAME"
8
+
9
+ #Gets the location of all items in the file
10
+ def get_all_items (file_to_overlook ):
11
+ all_items = []
12
+
13
+ for item in os .listdir (file_to_overlook ):
14
+
15
+ file_items = file_to_overlook + "/" + item
16
+
17
+ if (path .isdir (file_items ) == True ):
18
+ all_items += get_all_items (file_items )
19
+
20
+ else :
21
+ all_items .append (file_items )
22
+
23
+ return all_items
24
+
25
+ #If the log file exists, looks through the file and
26
+ try :
27
+ changes = False
28
+ removed = False
29
+ added = False
30
+
31
+ added_files = []
32
+ removed_files = []
33
+ existing_files = []
34
+ changed_files = []
35
+ hash_current_files = []
36
+
37
+ log_items = []
38
+ log_hashes = []
39
+
40
+ with open (log_file ) as log_to_read :
41
+ read_log = log_to_read .readlines ()
42
+ read_log = [back_n .strip () for back_n in read_log ]
43
+
44
+ #separates the name of the file from its hash value
45
+ for string in read_log :
46
+ separator = string .index (" : " )
47
+
48
+ log_items .append (string [0 :separator ])
49
+
50
+ log_hashes .append (string [(separator + 3 ):])
51
+
52
+ #Detects if the hashed content of all files currently in the folder are the same as in the log file
53
+ for file_to_hash in get_all_items (file_to_overlook ):
54
+
55
+ with open (file_to_hash , "r" ) as content_to_hash :
56
+ hashed_content = content_to_hash .readlines ()
57
+
58
+ hashed_content = [back_n .strip () for back_n in hashed_content ]
59
+
60
+ sha256 = hashlib .sha256 ()
61
+
62
+ sha256 .update (str (hashed_content ).encode ("utf-8" ))
63
+
64
+ file_hash = sha256 .hexdigest ()
65
+
66
+ hash_current_files .append (file_to_hash + " : " + file_hash )
67
+
68
+ #Detects if there are more or less items in the folder
69
+ for item in get_all_items (file_to_overlook ):
70
+
71
+ if (len (log_items ) <= len (get_all_items (file_to_overlook ))):
72
+ existing_files .append (item )
73
+
74
+ if (len (log_items ) >= len (get_all_items (file_to_overlook ))):
75
+ existing_files .append (item )
76
+
77
+ #Gets the removed files and puts them in the removed file list
78
+ for item in range (len (log_items )):
79
+
80
+ if (log_items [item ] not in existing_files ):
81
+
82
+ removed = True
83
+
84
+ removed_files .append (log_items [item ])
85
+
86
+ #Gets the added files and puts them in the added file list
87
+ for item in range (len (existing_files )):
88
+
89
+ if (existing_files [item ] not in log_items and existing_files [item ] not in added_files ):
90
+
91
+ added = True
92
+
93
+ added_files .append (existing_files [item ])
94
+
95
+ #If the hash value of a certain file has been altered it will be detected here
96
+ for item in read_log :
97
+ if (item in hash_current_files ):
98
+ del hash_current_files [hash_current_files .index (item )]
99
+
100
+ for item in hash_current_files :
101
+ if item not in read_log :
102
+ changes = True
103
+ changed_files .append (item )
104
+
105
+ for item in changed_files :
106
+ separator = item .index (" : " )
107
+
108
+ if (item [0 :separator ] in added_files ):
109
+ del changed_files [changed_files .index (item )]
110
+
111
+ if (item [0 :separator ] in removed_files ):
112
+ del changed_files [changed_files .index (item )]
113
+
114
+ #if files have been changed
115
+ if (changes == True ):
116
+
117
+ print ("The folowing file(s) have been changed\n " )
118
+ print (* changed_files , sep = "\n " )
119
+
120
+ item_to_log = {}
121
+
122
+ outfile = open (log_file , "w+" )
123
+
124
+ #updates the log file
125
+ for item in get_all_items (file_to_overlook ):
126
+
127
+ with open (item , "r" ) as content_to_hash :
128
+ hashed_content = content_to_hash .readlines ()
129
+
130
+ hashed_content = [back_n .strip () for back_n in hashed_content ]
131
+
132
+ sha256 = hashlib .sha256 ()
133
+
134
+ sha256 .update (str (hashed_content ).encode ("utf-8" ))
135
+
136
+ file_hash = sha256 .hexdigest ()
137
+
138
+ item_to_log [str (hashed_content )] = file_hash
139
+
140
+ outfile .write (item + " : " + item_to_log [str (hashed_content )])
141
+
142
+ outfile .write ("\n " )
143
+
144
+ outfile .close ()
145
+
146
+ #if files have been added
147
+ if (added == True ):
148
+ print ("These files have been added:\n " )
149
+ print (* added_files , sep = "\n " )
150
+
151
+ item_to_log = {}
152
+
153
+ outfile = open (log_file , "w+" )
154
+
155
+ #updates the log file
156
+ for item in get_all_items (file_to_overlook ):
157
+
158
+ with open (item , "r" ) as content_to_hash :
159
+ hashed_content = content_to_hash .readlines ()
160
+
161
+ hashed_content = [back_n .strip () for back_n in hashed_content ]
162
+
163
+ sha256 = hashlib .sha256 ()
164
+
165
+ sha256 .update (str (hashed_content ).encode ("utf-8" ))
166
+
167
+ file_hash = sha256 .hexdigest ()
168
+
169
+ item_to_log [str (hashed_content )] = file_hash
170
+
171
+ outfile .write (item + " : " + item_to_log [str (hashed_content )])
172
+
173
+ outfile .write ("\n " )
174
+
175
+ outfile .close ()
176
+
177
+ #if files have been removed
178
+ if (removed == True ):
179
+ print ("These files have been removed:\n " )
180
+ print (* removed_files , sep = "\n " )
181
+
182
+ item_to_log = {}
183
+
184
+ outfile = open (log_file , "w+" )
185
+
186
+ #updates the log file
187
+ for item in get_all_items (file_to_overlook ):
188
+
189
+ with open (item , "r" ) as content_to_hash :
190
+ hashed_content = content_to_hash .readlines ()
191
+
192
+ hashed_content = [back_n .strip () for back_n in hashed_content ]
193
+
194
+ sha256 = hashlib .sha256 ()
195
+
196
+ sha256 .update (str (hashed_content ).encode ("utf-8" ))
197
+
198
+ file_hash = sha256 .hexdigest ()
199
+
200
+ item_to_log [str (hashed_content )] = file_hash
201
+
202
+ outfile .write (item + " : " + item_to_log [str (hashed_content )])
203
+
204
+ outfile .write ("\n " )
205
+
206
+ outfile .close ()
207
+
208
+ #if nothing has changed
209
+ if (changes == False and added == False and removed == False ):
210
+ print ("No changes" )
211
+
212
+ #If the log file does not already exist ceate one
213
+ except FileNotFoundError :
214
+
215
+ item_to_log = {}
216
+
217
+ outfile = open (log_file , "w+" )
218
+
219
+ for item in get_all_items (file_to_overlook ):
220
+
221
+ with open (item , "r" ) as content_to_hash :
222
+ hashed_content = content_to_hash .readlines ()
223
+
224
+ hashed_content = [back_n .strip () for back_n in hashed_content ]
225
+
226
+ sha256 = hashlib .sha256 ()
227
+
228
+ sha256 .update (str (hashed_content ).encode ("utf-8" ))
229
+
230
+ file_hash = sha256 .hexdigest ()
231
+
232
+ item_to_log [str (hashed_content )] = file_hash
233
+
234
+ outfile .write (item + " : " + item_to_log [str (hashed_content )])
235
+
236
+ outfile .write ("\n " )
237
+
238
+ outfile .close ()
0 commit comments