|
| 1 | +""" |
| 2 | +Utility for locating the module (or package's __init__.py) |
| 3 | +associated with a given console_script name |
| 4 | +and verifying it contains the PYTHON_ARGCOMPLETE_OK marker. |
| 5 | +
|
| 6 | +Such scripts are automatically generated and cannot contain |
| 7 | +the marker themselves, so we defer to the containing module or package. |
| 8 | +
|
| 9 | +For more information on setuptools console_scripts, see |
| 10 | +https://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation |
| 11 | +
|
| 12 | +Intended to be invoked by argcomplete's global completion function. |
| 13 | +""" |
| 14 | +import os |
| 15 | +import sys |
| 16 | + |
| 17 | +try: |
| 18 | + from importlib.metadata import entry_points as importlib_entry_points |
| 19 | + from importlib.metadata import EntryPoint |
| 20 | + use_entry_points_backport = False |
| 21 | +except ImportError: |
| 22 | + from importlib_metadata import entry_points as importlib_entry_points # type:ignore |
| 23 | + from importlib_metadata import EntryPoint # type:ignore |
| 24 | + use_entry_points_backport = True |
| 25 | + |
| 26 | +from ._check_module import ArgcompleteMarkerNotFound, find |
| 27 | +from typing import Iterable |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + # Argument is the full path to the console script. |
| 32 | + script_path = sys.argv[1] |
| 33 | + |
| 34 | + # Find the module and function names that correspond to this |
| 35 | + # assuming it is actually a console script. |
| 36 | + name = os.path.basename(script_path) |
| 37 | + |
| 38 | + entry_points : Iterable[EntryPoint] = importlib_entry_points() # type:ignore |
| 39 | + |
| 40 | + # The importlib_metadata backport returns a tuple of entry point objects |
| 41 | + # whereas the official library returns a SelectableGroups object |
| 42 | + # Python 3.12+ behaves like the importlib_metadata backport |
| 43 | + if not use_entry_points_backport and sys.version_info < (3, 12): |
| 44 | + entry_points = entry_points["console_scripts"] # type:ignore |
| 45 | + |
| 46 | + entry_points = [ep for ep in entry_points \ |
| 47 | + if ep.name == name and ep.group == "console_scripts" ] # type:ignore |
| 48 | + |
| 49 | + if not entry_points: |
| 50 | + raise ArgcompleteMarkerNotFound("no entry point found matching script") |
| 51 | + entry_point = entry_points[0] |
| 52 | + module_name, function_name = entry_point.value.split(":", 1) |
| 53 | + |
| 54 | + # Check this looks like the script we really expected. |
| 55 | + with open(script_path) as f: |
| 56 | + script = f.read() |
| 57 | + if "from {} import {}".format(module_name, function_name) not in script: |
| 58 | + raise ArgcompleteMarkerNotFound("does not appear to be a console script") |
| 59 | + if "sys.exit({}())".format(function_name) not in script: |
| 60 | + raise ArgcompleteMarkerNotFound("does not appear to be a console script") |
| 61 | + |
| 62 | + # Look for the argcomplete marker in the script it imports. |
| 63 | + with open(find(module_name, return_package=True)) as f: |
| 64 | + head = f.read(1024) |
| 65 | + if "PYTHON_ARGCOMPLETE_OK" not in head: |
| 66 | + raise ArgcompleteMarkerNotFound("marker not found") |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == "__main__": |
| 70 | + try: |
| 71 | + main() |
| 72 | + except ArgcompleteMarkerNotFound as e: |
| 73 | + sys.exit(str(e)) |
0 commit comments