1
|
|
|
#!/usr/bin/env python |
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
|
|
|
with open(stdout_filename, 'a') as stdout_file: |
49
|
|
|
with open(stderr_filename, 'a') as stderr_file: |
50
|
|
|
log_datetime = t.strftime("## SMART_DISPATCH - Started on: %Y-%m-%d %H:%M:%S ##\n") |
51
|
|
|
if stdout_file.tell() > 0: # Not the first line in the log file. |
52
|
|
|
log_datetime = t.strftime("\n## SMART_DISPATCH - Resumed on: %Y-%m-%d %H:%M:%S ##\n") |
53
|
|
|
|
54
|
|
|
log_command = "## SMART_DISPATCH - Command: " + command + '\n' |
55
|
|
|
|
56
|
|
|
stdout_file.write(log_datetime + log_command) |
57
|
|
|
stdout_file.flush() |
58
|
|
|
stderr_file.write(log_datetime + log_command) |
59
|
|
|
stderr_file.flush() |
60
|
|
|
|
61
|
|
|
error_code = subprocess.call(command, stdout=stdout_file, stderr=stderr_file, shell=True) |
62
|
|
|
|
63
|
|
|
command_manager.set_running_command_as_finished(command, error_code) |
64
|
|
|
|
65
|
|
|
if __name__ == '__main__': |
66
|
|
|
main() |
67
|
|
|
|