Bundler update

Replace deprecated pkg_resources

frdel committed Oct 10, 2024 at 21:17 UTC 9d86cb3a1f1578b2c1d395b0069dd5718767f069
1 file changed +29 -22
bundle.py
+29 -22
@@ -5,43 +5,50 @@ import site
5 import shutil
6 from pathlib import Path
7 import pathspec
8 -import pkg_resources
8 import importlib
9 +import importlib.metadata as metadata
10
11 -def get_package_data_files(package_name):
12 - """Discover data files for a given package."""
11 +def get_package_data_folder(package_name):
12 + """Return the package path if it contains data files."""
13 try:
14 + # Get the module and its file path
15 package = importlib.import_module(package_name)
15 - package_path = os.path.dirname(package.__file__) # type: ignore
16 - data_files = []
16 + package_path = os.path.dirname(package.__file__) # type: ignore
17 + #only if package path does not end with "site-packages"
18 + if not package_path.endswith("site-packages"):
19 + # Check if the package contains any relevant data files
20 + has_data = any(
21 + file.endswith(('.json', '.txt', '.csv', '.yml', '.yaml'))
22 + for root, dirs, files in os.walk(package_path)
23 + for file in files
24 + )
25 +
26 + if has_data:
27 + return package_path
28
18 - for root, dirs, files in os.walk(package_path):
19 - for file in files:
20 - if file.endswith(('.json', '.txt', '.csv', '.yml', '.yaml')):
21 - file_path = os.path.join(root, file)
22 - relative_path = os.path.relpath(root, package_path)
23 - data_files.append((file_path, os.path.join(package_name, relative_path)))
24 -
25 - return data_files
29 except ImportError:
27 - print(f"Warning: Unable to import {package_name}. Skipping data file discovery for this package.")
28 - return []
30 + print(f"Warning: Unable to import {package_name}. Skipping data folder discovery for this package.")
31 +
32 + return None
33
34 def get_add_data_args():
31 - """Return an array of --add-data arguments for PyInstaller."""
35 + """Return an array of --add-data arguments for PyInstaller, one per package."""
36 add_data_args = []
37
34 - # Get all installed packages
35 - installed_packages = [d.project_name for d in pkg_resources.working_set]
38 + # Use importlib.metadata to get the installed packages
39 + installed_packages = [dist.metadata['Name'] for dist in metadata.distributions()]
40
37 - # Discover data files for each package
41 + # Discover the data folder for each package and add it as a --add-data argument
42 for package in installed_packages:
39 - data_files = get_package_data_files(package)
40 - for file_path, dest_path in data_files:
41 - add_data_args.append(f"--add-data={file_path}{os.pathsep}{dest_path}")
43 + package_data_folder = get_package_data_folder(package)
44 + if package_data_folder:
45 + # Add the whole package directory
46 + add_data_args.append(f"--add-data={package_data_folder}{os.pathsep}{package}")
47
48 return add_data_args
49
50 +
51 +
52 def get_site_packages_path():
53 """Get the path to the site-packages directory of the current environment."""
54 if hasattr(site, "getsitepackages"):