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 |
||
102 | def run(self, action_parameters): |
||
103 | pack = self.get_pack_name() |
||
104 | serialized_parameters = json.dumps(action_parameters) if action_parameters else '' |
||
105 | virtualenv_path = get_sandbox_virtualenv_path(pack=pack) |
||
106 | python_path = get_sandbox_python_binary_path(pack=pack) |
||
107 | |||
108 | if virtualenv_path and not os.path.isdir(virtualenv_path): |
||
109 | format_values = {'pack': pack, 'virtualenv_path': virtualenv_path} |
||
110 | msg = PACK_VIRTUALENV_DOESNT_EXIST % format_values |
||
111 | raise Exception(msg) |
||
112 | |||
113 | if not self.entry_point: |
||
114 | raise Exception('Action "%s" is missing entry_point attribute' % (self.action.name)) |
||
115 | |||
116 | args = [ |
||
117 | python_path, |
||
118 | WRAPPER_SCRIPT_PATH, |
||
119 | '--pack=%s' % (pack), |
||
120 | '--file-path=%s' % (self.entry_point), |
||
121 | '--parameters=%s' % (serialized_parameters), |
||
122 | '--parent-args=%s' % (json.dumps(sys.argv[1:])) |
||
123 | ] |
||
124 | |||
125 | # We need to ensure all the st2 dependencies are also available to the |
||
126 | # subprocess |
||
127 | env = os.environ.copy() |
||
128 | env['PATH'] = get_sandbox_path(virtualenv_path=virtualenv_path) |
||
129 | env['PYTHONPATH'] = get_sandbox_python_path(inherit_from_parent=True, |
||
130 | inherit_parent_virtualenv=True) |
||
131 | |||
132 | # Include user provided environment variables (if any) |
||
133 | user_env_vars = self._get_env_vars() |
||
134 | env.update(user_env_vars) |
||
135 | |||
136 | # Include common st2 environment variables |
||
137 | st2_env_vars = self._get_common_action_env_variables() |
||
138 | env.update(st2_env_vars) |
||
139 | |||
140 | exit_code, stdout, stderr, timed_out = run_command(cmd=args, stdout=subprocess.PIPE, |
||
141 | stderr=subprocess.PIPE, shell=False, |
||
142 | env=env, timeout=self._timeout) |
||
143 | |||
144 | if timed_out: |
||
145 | error = 'Action failed to complete in %s seconds' % (self._timeout) |
||
146 | else: |
||
147 | error = None |
||
148 | |||
149 | if ACTION_OUTPUT_RESULT_DELIMITER in stdout: |
||
150 | split = stdout.split(ACTION_OUTPUT_RESULT_DELIMITER) |
||
151 | assert len(split) == 3 |
||
152 | result = split[1].strip() |
||
153 | stdout = split[0] + split[2] |
||
154 | else: |
||
155 | result = None |
||
156 | |||
157 | try: |
||
158 | result = json.loads(result) |
||
159 | except: |
||
160 | pass |
||
161 | |||
162 | output = { |
||
163 | 'stdout': stdout, |
||
164 | 'stderr': stderr, |
||
165 | 'exit_code': exit_code, |
||
166 | 'result': result |
||
167 | } |
||
168 | |||
169 | if error: |
||
170 | output['error'] = error |
||
171 | |||
172 | if exit_code == 0: |
||
173 | status = LIVEACTION_STATUS_SUCCEEDED |
||
174 | elif timed_out: |
||
175 | status = LIVEACTION_STATUS_TIMED_OUT |
||
176 | else: |
||
177 | status = LIVEACTION_STATUS_FAILED |
||
178 | |||
179 | return (status, output, None) |
||
180 | |||
205 |