Completed
Pull Request — master (#135)
by
unknown
01:45
created

main()   F

Complexity

Conditions 10

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 22
Bugs 0 Features 0
Metric Value
cc 10
c 22
b 0
f 0
dl 0
loc 49
rs 3.7894

How to fix   Complexity   

Complexity

Complex classes like main() 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
#!/usr/bin/env python2
2
# -*- coding: utf-8 -*-
3
4
import os
5
import argparse
6
import subprocess
7
import logging
8
import time as t
9
10
from smartdispatch import utils
11
from smartdispatch.command_manager import CommandManager
12
13
14
def parse_arguments():
15
    parser = argparse.ArgumentParser()
16
    parser.add_argument('commands_filename', type=str, help='File containing all commands to execute.')
17
    parser.add_argument('logs_dir', type=str, help="Folder where to put commands' stdout and stderr.")
18
    args = parser.parse_args()
19
20
    # Check for invalid arguments
21
    if not os.path.isfile(args.commands_filename):
22
        parser.error("Invalid file path. Specify path to a file containing commands.")
23
24
    if not os.path.isdir(args.logs_dir):
25
        parser.error("You need to specify the folder path where to put command' stdout and stderr.")
26
27
    return args
28
29
30
def main():
31
    # Necessary if we want 'logging.info' to appear in stderr.
32
    logging.root.setLevel(logging.INFO)
33
34
    args = parse_arguments()
35
36
    command_manager = CommandManager(args.commands_filename)
37
38
    while True:
39
        command = command_manager.get_command_to_run()
40
41
        if command is None:
42
            break
43
44
        uid = utils.generate_uid_from_string(command)
45
        stdout_filename = os.path.join(args.logs_dir, uid + ".out")
46
        stderr_filename = os.path.join(args.logs_dir, uid + ".err")
47
48
        # Get job and node ID
49
        try:
50
            job = os.environ['PBS_JOBID']
51
        except KeyError:
52
            job = 'undefined'
53
54
        try:
55
            with open(os.environ['PBS_NODEFILE']) as nodefile:
56
                node = ""
57
                for line in nodefile:
58
                    node += line + ','
59
                node = str.rstrip(node, ' ,\n')
60
        except KeyError:
61
            node = 'undefined'
62
63
        with open(stdout_filename, 'a') as stdout_file:
64
            with open(stderr_filename, 'a') as stderr_file:
65
                log_datetime = t.strftime("## SMART-DISPATCH - Started on: %Y-%m-%d %H:%M:%S - In job: {job} - On nodes: {node} ##\n".format(job=job, node=node))
66
                if stdout_file.tell() > 0:  # Not the first line in the log file.
67
                    log_datetime = t.strftime("\n## SMART-DISPATCH - Resumed on: %Y-%m-%d %H:%M:%S - In job: {job} - On nodes: {node} ##\n".format(job=job, node=node))
68
69
                log_command = "## SMART-DISPATCH - Command: " + command + '\n'
70
71
                stdout_file.write(log_datetime + log_command)
72
                stdout_file.flush()
73
                stderr_file.write(log_datetime + log_command)
74
                stderr_file.flush()
75
76
                error_code = subprocess.call(command, stdout=stdout_file, stderr=stderr_file, shell=True)
77
78
        command_manager.set_running_command_as_finished(command, error_code)
79
80
if __name__ == '__main__':
81
    main()
82