|
@@ -1,20 +1,25 @@
|
|
|
-import csv, os, re, sys, argparse
|
|
|
+import csv
|
|
|
+import os
|
|
|
+import sys
|
|
|
+import argparse
|
|
|
from logger import *
|
|
|
from datetime import datetime
|
|
|
|
|
|
+ARGS = None
|
|
|
|
|
|
-
|
|
|
parse = argparse.ArgumentParser(
|
|
|
- description="Программа для сбора информации из csv файлов")
|
|
|
+ description="Программа для сбора информации из csv файлов")
|
|
|
+
|
|
|
parse.add_argument('-p', '--path', help='Указать в какой директории искать',
|
|
|
default=os.path.dirname(sys.argv[0]))
|
|
|
parse.add_argument('-e', '--encoding',
|
|
|
- help='Указать в какой кодировке сохранять', default='windows-1251')
|
|
|
+ help='Указать в какой кодировке сохранять', default='ANSI')
|
|
|
parse.add_argument('-s', '--open_encoding',
|
|
|
help='Указать в какой кодировке открывать', default='utf-16')
|
|
|
-args = parse.parse_args()
|
|
|
+parse.add_argument('-d', '--delimiter', help='разделитель csv', default=',')
|
|
|
+
|
|
|
|
|
|
-print(args)
|
|
|
+
|
|
|
LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
|
|
|
'Kaspersky', 'VirtualBox', 'TAP-Windows',
|
|
|
'Wintun', '802.11', 'VMware', 'VPN', 'Wi-Fi',
|
|
@@ -29,29 +34,37 @@ def check_correct_controller(name) -> bool:
|
|
|
|
|
|
|
|
|
class ObjectReady:
|
|
|
+ frame: str = None
|
|
|
+ cabinet: str = None
|
|
|
+ os: str = None
|
|
|
+ cpu: str = None
|
|
|
+ ram: str = None
|
|
|
+ motherboard: str = None
|
|
|
+ count_interface: int = 0
|
|
|
+ path: str = None
|
|
|
+
|
|
|
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))
|
|
|
+ return F"{ARGS.delimiter}".join((self.frame, self.cabinet))
|
|
|
|
|
|
- dic = self.__dict__
|
|
|
- # print(dic)
|
|
|
try:
|
|
|
- row_interface = ';'.join(
|
|
|
- [';'.join((dic.get(F"ip{i}"), dic.get(F"mac{i}"))) for i in range(self.count_interface)
|
|
|
- ])
|
|
|
+ row_interface = F"{ARGS.delimiter}".join(
|
|
|
+ [ARGS.delimiter.join(
|
|
|
+ (self.__dict__.get(F"ip{i}"), self.__dict__.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'
|
|
|
+ return ARGS.delimiter.join((self.frame, self.cabinet, self.os, self.motherboard, self.cpu, self.ram, row_interface))+'\n'
|
|
|
except AttributeError:
|
|
|
- return ';'.join((self.frame, self.cabinet))+"\n"
|
|
|
+ return None
|
|
|
|
|
|
|
|
|
def get_paths() -> list:
|
|
|
list_path = []
|
|
|
- for root, _, files in os.walk(args.path):
|
|
|
+ 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
|
|
@@ -74,7 +87,7 @@ def get_ser_motherboard(row: str) -> str:
|
|
|
|
|
|
def get_data_from_file(path: str, obj: ObjectReady) -> None:
|
|
|
global COUNT_INTERFACE
|
|
|
- with open(path, 'r', encoding=args.open_encoding) as file:
|
|
|
+ with open(path, 'r', encoding=ARGS.open_encoding) as file:
|
|
|
cs = csv.reader(file, delimiter=',')
|
|
|
|
|
|
next(cs)
|
|
@@ -110,40 +123,63 @@ def get_ready_information() -> list[ObjectReady]:
|
|
|
|
|
|
obj = ObjectReady()
|
|
|
obj.frame = os.path.basename(os.path.dirname(path))
|
|
|
- obj.cabinet = re.findall(
|
|
|
- r"\/([\w+?\ ?[а-яА-Я]+\ ?|\w+?\ ?[a-zA-Z]+\ ?|\d+])[\.|\,]", path)[-1]
|
|
|
+ obj.path = path
|
|
|
+
|
|
|
+ try:
|
|
|
+ obj.cabinet = str(os.path.basename(path)).split('.')[0]
|
|
|
+ except IndexError as er:
|
|
|
+ logger.error(F"index error path: {path}")
|
|
|
+ continue
|
|
|
|
|
|
try:
|
|
|
get_data_from_file(path, obj)
|
|
|
+ logger.info(F'Parse success file: {path}')
|
|
|
except UnicodeDecodeError as ude:
|
|
|
- logger.error(F"{path} --> {ude}\n")
|
|
|
+ logger.error(F"{path} --> {ude}")
|
|
|
except UnicodeError as er:
|
|
|
- logger.error(F"{path} --> {er}\n")
|
|
|
- except StopIteration as si:
|
|
|
- logger.error(F"{path} --> Пустой файл или тмпо того\n")
|
|
|
+ logger.error(F"{path} --> {er}")
|
|
|
+ except StopIteration:
|
|
|
+ logger.error(F"{path} --> Пустой файл или тмпо того")
|
|
|
|
|
|
list_objects.append(obj)
|
|
|
return list_objects
|
|
|
|
|
|
|
|
|
+def create_header_interface() -> str:
|
|
|
+ return ARGS.delimiter.join([
|
|
|
+ ARGS.delimiter.join((F"ip{i+1}", F"mac{i+1}"))
|
|
|
+ for i in range(COUNT_INTERFACE)
|
|
|
+ ])
|
|
|
+
|
|
|
+
|
|
|
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='', 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)
|
|
|
+ with open(F"{namefile}.csv", 'w', newline='', encoding=ARGS.encoding) as f:
|
|
|
+ header_interface = create_header_interface()
|
|
|
+ header_csv = ARGS.delimiter.join(
|
|
|
+ ("frame", "cabinet", "os", "motherboard", "cpu", "ram", header_interface))
|
|
|
+
|
|
|
+ f.write(header_csv + "\n")
|
|
|
|
|
|
for obj in list_obj:
|
|
|
- row = obj.get_csv_row()
|
|
|
- logger.info(row)
|
|
|
- f.write(obj.get_csv_row())
|
|
|
-
|
|
|
+ try:
|
|
|
+ f.write(obj.get_csv_row())
|
|
|
+ except TypeError as te:
|
|
|
+ logger.error(F'{obj.path} -> {te}')
|
|
|
+ continue
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
+ print('start program')
|
|
|
+ try:
|
|
|
+ ARGS = parse.parse_args(sys.argv[1:])
|
|
|
+ except argparse.ArgumentError:
|
|
|
+ parse.print_help()
|
|
|
+
|
|
|
create_logger()
|
|
|
-
|
|
|
+
|
|
|
logger.info("Start programm")
|
|
|
- logger.info(F"Base dir: {args.path}")
|
|
|
-
|
|
|
+ logger.info(F"Base dir: {ARGS.path}")
|
|
|
create_csv(get_ready_information())
|
|
|
logger.info("Stop programm")
|