#!/usr/bin/env python3 """ProjectTrans.py - 工程级 TransPyC 翻译器""" import lib._bootstrap # noqa: F401 设置 sys.path(项目根 + lib) from lib.Projectrans import ( compute_sha1, _check_annotation_for_export, sha1_file, get_file_dependencies, find_reachable_files_from_entries, topological_sort_files, Phase1Generator, DeclarationGenerator, Phase2Translator, _parallel_translate_worker, save_sha1_map, Load_sha1_map, get_sha1_map_store, clear_sha1_map_store, Load_project_config, resolve_paths, main ) __all__ = [ 'compute_sha1', '_check_annotation_for_export', 'sha1_file', 'get_file_dependencies', 'find_reachable_files_from_entries', 'topological_sort_files', 'Phase1Generator', 'DeclarationGenerator', 'Phase2Translator', '_parallel_translate_worker', 'save_sha1_map', 'Load_sha1_map', 'get_sha1_map_store', 'clear_sha1_map_store', 'Load_project_config', 'resolve_paths', 'main' ] if __name__ == '__main__': # 主进程性能剖析:整次编译过程(Phase1 声明生成 + Phase2 翻译/编译/链接) # 自动导出 cProfile dump 到 ./PROF/<启动时间>/main_prof_{pid}.prof(与 worker # 子进程 worker_prof_*.prof 同目录)。主进程 profile 主要 # 覆盖 Phase1/依赖扫描/编译调度/链接等主进程侧耗时;worker 内部 CPU 分布 # 仍由 _ProfileWorker 的 worker_prof_*.prof 覆盖(多进程下主进程 cProfile # 看不到子进程内部热点,两者互补)。 import cProfile as _cProfile import os as _os import sys as _sys import time as _time from lib.Projectrans.Phase2Translator import GetProfRunDir as _GetProfRunDir from lib.Projectrans.Phase2Translator import ReportedCompileError as _ReportedCompileError _main_prof: _cProfile.Profile = _cProfile.Profile() _main_prof.enable() _start_ts: float = _time.perf_counter() try: main() _timing_status: str = '成功' except _ReportedCompileError: # 完整错误(含源码定位)已由主进程打印过一次:这里只置失败并退出, # 不再重复输出同一错误。 _timing_status = '失败' raise SystemExit(1) except (TypeError, SyntaxError) as _e: _timing_status = '失败' import re as _re _lines: list[str] = ['Traceback (most recent call last, compile-time):'] _file: str = getattr(_e, 'Filename', '') or '' _lineno: int = 0 _m = _re.search(r'(?:第\s*|line\s*)(\d+)', str(_e)) if _m: _lineno = int(_m.group(1)) if _file: _lines.append(f' File "{_file}", line {_lineno}, in ') try: with open(_file, 'r', encoding='utf-8') as _f: _src: list[str] = _f.read().splitlines() if 0 < _lineno <= len(_src): _lines.append(f' {_src[_lineno - 1].strip()}') except Exception: pass _lines.append(f'Err: {_e}') _sys.stderr.write('\n'.join(_lines) + '\n') raise SystemExit(1) except BaseException: _timing_status = '失败' raise finally: _main_prof.disable() _elapsed_s: float = _time.perf_counter() - _start_ts _timing_msg: str = f'[TIMING] 编译{_timing_status},总耗时 {_elapsed_s:.2f}s' print(_timing_msg, file=_sys.stderr) try: _prof_dir: str = _GetProfRunDir() _os.makedirs(_prof_dir, exist_ok=True) _main_prof.dump_stats(_os.path.join(_prof_dir, f'main_prof_{_os.getpid()}.prof')) with open(_os.path.join(_prof_dir, 'main_time.txt'), 'w', encoding='utf-8') as _tf: _tf.write(f'{_timing_status}\t{_elapsed_s:.2f}\n') except Exception: pass