Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # ex:ts=4:sw=4:sts=4:et |
| 4 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 5 | # |
| 6 | # Copyright (C) 2015 Alexandru Damian for Intel Corp. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | |
| 21 | # Program to run the next task listed from the backlog.txt; designed to be |
| 22 | # run from crontab. |
| 23 | |
| 24 | from __future__ import print_function |
| 25 | import sys, os, config, shellutils |
| 26 | from shellutils import ShellCmdException |
| 27 | |
| 28 | # Import smtplib for the actual sending function |
| 29 | import smtplib |
| 30 | |
| 31 | # Import the email modules we'll need |
| 32 | from email.mime.text import MIMEText |
| 33 | |
| 34 | def _take_lockfile(): |
| 35 | return shellutils.lockfile(shellutils.mk_lock_filename()) |
| 36 | |
| 37 | |
| 38 | def read_next_task_by_state(task_state, task_name=None): |
| 39 | if not os.path.exists(os.path.join(os.path.dirname(__file__), config.BACKLOGFILE)): |
| 40 | return None |
| 41 | os.rename(config.BACKLOGFILE, config.BACKLOGFILE + ".tmp") |
| 42 | task = None |
| 43 | with open(config.BACKLOGFILE + ".tmp", "r") as f_in: |
| 44 | with open(config.BACKLOGFILE, "w") as f_out: |
| 45 | for line in f_in.readlines(): |
| 46 | if task is None: |
| 47 | fields = line.strip().split("|", 2) |
| 48 | if fields[1] == task_state: |
| 49 | if task_name is None or task_name == fields[0]: |
| 50 | task = fields[0] |
| 51 | print("Updating %s %s to %s" % (task, task_state, config.TASKS.next_task(task_state))) |
| 52 | line = "%s|%s\n" % (task, config.TASKS.next_task(task_state)) |
| 53 | f_out.write(line) |
| 54 | os.remove(config.BACKLOGFILE + ".tmp") |
| 55 | return task |
| 56 | |
| 57 | def send_report(task_name, plaintext, errtext=None): |
| 58 | if errtext is None: |
| 59 | msg = MIMEText(plaintext) |
| 60 | else: |
| 61 | if plaintext is None: |
| 62 | plaintext = "" |
| 63 | msg = MIMEText("--STDOUT dump--\n\n%s\n\n--STDERR dump--\n\n%s" % (plaintext, errtext)) |
| 64 | |
| 65 | msg['Subject'] = "[review-request] %s - smoke test results" % task_name |
| 66 | msg['From'] = config.OWN_EMAIL_ADDRESS |
| 67 | msg['To'] = config.REPORT_EMAIL_ADDRESS |
| 68 | |
| 69 | smtp_connection = smtplib.SMTP("localhost") |
| 70 | smtp_connection.sendmail(config.OWN_EMAIL_ADDRESS, [config.REPORT_EMAIL_ADDRESS], msg.as_string()) |
| 71 | smtp_connection.quit() |
| 72 | |
| 73 | def main(): |
| 74 | # we don't do anything if we have another instance of us running |
| 75 | lock_file = _take_lockfile() |
| 76 | |
| 77 | if lock_file is None: |
| 78 | if config.DEBUG: |
| 79 | print("Concurrent script in progress, exiting") |
| 80 | sys.exit(1) |
| 81 | |
| 82 | next_task = read_next_task_by_state(config.TASKS.PENDING) |
| 83 | if next_task is not None: |
| 84 | print("Next task is", next_task) |
| 85 | errtext = None |
| 86 | out = None |
| 87 | try: |
| 88 | out = shellutils.run_shell_cmd("%s %s" % (os.path.join(os.path.dirname(__file__), "runner.py"), next_task)) |
| 89 | except ShellCmdException as exc: |
| 90 | print("Failed while running the test runner: %s", exc) |
| 91 | errtext = exc.__str__() |
| 92 | send_report(next_task, out, errtext) |
| 93 | read_next_task_by_state(config.TASKS.INPROGRESS, next_task) |
| 94 | else: |
| 95 | print("No task") |
| 96 | |
| 97 | shellutils.unlockfile(lock_file) |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | main() |