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