extract_low_level.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import argparse
  2. import sys
  3. import subprocess
  4. import json
  5. from pathlib import Path
  6. # Add the parent directory to the Python path to find utils
  7. utils_path = Path(__file__).parent.parent / 'utils'
  8. sys.path.append(str(utils_path))
  9. from config_utils import load_config
  10. def run_subprocess(script_name, input_dir, output_dir, indent=None, shrink=False, ignored_folders=None):
  11. """Helper function to run a parser subprocess and stream its output."""
  12. script_path = Path(__file__).parent.parent / "parsers" / script_name
  13. command = [
  14. sys.executable,
  15. str(script_path),
  16. "--input", str(input_dir),
  17. "--output", str(output_dir)
  18. ]
  19. if indent is not None:
  20. command.extend(["--indent", str(indent)])
  21. if shrink:
  22. command.append("--shrink-json")
  23. if ignored_folders:
  24. command.extend(["--ignored-folders", json.dumps(ignored_folders)])
  25. # For logging, create a display-friendly version of the command
  26. display_command = ' '.join(f'"{c}"' if ' ' in c else c for c in command)
  27. print(f"--> Executing: {display_command}")
  28. try:
  29. # Use Popen to stream output in real-time
  30. process = subprocess.Popen(
  31. command,
  32. stdout=subprocess.PIPE,
  33. stderr=subprocess.PIPE,
  34. text=True,
  35. encoding='utf-8',
  36. bufsize=1 # Line-buffered
  37. )
  38. # Stream stdout
  39. for line in process.stdout:
  40. print(line, end='')
  41. # Stream stderr
  42. for line in process.stderr:
  43. print(line, end='', file=sys.stderr)
  44. process.wait() # Wait for the subprocess to finish
  45. if process.returncode != 0:
  46. print(f"--- ERROR in {script_name} completed with exit code {process.returncode} ---", file=sys.stderr)
  47. except FileNotFoundError:
  48. print(f"--- ERROR: Script not found at {script_path} ---", file=sys.stderr)
  49. except Exception as e:
  50. print(f"--- An unexpected error occurred while running {script_name}: {e} ---", file=sys.stderr)
  51. def main():
  52. """
  53. Main function to orchestrate the low-level data extraction pipeline.
  54. """
  55. parser = argparse.ArgumentParser(
  56. description="Orchestrates a pipeline of parsers for a detailed data breakdown."
  57. )
  58. parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
  59. parser.add_argument("--output", type=str, required=True, help="The directory where the generated output folder will be saved.")
  60. args = parser.parse_args()
  61. # --- Load Configuration ---
  62. config = load_config()
  63. ignored_folders = config.get('ignored_folders', [])
  64. shrink_json = config.get('shrink_json', False)
  65. indent_level = config.get('indentation_level', 4)
  66. input_dir = Path(args.input).resolve()
  67. output_dir = Path(args.output).resolve()
  68. if not input_dir.is_dir():
  69. print(f"Error: Input path '{input_dir}' is not a valid directory.", file=sys.stderr)
  70. sys.exit(1)
  71. low_level_output_dir = output_dir / "LowLevel"
  72. low_level_output_dir.mkdir(parents=True, exist_ok=True)
  73. print(f"Output will be saved to: {low_level_output_dir}")
  74. # --- Run Extraction Pipeline ---
  75. print("\n--- Running Low-Level Extraction Pipeline ---")
  76. print("\n[1/5] Starting: Copy scripts...")
  77. run_subprocess("copy_scripts.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
  78. print("--> Finished: Copy scripts.")
  79. print("\n[2/5] Starting: Copy shaders...")
  80. run_subprocess("copy_shaders.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
  81. print("--> Finished: Copy shaders.")
  82. print("\n[3/5] Starting: Parse project settings...")
  83. run_subprocess("parse_project_settings.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
  84. print("--> Finished: Parse project settings.")
  85. print("\n[4/5] Starting: Parse generic assets...")
  86. run_subprocess("parse_generic_assets.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
  87. print("--> Finished: Parse generic assets.")
  88. print("\n[5/5] Starting: Parse scenes and prefabs...")
  89. run_subprocess("parse_scenes_and_prefabs.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
  90. print("--> Finished: Parse scenes and prefabs.")
  91. print("\nLow-level extraction pipeline complete.")
  92. if __name__ == "__main__":
  93. main()