11#!/usr/bin/env python
22
33import sys
4- from glob import glob
54from importlib import import_module
65from inspect import getmembers , isclass , signature
76from os import makedirs , path
7+ from pathlib import Path
88from types import FunctionType
99
1010from pyinfra .api .facts import FactBase
1111
1212sys .path .append ("." )
13- from docs .utils import format_doc_line , title_line # noqa: E402
13+ from docs .utils import (
14+ format_doc_line ,
15+ get_module_names ,
16+ including_sub_modules ,
17+ remove_dups ,
18+ title_line ,
19+ ) # noqa: E402
1420
1521MODULE_DEF_LINE_MAX = 90
1622
1723
1824def build_operations_docs ():
1925 this_dir = path .dirname (path .realpath (__file__ ))
2026 docs_dir = path .abspath (path .join (this_dir , ".." , "docs" ))
21- operations_dir = path . join ( this_dir , ".." , "src" , "pyinfra" , "operations" , "*.py" )
27+ pyinfra_dir = Path ( docs_dir ). parent / "src" / "pyinfra"
2228
2329 makedirs (path .join (docs_dir , "operations" ), exist_ok = True )
2430
25- op_module_names = [
26- path .basename (name )[:- 3 ]
27- for name in glob (operations_dir )
28- if not name .endswith ("__init__.py" )
29- ]
30-
31- for module_name in op_module_names :
31+ for module_name in get_module_names (pyinfra_dir / "operations" ):
3232 lines = []
3333
3434 print ("--> Doing module: {0}" .format (module_name ))
@@ -44,7 +44,8 @@ def build_operations_docs():
4444
4545 operation_facts = [
4646 (key , value )
47- for key , value in getmembers (module )
47+ for m in including_sub_modules (module )
48+ for key , value in getmembers (m )
4849 if (isclass (value ) and issubclass (value , FactBase ))
4950 ]
5051
@@ -59,17 +60,20 @@ def build_operations_docs():
5960 lines .append ("Facts used in these operations: {0}." .format (", " .join (items )))
6061 lines .append ("" )
6162
62- operation_functions = [
63- (key , value ._inner )
64- for key , value in getmembers (module )
63+ all_operation_functions = [
64+ (f"{ m .__name__ .split ('.' )[- 1 ]} .{ key } " if m != module else key , value ._inner )
65+ for m in including_sub_modules (module )
66+ for key , value in getmembers (m )
6567 if (
6668 isinstance (value , FunctionType )
67- and value .__module__ == module . __name__
69+ and value .__module__ . startswith ( m . __name__ )
6870 and getattr (value , "_inner" , False )
6971 and not value .__name__ .startswith ("_" )
7072 and not key .startswith ("_" )
7173 )
7274 ]
75+ # remove duplicates in case functions imported by __init__.py and then also seen where defined
76+ operation_functions = remove_dups (all_operation_functions )
7377
7478 for name , func in operation_functions :
7579 decorated_func = getattr (func , "_inner" , None )
0 commit comments