Conditions | 19 |
Total Lines | 100 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 exabgp.application.server.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 | # encoding: utf-8 |
||
152 | def run(comment, configurations, validate, pid=0): |
||
153 | env = getenv() |
||
154 | |||
155 | log.notice('Thank you for using ExaBGP', 'welcome') |
||
156 | log.notice('%s' % version, 'version') |
||
157 | log.notice('%s' % sys.version.replace('\n', ' '), 'interpreter') |
||
158 | log.notice('%s' % ' '.join(platform.uname()[:5]), 'os') |
||
159 | log.notice('%s' % ROOT, 'installation') |
||
160 | |||
161 | if comment: |
||
162 | log.notice(comment, 'advice') |
||
163 | |||
164 | warning = warn() |
||
165 | if warning: |
||
166 | log.warning(warning, 'advice') |
||
167 | |||
168 | if env.api.cli: |
||
169 | pipename = 'exabgp' if env.api.pipename is None else env.api.pipename |
||
170 | pipes = named_pipe(ROOT, pipename) |
||
171 | if len(pipes) != 1: |
||
172 | env.api.cli = False |
||
173 | log.error( |
||
174 | 'could not find the named pipes (%s.in and %s.out) required for the cli' % (pipename, pipename), 'cli' |
||
175 | ) |
||
176 | log.error('we scanned the following folders (the number is your PID):', 'cli') |
||
177 | for location in pipes: |
||
178 | log.error(' - %s' % location, 'cli control') |
||
179 | log.error('please make them in one of the folder with the following commands:', 'cli control') |
||
180 | log.error('> mkfifo %s/run/%s.{in,out}' % (os.getcwd(), pipename), 'cli control') |
||
181 | log.error('> chmod 600 %s/run/%s.{in,out}' % (os.getcwd(), pipename), 'cli control') |
||
182 | if os.getuid() != 0: |
||
183 | log.error( |
||
184 | '> chown %d:%d %s/run/%s.{in,out}' % (os.getuid(), os.getgid(), os.getcwd(), pipename), |
||
185 | 'cli control', |
||
186 | ) |
||
187 | else: |
||
188 | pipe = pipes[0] |
||
189 | os.environ['exabgp_cli_pipe'] = pipe |
||
190 | os.environ['exabgp_api_pipename'] = pipename |
||
191 | |||
192 | log.info('named pipes for the cli are:', 'cli control') |
||
193 | log.info('to send commands %s%s.in' % (pipe, pipename), 'cli control') |
||
194 | log.info('to read responses %s%s.out' % (pipe, pipename), 'cli control') |
||
195 | |||
196 | if not env.profile.enable: |
||
197 | exit_code = Reactor(configurations).run(validate, ROOT) |
||
198 | __exit(env.debug.memory, exit_code) |
||
199 | |||
200 | try: |
||
201 | import cProfile as profile |
||
202 | except ImportError: |
||
203 | import profile |
||
204 | |||
205 | if env.profile.file == 'stdout': |
||
206 | profiled = 'Reactor(%s).run(%s,"%s")' % (str(configurations), str(validate), str(ROOT)) |
||
207 | exit_code = profile.run(profiled) |
||
208 | __exit(env.debug.memory, exit_code) |
||
209 | |||
210 | if pid: |
||
211 | profile_name = "%s-pid-%d" % (env.profile.file, pid) |
||
212 | else: |
||
213 | profile_name = env.profile.file |
||
214 | |||
215 | notice = '' |
||
216 | if os.path.isdir(profile_name): |
||
217 | notice = 'profile can not use this filename as output, it is not a directory (%s)' % profile_name |
||
218 | if os.path.exists(profile_name): |
||
219 | notice = 'profile can not use this filename as output, it already exists (%s)' % profile_name |
||
220 | |||
221 | if not notice: |
||
222 | cwd = os.getcwd() |
||
223 | log.debug('profiling ....', 'reactor') |
||
224 | profiler = profile.Profile() |
||
225 | profiler.enable() |
||
226 | try: |
||
227 | exit_code = Reactor(configurations).run(validate, ROOT) |
||
228 | except Exception: |
||
229 | exit_code = Reactor.Exit.unknown |
||
230 | raise |
||
231 | finally: |
||
232 | from exabgp.vendoring import lsprofcalltree |
||
233 | |||
234 | profiler.disable() |
||
235 | kprofile = lsprofcalltree.KCacheGrind(profiler) |
||
236 | try: |
||
237 | destination = profile_name if profile_name.startswith('/') else os.path.join(cwd, profile_name) |
||
238 | with open(destination, 'w+') as write: |
||
239 | kprofile.output(write) |
||
240 | except IOError: |
||
241 | notice = 'could not save profiling in formation at: ' + destination |
||
242 | log.debug("-" * len(notice), 'reactor') |
||
243 | log.debug(notice, 'reactor') |
||
244 | log.debug("-" * len(notice), 'reactor') |
||
245 | __exit(env.debug.memory, exit_code) |
||
246 | else: |
||
247 | log.debug("-" * len(notice), 'reactor') |
||
248 | log.debug(notice, 'reactor') |
||
249 | log.debug("-" * len(notice), 'reactor') |
||
250 | Reactor(configurations).run(validate, ROOT) |
||
251 | __exit(env.debug.memory, 1) |
||
252 | |||
263 |