Skip to content

Commit f355b2c

Browse files
committed
print progress as json
1 parent 7420995 commit f355b2c

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/mapfile_parser/frontends/progress.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from __future__ import annotations
77

88
import argparse
9+
import json
910
from pathlib import Path
1011

1112
from .. import mapfile
@@ -20,14 +21,22 @@ def getProgress(mapPath: Path, asmPath: Path, nonmatchingsPath: Path, pathIndex:
2021

2122
return mapFile.filterBySectionType(".text").fixupNonMatchingSymbols().getProgress(asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles)
2223

23-
def doProgress(mapPath: Path, asmPath: Path, nonmatchingsPath: Path, pathIndex: int=2, checkFunctionFiles: bool=True, debugging: bool=False) -> int:
24+
def doProgress(mapPath: Path, asmPath: Path, nonmatchingsPath: Path, pathIndex: int=2, checkFunctionFiles: bool=True, print_json: bool=False, debugging: bool=False) -> int:
2425
if not mapPath.exists():
2526
print(f"Could not find mapfile at '{mapPath}'")
2627
return 1
2728

2829
totalStats, progressPerFolder = getProgress(mapPath, asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles, debugging=debugging)
2930

30-
progress_stats.printStats(totalStats, progressPerFolder)
31+
if print_json:
32+
json_temp: dict[str, dict[str, int|float]] = {
33+
"all": totalStats.asJsonEntry()
34+
}
35+
for folder, statsEntry in progressPerFolder.items():
36+
json_temp[folder] = statsEntry.asJsonEntry()
37+
print(json.dumps(json_temp, indent=4))
38+
else:
39+
progress_stats.printStats(totalStats, progressPerFolder)
3140
return 0
3241

3342

@@ -38,8 +47,9 @@ def processArguments(args: argparse.Namespace):
3847
pathIndex: int = args.path_index
3948
checkFunctionFiles: bool = not args.avoid_function_files
4049
debugging: bool = args.debugging #! @deprecated
50+
print_json: bool = args.json
4151

42-
exit(doProgress(mapPath, asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles, debugging=debugging))
52+
exit(doProgress(mapPath, asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles, print_json=print_json, debugging=debugging))
4353

4454
def addSubparser(subparser: argparse._SubParsersAction[argparse.ArgumentParser]):
4555
parser = subparser.add_parser("progress", help="Computes current progress of the matched functions. Relies on a splat (https://github.com/ethteck/splat) folder structure and matched functions not longer having a file.")
@@ -50,5 +60,6 @@ def addSubparser(subparser: argparse._SubParsersAction[argparse.ArgumentParser])
5060
parser.add_argument("-i", "--path-index", help="Specify the index to start reading the file paths. Defaults to 2", type=int, default=2)
5161
parser.add_argument("-f", "--avoid-function-files", help="Avoid checking if the assembly file for a function exists as a way to determine if the function has been matched or not", action="store_true")
5262
parser.add_argument("-d", "--debugging", help="Enable debugging prints. This option is deprecated", action="store_true")
63+
parser.add_argument("-j", "--json", help="Print the stats as json instead of a human readable format.", action="store_true")
5364

5465
parser.set_defaults(func=processArguments)

src/mapfile_parser/progress_stats.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ def getEntryAsStr(self, category: str, totalStats: ProgressStats, categoryColumn
5151
def print(self, category: str, totalStats: ProgressStats, categoryColumnSize: int=28):
5252
print(self.getEntryAsStr(category, totalStats, categoryColumnSize=categoryColumnSize))
5353

54+
def asJsonEntry(self) -> dict[str, int|float]:
55+
return {
56+
"decomped": self.decompedSize,
57+
"total": self.total,
58+
"percentage": round(self.decompedPercentage(), 4)
59+
}
60+
5461

5562
def printStats(totalStats: ProgressStats, progressPerFolder: dict[str, ProgressStats]):
5663
ProgressStats.printHeader()

0 commit comments

Comments
 (0)