def get_drive_info(self, smartctl_drive_identifier: str) -> DriveInfo: drive_info_output = CommandRunner.run_command( "smartctl", "-i " + smartctl_drive_identifier, True) dictionary = {} drive_info_output_lines = drive_info_output.decode("utf-8").split('\n') for i, line in enumerate(drive_info_output_lines): # disregard header information in file if i > 3 and line.strip(): dictionary[line[:18].strip()] = line[18:].strip() drive_info = DriveInfo(dictionary) # Create drive folder if doesn't exist. drive_directory = Constants.instance().drive_directory( drive_info.serial_number) if not os.path.exists(drive_directory): os.mkdir(drive_directory) # Write drive info into a file. drive_info_file_path = Constants.instance().drive_info_file_path( drive_info.serial_number) with open(drive_info_file_path, "w+") as f: for line in drive_info_output_lines: f.writelines(line + '\n') return drive_info
def __store_email_to_file(email: str, inlined_email: str, drive_serial_number: str) -> None: file = os.path.join(Constants.instance().uninlined_email_file_path( drive_serial_number)) with open(file, "w+") as f: f.write(email) inlined_file = os.path.join( Constants.instance().inlined_email_file_path(drive_serial_number)) with open(inlined_file, "w+") as f: f.write(inlined_email)
def __get_attribute_readings_from_file(self, file_name: str) -> Run: attribute_file_path = os.path.join( Constants.instance().drive_directory(self.drive_serial_number), file_name) run_time = datetime.strptime( file_name, Constants.instance().attribute_file_name_format) attributes = [] with open(attribute_file_path, "r") as f: for i, line in enumerate(f): # disregard header information in file if i > 6 and line.strip(): attributes.append( Attribute(self.__extract_attribute_values(line))) return Run(attributes, run_time)
def __get_current_attributes_reading( self, smartctl_drive_identifier: str) -> Run: timestamp = datetime.now() output = CommandRunner.run_command( "smartctl", "--attributes " + smartctl_drive_identifier, True) filename = timestamp.strftime( Constants.instance().attribute_file_name_format) file_path = os.path.join( Constants.instance().drive_directory(self.drive_serial_number), filename) with open(file_path, "w+b") as f: f.write(output) return self.__get_attribute_readings_from_file(filename)
def create_config_file_if_doesnt_exist() -> None: if os.path.exists(Constants.instance().config_file_path): return with open(Constants.instance().config_file_path, 'w+') as configfile: config = configparser.ConfigParser() config['MAIL'] = { 'SERVER': 'smtp.gmail.com', 'PORT': 465, 'USE_TLS': False, 'USE_SSL': True, 'USERNAME': '******', 'PASSWORD': '******', 'FROM': '*****@*****.**', 'TO': '*****@*****.**' } config.write(configfile)
def get_current_previous_and_initial_runs( self, smartctl_drive_identifier: str) -> (Run, Run, Run): current_attributes_reading = self.__get_current_attributes_reading( smartctl_drive_identifier) attribute_files = os.listdir(Constants.instance().drive_directory( self.drive_serial_number)) attribute_files.remove("info.txt") attribute_files.sort() if len(attribute_files) == 1: return current_attributes_reading, None, None elif len(attribute_files) == 2: initial_attribute_reading = self.__get_attribute_readings_from_file( attribute_files[0]) return current_attributes_reading, None, initial_attribute_reading else: initial_attribute_reading = self.__get_attribute_readings_from_file( attribute_files[0]) previous_attribute_reading = self.__get_attribute_readings_from_file( attribute_files[-2]) return current_attributes_reading, previous_attribute_reading, initial_attribute_reading
def process_runtime_variables() -> None: Constants.instance().package_directory = os.path.dirname( os.path.abspath(__file__))
def create_history_folder() -> None: if not os.path.exists(Constants.instance().history_directory): os.mkdir(Constants.instance().history_directory)
def __get_email_template(): template_file_path = Constants.instance().email_template_file_path with open(template_file_path, "r") as f: template = f.read() return template
def __init__(self): self.configParser = configparser.ConfigParser() if not self.configParser.read(Constants.instance().config_file_path): self.create_config_file_if_doesnt_exist() self.configParser.read(Constants.instance().config_file_path)