]> jfr.im git - yt-dlp.git/commitdiff
[plugins] Fix zip search paths
authorpukkandan <redacted>
Fri, 20 Jan 2023 18:04:16 +0000 (23:34 +0530)
committerpukkandan <redacted>
Fri, 20 Jan 2023 18:05:34 +0000 (23:35 +0530)
Closes #6011

yt_dlp/plugins.py

index ff5ab9d5e2c8e6f584cdb95316b1b86d038928a9..6eecdb4d0c6b404dd55405902302557794bfd224 100644 (file)
@@ -34,9 +34,15 @@ def exec_module(self, module):
 
 @functools.cache
 def dirs_in_zip(archive):
-    with ZipFile(archive) as zip:
-        return set(itertools.chain.from_iterable(
-            Path(file).parents for file in zip.namelist()))
+    try:
+        with ZipFile(archive) as zip_:
+            return set(itertools.chain.from_iterable(
+                Path(file).parents for file in zip_.namelist()))
+    except FileNotFoundError:
+        pass
+    except Exception as e:
+        write_string(f'WARNING: Could not read zip file {archive}: {e}\n')
+    return set()
 
 
 class PluginFinder(importlib.abc.MetaPathFinder):
@@ -57,10 +63,8 @@ def search_locations(self, fullname):
 
         def _get_package_paths(*root_paths, containing_folder='plugins'):
             for config_dir in orderedSet(map(Path, root_paths), lazy=True):
-                plugin_dir = config_dir / containing_folder
-                if not plugin_dir.is_dir():
-                    continue
-                yield from plugin_dir.iterdir()
+                with contextlib.suppress(OSError):
+                    yield from (config_dir / containing_folder).iterdir()
 
         # Load from yt-dlp config folders
         candidate_locations.extend(_get_package_paths(
@@ -76,24 +80,23 @@ def _get_package_paths(*root_paths, containing_folder='plugins'):
             containing_folder='yt-dlp-plugins'))
 
         candidate_locations.extend(map(Path, sys.path))  # PYTHONPATH
+        with contextlib.suppress(ValueError):  # Added when running __main__.py directly
+            candidate_locations.remove(Path(__file__).parent)
 
         parts = Path(*fullname.split('.'))
-        locations = set()
-        for path in dict.fromkeys(candidate_locations):
+        for path in orderedSet(candidate_locations, lazy=True):
             candidate = path / parts
             if candidate.is_dir():
-                locations.add(str(candidate))
-            elif path.name and any(path.with_suffix(suffix).is_file() for suffix in {'.zip', '.egg', '.whl'}):
-                with contextlib.suppress(FileNotFoundError):
-                    if parts in dirs_in_zip(path):
-                        locations.add(str(candidate))
-        return locations
+                yield candidate
+            elif path.suffix in ('.zip', '.egg', '.whl'):
+                if parts in dirs_in_zip(path):
+                    yield candidate
 
     def find_spec(self, fullname, path=None, target=None):
         if fullname not in self.packages:
             return None
 
-        search_locations = self.search_locations(fullname)
+        search_locations = list(map(str, self.search_locations(fullname)))
         if not search_locations:
             return None