86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
import subprocess
|
|
import sys
|
|
import os
|
|
from collections import Counter
|
|
|
|
SUBPROGRAM_PATH = "./file-analyzer"
|
|
|
|
def most_common_in_list(searched_list, key):
|
|
return Counter(key(el) for el in searched_list).most_common(1)
|
|
|
|
def summarize(analyses):
|
|
char_counter = most_common_in_list(analyses, lambda x: x['most_common_char'])
|
|
word_counter = most_common_in_list(analyses, lambda x: x['most_common_word'])
|
|
return {
|
|
'total_files_read': len(analyses),
|
|
'chars_total': sum(analysis['chars_total'] for analysis in analyses),
|
|
'words_total': sum(analysis['words_total'] for analysis in analyses),
|
|
'rows_total': sum(analysis['rows_total'] for analysis in analyses),
|
|
'most_common_char': char_counter[0][0] if len(char_counter) != 0 else " ",
|
|
'most_common_word': word_counter[0][0] if len(word_counter) != 0 else "",
|
|
|
|
}
|
|
|
|
def main(path):
|
|
analyzedFiles = []
|
|
|
|
try:
|
|
process = subprocess.Popen(
|
|
[SUBPROGRAM_PATH],
|
|
stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE,
|
|
text=True
|
|
)
|
|
except:
|
|
print("Could not open analyzer!")
|
|
exit(1)
|
|
|
|
|
|
for dirpath, dirs, files in os.walk(path):
|
|
for filename in files:
|
|
path = os.path.join(dirpath, filename)
|
|
|
|
print(path, file=process.stdin)
|
|
process.stdin.flush()
|
|
output = process.stdout.readline()
|
|
|
|
data = output.strip().split("\t")
|
|
if len(data) != 6:
|
|
# error response
|
|
print(data[0])
|
|
continue
|
|
|
|
analysis = {
|
|
'path': data[0],
|
|
'chars_total': int(data[1]),
|
|
'words_total': int(data[2]),
|
|
'rows_total': int(data[3]),
|
|
'most_common_char': data[4][1:-1],
|
|
'most_common_word': data[5][1:-1]
|
|
}
|
|
analyzedFiles.append(analysis)
|
|
|
|
process.terminate()
|
|
|
|
summary = summarize(analyzedFiles)
|
|
|
|
print(f"Number of read files: {summary['total_files_read']}" )
|
|
print(f"Total characters in all files: {summary['chars_total']}" )
|
|
print(f"Total words in all files: {summary['words_total']}" )
|
|
print(f"Total rows in all files: {summary['rows_total']}" )
|
|
print(f"Most common character between files: '{summary['most_common_char']}'" )
|
|
print(f"Most common word between files: \"{summary['most_common_word']}\"" )
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print(f"Usage: {sys.argv[0]} <directory>")
|
|
exit(1)
|
|
if not os.path.exists(sys.argv[1]):
|
|
print(f"Path {sys.argv[1]} does not exist!")
|
|
exit(1)
|
|
if not os.path.isdir(sys.argv[1]):
|
|
print(f"Path {sys.argv[1]} is not a directory!")
|
|
exit(1)
|
|
main(sys.argv[1]) |