|
@@ -2,8 +2,24 @@ import csv
|
|
|
import os
|
|
|
import re
|
|
|
import datetime
|
|
|
+import argparse
|
|
|
+from logger import *
|
|
|
+from datetime import datetime
|
|
|
|
|
|
-DIR_WORK = "./"
|
|
|
+
|
|
|
+
|
|
|
+parse = argparse.ArgumentParser(
|
|
|
+ description="Программа для сбора информации из csv файлов")
|
|
|
+parse.add_argument('-p', '--path', help='Указать в какой директории искать',
|
|
|
+ default=os.path.dirname(__file__))
|
|
|
+parse.add_argument('-e', '--encoding',
|
|
|
+ help='Указать в какой кодировке сохранять', default='utf-8')
|
|
|
+parse.add_argument('-s', '--open_encoding',
|
|
|
+ help='Указать в какой кодировке открывать', default='utf-16')
|
|
|
+args = parse.parse_args()
|
|
|
+
|
|
|
+print(args)
|
|
|
+# DIR_WORK = "./sbor"
|
|
|
LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
|
|
|
'Kaspersky', 'VirtualBox', 'TAP-Windows',
|
|
|
'Wintun', '802.11', 'VMware', 'VPN', 'Wi-Fi',
|
|
@@ -13,29 +29,44 @@ LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
|
|
|
COUNT_INTERFACE = 1
|
|
|
|
|
|
|
|
|
-def check_correct_controller(name):
|
|
|
+def check_correct_controller(name) -> bool:
|
|
|
return any(ext not in name for ext in LIST_BAD_ADAPTER)
|
|
|
|
|
|
|
|
|
class ObjectReady:
|
|
|
- def get_len_dict(self):
|
|
|
+ def get_len_dict(self) -> int:
|
|
|
return len(self.__dict__)
|
|
|
|
|
|
+ def get_csv_row(self) -> str:
|
|
|
+ if self.get_len_dict() < 11:
|
|
|
+ return ';'.join((self.frame, self.cabinet))
|
|
|
+
|
|
|
+ dic = self.__dict__
|
|
|
+ try:
|
|
|
+ row_interface = ';'.join(
|
|
|
+ [';'.join((dic.get(F"ip{i}"), dic.get(F"mac{i}"))) for i in range(self.count_interface)
|
|
|
+ ])
|
|
|
+
|
|
|
+ return ';'.join((str(dic.get("frame")), str(dic.get("cabinet")), str(dic.get("os")), str(
|
|
|
+ dic.get("motherboard")), dic.get("cpu"), str(dic.get("ram")), row_interface))+'\n'
|
|
|
+ except AttributeError:
|
|
|
+ return ';'.join((self.frame, self.cabinet))+"\n"
|
|
|
|
|
|
-def get_paths():
|
|
|
+
|
|
|
+def get_paths() -> list:
|
|
|
list_path = []
|
|
|
- for root, _, files in os.walk(DIR_WORK):
|
|
|
+ for root, _, files in os.walk(args.path):
|
|
|
list_path += [
|
|
|
F"{root}/{file}" for file in files if file.split('.')[-1] == 'csv' and "result" not in file]
|
|
|
return list_path
|
|
|
|
|
|
|
|
|
-def convert_mb_to_gb(val):
|
|
|
+def convert_mb_to_gb(val: str) -> str:
|
|
|
numbers = int(''.join([x for x in val if x.isdigit()]))
|
|
|
return str(round(numbers/1024, 1))+' GB'
|
|
|
|
|
|
|
|
|
-def get_ser_motherboard(row):
|
|
|
+def get_ser_motherboard(row: str) -> str:
|
|
|
if len(row) > 0:
|
|
|
if row[6] not in ['To be filled by O.E.M.', 'Default string']:
|
|
|
return row[6]
|
|
@@ -45,9 +76,9 @@ def get_ser_motherboard(row):
|
|
|
return None
|
|
|
|
|
|
|
|
|
-def get_data_from_file(path, obj):
|
|
|
+def get_data_from_file(path: str, obj: ObjectReady) -> None:
|
|
|
global COUNT_INTERFACE
|
|
|
- with open(path, 'r', encoding='utf-16') as file:
|
|
|
+ with open(path, 'r', encoding=args.open_encoding) as file:
|
|
|
cs = csv.reader(file, delimiter=',')
|
|
|
|
|
|
next(cs)
|
|
@@ -64,7 +95,7 @@ def get_data_from_file(path, obj):
|
|
|
if x[0] == '6200':
|
|
|
obj.motherboard = get_ser_motherboard(x)
|
|
|
|
|
|
- if x[0] == '2600' and check_correct_controller(x[3]) and len(x) > 2:
|
|
|
+ if x[0] == '2600' and len(x) > 2 and check_correct_controller(x[3]):
|
|
|
setattr(obj, F'ip{count_interface}', x[6].split(' ')[0])
|
|
|
setattr(obj, F'mac{count_interface}', x[-3])
|
|
|
count_interface += 1
|
|
@@ -77,9 +108,7 @@ def get_data_from_file(path, obj):
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
-def get_ready_information():
|
|
|
- err_file = open(
|
|
|
- F"Error_file-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}", "w")
|
|
|
+def get_ready_information() -> list[ObjectReady]:
|
|
|
list_objects = []
|
|
|
for path in get_paths():
|
|
|
|
|
@@ -91,30 +120,27 @@ def get_ready_information():
|
|
|
try:
|
|
|
get_data_from_file(path, obj)
|
|
|
except UnicodeDecodeError as ude:
|
|
|
- err_file.write(F"{path} --> {ude}\n")
|
|
|
+ logger.error(F"{path} --> {ude}\n")
|
|
|
+ except UnicodeError as er:
|
|
|
+ logger.error(F"{path} --> {er}\n")
|
|
|
except StopIteration as si:
|
|
|
- err_file.write(F"{path} --> Пустой файл или тмпо того\n")
|
|
|
+ logger.error(F"{path} --> Пустой файл или тмпо того\n")
|
|
|
|
|
|
list_objects.append(obj)
|
|
|
return list_objects
|
|
|
|
|
|
|
|
|
-def create_csv(list_obj):
|
|
|
- namefile = F"result-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}"
|
|
|
+def create_csv(list_obj: list[ObjectReady]) -> None:
|
|
|
+ namefile = F"result-{datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}"
|
|
|
|
|
|
- with open(F"{namefile}.csv", 'w', newline='') as f:
|
|
|
+ with open(F"{namefile}.csv", 'w', newline='', encoding=args.encoding) as f:
|
|
|
header_csv = F"frame;cabinet;os;motherboard;cpu;ram{''.join([F';ip{i+1};mac{i+1}' for i in range(COUNT_INTERFACE)])}\n"
|
|
|
f.write(header_csv)
|
|
|
|
|
|
for obj in list_obj:
|
|
|
- dic = obj.__dict__
|
|
|
- try:
|
|
|
- row_interface = ';'.join([F'{dic[F"ip{i}"]};{dic[F"mac{i}"]}' for i in range(obj.count_interface)])
|
|
|
- f.write(F'"{dic["frame"]}";"{dic["cabinet"]}";"{dic["os"]}";"{dic["motherboard"]}";"{dic["cpu"]}";"{dic["ram"]}";{row_interface}\n')
|
|
|
- except AttributeError:
|
|
|
- f.write(F'"{obj.frame}";"{obj.cabinet}"\n')
|
|
|
-
|
|
|
+ f.write(obj.get_csv_row())
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- ready_information = get_ready_information()
|
|
|
- create_csv(ready_information)
|
|
|
+ create_logger()
|
|
|
+ create_csv(get_ready_information())
|