22import sys
33import logging
44import subprocess
5+ import platform
56from pathlib import Path
67from glob import glob
78from time import sleep
@@ -22,17 +23,30 @@ def _log_modules(modules: List["Module"]) -> None:
2223 for module in modules :
2324 logger .info (" - {} at {}" .format (module .name , module .path ))
2425
26+ def is_executable (path : str ) -> bool :
27+ if not os .path .isfile (path ):
28+ return False
29+ filename = os .path .basename (path )
30+ # On windows all files ending with .exe are executables
31+ if platform .system () == "Windows" :
32+ return filename .endswith (".exe" )
33+ # On Unix platforms all files having executable permissions are executables
34+ # We do not however want to include .desktop files
35+ else : # Assumes Unix
36+ if not os .access (path , os .X_OK ):
37+ return False
38+ if filename .endswith (".desktop" ):
39+ return False
40+ return True
41+
2542
2643def _discover_modules_in_directory (path : str ) -> List ["Module" ]:
2744 """Look for modules in given directory path and recursively in subdirs matching aw-*"""
2845 modules = []
2946 matches = glob (os .path .join (path , "aw-*" ))
3047 for match in matches :
31- filename = os .path .basename (match )
32- if not _validate_module (filename ):
33- continue
34- # NOTE: os.access(match, os.X_OK) seems to always return True on Windows
35- elif os .path .isfile (match ) and os .access (match , os .X_OK ):
48+ if is_executable (match ) and match .startswith ("aw-" ):
49+ filename = os .path .basename (match )
3650 name = _filename_to_name (filename )
3751 modules .append (Module (name , Path (match ), "bundled" ))
3852 elif os .path .isdir (match ) and os .access (match , os .X_OK ):
@@ -48,21 +62,6 @@ def _filename_to_name(filename: str) -> str:
4862 return filename .replace (".exe" , "" )
4963
5064
51- def _validate_module (fn : str ) -> bool :
52- # NOTE: Checks for bundled non-executable files that match aw-* are needed
53- # since the os.access(..., os.X_OS) check will always return True on Windows.
54- return (
55- fn .startswith ("aw-" )
56- and (".manifest" not in fn )
57- and (".desktop" not in fn )
58- and (".service" not in fn )
59- )
60-
61-
62- def _filter_filenames (filenames : List [str ]) -> List [str ]:
63- return [fn for fn in filenames if _validate_module (fn )]
64-
65-
6665def _discover_modules_bundled () -> List ["Module" ]:
6766 """Use ``_discover_modules_in_directory`` to find all bundled modules """
6867 _search_paths = [_module_dir , _parent_dir ]
@@ -88,7 +87,11 @@ def _discover_modules_system() -> List["Module"]:
8887 modules : List ["Module" ] = []
8988 paths = [p for p in search_paths if os .path .isdir (p )]
9089 for path in paths :
91- for filename in _filter_filenames (os .listdir (path )):
90+ for filename in os .listdir (path ):
91+ if not filename .startswith ("aw-" ):
92+ continue
93+ if not is_executable (path + "/" + filename ):
94+ continue
9295 name = _filename_to_name (filename )
9396 # Only pick the first match (to respect PATH priority)
9497 if name not in [m .name for m in modules ]:
0 commit comments