Skip to content

Commit b12ad8e

Browse files
theotherjimmy0xc0170
authored andcommitted
Format and document hooks.py
1 parent efb7cfb commit b12ad8e

File tree

1 file changed

+112
-23
lines changed

1 file changed

+112
-23
lines changed

tools/hooks.py

Lines changed: 112 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
# Configurable hooks in the build system. Can be used by various platforms
2-
# to customize the build process.
1+
""" Configurable hooks in the build system. Can be used by various platforms
2+
to customize the build process.
3+
"""
34

45
################################################################################
56
# Hooks for the various parts of the build process
67

78
# Internal mapping of hooks per tool
8-
_hooks = {}
9+
_HOOKS = {}
910

1011
# Internal mapping of running hooks
11-
_running_hooks = {}
12+
_RUNNING_HOOKS = {}
1213

1314
# Available hook types
14-
_hook_types = ["binary", "compile", "link", "assemble"]
15+
_HOOK_TYPES = ["binary", "compile", "link", "assemble"]
1516

1617
# Available hook steps
17-
_hook_steps = ["pre", "replace", "post"]
18+
_HOOK_STEPS = ["pre", "replace", "post"]
1819

1920
# Hook the given function. Use this function as a decorator
2021
def hook_tool(function):
22+
"""Decorate a function as a tool that may be hooked"""
2123
tool = function.__name__
2224
tool_flag = "_" + tool + "_done"
2325
def wrapper(t_self, *args, **kwargs):
26+
"""The hooked function itself"""
2427
# if a hook for this tool is already running, it's most likely
2528
# coming from a derived class, so don't hook the super class version
26-
if _running_hooks.get(tool, False):
29+
if _RUNNING_HOOKS.get(tool, False):
2730
return function(t_self, *args, **kwargs)
28-
_running_hooks[tool] = True
31+
_RUNNING_HOOKS[tool] = True
2932
# If this tool isn't hooked, return original function
30-
if not _hooks.has_key(tool):
33+
if not _HOOKS.has_key(tool):
3134
res = function(t_self, *args, **kwargs)
32-
_running_hooks[tool] = False
35+
_RUNNING_HOOKS[tool] = False
3336
return res
34-
tooldesc = _hooks[tool]
37+
tooldesc = _HOOKS[tool]
3538
setattr(t_self, tool_flag, False)
3639
# If there is a replace hook, execute the replacement instead
3740
if tooldesc.has_key("replace"):
3841
res = tooldesc["replace"](t_self, *args, **kwargs)
3942
# If the replacement has set the "done" flag, exit now
4043
# Otherwise continue as usual
4144
if getattr(t_self, tool_flag, False):
42-
_running_hooks[tool] = False
45+
_RUNNING_HOOKS[tool] = False
4346
return res
4447
# Execute pre-function before main function if specified
4548
if tooldesc.has_key("pre"):
@@ -49,76 +52,162 @@ def wrapper(t_self, *args, **kwargs):
4952
# Execute post-function after main function if specified
5053
if tooldesc.has_key("post"):
5154
post_res = tooldesc["post"](t_self, *args, **kwargs)
52-
_running_hooks[tool] = False
55+
_RUNNING_HOOKS[tool] = False
5356
return post_res or res
5457
else:
55-
_running_hooks[tool] = False
58+
_RUNNING_HOOKS[tool] = False
5659
return res
5760
return wrapper
5861

59-
class Hook:
62+
class Hook(object):
63+
"""A compiler class that may be hooked"""
6064
def __init__(self, target, toolchain):
61-
_hooks.clear()
65+
_HOOKS.clear()
6266
self._cmdline_hooks = {}
6367
self.toolchain = toolchain
6468
target.init_hooks(self, toolchain.__class__.__name__)
6569

6670
# Hook various functions directly
67-
def _hook_add(self, hook_type, hook_step, function):
68-
if not hook_type in _hook_types or not hook_step in _hook_steps:
71+
@staticmethod
72+
def _hook_add(hook_type, hook_step, function):
73+
"""Add a hook to a compile function
74+
75+
Positional arguments:
76+
hook_type - one of the _HOOK_TYPES
77+
hook_step - one of the _HOOK_STEPS
78+
function - the function to add to the list of hooks
79+
"""
80+
if hook_type not in _HOOK_TYPES or hook_step not in _HOOK_STEPS:
6981
return False
70-
if not hook_type in _hooks:
71-
_hooks[hook_type] = {}
72-
_hooks[hook_type][hook_step] = function
82+
if hook_type not in _HOOKS:
83+
_HOOKS[hook_type] = {}
84+
_HOOKS[hook_type][hook_step] = function
7385
return True
7486

7587
def hook_add_compiler(self, hook_step, function):
88+
"""Add a hook to the compiler
89+
90+
Positional Arguments:
91+
hook_step - one of the _HOOK_STEPS
92+
function - the function to add to the list of hooks
93+
"""
7694
return self._hook_add("compile", hook_step, function)
7795

7896
def hook_add_linker(self, hook_step, function):
97+
"""Add a hook to the linker
98+
99+
Positional Arguments:
100+
hook_step - one of the _HOOK_STEPS
101+
function - the function to add to the list of hooks
102+
"""
79103
return self._hook_add("link", hook_step, function)
80104

81105
def hook_add_assembler(self, hook_step, function):
106+
"""Add a hook to the assemble
107+
108+
Positional Arguments:
109+
hook_step - one of the _HOOK_STEPS
110+
function - the function to add to the list of hooks
111+
"""
82112
return self._hook_add("assemble", hook_step, function)
83113

84114
def hook_add_binary(self, hook_step, function):
115+
"""Add a hook to the elf to binary tool
116+
117+
Positional Arguments:
118+
hook_step - one of the _HOOK_STEPS
119+
function - the function to add to the list of hooks
120+
"""
85121
return self._hook_add("binary", hook_step, function)
86122

87123
# Hook command lines
88124
def _hook_cmdline(self, hook_type, function):
89-
if not hook_type in _hook_types:
125+
"""Add a hook to a command line function
126+
127+
Positional arguments:
128+
hook_type - one of the _HOOK_TYPES
129+
function - the function to add to the list of hooks
130+
"""
131+
if hook_type not in _HOOK_TYPES:
90132
return False
91133
self._cmdline_hooks[hook_type] = function
92134
return True
93135

94136
def hook_cmdline_compiler(self, function):
137+
"""Add a hook to the compiler command line
138+
139+
Positional arguments:
140+
function - the function to call
141+
"""
95142
return self._hook_cmdline("compile", function)
96143

97144
def hook_cmdline_linker(self, function):
145+
"""Add a hook to the linker command line
146+
147+
Positional arguments:
148+
function - the function to call
149+
"""
98150
return self._hook_cmdline("link", function)
99151

100152
def hook_cmdline_assembler(self, function):
153+
"""Add a hook to the assembler command line
154+
155+
Positional arguments:
156+
function - the function to call
157+
"""
101158
return self._hook_cmdline("assemble", function)
102159

103160
def hook_cmdline_binary(self, function):
161+
"""Add a hook to the elf to bin tool command line
162+
163+
Positional arguments:
164+
function - the function to call
165+
"""
104166
return self._hook_cmdline("binary", function)
105167

106168
# Return the command line after applying the hook
107169
def _get_cmdline(self, hook_type, cmdline):
170+
"""Get the command line after running all hooks
171+
172+
Positional arguments:
173+
hook_type - one of the _HOOK_TYPES
174+
cmdline - the initial command line
175+
"""
108176
if self._cmdline_hooks.has_key(hook_type):
109-
cmdline = self._cmdline_hooks[hook_type](self.toolchain.__class__.__name__, cmdline)
177+
cmdline = self._cmdline_hooks[hook_type](
178+
self.toolchain.__class__.__name__, cmdline)
110179
return cmdline
111180

112181
def get_cmdline_compiler(self, cmdline):
182+
"""Get the compiler command line after running all hooks
183+
184+
Positional arguments:
185+
cmdline - the initial command line
186+
"""
113187
return self._get_cmdline("compile", cmdline)
114188

115189
def get_cmdline_linker(self, cmdline):
190+
"""Get the linker command line after running all hooks
191+
192+
Positional arguments:
193+
cmdline - the initial command line
194+
"""
116195
return self._get_cmdline("link", cmdline)
117196

118197
def get_cmdline_assembler(self, cmdline):
198+
"""Get the assmebler command line after running all hooks
199+
200+
Positional arguments:
201+
cmdline - the initial command line
202+
"""
119203
return self._get_cmdline("assemble", cmdline)
120204

121205
def get_cmdline_binary(self, cmdline):
206+
"""Get the binary command line after running all hooks
207+
208+
Positional arguments:
209+
cmdline - the initial command line
210+
"""
122211
return self._get_cmdline("binary", cmdline)
123212

124213
################################################################################

0 commit comments

Comments
 (0)