def test_no_update_if_time_is_same(self, mock_getmtime): mock_getmtime.return_value = 100 file = tempfile.NamedTemporaryFile('wb', delete=False) try: tokens.write_csv( database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)), file) file.close() detok = detokenize.AutoUpdatingDetokenizer(file, min_poll_period_s=0) self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) # Empty the database, but keep the mock modified time the same. with open(file.name, 'wb'): pass self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) # Move back time so the now-empty file is reloaded. mock_getmtime.return_value = 50 self.assertFalse(detok.detokenize(JELLO_WORLD_TOKEN).ok()) finally: os.unlink(file.name)
def test_update(self, mock_getmtime): """Tests the update command.""" db = database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)) self.assertEqual(len(db), TOKENS_IN_ELF) the_time = [100] def move_back_time_if_file_exists(path): if os.path.exists(path): the_time[0] -= 1 return the_time[0] raise FileNotFoundError mock_getmtime.side_effect = move_back_time_if_file_exists file = tempfile.NamedTemporaryFile('wb', delete=False) try: file.close() detok = detokenize.AutoUpdatingDetokenizer(file.name, min_poll_period_s=0) self.assertFalse(detok.detokenize(JELLO_WORLD_TOKEN).ok()) with open(file.name, 'wb') as fd: tokens.write_binary(db, fd) self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) finally: os.unlink(file.name) # The database stays around if the file is deleted. self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok())
def test_update(self, mock_getmtime): db = database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)) self.assertEqual(len(db), 16) the_time = [100] def move_back_time_if_file_exists(path): if os.path.exists(path): the_time[0] -= 1 return the_time[0] raise FileNotFoundError mock_getmtime.side_effect = move_back_time_if_file_exists with tempfile.NamedTemporaryFile('wb', delete=True) as fd: detok = detokenize.AutoUpdatingDetokenizer(fd.name, min_poll_period_s=0) self.assertFalse(detok.detokenize(JELLO_WORLD_TOKEN).ok()) tokens.write_binary(db, fd) fd.flush() self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) # The database stays around if the file is deleted. self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok())
def setUp(self): super().setUp() db = database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)) db.add( tokens.TokenizedStringEntry(tokens.default_hash(s), s) for s in [self.RECURSION_STRING, self.RECURSION_STRING_2]) self.detok = detokenize.Detokenizer(db)
def _main(args): token_database = \ database.load_token_database(args.trace_token_database, domain="trace") _LOG.info(database.database_summary(token_database)) client = get_hdlc_rpc_client(**vars(args)) data = get_trace_data_from_device(client) events = trace_tokenized.get_trace_events([token_database], data) json_lines = trace.generate_trace_json(events) trace_tokenized.save_trace_file(json_lines, args.trace_output_file)
def __init__(self, *token_database_or_elf, show_errors: bool = False): """Decodes and detokenizes binary messages. Args: *token_database_or_elf: a path or file object for an ELF or CSV database, a tokens.Database, or an elf_reader.Elf show_errors: if True, an error message is used in place of the % conversion specifier when an argument fails to decode """ self.database = database.load_token_database(*token_database_or_elf) self.show_errors = show_errors # Cache FormatStrings for faster lookup & formatting. self._cache: Dict[int, List[_TokenizedFormatString]] = {}
def test_no_update_if_time_is_same(self, mock_getmtime): mock_getmtime.return_value = 100 with tempfile.NamedTemporaryFile('wb', delete=True) as fd: tokens.write_csv( database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)), fd) fd.flush() detok = detokenize.AutoUpdatingDetokenizer(fd, min_poll_period_s=0) self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) # Empty the database, but keep the modified time the same. fd.truncate(0) fd.flush() self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) self.assertTrue(detok.detokenize(JELLO_WORLD_TOKEN).ok()) # Move back time so the now-empty file is reloaded. mock_getmtime.return_value = 50 self.assertFalse(detok.detokenize(JELLO_WORLD_TOKEN).ok())
def load(self) -> tokens.Database: try: return database.load_token_database(self.path) except FileNotFoundError: return database.load_token_database()
def setUp(self): super().setUp() db = database.load_token_database( io.BytesIO(ELF_WITH_TOKENIZER_SECTIONS)) db.add([self.RECURSION_STRING, self.RECURSION_STRING_2]) self.detok = detokenize.Detokenizer(db)