import platform import sys from pathlib import Path import subprocess # Keep read-only diagnostics from leaving bytecode in the distribution. sys.dont_write_bytecode = True from drama3_small_asmr.config import GENERATION_PLATFORM_ERROR, generation_platform SPATIAL = {"spatial-say-file": "say-file", "spatial-apply": "apply", "spatial-check": "check"} def main(argv=None): args = list(sys.argv[1:] if argv is None else argv) # The global cache option is accepted before or after `doctor`. action = 0 if args and args[0] == "--cache" and len(args) >= 3: action = 2 elif args and args[0].startswith("--cache="): action = 1 if args[action:action + 1] == ["doctor"]: from drama3_small_asmr.doctor import main as doctor for stream in (sys.stdout, sys.stderr): if hasattr(stream, "reconfigure"): stream.reconfigure(errors="backslashreplace") return doctor(args[:action] + args[action + 1:]) if action < len(args) and args[action] in SPATIAL: command = SPATIAL[args[action]] forwarded = args[action + 1:] end = forwarded.index("--") if "--" in forwarded else len(forwarded) show_help = bool({"-h", "--help"}.intersection(forwarded[:end])) if command == "say-file" and not generation_platform() and not show_help: print(f"エラー: {GENERATION_PLATFORM_ERROR}", file=sys.stderr) return 1 entry = Path(__file__).resolve().parent / "spatial" / "run_spatial.py" for source in (entry, entry.with_name("spatial_wrap.py")): if not source.is_file(): print(f"エラー: spatial/{source.name} がありません。" "配布リポジトリ全体を同じフォルダーに取得し直してください。", file=sys.stderr) return 2 try: return subprocess.run([sys.executable, "-B", "-X", "utf8", str(entry), command, *forwarded]).returncode except KeyboardInterrupt: print("中断しました。", file=sys.stderr) return 130 except OSError as error: print(f"エラー: 位置の処理を起動できません: {error}", file=sys.stderr) return 1 if platform.system() == "Windows" and args[action:action + 1] in ( ["setup"], ["say"], ["say-file"], ["serve"], ["selftest"], ["stop"] ) and not {"-h", "--help"}.intersection(args[action + 1:]): print(f"エラー: {GENERATION_PLATFORM_ERROR}", file=sys.stderr) return 1 try: from drama3_small_asmr.cli import main as speech except ModuleNotFoundError as error: if error.name != "numpy": raise print("エラー: NumPy がありません。同じ Python に python3 -m pip install 'numpy>=1.24' で導入してください。", file=sys.stderr) return 1 return speech(args) if __name__ == "__main__": raise SystemExit(main())