Skip to content

Commit 6af247e

Browse files
pdgendtkartben
authored andcommitted
scripts: logging: dictionary: database_gen: Fix linter issues
Fix issues reported by ruff. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 9e5aa82 commit 6af247e

File tree

2 files changed

+44
-68
lines changed

2 files changed

+44
-68
lines changed

.ruff-excludes.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,15 +568,6 @@
568568
"UP036", # https://docs.astral.sh/ruff/rules/outdated-version-block
569569
"UP038", # https://docs.astral.sh/ruff/rules/non-pep604-isinstance
570570
]
571-
"./scripts/logging/dictionary/database_gen.py" = [
572-
"E713", # https://docs.astral.sh/ruff/rules/not-in-test
573-
"E741", # https://docs.astral.sh/ruff/rules/ambiguous-variable-name
574-
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
575-
"SIM102", # https://docs.astral.sh/ruff/rules/collapsible-if
576-
"SIM103", # https://docs.astral.sh/ruff/rules/needless-bool
577-
"SIM113", # https://docs.astral.sh/ruff/rules/enumerate-for-loop
578-
"SIM115", # https://docs.astral.sh/ruff/rules/open-file-with-context-handler
579-
]
580571
"./scripts/logging/dictionary/dictionary_parser/data_types.py" = [
581572
"I001", # https://docs.astral.sh/ruff/rules/unsorted-imports
582573
"UP039", # https://docs.astral.sh/ruff/rules/unnecessary-class-parentheses

scripts/logging/dictionary/database_gen.py

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,15 @@
2121
import sys
2222

2323
import dictionary_parser.log_database
24-
from dictionary_parser.log_database import LogDatabase
25-
from dictionary_parser.utils import extract_one_string_in_section
26-
from dictionary_parser.utils import find_string_in_mappings
27-
2824
import elftools
25+
from dictionary_parser.log_database import LogDatabase
26+
from dictionary_parser.utils import extract_one_string_in_section, find_string_in_mappings
27+
from elftools.dwarf.descriptions import describe_DWARF_expr
28+
from elftools.dwarf.locationlists import LocationExpr, LocationParser
2929
from elftools.elf.constants import SH_FLAGS
30-
from elftools.elf.elffile import ELFFile
3130
from elftools.elf.descriptions import describe_ei_data
31+
from elftools.elf.elffile import ELFFile
3232
from elftools.elf.sections import SymbolTableSection
33-
from elftools.dwarf.descriptions import (
34-
describe_DWARF_expr
35-
)
36-
from elftools.dwarf.locationlists import (
37-
LocationExpr, LocationParser
38-
)
39-
4033

4134
LOGGER_FORMAT = "%(name)s: %(levelname)s: %(message)s"
4235
logger = logging.getLogger(os.path.basename(sys.argv[0]))
@@ -309,7 +302,7 @@ def find_die_var_base_type(compile_unit, die, is_const):
309302
return die.attributes['DW_AT_name'].value.decode('ascii'), is_const
310303

311304
# Not a type, cannot continue
312-
if not 'DW_AT_type' in die.attributes:
305+
if 'DW_AT_type' not in die.attributes:
313306
return None, None
314307

315308
if die.tag == 'DW_TAG_const_type':
@@ -334,10 +327,7 @@ def is_die_var_const_char(compile_unit, die):
334327
"""
335328
var_type, is_const = find_die_var_base_type(compile_unit, die, False)
336329

337-
if var_type is not None and var_type.endswith('char') and is_const:
338-
return True
339-
340-
return False
330+
return bool(var_type is not None and var_type.endswith('char') and is_const)
341331

342332

343333
def extract_string_variables(elf):
@@ -357,32 +347,31 @@ def extract_string_variables(elf):
357347
for die in compile_unit.iter_DIEs():
358348
# Only care about variables with location information
359349
# and of type "char"
360-
if die.tag == 'DW_TAG_variable':
361-
if ('DW_AT_type' in die.attributes
362-
and 'DW_AT_location' in die.attributes
363-
and is_die_var_const_char(compile_unit, die)
364-
):
365-
# Extract location information, which is
366-
# its address in memory.
367-
loc_attr = die.attributes['DW_AT_location']
368-
if loc_parser.attribute_has_location(loc_attr, die.cu['version']):
369-
loc = loc_parser.parse_from_attribute(loc_attr, die.cu['version'], die)
370-
if isinstance(loc, LocationExpr):
371-
try:
372-
addr = describe_DWARF_expr(loc.loc_expr,
373-
dwarf_info.structs)
374-
375-
matcher = DT_LOCATION_REGEX.match(addr)
376-
if matcher:
377-
addr = int(matcher.group(1), 16)
378-
if addr > 0:
379-
strings.append({
380-
'name': die.attributes['DW_AT_name'].value,
381-
'addr': addr,
382-
'die': die
383-
})
384-
except KeyError:
385-
pass
350+
if die.tag == 'DW_TAG_variable' and ('DW_AT_type' in die.attributes
351+
and 'DW_AT_location' in die.attributes
352+
and is_die_var_const_char(compile_unit, die)
353+
):
354+
# Extract location information, which is
355+
# its address in memory.
356+
loc_attr = die.attributes['DW_AT_location']
357+
if loc_parser.attribute_has_location(loc_attr, die.cu['version']):
358+
loc = loc_parser.parse_from_attribute(loc_attr, die.cu['version'], die)
359+
if isinstance(loc, LocationExpr):
360+
try:
361+
addr = describe_DWARF_expr(loc.loc_expr,
362+
dwarf_info.structs)
363+
364+
matcher = DT_LOCATION_REGEX.match(addr)
365+
if matcher:
366+
addr = int(matcher.group(1), 16)
367+
if addr > 0:
368+
strings.append({
369+
'name': die.attributes['DW_AT_name'].value,
370+
'addr': addr,
371+
'die': die
372+
})
373+
except KeyError:
374+
pass
386375

387376
return strings
388377

@@ -408,9 +397,8 @@ def is_printable(b):
408397
def extract_strings_in_one_section(section, str_mappings):
409398
"""Extract NULL-terminated strings in one ELF section"""
410399
data = section['data']
411-
idx = 0
412400
start = None
413-
for x in data:
401+
for idx, x in enumerate(data):
414402
if is_printable(chr(x)):
415403
# Printable character, potential part of string
416404
if start is None:
@@ -449,7 +437,6 @@ def extract_strings_in_one_section(section, str_mappings):
449437
else:
450438
# Non-printable byte, remove start location
451439
start = None
452-
idx += 1
453440

454441
return str_mappings
455442

@@ -508,7 +495,7 @@ def main():
508495
elif args.verbose:
509496
logger.setLevel(logging.INFO)
510497

511-
elffile = open(args.elffile, "rb")
498+
elffile = open(args.elffile, "rb") # noqa: SIM115
512499
if not elffile:
513500
logger.error("ERROR: Cannot open ELF file: %s, exiting...", args.elffile)
514501
sys.exit(1)
@@ -529,8 +516,8 @@ def main():
529516

530517
if args.build_header:
531518
with open(args.build_header) as f:
532-
for l in f:
533-
match = re.match(r'\s*#define\s+BUILD_VERSION\s+(.*)', l)
519+
for line in f:
520+
match = re.match(r'\s*#define\s+BUILD_VERSION\s+(.*)', line)
534521
if match:
535522
database.set_build_id(match.group(1))
536523
break
@@ -570,17 +557,15 @@ def main():
570557
extract_logging_subsys_information(elf, database, string_mappings)
571558

572559
# Write database file
573-
if args.json:
574-
if not LogDatabase.write_json_database(args.json, database):
575-
logger.error("ERROR: Cannot open database file for write: %s, exiting...",
576-
args.json)
577-
sys.exit(1)
560+
if args.json and not LogDatabase.write_json_database(args.json, database):
561+
logger.error("ERROR: Cannot open database file for write: %s, exiting...",
562+
args.json)
563+
sys.exit(1)
578564

579-
if args.syst:
580-
if not LogDatabase.write_syst_database(args.syst, database):
581-
logger.error("ERROR: Cannot open database file for write: %s, exiting...",
582-
args.syst)
583-
sys.exit(1)
565+
if args.syst and not LogDatabase.write_syst_database(args.syst, database):
566+
logger.error("ERROR: Cannot open database file for write: %s, exiting...",
567+
args.syst)
568+
sys.exit(1)
584569

585570
elffile.close()
586571

0 commit comments

Comments
 (0)