mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 19:48:37 +00:00
571 lines
25 KiB
Python
571 lines
25 KiB
Python
"""Catalog the XML documents recovered from a carved RPF9 archive.
|
|
|
|
RPF9 stores a logical XML document as one or more zlib streams. A carve from ``rpf9_carve.py`` has an
|
|
``index.json`` plus one inflated ``<offset>.bin`` per stream, so ``gamedata_index.py`` can group the streams
|
|
back into payloads. This tool parses those payloads and writes an aggregate catalog:
|
|
|
|
python -I gamedata_xml.py <carve_dir> [--csv FILE] [--json FILE] [--check]
|
|
python -I gamedata_xml.py <file.xml> [<file.xml> ...] [--csv FILE] [--json FILE] [--check]
|
|
|
|
For every file it records the root element, element count, maximum depth, attribute names, ``$(name)`` and
|
|
``platform:/`` references, and include-like structures. For every root it records the file count, category,
|
|
element-name histogram, attribute histogram and reference targets. ``--check`` validates well-formedness and
|
|
separates rootless fragments, tail-only documents and malformed documents.
|
|
|
|
Only schema names, element/attribute names and reference tokens are written. Element text, attribute values,
|
|
``type``/``content`` values and entry paths are not. For a carve directory a row is identified by offset; an
|
|
individual file is identified by the basename that was passed. The output is a local work product: it must not
|
|
be committed or attached to an issue. The test fixtures are synthetic and contain no game data.
|
|
"""
|
|
import argparse
|
|
import csv
|
|
import io
|
|
import json
|
|
import os
|
|
import re
|
|
import statistics
|
|
import sys
|
|
import xml.etree.ElementTree as ET
|
|
from collections import Counter, defaultdict
|
|
|
|
ROOT = os.path.dirname(os.path.abspath(__file__))
|
|
if ROOT not in sys.path:
|
|
sys.path.insert(0, ROOT)
|
|
import gamedata_index as gi # noqa: E402
|
|
|
|
BOM = b"\xef\xbb\xbf"
|
|
MACRO_REF = re.compile(r"\$\(([A-Za-z_][\w.-]*)\)")
|
|
PLATFORM_REF = re.compile(r"platform:/([A-Za-z0-9_.-]+)?")
|
|
XI_INCLUDE = "{http://www.w3.org/2001/XInclude}include"
|
|
INCLUDE_ELEMENTS = ("fileList", "filename", "includedSets", "includedChangesets",
|
|
"changesetIncludeFlags", "loadPath")
|
|
|
|
# Category names and root membership follow the functional table in docs/gamedata.md. A category is a
|
|
# dispatch hint: the same root always belongs to one bucket, but a bucket is not a complete schema. Where a
|
|
# document straddles two labels, the table's bucket is kept so the file and root counts line up.
|
|
CATEGORY_ROOTS = {
|
|
"time, weather, sky, water": """
|
|
timecycle_modifier_data timecycle_keyframe_data CContentsOfWeatherXmlFile STS_Config CustomClouds
|
|
CScatteringTransmittance__FogVolumeListData FogVolumeProfileList OceanSimPresets
|
|
OceanWaveCalmingCurves EnvironmentalWaterDisturbManager ShoreWavePlacementData time_data
|
|
RegionTintMap
|
|
""",
|
|
"vehicles, tyres, handling data": """
|
|
CVehiclePoints CVehicleBoneOffsetsArray CVehiclePerformanceMaps CVehicleBaseRatingsAndEstimatesMap
|
|
CVehicleComponentSetManager CVehicleDistantLightData CVehiclePointsMeta CTyreDataSet CParkingSplines
|
|
CSnowTrackData CBreakableVehicleDoorLockManagerData CPaletteIndexedRecipeTintsArray
|
|
""",
|
|
"peds, schedules, appearance": """
|
|
CPedAccuracyManager CPedComponentSetManager CPedFormationInfoManager CPedHeadbobSettingsManager
|
|
CPedInventoryLoadOutManager CPedMoodConditionTreesManager CPedMoodSettingsManager
|
|
CPedMotivationInfoManager CPedScheduleInfoManager CPedSpeedDataBlock CPedTimeWarpSettingsManager
|
|
CPedVariationBodyTypeManager CPairedPedDataBlock CEnergyConfigInfos CIdlePreferenceManager
|
|
CUpperBodyIdleConditionManager CUpperBodyIdleSettingsManager CHeadbobConditionManager
|
|
CHealthConfigInfo sPedDamageModifiers sPedDistractionModifiers WalkAndTalkProfileData
|
|
STPedMotionPredictionSetContainer STPedNavigationPredictiontPresetContainer CBaitingInfo
|
|
CAnimalAnimationContexts CAnimalAttackBehaviours Relationships__GroupsData CMoodConditionManager
|
|
""",
|
|
"tuning, parameters, effects": """
|
|
CTuningFile CCoverProcessorTuning GridAtlasInfo PatchConstants CClimbTuneMgr CoverShadowTunables
|
|
LostTargetSearch__Tunables sCollisionEventScannerTuning hrsSimTuneMgr rage__fragTuneStruct
|
|
DiffractionProfiles GlintsProfiles LightAnimationPresets CGraphicsDefinitionsData
|
|
""",
|
|
"mount / content manifests": """
|
|
CDataFileMgr__ContentChangeSetDef CDataFileMgr__ContentDef
|
|
""",
|
|
"world, navigation, map": """
|
|
GameShape__Data__LocationNameLoopkup PathingStyleData CTrainConfigs CHazardNodes
|
|
MapState__Data__Templates aiNavIMapGroupData CLadderMetadataManager CLadderRailsMetadataManager
|
|
CDoorInteractionDataList CEntityComponentLodManagerData CGarageInitDataList
|
|
CSceneTerrainAnalyzerMgrDataInstance CScenicViewManager MaterialDataList TextureGridInfo
|
|
Sprinklers__RootData CProceduralInfo CFluidVesselManager CObjectComponentSetManager
|
|
""",
|
|
"combat and AI behaviour": """
|
|
CCombatInfoMgr CFiringPatternInfoManager CFleeDecisionMakerData Combat__GoalData__Store
|
|
CSearchPointOfInterestRuleSet CTacticalAnalysisDistributionStore
|
|
Modular__SM__AIBehaviour__LockControllerConfig Witnesses__Informations CTaskDataInfoManager
|
|
CDrivingStyleCollection CLassoDataManager CAimUpperBodyDataBlock SSetupData
|
|
""",
|
|
"animation, poses, weapons": """
|
|
WeaponAnimations__FullbodyControllerData WeaponAnimations__ConfigData CStandardPosesManager
|
|
CAnimSceneAttachSlotSetManager StudioConfigurationData rage__fwRetargetCoordinatorConfig
|
|
""",
|
|
"damage": "CDamageDataManager",
|
|
"audio, video, speech": """
|
|
LandingPageVideos CTVPlaylistContainer naSpeechEventTypeBatchRequestParams rage__MediaDecoderSettings
|
|
""",
|
|
"missions, scenarios, respawn": "CDynamicScenarioManager CMissions Respawn__MetaData",
|
|
"conversation": "CConversationList CConversationDictionary",
|
|
}
|
|
CATEGORY_BY_ROOT = {}
|
|
for _category, _roots in CATEGORY_ROOTS.items():
|
|
for _root in _roots.split():
|
|
CATEGORY_BY_ROOT[_root] = _category
|
|
UNCATALOGUED = "uncatalogued"
|
|
|
|
|
|
def category_for(root: str) -> str:
|
|
return CATEGORY_BY_ROOT.get(root, UNCATALOGUED)
|
|
|
|
|
|
def scan_refs(text: str, report: dict) -> None:
|
|
"""Count symbolic macros and platform prefixes in one text or attribute value."""
|
|
if not text:
|
|
return
|
|
if "$(" in text:
|
|
for match in MACRO_REF.finditer(text):
|
|
report["refs"]["$(" + match.group(1) + ")"] += 1
|
|
if "platform:/" in text:
|
|
for match in PLATFORM_REF.finditer(text):
|
|
# Keep the bucket coarse: the first segment after a platform prefix can be a directory or a
|
|
# file-like archive name, and only levels/other is needed to route a mount.
|
|
report["platform"]["levels" if (match.group(1) or "").lower() == "levels" else "other"] += 1
|
|
report["platform_total"] += 1
|
|
|
|
|
|
def scan_xml(data: bytes) -> dict:
|
|
"""Parse one document and return aggregate facts. Never keeps element text or values."""
|
|
report = {
|
|
"root": "",
|
|
"well_formed": False,
|
|
"error": "",
|
|
"element_count": 0,
|
|
"max_depth": 0,
|
|
"attributes": [],
|
|
"comments": 0,
|
|
"elements": Counter(),
|
|
"attrs": Counter(),
|
|
"refs": Counter(),
|
|
"platform": Counter(),
|
|
"platform_total": 0,
|
|
"filename": 0,
|
|
"parent_path": 0,
|
|
"parent_file": 0,
|
|
"xi_include": 0,
|
|
"type_attrs": 0,
|
|
"type_values": set(),
|
|
"content_values": set(),
|
|
}
|
|
depth = 0
|
|
try:
|
|
parser = ET.iterparse(io.BytesIO(data), events=("start", "end", "comment"))
|
|
for event, elem in parser:
|
|
if event == "comment":
|
|
report["comments"] += 1
|
|
elif event == "start":
|
|
depth += 1
|
|
report["max_depth"] = max(report["max_depth"], depth)
|
|
if not report["root"]:
|
|
report["root"] = elem.tag
|
|
report["element_count"] += 1
|
|
report["elements"][elem.tag] += 1
|
|
for name, value in elem.attrib.items():
|
|
report["attrs"][name] += 1
|
|
if name == "type":
|
|
report["type_attrs"] += 1
|
|
report["type_values"].add(value)
|
|
elif name == "content":
|
|
report["content_values"].add(value)
|
|
elif name == "parentPath":
|
|
report["parent_path"] += 1
|
|
elif name == "parentFile":
|
|
report["parent_file"] += 1
|
|
scan_refs(value, report)
|
|
if elem.tag == "filename":
|
|
report["filename"] += 1
|
|
if elem.tag == XI_INCLUDE or elem.tag == "xi:include":
|
|
report["xi_include"] += 1
|
|
else:
|
|
scan_refs(elem.text, report)
|
|
scan_refs(elem.tail, report)
|
|
depth -= 1
|
|
elem.clear()
|
|
report["well_formed"] = True
|
|
except ET.ParseError as exc:
|
|
# ET errors carry a line/column and a short reason, never the element content.
|
|
report["error"] = str(exc)
|
|
report["attributes"] = sorted(report["attrs"])
|
|
return report
|
|
|
|
|
|
def looks_truncated(data: bytes, root: str) -> bool:
|
|
"""True when the bytes start inside a document instead of at its root element."""
|
|
if not root:
|
|
return False
|
|
body = data[3:] if data.startswith(BOM) else data
|
|
body = body.lstrip(b" \t\r\n")
|
|
if body.startswith(b"<?xml"):
|
|
return False
|
|
return gi.xml_root(data) != root
|
|
|
|
|
|
def file_row(doc: dict, scan: dict) -> dict:
|
|
# A truncated tail can begin with an inner element, so the index's closing-root hint wins.
|
|
root = doc.get("root_hint") or scan["root"]
|
|
header = doc.get("header")
|
|
if header is None:
|
|
header = gi.has_xml_decl(doc["data"])
|
|
if doc.get("from_carve"):
|
|
tail_only = not header
|
|
fragment = not root or not doc.get("complete", True)
|
|
else:
|
|
tail_only = not scan["well_formed"] and looks_truncated(doc["data"], root)
|
|
fragment = not root
|
|
malformed = not scan["well_formed"] and not tail_only and not fragment
|
|
row = {
|
|
"source": doc.get("name", ""),
|
|
"offset": doc.get("offset"),
|
|
"offset_hex": doc.get("offset_hex", ""),
|
|
"bytes": len(doc["data"]),
|
|
"chunks": doc.get("chunks", 1),
|
|
"root": root,
|
|
"category": category_for(root) if root else UNCATALOGUED,
|
|
"header": bool(header),
|
|
"complete": doc.get("complete", scan["well_formed"]),
|
|
"well_formed": scan["well_formed"],
|
|
"fragment": fragment,
|
|
"tail_only": tail_only and not fragment,
|
|
"truncated": tail_only and not fragment and not scan["well_formed"],
|
|
"malformed": malformed,
|
|
"error": scan["error"],
|
|
"element_count": scan["element_count"] if scan["well_formed"] else None,
|
|
"max_depth": scan["max_depth"] if scan["well_formed"] else None,
|
|
"comments": scan["comments"],
|
|
"attributes": scan["attributes"],
|
|
"refs": dict(sorted(scan["refs"].items())),
|
|
"platform": dict(sorted(scan["platform"].items())),
|
|
"platform_total": scan["platform_total"],
|
|
"filename": scan["filename"],
|
|
"parent_path": scan["parent_path"],
|
|
"parent_file": scan["parent_file"],
|
|
"xi_include": scan["xi_include"],
|
|
"type_attrs": scan["type_attrs"],
|
|
"type_values": len(scan["type_values"]),
|
|
"content_values": len(scan["content_values"]),
|
|
}
|
|
row["_scan"] = scan
|
|
return row
|
|
|
|
|
|
def new_root_stats(root: str) -> dict:
|
|
return {
|
|
"root": root,
|
|
"category": category_for(root) if root else UNCATALOGUED,
|
|
"files": 0,
|
|
"well_formed": 0,
|
|
"fragments": 0,
|
|
"tail_only": 0,
|
|
"truncated": 0,
|
|
"malformed": 0,
|
|
"bytes": 0,
|
|
"elements": 0,
|
|
"max_depth": 0,
|
|
"element_names": Counter(),
|
|
"attributes": Counter(),
|
|
"refs": Counter(),
|
|
"platform": Counter(),
|
|
"platform_total": 0,
|
|
"filename": 0,
|
|
"parent_path": 0,
|
|
"parent_file": 0,
|
|
"xi_include": 0,
|
|
"type_attrs": 0,
|
|
"type_values": set(),
|
|
"content_values": set(),
|
|
}
|
|
|
|
|
|
def aggregate(rows: list) -> dict:
|
|
roots = {}
|
|
for row in rows:
|
|
stats = roots.setdefault(row["root"], new_root_stats(row["root"]))
|
|
stats["files"] += 1
|
|
stats["bytes"] += row["bytes"]
|
|
stats["fragments"] += int(row["fragment"])
|
|
stats["tail_only"] += int(row["tail_only"])
|
|
stats["truncated"] += int(row["truncated"])
|
|
stats["malformed"] += int(row["malformed"])
|
|
if not row["well_formed"]:
|
|
continue
|
|
scan = row["_scan"]
|
|
stats["well_formed"] += 1
|
|
stats["elements"] += scan["element_count"]
|
|
stats["max_depth"] = max(stats["max_depth"], scan["max_depth"])
|
|
stats["element_names"].update(scan["elements"])
|
|
stats["attributes"].update(scan["attrs"])
|
|
stats["refs"].update(scan["refs"])
|
|
stats["platform"].update(scan["platform"])
|
|
stats["platform_total"] += scan["platform_total"]
|
|
stats["filename"] += scan["filename"]
|
|
stats["parent_path"] += scan["parent_path"]
|
|
stats["parent_file"] += scan["parent_file"]
|
|
stats["xi_include"] += scan["xi_include"]
|
|
stats["type_attrs"] += scan["type_attrs"]
|
|
stats["type_values"].update(scan["type_values"])
|
|
stats["content_values"].update(scan["content_values"])
|
|
|
|
total_elements = Counter()
|
|
total_attributes = Counter()
|
|
total_refs = Counter()
|
|
total_platform = Counter()
|
|
all_type_values = set()
|
|
all_content_values = set()
|
|
by_category = defaultdict(lambda: {"files": 0, "roots": 0, "elements": 0, "bytes": 0})
|
|
for root, stats in roots.items():
|
|
total_elements.update(stats["element_names"])
|
|
total_attributes.update(stats["attributes"])
|
|
total_refs.update(stats["refs"])
|
|
total_platform.update(stats["platform"])
|
|
all_type_values.update(stats["type_values"])
|
|
all_content_values.update(stats["content_values"])
|
|
cat = by_category[stats["category"]]
|
|
cat["files"] += stats["files"]
|
|
cat["roots"] += 1
|
|
cat["elements"] += stats["elements"]
|
|
cat["bytes"] += stats["bytes"]
|
|
|
|
for stats in roots.values():
|
|
stats["element_names"] = dict(sorted(stats["element_names"].items(), key=lambda kv: (-kv[1], kv[0])))
|
|
stats["attributes"] = dict(sorted(stats["attributes"].items(), key=lambda kv: (-kv[1], kv[0])))
|
|
stats["refs"] = dict(sorted(stats["refs"].items()))
|
|
stats["platform"] = dict(sorted(stats["platform"].items()))
|
|
stats["type_values"] = len(stats["type_values"])
|
|
stats["content_values"] = len(stats["content_values"])
|
|
|
|
ordered_roots = dict(sorted(roots.items(), key=lambda kv: (-kv[1]["files"], kv[0])))
|
|
return {
|
|
"roots": ordered_roots,
|
|
"elements": total_elements,
|
|
"attributes": total_attributes,
|
|
"refs": total_refs,
|
|
"platform": total_platform,
|
|
"type_values": len(all_type_values),
|
|
"content_kinds": len(all_content_values),
|
|
"categories": dict(sorted(by_category.items())),
|
|
}
|
|
|
|
|
|
def summarize(rows: list, agg: dict, mode: str) -> dict:
|
|
sizes = sorted(row["bytes"] for row in rows)
|
|
header_sizes = sorted(row["bytes"] for row in rows if row["header"])
|
|
return {
|
|
"mode": mode,
|
|
"documents": len(rows),
|
|
"well_formed": sum(1 for row in rows if row["well_formed"]),
|
|
"fragments": sum(1 for row in rows if row["fragment"]),
|
|
"tail_only": sum(1 for row in rows if row["tail_only"]),
|
|
"truncated": sum(1 for row in rows if row["truncated"]),
|
|
"malformed": sum(1 for row in rows if row["malformed"]),
|
|
"root_count": sum(1 for root in agg["roots"] if root),
|
|
"unattributed": int("" in agg["roots"]),
|
|
"catalog_roots": sum(1 for root in agg["roots"] if root in CATEGORY_BY_ROOT),
|
|
"elements": sum(agg["elements"].values()),
|
|
"element_names": len(agg["elements"]),
|
|
"attributes": len(agg["attributes"]),
|
|
"max_depth": max((stats["max_depth"] for stats in agg["roots"].values()), default=0),
|
|
"comments": sum(row["comments"] for row in rows),
|
|
"comments_header": sum(row["comments"] for row in rows if row["header"]),
|
|
"header_documents": len(header_sizes),
|
|
"type_values": agg["type_values"],
|
|
"content_kinds": agg["content_kinds"],
|
|
"bytes": sum(sizes),
|
|
"min_bytes": sizes[0] if sizes else 0,
|
|
"median_bytes": int(statistics.median(sizes)) if sizes else 0,
|
|
"max_bytes": sizes[-1] if sizes else 0,
|
|
"header_bytes": {
|
|
"min": header_sizes[0] if header_sizes else 0,
|
|
"median": int(statistics.median(header_sizes)) if header_sizes else 0,
|
|
"max": header_sizes[-1] if header_sizes else 0,
|
|
},
|
|
"references": dict(sorted(agg["refs"].items())),
|
|
"platform": dict(sorted(agg["platform"].items())),
|
|
"platform_total": sum(agg["platform"].values()),
|
|
"filename": sum(stats["filename"] for stats in agg["roots"].values()),
|
|
"parent_path": sum(stats["parent_path"] for stats in agg["roots"].values()),
|
|
"parent_file": sum(stats["parent_file"] for stats in agg["roots"].values()),
|
|
"xi_include": sum(stats["xi_include"] for stats in agg["roots"].values()),
|
|
"include_like": {name: agg["elements"].get(name, 0) for name in INCLUDE_ELEMENTS},
|
|
"categories": agg["categories"],
|
|
"catalog": {
|
|
"roots": len(CATEGORY_BY_ROOT),
|
|
"categories": len(CATEGORY_ROOTS),
|
|
},
|
|
}
|
|
|
|
|
|
def carve_documents(directory: str):
|
|
"""Yield the XML payloads of an rpf9_carve.py directory, rebuilt by gamedata_index."""
|
|
source = gi.DirectorySource(directory)
|
|
rows = gi.build_rows(source.rows, source)
|
|
by_offset = {row["off"]: index for index, row in enumerate(source.rows)}
|
|
for row in rows:
|
|
if row["type"] != "xml":
|
|
continue
|
|
start = by_offset[row["offset"]]
|
|
data = b"".join(source.read(source.rows[index]) for index in range(start, start + row["chunks"]))
|
|
yield {
|
|
"data": data,
|
|
"from_carve": True,
|
|
"offset": row["offset"],
|
|
"offset_hex": row["offset_hex"],
|
|
"chunks": row["chunks"],
|
|
"header": row["header"],
|
|
"complete": row["complete"],
|
|
"root_hint": row["root"],
|
|
}
|
|
|
|
|
|
def xml_files(directory: str):
|
|
for base, _, names in os.walk(directory):
|
|
for name in sorted(names):
|
|
if name.lower().endswith(".xml"):
|
|
yield os.path.join(base, name)
|
|
|
|
|
|
def document_mode(targets: list) -> str:
|
|
for target in targets:
|
|
if os.path.isdir(target) and os.path.isfile(os.path.join(target, "index.json")):
|
|
return "carve"
|
|
return "files"
|
|
|
|
|
|
def load_documents(targets: list):
|
|
"""Yield one document per XML payload. Directories with index.json use the carve path."""
|
|
for target in targets:
|
|
if os.path.isdir(target):
|
|
if os.path.isfile(os.path.join(target, "index.json")):
|
|
for doc in carve_documents(target):
|
|
yield doc
|
|
else:
|
|
for path in xml_files(target):
|
|
yield file_document(path)
|
|
elif os.path.isfile(target):
|
|
yield file_document(target)
|
|
else:
|
|
raise FileNotFoundError(target)
|
|
|
|
|
|
def file_document(path: str) -> dict:
|
|
with open(path, "rb") as f:
|
|
data = f.read()
|
|
root = gi.tail_root(data) or gi.xml_root(data)
|
|
return {
|
|
"data": data,
|
|
"from_carve": False,
|
|
"name": os.path.basename(path),
|
|
"root_hint": root,
|
|
}
|
|
|
|
|
|
def write_csv(path: str, rows: list) -> None:
|
|
fields = ["source", "offset_hex", "bytes", "chunks", "root", "category", "header", "complete",
|
|
"well_formed", "fragment", "tail_only", "truncated", "malformed", "element_count",
|
|
"max_depth", "comments", "attributes", "refs", "platform", "platform_total", "filename",
|
|
"parent_path", "parent_file", "xi_include", "type_attrs", "type_values", "content_values",
|
|
"error"]
|
|
with open(path, "w", newline="", encoding="utf-8") as f:
|
|
writer = csv.DictWriter(f, fieldnames=fields, extrasaction="ignore")
|
|
writer.writeheader()
|
|
for row in rows:
|
|
out = dict(row)
|
|
out["attributes"] = " ".join(row["attributes"])
|
|
out["refs"] = ";".join(f"{key}={value}" for key, value in row["refs"].items())
|
|
out["platform"] = ";".join(f"{key}={value}" for key, value in row["platform"].items())
|
|
for flag in ("header", "complete", "well_formed", "fragment", "tail_only", "truncated",
|
|
"malformed"):
|
|
out[flag] = str(row[flag]).lower()
|
|
writer.writerow(out)
|
|
|
|
|
|
def plural(count: int, word: str) -> str:
|
|
return f"{count} {word}" if count == 1 else f"{count} {word}s"
|
|
|
|
|
|
def check_report(rows: list) -> bool:
|
|
"""Print the validation split. Return True when no complete document is malformed."""
|
|
fragments = sum(1 for row in rows if row["fragment"])
|
|
print(f"check: {sum(1 for row in rows if row['well_formed'])} well-formed, "
|
|
f"{sum(1 for row in rows if row['truncated'])} truncated at the start, "
|
|
f"{plural(fragments, 'rootless fragment')}, "
|
|
f"{sum(1 for row in rows if row['malformed'])} malformed")
|
|
for row in rows:
|
|
if not (row["truncated"] or row["fragment"] or row["malformed"]):
|
|
continue
|
|
where = row["offset_hex"] or row["source"] or "?"
|
|
if row["malformed"]:
|
|
print(f" malformed {row['root'] or '?'} @ {where}: {row['error']}")
|
|
elif row["truncated"]:
|
|
print(f" truncated {row['root']} @ {where}: starts mid-document")
|
|
else:
|
|
print(f" fragment @ {where}: no root element")
|
|
return not any(row["malformed"] for row in rows)
|
|
|
|
|
|
def print_summary(summary: dict, agg: dict) -> None:
|
|
docs = summary["documents"]
|
|
print(f'{docs} XML payloads, {summary["bytes"]:,} bytes '
|
|
f'(min {summary["min_bytes"]:,}, median {summary["median_bytes"]:,}, '
|
|
f'max {summary["max_bytes"]:,})')
|
|
print(f'{summary["well_formed"]} well-formed, {summary["tail_only"]} tail-only '
|
|
f'({summary["truncated"]} truncated), {plural(summary["fragments"], "rootless fragment")}, '
|
|
f'{summary["malformed"]} malformed')
|
|
print(f'{summary["root_count"]} named roots (+{summary["unattributed"]} unattributed), '
|
|
f'{summary["elements"]:,} elements, {summary["element_names"]:,} element names, '
|
|
f'{summary["attributes"]} attributes, max depth {summary["max_depth"]}, '
|
|
f'{summary["type_values"]} type values, {summary["content_kinds"]} content kinds')
|
|
print(f'{summary["comments"]} comment starts '
|
|
f'({summary["comments_header"]} in {summary["header_documents"]} declaration-bearing documents)')
|
|
refs = ", ".join(f"{key} {value:,}" for key, value in summary["references"].items())
|
|
platforms = ", ".join(f"{key} {value:,}" for key, value in summary["platform"].items())
|
|
print(f"references: {summary['platform_total']:,} platform:/ ({platforms}); {refs}")
|
|
print(f'include-like: {summary["filename"]:,} filename, {summary["parent_path"]} parentPath, '
|
|
f'{summary["parent_file"]} parentFile, {summary["xi_include"]} xi:include')
|
|
print("categories:")
|
|
for name, stats in summary["categories"].items():
|
|
print(f' {plural(stats["files"], "file"):8} {plural(stats["roots"], "root"):8} '
|
|
f'{stats["elements"]:7,} elements {name}')
|
|
print("roots:")
|
|
for root, stats in list(agg["roots"].items())[:10]:
|
|
if not root:
|
|
continue
|
|
print(f' {plural(stats["files"], "file"):8} {stats["elements"]:7,} elements '
|
|
f'{root} [{stats["category"]}]')
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
parser.add_argument("targets", nargs="+", help="carve directory or XML file(s)")
|
|
parser.add_argument("--csv", help="write one row per XML document to this CSV")
|
|
parser.add_argument("--json", help="write the summary, per-root catalog and rows to this JSON")
|
|
parser.add_argument("--check", action="store_true", help="validate well-formedness and report fragments")
|
|
args = parser.parse_args()
|
|
|
|
rows = []
|
|
mode = document_mode(args.targets)
|
|
try:
|
|
for doc in load_documents(args.targets):
|
|
rows.append(file_row(doc, scan_xml(doc["data"])))
|
|
except FileNotFoundError as exc:
|
|
print(f"not found: {exc}", file=sys.stderr)
|
|
return 2
|
|
|
|
agg = aggregate(rows)
|
|
summary = summarize(rows, agg, mode)
|
|
clean = True
|
|
if args.check:
|
|
clean = check_report(rows)
|
|
print_summary(summary, agg)
|
|
if args.csv:
|
|
write_csv(args.csv, rows)
|
|
print("wrote", args.csv)
|
|
if args.json:
|
|
public_rows = [{key: value for key, value in row.items() if key != "_scan"} for row in rows]
|
|
with open(args.json, "w", encoding="utf-8") as f:
|
|
json.dump({"summary": summary, "roots": agg["roots"], "rows": public_rows}, f, indent=1)
|
|
print("wrote", args.json)
|
|
return 0 if clean else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|