LunaTranslator/.github/scripts/collectpyruntime.py

205 lines
5.8 KiB
Python
Raw Normal View History

2024-11-02 19:55:32 +08:00
import os
2024-06-25 19:49:49 +08:00
import modulefinder, shutil, os, sys
2024-06-25 02:57:59 +08:00
import builtins, platform
2024-06-25 03:01:20 +08:00
import sys
2024-06-25 20:16:57 +08:00
2024-11-08 17:07:24 +08:00
rootDir = os.path.dirname(__file__)
if not rootDir:
rootDir = os.path.abspath(".")
else:
rootDir = os.path.abspath(rootDir)
rootthisfiledir=rootDir
rootDir=os.path.abspath(os.path.join(rootDir,'../../py'))
2024-06-25 02:57:59 +08:00
pyversion = platform.python_version()
pyversion2 = "".join(pyversion.split(".")[:2])
2024-06-25 03:03:24 +08:00
x86 = platform.architecture()[0] == "32bit"
2024-11-02 19:55:32 +08:00
runtime = r"pyrt\runtime"
2024-05-01 14:59:50 +08:00
if x86:
2024-11-02 19:55:32 +08:00
webviewpath = r"webviewpy\platform\win32\x86"
2024-05-01 14:59:50 +08:00
downlevel = r"C:\Windows\SysWOW64\downlevel"
else:
2024-11-02 19:55:32 +08:00
webviewpath = r"webviewpy\platform\win32\x64"
2024-05-01 14:59:50 +08:00
downlevel = r"C:\Windows\system32\downlevel"
2024-06-25 03:06:31 +08:00
py37Path = os.path.dirname(sys.executable)
2024-06-25 03:01:20 +08:00
print(py37Path)
2024-05-01 14:59:50 +08:00
def get_dependencies(filename):
2024-05-01 17:12:05 +08:00
saveopen = builtins.open
def __open(*arg, **kwarg):
if len(arg) > 1:
mode = arg[1]
else:
mode = ""
if "b" not in mode:
kwarg["encoding"] = "utf8"
return saveopen(*arg, **kwarg)
builtins.open = __open
2024-05-01 14:59:50 +08:00
finder = modulefinder.ModuleFinder()
finder.run_script(filename)
dependencies = []
for name, module in finder.modules.items():
if module.__file__ is not None:
dependencies.append(module.__file__)
2024-05-01 17:12:05 +08:00
builtins.open = saveopen
2024-05-01 14:59:50 +08:00
return dependencies
def copycheck(src, tgt):
2024-05-01 17:12:05 +08:00
print(src, tgt, os.path.exists(src))
2024-05-01 14:59:50 +08:00
if not os.path.exists(src):
return
if src.lower().endswith("_ssl.pyd"):
return
if not os.path.exists(tgt):
os.makedirs(tgt, exist_ok=True)
if os.path.isdir(src):
tgt = os.path.join(tgt, os.path.basename(src))
if os.path.exists(tgt):
shutil.rmtree(tgt)
shutil.copytree(src, tgt)
return
shutil.copy(src, tgt)
all_dependencies = set()
for _d, _, _fs in os.walk("./LunaTranslator"):
for f in _fs:
if not f.endswith(".py"):
continue
base = os.path.basename(_d)
if base in [
"tts",
"transoptimi",
"translator",
"scalemethod",
"ocrengines",
"winhttp",
"libcurl",
"network",
"hiraparse",
"cishu",
"textoutput",
]:
continue
print(base, f)
got = get_dependencies(os.path.join(_d, f))
all_dependencies = all_dependencies.union(set(got))
for dependency in all_dependencies:
if dependency.startswith("./"):
continue
2024-06-25 03:03:53 +08:00
if not dependency.startswith(py37Path):
2024-06-25 02:57:59 +08:00
continue
2024-05-01 14:59:50 +08:00
print(dependency)
2024-05-01 17:12:05 +08:00
end = dependency[len(py37Path) + 1 :]
2024-05-01 14:59:50 +08:00
if end.lower().startswith("lib"):
end = end[4:]
if end.lower().startswith("site-packages"):
end = end[len("site-packages") + 1 :]
elif end.lower().startswith("dlls"):
end = end[5:]
2024-05-01 17:12:05 +08:00
print(end)
tgtreal = os.path.join(runtime, os.path.dirname(end))
2024-05-01 14:59:50 +08:00
copycheck(dependency, tgtreal)
2024-06-25 02:57:59 +08:00
with open(os.path.join(runtime, f"python{pyversion2}._pth"), "w") as ff:
2024-07-25 15:48:39 +08:00
ff.write("..\n.")
2024-05-01 14:59:50 +08:00
2024-05-01 17:12:05 +08:00
copycheck(os.path.join(py37Path, "python3.dll"), runtime)
2024-06-25 02:57:59 +08:00
copycheck(os.path.join(py37Path, f"python{pyversion2}.dll"), runtime)
2024-05-01 17:12:05 +08:00
copycheck(os.path.join(py37Path, "Dlls/sqlite3.dll"), runtime)
2024-05-01 14:59:50 +08:00
2024-05-01 17:12:05 +08:00
copycheck(os.path.join(py37Path, "Lib/encodings"), runtime)
2024-06-25 03:44:03 +08:00
copycheck(os.path.join(py37Path, "DLLs/libffi-7.dll"), runtime)
2024-05-01 14:59:50 +08:00
2024-05-01 17:12:05 +08:00
copycheck(rf"{downlevel}\ucrtbase.dll", runtime)
2024-11-02 19:55:32 +08:00
copycheck(
os.path.join(py37Path, "Lib/site-packages", webviewpath, "webview.dll"),
os.path.join(runtime, webviewpath),
)
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/vcruntime140.dll"),
os.path.join(runtime),
)
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/vcruntime140_1.dll"),
os.path.join(runtime),
)
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/msvcp140.dll"),
os.path.join(runtime),
)
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/msvcp140_1.dll"),
os.path.join(runtime),
)
2024-05-01 14:59:50 +08:00
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/vcruntime140.dll"),
os.path.join(runtime),
2024-05-01 14:59:50 +08:00
)
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/vcruntime140_1.dll"),
os.path.join(runtime),
2024-05-01 14:59:50 +08:00
)
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/msvcp140.dll"),
os.path.join(runtime),
2024-05-01 14:59:50 +08:00
)
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/msvcp140_1.dll"),
os.path.join(runtime),
2024-05-01 14:59:50 +08:00
)
2024-05-01 17:12:05 +08:00
for _ in os.listdir(os.path.join(py37Path, "Lib/site-packages/PyQt5")):
2024-05-01 14:59:50 +08:00
if _.startswith("sip"):
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5", _),
os.path.join(runtime, "PyQt5"),
2024-05-01 14:59:50 +08:00
)
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Core.dll"),
os.path.join(runtime, "PyQt5/Qt5/bin"),
2024-05-01 14:59:50 +08:00
)
2024-05-27 22:26:00 +08:00
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Svg.dll"),
os.path.join(runtime, "PyQt5/Qt5/bin"),
)
2024-05-01 14:59:50 +08:00
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Gui.dll"),
os.path.join(runtime, "PyQt5/Qt5/bin"),
2024-05-01 14:59:50 +08:00
)
2024-05-17 16:25:05 +08:00
2024-05-01 14:59:50 +08:00
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/bin/Qt5Widgets.dll"),
os.path.join(runtime, "PyQt5/Qt5/bin"),
2024-05-01 14:59:50 +08:00
)
2024-05-27 22:26:00 +08:00
copycheck(
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/iconengines"),
os.path.join(runtime, "PyQt5/Qt5/plugins"),
)
2024-05-01 14:59:50 +08:00
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/imageformats"),
os.path.join(runtime, "PyQt5/Qt5/plugins"),
2024-05-01 14:59:50 +08:00
)
copycheck(
2024-05-01 17:12:05 +08:00
os.path.join(
py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/platforms/qwindows.dll"
),
os.path.join(runtime, "PyQt5/Qt5/plugins/platforms"),
2024-05-01 14:59:50 +08:00
)
copycheck(
os.path.join(
2024-05-01 17:12:05 +08:00
py37Path, "Lib/site-packages/PyQt5/Qt5/plugins/styles/qwindowsvistastyle.dll"
2024-05-01 14:59:50 +08:00
),
2024-05-01 17:12:05 +08:00
os.path.join(runtime, "PyQt5/Qt5/plugins/styles"),
2024-05-01 14:59:50 +08:00
)