def _check_create_filepath(filepath): """ Check file path existence, accessible and available, if not exist create the file Args: filepath (str): File path. """ permissions = os.R_OK | os.W_OK | os.X_OK os.umask(permissions << 3 | permissions) if os.path.exists(filepath): parse_summary_logger.error( 'Path %s has already existed, please choose a new output path.', filepath) return False mode = permissions << 6 os.makedirs(filepath, mode=mode) return True
def _check_filepath(filepath): """ Check file path existence, accessible and available Args: filepath (str): File path. """ if not os.path.isfile(filepath): parse_summary_logger.error('Summary file %s is not a valid file.', filepath) return False if not os.access(filepath, os.R_OK): parse_summary_logger.error( 'Path %s is not accessible, please check the file-authority.', filepath) return False return True
def run(self, args): """ Execute for start command. Args: args (Namespace): Parsed arguments to hold customized parameters. """ try: date_time = datetime.datetime.now().strftime( 'output_%Y%m%d_%H%M%S_%f') output_path = os.path.join(args.output, date_time) summary_dir = args.summary_dir if not self._check_dirpath(summary_dir): return summary_parser = _SummaryParser(summary_dir) summary_files = summary_parser.filter_files( os.listdir(summary_dir)) if not summary_files: parse_summary_logger.error('Path %s has no summary file.', summary_dir) return summary_files = summary_parser.sort_files(summary_files) filename = summary_files[-1] summary_file = FileHandler.join(summary_dir, filename) if not (self._check_filepath(summary_file) and self._check_create_filepath(output_path) and self._check_create_filepath( FileHandler.join(output_path, 'image'))): return eventparser = EventParser(summary_file, output_path) eventparser.parse() except Exception as ex: parse_summary_logger.error( "Parse summary file failed, detail: %r.", str(ex)) raise UnknownError(str(ex))
def _load(self, file_handler): """ Load a log file data. Args: file_handler (FileHandler): A file handler. Returns: bool, True if the summary file is finished loading. """ crc_check_time = 0 while True: start_offset = file_handler.offset try: event_str = _SummaryParser.event_load(file_handler) if start_offset != file_handler.offset: self._print_process(file_handler) crc_check_time = 0 if event_str is None: return True if len(event_str) > MAX_EVENT_STRING: parse_summary_logger.warning("file_path: %s, event string: %d exceeds %d and drop it.", file_handler.file_path, len(event_str), MAX_EVENT_STRING) continue self._event_parse(event_str) except exceptions.CRCLengthFailedError: if crc_check_time > RETRY_TIMES: parse_summary_logger.error( "Check crc length failed, please check the summary file integrity, " "the file may be in transfer, file_path: %s, offset=%s.", file_handler.file_path, start_offset) return True parse_summary_logger.warning( "Check crc failed, retrying %d/%d times.", crc_check_time + 1, RETRY_TIMES + 1) file_handler.reset_offset(start_offset) crc_check_time += 1 time.sleep(0.5) except (OSError, DecodeError, exceptions.MindInsightException) as ex: parse_summary_logger.error("Parse file fail, detail: %r, file path: %s.", str(ex), file_handler.file_path) return False
def _check_dirpath(filepath): """ Check file path existence, accessible and available Args: filepath (str): File path. """ if os.path.exists(filepath): if not os.path.isdir(filepath): parse_summary_logger.error( 'Summary directory %s is not a valid directory.', filepath) return False if not os.access(filepath, os.R_OK | os.X_OK): parse_summary_logger.error( 'Path %s is not accessible, please check the file-authority.', filepath) return False return True parse_summary_logger.error('Summary directory %s not exists.', filepath) return False