Skip to content

Commit 057397a

Browse files
author
Kristijan Petkov
committed
Initial commit md hash
1 parent 519010c commit 057397a

File tree

9 files changed

+157
-0
lines changed

9 files changed

+157
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/Python/CheckDuplicates.zip
22
/Python/CheckDuplicates/output1.html
33
/Python/TestVs2022/HelloWorldVS2022/.vs/
4+
Python/compare_dirs/test.json

Python/compare_dirs/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/compare_dirs.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/inspectionProfiles/Project_Default.xml

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/compare_dirs/main.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import datetime
2+
import glob
3+
import hashlib
4+
import threading
5+
import logging
6+
from typing import Dict
7+
import os
8+
import argparse
9+
import json
10+
11+
def get_file_size(file_path: str) -> float:
12+
"""
13+
Returns file size in MB
14+
:param file_path: Path of the file to calculate file size
15+
:return:
16+
"""
17+
file_stats = os.stat(file_path)
18+
# print(f'File Size in MegaBytes is {file_stats.st_size / (1024 * 1024)}') (1024 * 1024)
19+
return round(file_stats.st_size, 1)
20+
21+
22+
def calculate_file_md5(file_path: str) -> str:
23+
try:
24+
with open(file_path, 'rb') as file_to_check:
25+
# read contents of the file
26+
data = file_to_check.read()
27+
# pipe contents of the file through
28+
return hashlib.md5(data).hexdigest()
29+
except Exception as e:
30+
print(str(e))
31+
return ""
32+
33+
34+
def write_to_txt_file(s_file_name: str, lst_data) -> None:
35+
try:
36+
f = open(s_file_name, 'w', encoding='utf-8', newline='\n')
37+
if lst_data is not None:
38+
f.write(json.dumps(lst_data,indent=4))
39+
f.close()
40+
except Exception as e:
41+
print(str(e))
42+
43+
44+
def load_and_process_files(s_path: str, s_file_ext: str = ""):
45+
start_time = datetime.datetime.now()
46+
print('s_path' + s_path)
47+
all_files = glob.glob(s_path + '/**', recursive=True, include_hidden=True)
48+
49+
i_files_cnt = len(all_files)
50+
if i_files_cnt <= 0:
51+
print('No files under {}'.format(s_path))
52+
return
53+
else:
54+
print('Loaded {:d} files'.format(i_files_cnt))
55+
56+
# init loop vars
57+
file_info = {}
58+
lt_files = list()
59+
i = 0
60+
iProgressBarStep = int(round(i_files_cnt / 100))
61+
62+
for curr_file in all_files:
63+
if os.path.isfile(curr_file):
64+
md_hash = calculate_file_md5(curr_file)
65+
dt_curr_file_info = {'path': curr_file, 'md5': md_hash ,'size':get_file_size(curr_file) }
66+
lt_files.append(dt_curr_file_info)
67+
68+
# console progress bar
69+
if i % iProgressBarStep == 0:
70+
percent = int(round((i / i_files_cnt) * 100, 0))
71+
print("Complete: {:d}%".format(percent))
72+
elif i % 100 == 0:
73+
print('Processed {}/{} files.'.format(i, i_files_cnt))
74+
75+
i = i + 1
76+
77+
end_time = datetime.datetime.now()
78+
exec_time = end_time - start_time
79+
print(f'Execution time: {exec_time}')
80+
write_to_txt_file('test.json', lt_files)
81+
82+
83+
if __name__ == "__main__":
84+
# script arguments
85+
argParser = argparse.ArgumentParser()
86+
argParser.add_argument('--path')
87+
args = argParser.parse_args()
88+
print(args.path)
89+
90+
if args.path:
91+
load_and_process_files(args.path)
92+
# all done
93+
exit(0)
94+
else:
95+
exit(0)

0 commit comments

Comments
 (0)