| Conditions | 26 |
| Total Lines | 108 |
| Code Lines | 79 |
| 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.cmdline() 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 |
||
| 58 | def cmdline(cmdarg): |
||
| 59 | if not os.path.isfile(ENVFILE): |
||
| 60 | comment = 'environment file missing\ngenerate it using "exabgp env --fi > %s"' % ENVFILE |
||
| 61 | else: |
||
| 62 | comment = '' |
||
| 63 | |||
| 64 | # Must be done before setting the logger as it modify its behaviour |
||
| 65 | if cmdarg.debug: |
||
| 66 | env = getenv() |
||
| 67 | env.log.all = True |
||
| 68 | env.log.level = syslog.LOG_DEBUG |
||
| 69 | |||
| 70 | log.init() |
||
| 71 | |||
| 72 | duration = cmdarg.signal |
||
| 73 | if duration and duration.isdigit(): |
||
| 74 | pid = os.fork() |
||
| 75 | if pid: |
||
| 76 | import time |
||
| 77 | import signal |
||
| 78 | |||
| 79 | try: |
||
| 80 | time.sleep(int(duration)) |
||
| 81 | os.kill(pid, signal.SIGUSR1) |
||
| 82 | except KeyboardInterrupt: |
||
| 83 | pass |
||
| 84 | try: |
||
| 85 | pid, code = os.wait() |
||
| 86 | sys.exit(code) |
||
| 87 | except KeyboardInterrupt: |
||
| 88 | try: |
||
| 89 | pid, code = os.wait() |
||
| 90 | sys.exit(code) |
||
| 91 | except Exception: |
||
| 92 | sys.exit(0) |
||
| 93 | |||
| 94 | if cmdarg.profile: |
||
| 95 | env.profile.enable = True |
||
|
|
|||
| 96 | if cmdarg.profile.lower() in ['1', 'true']: |
||
| 97 | env.profile.file = True |
||
| 98 | elif cmdarg.profile.lower() in ['0', 'false']: |
||
| 99 | env.profile.file = False |
||
| 100 | else: |
||
| 101 | env.profile.file = cmdarg.profile |
||
| 102 | |||
| 103 | if cmdarg.once: |
||
| 104 | env.tcp.once = True |
||
| 105 | |||
| 106 | if cmdarg.pdb: |
||
| 107 | env.debug.pdb = True |
||
| 108 | |||
| 109 | if cmdarg.test: |
||
| 110 | env.debug.selfcheck = True |
||
| 111 | env.log.parser = True |
||
| 112 | |||
| 113 | if cmdarg.memory: |
||
| 114 | env.debug.memory = True |
||
| 115 | |||
| 116 | configurations = [] |
||
| 117 | # check the file only once that we have parsed all the command line options and allowed them to run |
||
| 118 | for f in cmdarg.configuration: |
||
| 119 | # some users are using symlinks for atomic change of the configuration file |
||
| 120 | # using mv may however be better practice :p |
||
| 121 | normalised = os.path.realpath(os.path.normpath(f)) |
||
| 122 | target = os.path.realpath(normalised) |
||
| 123 | if os.path.isfile(target): |
||
| 124 | configurations.append(normalised) |
||
| 125 | continue |
||
| 126 | if f.startswith('etc/exabgp'): |
||
| 127 | normalised = os.path.join(ETC, f[11:]) |
||
| 128 | if os.path.isfile(normalised): |
||
| 129 | configurations.append(normalised) |
||
| 130 | continue |
||
| 131 | |||
| 132 | log.debug('one of the arguments passed as configuration is not a file (%s)' % f, 'configuration') |
||
| 133 | sys.exit(1) |
||
| 134 | |||
| 135 | from exabgp.bgp.message.update.attribute import Attribute |
||
| 136 | |||
| 137 | Attribute.caching = env.cache.attributes |
||
| 138 | |||
| 139 | if env.debug.rotate or len(configurations) == 1: |
||
| 140 | run(env, comment, configurations, cmdarg.validate) |
||
| 141 | |||
| 142 | log.error('can not log to files when running multiple configuration (as we fork)', 'configuration') |
||
| 143 | sys.exit(1) |
||
| 144 | |||
| 145 | try: |
||
| 146 | # run each configuration in its own process |
||
| 147 | pids = [] |
||
| 148 | for configuration in configurations: |
||
| 149 | pid = os.fork() |
||
| 150 | if pid == 0: |
||
| 151 | run(env, comment, [configuration], cmdarg.validate, os.getpid()) |
||
| 152 | else: |
||
| 153 | pids.append(pid) |
||
| 154 | |||
| 155 | # If we get a ^C / SIGTERM, ignore just continue waiting for our child process |
||
| 156 | import signal |
||
| 157 | |||
| 158 | signal.signal(signal.SIGINT, signal.SIG_IGN) |
||
| 159 | |||
| 160 | # wait for the forked processes |
||
| 161 | for pid in pids: |
||
| 162 | os.waitpid(pid, 0) |
||
| 163 | except OSError as exc: |
||
| 164 | log.critical('can not fork, errno %d : %s' % (exc.errno, exc.strerror), 'reactor') |
||
| 165 | sys.exit(1) |
||
| 166 | |||
| 277 |