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