comparison env/lib/python3.9/site-packages/argcomplete/_check_console_script.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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 except ImportError:
20 from importlib_metadata import entry_points as importlib_entry_points
21
22 from ._check_module import ArgcompleteMarkerNotFound, find
23
24
25 def main():
26 # Argument is the full path to the console script.
27 script_path = sys.argv[1]
28
29 # Find the module and function names that correspond to this
30 # assuming it is actually a console script.
31 name = os.path.basename(script_path)
32 entry_points = [ep for ep in importlib_entry_points()["console_scripts"] if ep.name == name]
33 if not entry_points:
34 raise ArgcompleteMarkerNotFound('no entry point found matching script')
35 entry_point = entry_points[0]
36 module_name, function_name = entry_point.value.split(":", 1)
37
38 # Check this looks like the script we really expected.
39 with open(script_path) as f:
40 script = f.read()
41 if 'from {} import {}'.format(module_name, function_name) not in script:
42 raise ArgcompleteMarkerNotFound('does not appear to be a console script')
43 if 'sys.exit({}())'.format(function_name) not in script:
44 raise ArgcompleteMarkerNotFound('does not appear to be a console script')
45
46 # Look for the argcomplete marker in the script it imports.
47 with open(find(module_name, return_package=True)) as f:
48 head = f.read(1024)
49 if 'PYTHON_ARGCOMPLETE_OK' not in head:
50 raise ArgcompleteMarkerNotFound('marker not found')
51
52
53 if __name__ == '__main__':
54 try:
55 main()
56 except ArgcompleteMarkerNotFound as e:
57 sys.exit(e)