Skip to content

Commit

Permalink
print progress as json
Browse files Browse the repository at this point in the history
  • Loading branch information
AngheloAlf committed Sep 25, 2024
1 parent 7420995 commit f355b2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/mapfile_parser/frontends/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from __future__ import annotations

import argparse
import json
from pathlib import Path

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

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

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

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

progress_stats.printStats(totalStats, progressPerFolder)
if print_json:
json_temp: dict[str, dict[str, int|float]] = {
"all": totalStats.asJsonEntry()
}
for folder, statsEntry in progressPerFolder.items():
json_temp[folder] = statsEntry.asJsonEntry()
print(json.dumps(json_temp, indent=4))
else:
progress_stats.printStats(totalStats, progressPerFolder)
return 0


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

exit(doProgress(mapPath, asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles, debugging=debugging))
exit(doProgress(mapPath, asmPath, nonmatchingsPath, pathIndex=pathIndex, checkFunctionFiles=checkFunctionFiles, print_json=print_json, debugging=debugging))

def addSubparser(subparser: argparse._SubParsersAction[argparse.ArgumentParser]):
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.")
Expand All @@ -50,5 +60,6 @@ def addSubparser(subparser: argparse._SubParsersAction[argparse.ArgumentParser])
parser.add_argument("-i", "--path-index", help="Specify the index to start reading the file paths. Defaults to 2", type=int, default=2)
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")
parser.add_argument("-d", "--debugging", help="Enable debugging prints. This option is deprecated", action="store_true")
parser.add_argument("-j", "--json", help="Print the stats as json instead of a human readable format.", action="store_true")

parser.set_defaults(func=processArguments)
7 changes: 7 additions & 0 deletions src/mapfile_parser/progress_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def getEntryAsStr(self, category: str, totalStats: ProgressStats, categoryColumn
def print(self, category: str, totalStats: ProgressStats, categoryColumnSize: int=28):
print(self.getEntryAsStr(category, totalStats, categoryColumnSize=categoryColumnSize))

def asJsonEntry(self) -> dict[str, int|float]:
return {
"decomped": self.decompedSize,
"total": self.total,
"percentage": round(self.decompedPercentage(), 4)
}


def printStats(totalStats: ProgressStats, progressPerFolder: dict[str, ProgressStats]):
ProgressStats.printHeader()
Expand Down

0 comments on commit f355b2c

Please sign in to comment.