This commit is contained in:
jxxghp 2023-10-11 08:19:44 +08:00
parent 149104063c
commit 0b2d419000

View File

@ -4,8 +4,9 @@ def collect_pkg_data(package: str, include_py_files: bool = False, subdir: str =
""" """
Collect all data files from the given package. Collect all data files from the given package.
""" """
import os from pathlib import Path
from PyInstaller.utils.hooks import get_package_paths, remove_prefix, PY_IGNORE_EXTENSIONS from PyInstaller.utils.hooks import get_package_paths, PY_IGNORE_EXTENSIONS
from PyInstaller.building.datastruct import TOC
# Accept only strings as packages. # Accept only strings as packages.
if type(package) is not str: if type(package) is not str:
@ -13,17 +14,17 @@ def collect_pkg_data(package: str, include_py_files: bool = False, subdir: str =
pkg_base, pkg_dir = get_package_paths(package) pkg_base, pkg_dir = get_package_paths(package)
if subdir: if subdir:
pkg_dir = os.path.join(pkg_dir, subdir) pkg_path = Path(pkg_dir) / subdir
else:
pkg_path = Path(pkg_dir)
# Walk through all file in the given package, looking for data files. # Walk through all file in the given package, looking for data files.
data_toc = TOC() data_toc = TOC()
for dir_path, dir_names, files in os.walk(pkg_dir): for file in pkg_path.rglob('*'):
for f in files: if file.is_file():
extension = os.path.splitext(f)[1] extension = file.suffix
if include_py_files or (extension not in PY_IGNORE_EXTENSIONS): if not include_py_files and (extension in PY_IGNORE_EXTENSIONS):
source_file = os.path.join(dir_path, f) continue
dest_folder = remove_prefix(dir_path, os.path.dirname(pkg_base) + os.sep) data_toc.append((str(file.relative_to(pkg_base)), str(file), 'DATA'))
dest_file = os.path.join(dest_folder, f)
data_toc.append((dest_file, source_file, 'DATA'))
return data_toc return data_toc