blight.action

The different actions supported by blight.

  1"""
  2The different actions supported by blight.
  3"""
  4
  5from typing import Any, Dict, Optional
  6
  7import blight.tool
  8from blight.tool import Tool
  9
 10
 11class Action:
 12    """
 13    A generic action, run with every tool (both before and after the tool's execution).
 14    """
 15
 16    def __init__(self, config: Dict[str, str]):
 17        self._config = config
 18        self._result: Optional[Dict[str, Any]] = None
 19
 20    def _should_run_on(self, tool: Tool) -> bool:
 21        return True
 22
 23    def before_run(self, tool: Tool) -> None:  # pragma: no cover
 24        """
 25        Invoked right before the underlying tool is run.
 26
 27        Args:
 28            tool: The tool about to run
 29        """
 30        pass
 31
 32    def _before_run(self, tool: Tool) -> None:
 33        if self._should_run_on(tool):
 34            self.before_run(tool)
 35
 36    def after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:  # pragma: no cover
 37        """
 38        Invoked right after the underlying tool is run.
 39
 40        Args:
 41            tool: The tool that just ran
 42        """
 43        pass
 44
 45    def _after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:
 46        if self._should_run_on(tool):
 47            self.after_run(tool, run_skipped=run_skipped)
 48
 49    @property
 50    def result(self) -> Optional[Dict[str, Any]]:
 51        """
 52        Returns the result computed by this action, if there is one.
 53        """
 54        return self._result
 55
 56
 57class CCAction(Action):
 58    """
 59    A `cc` action, run whenever the tool is a `blight.tool.CC` instance.
 60    """
 61
 62    def _should_run_on(self, tool: Tool) -> bool:
 63        return isinstance(tool, blight.tool.CC)
 64
 65
 66class CXXAction(Action):
 67    """
 68    A `c++` action, run whenever the tool is a `blight.tool.CXX` instance.
 69    """
 70
 71    def _should_run_on(self, tool: Tool) -> bool:
 72        return isinstance(tool, blight.tool.CXX)
 73
 74
 75class CompilerAction(CCAction, CXXAction):
 76    """
 77    A generic compiler action, run whenever the tool is a `blight.tool.CC`
 78    or `blight.tool.CXX` instance.
 79
 80    **NOTE:** Action writers should generally prefer this over `CCAction` and `CXXAction`,
 81    as messy builds may use `cc` to compile C++ sources (via `-x c`) and vice versa.
 82    """
 83
 84    def _should_run_on(self, tool: Tool) -> bool:
 85        return isinstance(tool, (blight.tool.CC, blight.tool.CXX))
 86
 87
 88class CPPAction(Action):
 89    """
 90    A `cpp` action, run whenever the tool is a `blight.tool.CPP` instance.
 91    """
 92
 93    def _should_run_on(self, tool: Tool) -> bool:
 94        return isinstance(tool, blight.tool.CPP)
 95
 96
 97class LDAction(Action):
 98    """
 99    An `ld` action, run whenever the tool is a `blight.tool.LD` instance.
100    """
101
102    def _should_run_on(self, tool: Tool) -> bool:
103        return isinstance(tool, blight.tool.LD)
104
105
106class ASAction(Action):
107    """
108    An `as` action, run whenever the tool is a `blight.tool.AS` instance.
109    """
110
111    def _should_run_on(self, tool: Tool) -> bool:
112        return isinstance(tool, blight.tool.AS)
113
114
115class ARAction(Action):
116    """
117    An `ar` action, run whenever the tool is a `blight.tool.AR` instance.
118    """
119
120    def _should_run_on(self, tool: Tool) -> bool:
121        return isinstance(tool, blight.tool.AR)
122
123
124class STRIPAction(Action):
125    """
126    A `strip` action, run whenever the tool is a `blight.tool.STRIP` instance.
127    """
128
129    def _should_run_on(self, tool: Tool) -> bool:
130        return isinstance(tool, blight.tool.STRIP)
class Action:
12class Action:
13    """
14    A generic action, run with every tool (both before and after the tool's execution).
15    """
16
17    def __init__(self, config: Dict[str, str]):
18        self._config = config
19        self._result: Optional[Dict[str, Any]] = None
20
21    def _should_run_on(self, tool: Tool) -> bool:
22        return True
23
24    def before_run(self, tool: Tool) -> None:  # pragma: no cover
25        """
26        Invoked right before the underlying tool is run.
27
28        Args:
29            tool: The tool about to run
30        """
31        pass
32
33    def _before_run(self, tool: Tool) -> None:
34        if self._should_run_on(tool):
35            self.before_run(tool)
36
37    def after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:  # pragma: no cover
38        """
39        Invoked right after the underlying tool is run.
40
41        Args:
42            tool: The tool that just ran
43        """
44        pass
45
46    def _after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:
47        if self._should_run_on(tool):
48            self.after_run(tool, run_skipped=run_skipped)
49
50    @property
51    def result(self) -> Optional[Dict[str, Any]]:
52        """
53        Returns the result computed by this action, if there is one.
54        """
55        return self._result

A generic action, run with every tool (both before and after the tool's execution).

Action(config: Dict[str, str])
17    def __init__(self, config: Dict[str, str]):
18        self._config = config
19        self._result: Optional[Dict[str, Any]] = None
def before_run(self, tool: blight.tool.Tool) -> None:
24    def before_run(self, tool: Tool) -> None:  # pragma: no cover
25        """
26        Invoked right before the underlying tool is run.
27
28        Args:
29            tool: The tool about to run
30        """
31        pass

Invoked right before the underlying tool is run.

Args: tool: The tool about to run

def after_run(self, tool: blight.tool.Tool, *, run_skipped: bool = False) -> None:
37    def after_run(self, tool: Tool, *, run_skipped: bool = False) -> None:  # pragma: no cover
38        """
39        Invoked right after the underlying tool is run.
40
41        Args:
42            tool: The tool that just ran
43        """
44        pass

Invoked right after the underlying tool is run.

Args: tool: The tool that just ran

result: Optional[Dict[str, Any]]
50    @property
51    def result(self) -> Optional[Dict[str, Any]]:
52        """
53        Returns the result computed by this action, if there is one.
54        """
55        return self._result

Returns the result computed by this action, if there is one.

class CCAction(Action):
58class CCAction(Action):
59    """
60    A `cc` action, run whenever the tool is a `blight.tool.CC` instance.
61    """
62
63    def _should_run_on(self, tool: Tool) -> bool:
64        return isinstance(tool, blight.tool.CC)

A cc action, run whenever the tool is a blight.tool.CC instance.

class CXXAction(Action):
67class CXXAction(Action):
68    """
69    A `c++` action, run whenever the tool is a `blight.tool.CXX` instance.
70    """
71
72    def _should_run_on(self, tool: Tool) -> bool:
73        return isinstance(tool, blight.tool.CXX)

A c++ action, run whenever the tool is a blight.tool.CXX instance.

class CompilerAction(CCAction, CXXAction):
76class CompilerAction(CCAction, CXXAction):
77    """
78    A generic compiler action, run whenever the tool is a `blight.tool.CC`
79    or `blight.tool.CXX` instance.
80
81    **NOTE:** Action writers should generally prefer this over `CCAction` and `CXXAction`,
82    as messy builds may use `cc` to compile C++ sources (via `-x c`) and vice versa.
83    """
84
85    def _should_run_on(self, tool: Tool) -> bool:
86        return isinstance(tool, (blight.tool.CC, blight.tool.CXX))

A generic compiler action, run whenever the tool is a blight.tool.CC or blight.tool.CXX instance.

NOTE: Action writers should generally prefer this over CCAction and CXXAction, as messy builds may use cc to compile C++ sources (via -x c) and vice versa.

class CPPAction(Action):
89class CPPAction(Action):
90    """
91    A `cpp` action, run whenever the tool is a `blight.tool.CPP` instance.
92    """
93
94    def _should_run_on(self, tool: Tool) -> bool:
95        return isinstance(tool, blight.tool.CPP)

A cpp action, run whenever the tool is a blight.tool.CPP instance.

class LDAction(Action):
 98class LDAction(Action):
 99    """
100    An `ld` action, run whenever the tool is a `blight.tool.LD` instance.
101    """
102
103    def _should_run_on(self, tool: Tool) -> bool:
104        return isinstance(tool, blight.tool.LD)

An ld action, run whenever the tool is a blight.tool.LD instance.

class ASAction(Action):
107class ASAction(Action):
108    """
109    An `as` action, run whenever the tool is a `blight.tool.AS` instance.
110    """
111
112    def _should_run_on(self, tool: Tool) -> bool:
113        return isinstance(tool, blight.tool.AS)

An as action, run whenever the tool is a blight.tool.AS instance.

class ARAction(Action):
116class ARAction(Action):
117    """
118    An `ar` action, run whenever the tool is a `blight.tool.AR` instance.
119    """
120
121    def _should_run_on(self, tool: Tool) -> bool:
122        return isinstance(tool, blight.tool.AR)

An ar action, run whenever the tool is a blight.tool.AR instance.

class STRIPAction(Action):
125class STRIPAction(Action):
126    """
127    A `strip` action, run whenever the tool is a `blight.tool.STRIP` instance.
128    """
129
130    def _should_run_on(self, tool: Tool) -> bool:
131        return isinstance(tool, blight.tool.STRIP)

A strip action, run whenever the tool is a blight.tool.STRIP instance.