scripts: dbgflags.py: Added -d/--diff, lineno, better find reuse
This started with adding -d/--diff support to dbgflags.py, which is very
useful for comparing flags during test failure.
The flag asserts in our tests generally look like this:
tests/test_mount.toml:171:assert: assert failed with 33570064,
expected eq 33570576
assert(fsinfo.flags == (
Which can now be quickly compared with dbgflags.py:
$ ./scripts/dbgflags.py +i 33570064 -d 33570576
LFS3_I_GBMAP 0x02000000 Global on-disk block-map in use
LFS3_I_REVPERTURB 0x00000010 Mounted with LFS3_M_REVPERTURB
LFS3_I_MKCONSISTENT 0x00000100 Filesystem needs mkconsistent to write
-LFS3_I_LOOKAHEAD 0x00000200 Lookahead buffer is not full
LFS3_I_PREERASE 0x00000400 Blocks can be pre-erased
LFS3_I_COMPACT 0x00000800 Filesystem may have uncompacted metadata
LFS3_I_CKMETA 0x00001000 Metadata checksums not checked recently
LFS3_I_CKDATA 0x00002000 Data checksums not checked recently
The assert print is a bit more annoying than it needs to be, as it only
prints in decimal. But, since our prettyasserts.py only works at the
syntax layer, it's not possible to make it any smarter.
---
To make this diffing work required a couple more features in our
self-parsing Flag class:
- Keep track of lineno, mainly for ordering things
- Moved find logic into a staticmethod on all classes
- Added _sentinel based defaults to find functions
- Allowed self to be non-class in line functions to deduplicate "Unknown
flag" messages
I went ahead and extended these to the other self-parsing classes (Err
and Tag) in case they're useful in the future.
This commit is contained in:
+21
-9
@@ -167,11 +167,13 @@ TAG_GCKSUMDELTA = 0x3300 ## v-11 --11 ++++ ++++
|
||||
|
||||
# self-parsing tag repr
|
||||
class Tag:
|
||||
def __init__(self, name, tag, encoding, help):
|
||||
def __init__(self, name, tag, encoding, help, *,
|
||||
lineno=0):
|
||||
self.name = name
|
||||
self.tag = tag
|
||||
self.encoding = encoding
|
||||
self.help = help
|
||||
self.lineno = lineno
|
||||
# derive mask from encoding
|
||||
self.mask = sum(
|
||||
(1 if x in 'v-01' else 0) << len(self.encoding)-1-i
|
||||
@@ -240,29 +242,37 @@ class Tag:
|
||||
tag_pattern = re.compile(
|
||||
'^(?P<name>TAG_[^ ]*) *= *(?P<tag>[^#]*?) *'
|
||||
'#+ *(?P<encoding>(?:[^ ] *?){16}) *(?P<help>.*)$')
|
||||
for line in (inspect.getsource(
|
||||
inspect.getmodule(inspect.currentframe()))
|
||||
.replace('\\\n', '')
|
||||
.splitlines()):
|
||||
for i, line in enumerate(
|
||||
inspect.getsource(inspect.getmodule(inspect.currentframe()))
|
||||
.replace('\\\n', '')
|
||||
.splitlines()):
|
||||
m = tag_pattern.match(line)
|
||||
if m:
|
||||
tags.append(Tag(
|
||||
m.group('name'),
|
||||
globals()[m.group('name')],
|
||||
m.group('encoding').replace(' ', ''),
|
||||
m.group('help')))
|
||||
m.group('help'),
|
||||
lineno=1+i))
|
||||
return tags
|
||||
|
||||
# find best matching tag
|
||||
_sentinel = object()
|
||||
@staticmethod
|
||||
def find(tag):
|
||||
def find(tag, *, default=_sentinel):
|
||||
# find tags, note this is cached
|
||||
tags__ = Tag.tags()
|
||||
|
||||
# find the most specific matching tag, ignoring valid bits
|
||||
return max((t for t in tags__ if t.matches(tag & 0x7fff)),
|
||||
t = max((t for t in tags__ if t.matches(tag & 0x7fff)),
|
||||
key=lambda t: t.specificity(),
|
||||
default=None)
|
||||
if t is not None:
|
||||
return t
|
||||
elif default is Tag._sentinel:
|
||||
raise KeyError(tag)
|
||||
else:
|
||||
return default
|
||||
|
||||
# human readable tag repr
|
||||
@staticmethod
|
||||
@@ -270,7 +280,9 @@ class Tag:
|
||||
global_=False,
|
||||
toff=None):
|
||||
# find the most specific matching tag, ignoring the shrub bit
|
||||
t = Tag.find(tag & ~(TAG_SHRUB if tag & 0x7000 == TAG_SHRUB else 0))
|
||||
t = Tag.find(
|
||||
tag & ~(TAG_SHRUB if tag & 0x7000 == TAG_SHRUB else 0),
|
||||
default=None)
|
||||
|
||||
# build repr
|
||||
r = []
|
||||
|
||||
Reference in New Issue
Block a user