| Conditions | 4 |
| Total Lines | 52 |
| Code Lines | 33 |
| 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:
| 1 | import ConfigParser |
||
| 50 | def run(self): |
||
| 51 | try: |
||
| 52 | logger.info("Working for job URL: " + self.joburl) |
||
| 53 | |||
| 54 | # Create tmp directories |
||
| 55 | tmpdir = tempfile.mkdtemp() |
||
| 56 | tmpfile = tempfile.NamedTemporaryFile(dir=tmpdir, delete=False) |
||
| 57 | |||
| 58 | # Fetch input data and store it |
||
| 59 | input_data = urllib2.urlopen(self.joburl).read() |
||
| 60 | tmpfile.write(input_data) |
||
| 61 | # logger.debug(input_data) |
||
| 62 | tmpfile.close() |
||
| 63 | |||
| 64 | # There trick is that we do not need to know the operational details |
||
| 65 | # of this job here, since the calling convention comes from daemon.ini |
||
| 66 | # and the input file format is determined by the web server on download. |
||
| 67 | # Alle backend executables are just expected to follow the same |
||
| 68 | # command-line pattern as render.py. |
||
| 69 | cmd = "%s %s %s %s %s" % (backends[self.jobtype]['executable'], |
||
| 70 | tmpfile.name, |
||
| 71 | tmpdir + os.sep + |
||
| 72 | backends[self.jobtype]['output'], |
||
| 73 | tmpdir, |
||
| 74 | backends[self.jobtype]['log_file']) |
||
| 75 | logger.info("Running " + cmd) |
||
| 76 | output_file = backends[self.jobtype]['output'] |
||
| 77 | |||
| 78 | # Run command synchronousely and wait for the exit code |
||
| 79 | exit_code = os.system(cmd) |
||
| 80 | if exit_code == 0: |
||
| 81 | logger.info("Exit code 0, preparing result upload") |
||
| 82 | # multiple result file upload not implemented |
||
| 83 | assert(not output_file.startswith("*")) |
||
| 84 | with open(tmpdir + os.sep + output_file, "rb") as fd: |
||
| 85 | data = fd.read() |
||
| 86 | self.sendResult(0, data, output_file) |
||
| 87 | # logger.debug(data) |
||
| 88 | else: |
||
| 89 | logger.error("Error on execution: Exit code " + str(exit_code)) |
||
| 90 | logger.error( |
||
| 91 | "Saving input file for later reference: /tmp/lastinput.xml") |
||
| 92 | os.system("cp %s /tmp/lastinput.xml" % tmpfile.name) |
||
| 93 | self.sendResult(exit_code) |
||
| 94 | |||
| 95 | except Exception as e: |
||
| 96 | logger.debug( |
||
| 97 | 'Exception, delivering -1 exit code to frontend: ' + str(e)) |
||
| 98 | self.sendResult(-1) |
||
| 99 | |||
| 100 | finally: |
||
| 101 | shutil.rmtree(tmpdir, ignore_errors=True) |
||
|
|
|||
| 102 | |||
| 144 |