adamrehn / ue4cli

Command-line interface for Unreal Engine 4
https://docs.adamrehn.com/ue4cli/
MIT License
254 stars 48 forks source link

ue4cli depends on 'pkg_resources' which prevents it from running on vanilla python #71

Open sleeptightAnsiC opened 6 months ago

sleeptightAnsiC commented 6 months ago

Looks like I missed this one during https://github.com/adamrehn/ue4cli/pull/59

ue4cli still depends on package outside of vanilla python and cannot be run directly from source without it:

$ python ~/ue4cli
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/korn/ue4cli/__main__.py", line 1, in <module>
    from ue4cli.cli import main
  File "/home/korn/ue4cli/ue4cli/cli.py", line 2, in <module>
    from .PluginManager import PluginManager
  File "/home/korn/ue4cli/ue4cli/PluginManager.py", line 2, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

For now, I only spotted pkg_resources (provided by setuptools) which is used in two places:

  1. pkg_resources.iter_entry_points https://github.com/adamrehn/ue4cli/blob/fed71c1af4cffe3fed9358b08430068ad9454b77/ue4cli/PluginManager.py#L15-L20
  2. pkg_resources.parse_version https://github.com/adamrehn/ue4cli/blob/fed71c1af4cffe3fed9358b08430068ad9454b77/ue4cli/UnrealManagerDarwin.py#L35-L47

First one is tricky, we have to use it for loading ue4cli-plugins which is invoked in ue4cli/cli.py. Second one should be fairly easy to replace (I hope).

TODO: I gotta make sure there is no more dependencies like this one and find good workaround for this. Perhaps we can try; import pkg_resources: and load ue4cli-plugins only if said dependency is resolved.

Either way, I'll probably take this one :)

sleeptightAnsiC commented 3 months ago

Potential fix for 1. This already makes it work on Windows and Linux

diff --git a/ue4cli/PluginManager.py b/ue4cli/PluginManager.py
index e157394..8df7b13 100644
--- a/ue4cli/PluginManager.py
+++ b/ue4cli/PluginManager.py
@@ -1,5 +1,14 @@
 from inspect import signature
-import pkg_resources
+from .Utility import Utility
+
+# HACK: dummy iter_entry_points in case package 'setuptools' hasn't been provided
+try:
+   from pkg_resources import iter_entry_points
+except ModuleNotFoundError as e:
+   Utility.printStderr(f'Warning: ue4cli plugins are unavailable due to: {str(e)}')
+   def iter_entry_points(unused):
+       return {}
+

 class PluginManager:
    """
@@ -16,7 +25,7 @@ class PluginManager:
        plugins = {
            entry_point.name: entry_point.load()
            for entry_point
-           in pkg_resources.iter_entry_points('ue4cli.plugins')
+           in iter_entry_points('ue4cli.plugins')
        }

        # Filter out any invalid plugins