| Conditions | 12 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like st2actions.runners.PythonRunner.run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 98 | def run(self, action_parameters): |
||
| 99 | pack = self.get_pack_name() |
||
| 100 | serialized_parameters = json.dumps(action_parameters) if action_parameters else '' |
||
| 101 | virtualenv_path = get_sandbox_virtualenv_path(pack=pack) |
||
| 102 | python_path = get_sandbox_python_binary_path(pack=pack) |
||
| 103 | |||
| 104 | if virtualenv_path and not os.path.isdir(virtualenv_path): |
||
| 105 | format_values = {'pack': pack, 'virtualenv_path': virtualenv_path} |
||
| 106 | msg = PACK_VIRTUALENV_DOESNT_EXIST % format_values |
||
| 107 | raise Exception(msg) |
||
| 108 | |||
| 109 | if not self.entry_point: |
||
| 110 | raise Exception('Action "%s" is missing entry_point attribute' % (self.action.name)) |
||
| 111 | |||
| 112 | args = [ |
||
| 113 | python_path, |
||
| 114 | WRAPPER_SCRIPT_PATH, |
||
| 115 | '--pack=%s' % (pack), |
||
| 116 | '--file-path=%s' % (self.entry_point), |
||
| 117 | '--parameters=%s' % (serialized_parameters), |
||
| 118 | '--parent-args=%s' % (json.dumps(sys.argv[1:])) |
||
| 119 | ] |
||
| 120 | |||
| 121 | # We need to ensure all the st2 dependencies are also available to the |
||
| 122 | # subprocess |
||
| 123 | env = os.environ.copy() |
||
| 124 | env['PATH'] = get_sandbox_path(virtualenv_path=virtualenv_path) |
||
| 125 | env['PYTHONPATH'] = get_sandbox_python_path(inherit_from_parent=True, |
||
| 126 | inherit_parent_virtualenv=True) |
||
| 127 | |||
| 128 | # Include user provided environment variables (if any) |
||
| 129 | user_env_vars = self._get_env_vars() |
||
| 130 | env.update(user_env_vars) |
||
| 131 | |||
| 132 | # Include common st2 environment variables |
||
| 133 | st2_env_vars = self._get_common_action_env_variables() |
||
| 134 | env.update(st2_env_vars) |
||
| 135 | |||
| 136 | exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE, |
||
| 137 | stderr=subprocess.PIPE, shell=False, |
||
| 138 | env=env, timeout=self._timeout) |
||
| 139 | |||
| 140 | if timed_out: |
||
| 141 | error = 'Action failed to complete in %s seconds' % (self._timeout) |
||
| 142 | else: |
||
| 143 | error = None |
||
| 144 | |||
| 145 | if ACTION_OUTPUT_RESULT_DELIMITER in stdout: |
||
| 146 | split = stdout.split(ACTION_OUTPUT_RESULT_DELIMITER) |
||
| 147 | assert len(split) == 3 |
||
| 148 | result = split[1].strip() |
||
| 149 | stdout = split[0] + split[2] |
||
| 150 | else: |
||
| 151 | result = None |
||
| 152 | |||
| 153 | try: |
||
| 154 | result = json.loads(result) |
||
| 155 | except: |
||
| 156 | pass |
||
| 157 | |||
| 158 | output = { |
||
| 159 | 'stdout': stdout, |
||
| 160 | 'stderr': stderr, |
||
| 161 | 'exit_code': exit_code, |
||
| 162 | 'result': result |
||
| 163 | } |
||
| 164 | |||
| 165 | if error: |
||
| 166 | output['error'] = error |
||
| 167 | |||
| 168 | if exit_code == 0: |
||
| 169 | status = LIVEACTION_STATUS_SUCCEEDED |
||
| 170 | elif timed_out: |
||
| 171 | status = LIVEACTION_STATUS_TIMED_OUT |
||
| 172 | else: |
||
| 173 | status = LIVEACTION_STATUS_FAILED |
||
| 174 | |||
| 175 | return (status, output, None) |
||
| 176 | |||
| 201 |