main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import csv
  2. import os
  3. import re
  4. import datetime
  5. DIR_WORK = "./"
  6. LIST_BAD_ADAPTER = ['Wireless', 'Bluetooth', 'Wireless', 'WiFi',
  7. 'Kaspersky', 'VirtualBox', 'TAP-Windows',
  8. 'Wintun', '802.11', 'VMware', 'VPN', 'Wi-Fi',
  9. '1394', 'Mobile']
  10. # Количетсво интерфейсов по умолчанию
  11. COUNT_INTERFACE = 1
  12. def check_correct_controller(name):
  13. return any(ext not in name for ext in LIST_BAD_ADAPTER)
  14. class ObjectReady:
  15. def get_len_dict(self):
  16. return len(self.__dict__)
  17. def get_paths():
  18. list_path = []
  19. for root, _, files in os.walk(DIR_WORK):
  20. list_path += [
  21. F"{root}/{file}" for file in files if file.split('.')[-1] == 'csv' and "result" not in file]
  22. return list_path
  23. def convert_mb_to_gb(val):
  24. numbers = int(''.join([x for x in val if x.isdigit()]))
  25. return str(round(numbers/1024, 1))+' GB'
  26. def get_ser_motherboard(row):
  27. if len(row) > 0:
  28. if row[6] not in ['To be filled by O.E.M.', 'Default string']:
  29. return row[6]
  30. else:
  31. return None
  32. else:
  33. return None
  34. def get_data_from_file(path, obj):
  35. global COUNT_INTERFACE
  36. with open(path, 'r', encoding='utf-16') as file:
  37. cs = csv.reader(file, delimiter=',')
  38. next(cs)
  39. line_info_computers = next(cs)
  40. obj.os = line_info_computers[7]
  41. obj.cpu = line_info_computers[13]
  42. obj.ram = convert_mb_to_gb(line_info_computers[14])
  43. obj.ip1 = None
  44. obj.mac1 = None
  45. count_interface = 0
  46. for x in cs:
  47. if x[0] == '6200':
  48. obj.motherboard = get_ser_motherboard(x)
  49. if x[0] == '2600' and check_correct_controller(x[3]) and len(x) > 2:
  50. setattr(obj, F'ip{count_interface}', x[6].split(' ')[0])
  51. setattr(obj, F'mac{count_interface}', x[-3])
  52. count_interface += 1
  53. # Увеличиваем значение если количестов интерфейсов больше в этой строке
  54. obj.count_interface = count_interface
  55. if count_interface > COUNT_INTERFACE:
  56. COUNT_INTERFACE = count_interface
  57. file.close()
  58. def get_ready_information():
  59. err_file = open(
  60. F"Error_file-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}", "w")
  61. list_objects = []
  62. for path in get_paths():
  63. obj = ObjectReady()
  64. obj.frame = path.split('/')[-2]
  65. obj.cabinet = re.findall(
  66. r"\/([\w+?\ ?[а-яА-Я]+\ ?|\w+?\ ?[a-zA-Z]+\ ?|\d+])[\.|\,]", path)[-1]
  67. try:
  68. get_data_from_file(path, obj)
  69. except UnicodeDecodeError as ude:
  70. err_file.write(F"{path} --> {ude}\n")
  71. except StopIteration as si:
  72. err_file.write(F"{path} --> Пустой файл или тмпо того\n")
  73. list_objects.append(obj)
  74. return list_objects
  75. def create_csv(list_obj):
  76. namefile = F"result-{datetime.datetime.today().strftime('%Y-%m-%d-%H.%M.%S')}"
  77. with open(F"{namefile}.csv", 'w', newline='') as f:
  78. header_csv = F"frame;cabinet;os;motherboard;cpu;ram{''.join([F';ip{i+1};mac{i+1}' for i in range(COUNT_INTERFACE)])}\n"
  79. f.write(header_csv)
  80. for obj in list_obj:
  81. dic = obj.__dict__
  82. try:
  83. row_interface = ';'.join([F'{dic[F"ip{i}"]};{dic[F"mac{i}"]}' for i in range(obj.count_interface)])
  84. f.write(F'"{dic["frame"]}";"{dic["cabinet"]}";"{dic["os"]}";"{dic["motherboard"]}";"{dic["cpu"]}";"{dic["ram"]}";{row_interface}\n')
  85. except AttributeError:
  86. f.write(F'"{obj.frame}";"{obj.cabinet}"\n')
  87. if __name__ == '__main__':
  88. ready_information = get_ready_information()
  89. create_csv(ready_information)