Skip to content

Commit

Permalink
scripts: Tweaked -p/--percent to accept the csv file for diffing
Browse files Browse the repository at this point in the history
This makes the -p/--percent flag a bit more consistent with -d/--diff
and -c/--compare, both of which change the printing strategy based on
additional context.
  • Loading branch information
geky committed Nov 17, 2024
1 parent 9a2b561 commit ef3accc
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 225 deletions.
55 changes: 27 additions & 28 deletions scripts/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ def table(Result, results, diff_results=None, *,
by=None,
fields=None,
sort=None,
summary=False,
diff=None,
percent=None,
all=False,
percent=False,
compare=None,
summary=False,
**_):
all_, all = all, __builtins__.all

Expand Down Expand Up @@ -411,12 +412,12 @@ def table(Result, results, diff_results=None, *,
for k in fields)]

# find compare entry if there is one
if compare is not None:
if compare:
compare_result = table.get(','.join(str(k) for k in compare))

# sort again, now with diff info, note that python's sort is stable
names.sort()
if compare is not None:
if compare:
names.sort(
key=lambda n: (
table.get(n) == compare_result,
Expand All @@ -426,7 +427,7 @@ def table(Result, results, diff_results=None, *,
getattr(compare_result, k, None))
for k in fields)),
reverse=True)
if diff_results is not None:
if diff or percent:
names.sort(
key=lambda n: tuple(
types[k].ratio(
Expand Down Expand Up @@ -457,12 +458,9 @@ def table(Result, results, diff_results=None, *,
' (%d added, %d removed)' % (
sum(1 for n in table if n not in diff_table),
sum(1 for n in diff_table if n not in table))
if diff_results is not None and not percent else '')
if diff else '')
if not summary else '']
if diff_results is None:
for k in fields:
header.append(k)
elif percent:
if not diff:
for k in fields:
header.append(k)
else:
Expand All @@ -479,15 +477,16 @@ def table_entry(name, r, diff_r=None):
entry = [name]
# normal entry?
if ((compare is None or r == compare_result)
and diff_results is None):
and not percent
and not diff):
for k in fields:
entry.append(
(getattr(r, k).table(),
getattr(getattr(r, k), 'notes', lambda: [])())
if getattr(r, k, None) is not None
else types[k].none)
# compare entry?
elif compare is not None and diff_results is None:
elif not percent and not diff:
for k in fields:
entry.append(
(getattr(r, k).table()
Expand All @@ -500,7 +499,7 @@ def table_entry(name, r, diff_r=None):
getattr(r, k, None),
getattr(compare_result, k, None)))))
# percent entry?
elif diff_results is not None and percent:
elif not diff:
for k in fields:
entry.append(
(getattr(r, k).table()
Expand All @@ -513,7 +512,7 @@ def table_entry(name, r, diff_r=None):
getattr(r, k, None),
getattr(diff_r, k, None)))))
# diff entry?
elif diff_results is not None:
else:
for k in fields:
entry.append(getattr(diff_r, k).table()
if getattr(diff_r, k, None) is not None
Expand All @@ -537,7 +536,7 @@ def table_entry(name, r, diff_r=None):
return entry

# entries
if not summary or compare:
if (not summary) or compare:
for name in names:
r = table.get(name)
if diff_results is None:
Expand All @@ -547,7 +546,7 @@ def table_entry(name, r, diff_r=None):
lines.append(table_entry(name, r, diff_r))

# total, unless we're comparing
if not (compare is not None and diff_results is None):
if not (compare and not percent and not diff):
r = next(iter(fold(Result, results, by=[])), None)
if diff_results is None:
diff_r = None
Expand Down Expand Up @@ -640,10 +639,11 @@ def main(obj_paths, *,
else CodeResult._fields)})

# find previous results?
if args.get('diff'):
diff_results = None
if args.get('diff') or args.get('percent'):
diff_results = []
try:
with openio(args['diff']) as f:
with openio(args.get('diff') or args.get('percent')) as f:
reader = csv.DictReader(f, restval='')
for r in reader:
# filter by matching defines
Expand All @@ -669,8 +669,7 @@ def main(obj_paths, *,

# print table
if not args.get('quiet'):
table(CodeResult, results,
diff_results if args.get('diff') else None,
table(CodeResult, results, diff_results,
by=by if by is not None else ['function'],
fields=fields,
sort=sort,
Expand Down Expand Up @@ -704,18 +703,22 @@ def main(obj_paths, *,
parser.add_argument(
'-d', '--diff',
help="Specify CSV file to diff against.")
parser.add_argument(
'-p', '--percent',
help="Specify CSV file to diff against, but only show precentage "
"change, not a full diff.")
parser.add_argument(
'-a', '--all',
action='store_true',
help="Show all, not just the ones that changed.")
parser.add_argument(
'-p', '--percent',
action='store_true',
help="Only show percentage change, not a full diff.")
parser.add_argument(
'-c', '--compare',
type=lambda x: tuple(v.strip() for v in x.split(',')),
help="Compare results to the row matching this by pattern.")
parser.add_argument(
'-Y', '--summary',
action='store_true',
help="Only show the total.")
parser.add_argument(
'-b', '--by',
action='append',
Expand Down Expand Up @@ -752,10 +755,6 @@ def __call__(self, parser, namespace, value, option):
nargs='?',
action=AppendSort,
help="Sort by this field, but backwards.")
parser.add_argument(
'-Y', '--summary',
action='store_true',
help="Only show the total.")
parser.add_argument(
'-F', '--source',
dest='sources',
Expand Down
55 changes: 27 additions & 28 deletions scripts/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,11 @@ def table(Result, results, diff_results=None, *,
by=None,
fields=None,
sort=None,
summary=False,
diff=None,
percent=None,
all=False,
percent=False,
compare=None,
summary=False,
**_):
all_, all = all, __builtins__.all

Expand Down Expand Up @@ -421,12 +422,12 @@ def table(Result, results, diff_results=None, *,
for k in fields)]

# find compare entry if there is one
if compare is not None:
if compare:
compare_result = table.get(','.join(str(k) for k in compare))

# sort again, now with diff info, note that python's sort is stable
names.sort()
if compare is not None:
if compare:
names.sort(
key=lambda n: (
table.get(n) == compare_result,
Expand All @@ -436,7 +437,7 @@ def table(Result, results, diff_results=None, *,
getattr(compare_result, k, None))
for k in fields)),
reverse=True)
if diff_results is not None:
if diff or percent:
names.sort(
key=lambda n: tuple(
types[k].ratio(
Expand Down Expand Up @@ -467,12 +468,9 @@ def table(Result, results, diff_results=None, *,
' (%d added, %d removed)' % (
sum(1 for n in table if n not in diff_table),
sum(1 for n in diff_table if n not in table))
if diff_results is not None and not percent else '')
if diff else '')
if not summary else '']
if diff_results is None:
for k in fields:
header.append(k)
elif percent:
if not diff:
for k in fields:
header.append(k)
else:
Expand All @@ -489,15 +487,16 @@ def table_entry(name, r, diff_r=None):
entry = [name]
# normal entry?
if ((compare is None or r == compare_result)
and diff_results is None):
and not percent
and not diff):
for k in fields:
entry.append(
(getattr(r, k).table(),
getattr(getattr(r, k), 'notes', lambda: [])())
if getattr(r, k, None) is not None
else types[k].none)
# compare entry?
elif compare is not None and diff_results is None:
elif not percent and not diff:
for k in fields:
entry.append(
(getattr(r, k).table()
Expand All @@ -510,7 +509,7 @@ def table_entry(name, r, diff_r=None):
getattr(r, k, None),
getattr(compare_result, k, None)))))
# percent entry?
elif diff_results is not None and percent:
elif not diff:
for k in fields:
entry.append(
(getattr(r, k).table()
Expand All @@ -523,7 +522,7 @@ def table_entry(name, r, diff_r=None):
getattr(r, k, None),
getattr(diff_r, k, None)))))
# diff entry?
elif diff_results is not None:
else:
for k in fields:
entry.append(getattr(diff_r, k).table()
if getattr(diff_r, k, None) is not None
Expand All @@ -547,7 +546,7 @@ def table_entry(name, r, diff_r=None):
return entry

# entries
if not summary or compare:
if (not summary) or compare:
for name in names:
r = table.get(name)
if diff_results is None:
Expand All @@ -557,7 +556,7 @@ def table_entry(name, r, diff_r=None):
lines.append(table_entry(name, r, diff_r))

# total, unless we're comparing
if not (compare is not None and diff_results is None):
if not (compare and not percent and not diff):
r = next(iter(fold(Result, results, by=[])), None)
if diff_results is None:
diff_r = None
Expand Down Expand Up @@ -735,10 +734,11 @@ def main(gcda_paths, *,
else CovResult._fields)})

# find previous results?
if args.get('diff'):
diff_results = None
if args.get('diff') or args.get('percent'):
diff_results = []
try:
with openio(args['diff']) as f:
with openio(args.get('diff') or args.get('percent')) as f:
reader = csv.DictReader(f, restval='')
for r in reader:
# filter by matching defines
Expand Down Expand Up @@ -771,8 +771,7 @@ def main(gcda_paths, *,
annotate(CovResult, results, **args)
else:
# print table
table(CovResult, results,
diff_results if args.get('diff') else None,
table(CovResult, results, diff_results,
by=by if by is not None else ['function'],
fields=fields if fields is not None
else ['lines', 'branches'] if not hits
Expand Down Expand Up @@ -816,18 +815,22 @@ def main(gcda_paths, *,
parser.add_argument(
'-d', '--diff',
help="Specify CSV file to diff against.")
parser.add_argument(
'-p', '--percent',
help="Specify CSV file to diff against, but only show precentage "
"change, not a full diff.")
parser.add_argument(
'-a', '--all',
action='store_true',
help="Show all, not just the ones that changed.")
parser.add_argument(
'-p', '--percent',
action='store_true',
help="Only show percentage change, not a full diff.")
parser.add_argument(
'-c', '--compare',
type=lambda x: tuple(v.strip() for v in x.split(',')),
help="Compare results to the row matching this by pattern.")
parser.add_argument(
'-Y', '--summary',
action='store_true',
help="Only show the total.")
parser.add_argument(
'-b', '--by',
action='append',
Expand Down Expand Up @@ -864,10 +867,6 @@ def __call__(self, parser, namespace, value, option):
nargs='?',
action=AppendSort,
help="Sort by this field, but backwards.")
parser.add_argument(
'-Y', '--summary',
action='store_true',
help="Only show the total.")
parser.add_argument(
'-F', '--source',
dest='sources',
Expand Down
Loading

0 comments on commit ef3accc

Please sign in to comment.