launcher.py 3.6 KB

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