1
1
import shutil
2
2
from pathlib import Path
3
+ import hashlib
3
4
4
5
def find_all_target_directories_with_file (base_path , target_folder , target_file ):
5
6
found_directories = []
@@ -20,6 +21,14 @@ def find_closest_directory(directories, base_directory):
20
21
depths = [(dir , get_directory_depth (dir , base_directory )) for dir in directories ]
21
22
return min (depths , key = lambda x : x [1 ])[0 ]
22
23
24
+ def hash_file (filepath ):
25
+ """Compute the SHA-256 hash of a file."""
26
+ hasher = hashlib .sha256 ()
27
+ with open (filepath , 'rb' ) as f :
28
+ buf = f .read ()
29
+ hasher .update (buf )
30
+ return hasher .hexdigest ()
31
+
23
32
def replace_pdf_in_parsers ():
24
33
script_dir = Path (__file__ ).parent
25
34
user_manual_pdf_path = script_dir / "User_Manual" / "PDF.py"
@@ -28,7 +37,7 @@ def replace_pdf_in_parsers():
28
37
print ("No 'pdf.py' file found in 'User_Manual' directory." )
29
38
return
30
39
31
- base_dir = script_dir .parent . parent # Move up two levels from the script's location
40
+ base_dir = script_dir .parent # Move up one level from the script's location
32
41
target_folder = "parsers"
33
42
target_file = "pdf.py"
34
43
found_paths = find_all_target_directories_with_file (base_dir , target_folder , target_file )
@@ -44,16 +53,16 @@ def replace_pdf_in_parsers():
44
53
print (f"Chosen 'parsers' directory based on path depth: { closest_parsers_path } " )
45
54
chosen_pdf_path = closest_parsers_path / target_file
46
55
47
- # File size comparison and replacement
48
- user_manual_pdf_size = user_manual_pdf_path . stat (). st_size
49
- chosen_pdf_size = chosen_pdf_path . stat (). st_size
56
+ # Hash comparison and replacement
57
+ user_manual_pdf_hash = hash_file ( user_manual_pdf_path )
58
+ chosen_pdf_hash = hash_file ( chosen_pdf_path )
50
59
51
- if user_manual_pdf_size != chosen_pdf_size :
60
+ if user_manual_pdf_hash != chosen_pdf_hash :
52
61
print ("Replacing the existing pdf.py with the new one..." )
53
62
shutil .copy (user_manual_pdf_path , chosen_pdf_path )
54
63
print (f"PDF.py replaced at: { chosen_pdf_path } " )
55
64
else :
56
- print ("No replacement needed. The files are of the same size ." )
65
+ print ("No replacement needed. The files are identical ." )
57
66
58
67
if __name__ == "__main__" :
59
68
replace_pdf_in_parsers ()
0 commit comments