| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# @HEADER |
| 4 |
# ***************************************************************************** |
| 5 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 6 |
# |
| 7 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 8 |
# SPDX-License-Identifier: BSD-3-Clause |
| 9 |
# ***************************************************************************** |
| 10 |
# @HEADER |
| 11 |
|
| 12 |
|
| 13 |
# TODO: Put a time limit on the command so that it will not run past the |
| 14 |
# RUNTILL time. You could do this by running a cmake -P driver script that |
| 15 |
# calls EXECUTE_PROCESS(...). There is also some python code that exists out |
| 16 |
# there that could also implement a time limit but it is complex. |
| 17 |
|
| 18 |
|
| 19 |
# |
| 20 |
# Read in the command-line arguments |
| 21 |
# |
| 22 |
|
| 23 |
usageHelp = r"""generic-looping-demon.py [OPTIONS] |
| 24 |
|
| 25 |
This simple program takes a command as input and runs it over and over again |
| 26 |
(pausing in-between iterations for a given time) and then stops at the given |
| 27 |
absolute time. |
| 28 |
|
| 29 |
The reason that the script takes an absolute time instead of a relative time |
| 30 |
is that this script is designed to drive continuous integration (CI) processes |
| 31 |
where the CI process should shut down at some point. |
| 32 |
|
| 33 |
NOTE: The last iteration will not start later than --run-till=RUNTILL but the |
| 34 |
last command could run until significantly after that time. |
| 35 |
""" |
| 36 |
|
| 37 |
from optparse import OptionParser |
| 38 |
|
| 39 |
clp = OptionParser(usage=usageHelp) |
| 40 |
|
| 41 |
clp.add_option( |
| 42 |
"--command", dest="command", type="string", default="", |
| 43 |
help="The shell command that will get run in the loop" ) |
| 44 |
|
| 45 |
clp.add_option( |
| 46 |
"--loop-interval", dest="loopInterval", type="string", default="", |
| 47 |
help="Input to the standard unix/linux 'sleep' command" \ |
| 48 |
+" (e.g. '60s') to space out iterations of the script.") |
| 49 |
|
| 50 |
dateFormat = "%Y-%m-%d" |
| 51 |
timeFormat = "%H:%M:%S" |
| 52 |
dateTimeFormat = dateFormat+" "+timeFormat |
| 53 |
clp.add_option( |
| 54 |
"--run-till", dest="runTill", type="string", default="", |
| 55 |
help="The absolute time the script will run iterations till." \ |
| 56 |
+" This takes the format "+dateTimeFormat+" (e.g. 2011-06-09 18:00:00).") |
| 57 |
|
| 58 |
clp.add_option( |
| 59 |
"--today-run-till", dest="todayRunTill", type="string", default="", |
| 60 |
help="The time today the script will run iterations till." \ |
| 61 |
+" This takes the format "+timeFormat+" (e.g. 18:00:00).") |
| 62 |
|
| 63 |
clp.add_option( |
| 64 |
"--pause-file", dest="pauseFile", type="string", default="", |
| 65 |
help="The name of a file, that if exists, will prevent command" \ |
| 66 |
+" from being run.") |
| 67 |
|
| 68 |
|
| 69 |
(options, args) = clp.parse_args() |
| 70 |
|
| 71 |
|
| 72 |
# |
| 73 |
# Check the input arguments |
| 74 |
# |
| 75 |
|
| 76 |
import sys |
| 77 |
|
| 78 |
if not options.command: |
| 79 |
print("\nError, you must set the --command argument!") |
| 80 |
sys.exit(1) |
| 81 |
|
| 82 |
if not options.loopInterval: |
| 83 |
print("\nError, you must set the --loop-interval argument!") |
| 84 |
sys.exit(1) |
| 85 |
|
| 86 |
if not (options.runTill or options.todayRunTill): |
| 87 |
print("\nError, you must set either the --run-till or --today-run-till argument!") |
| 88 |
sys.exit(1) |
| 89 |
|
| 90 |
|
| 91 |
# |
| 92 |
# Echo the command-line |
| 93 |
# |
| 94 |
|
| 95 |
print("") |
| 96 |
print("**************************************************************************") |
| 97 |
print("Script: generic-looping-demon.py \\") |
| 98 |
print(" --command='" + options.command + "' \\") |
| 99 |
print(" --loop-interval='" + options.loopInterval + "' \\") |
| 100 |
|
| 101 |
if options.runTill: |
| 102 |
print(" --run-till='" + options.runTill + "' \\") |
| 103 |
if options.todayRunTill: |
| 104 |
print(" --run-till='" + options.todayRunTill + "' \\") |
| 105 |
print(" --pause-file='" + options.pauseFile + "' \\") |
| 106 |
|
| 107 |
|
| 108 |
# |
| 109 |
# Helper functions |
| 110 |
# |
| 111 |
|
| 112 |
# Parses "%Y-%m-%d %H:%M:%S" into a datetime.datetime object |
| 113 |
def parseDateTimeString(dateTimeStr): |
| 114 |
(dateStr, timeStr) = dateTimeStr.split(" ") |
| 115 |
(year, month, day) = dateStr.split("-") |
| 116 |
(hour, minute, second) = timeStr.split(":") |
| 117 |
return datetime.datetime(int(year), int(month), int(day), |
| 118 |
int(hour), int(minute), int(second)) |
| 119 |
|
| 120 |
|
| 121 |
def formatDateTime(dateTimeObj): |
| 122 |
return datetime.datetime.strftime(dateTimeObj, dateTimeFormat) |
| 123 |
|
| 124 |
|
| 125 |
def pauseFileExists(pauseFile): |
| 126 |
if pauseFile and os.path.exists(pauseFile): |
| 127 |
return True |
| 128 |
return False |
| 129 |
|
| 130 |
# |
| 131 |
# Executable statements |
| 132 |
# |
| 133 |
|
| 134 |
import os |
| 135 |
import datetime |
| 136 |
|
| 137 |
scriptsDir = os.path.abspath(os.path.dirname(sys.argv[0]))+"/cmake/python" |
| 138 |
sys.path.insert(0, scriptsDir) |
| 139 |
|
| 140 |
from GeneralScriptSupport import * |
| 141 |
|
| 142 |
if options.runTill: |
| 143 |
finalDateTime = parseDateTimeString(options.runTill) |
| 144 |
elif options.todayRunTill: |
| 145 |
todayDate = datetime.datetime.today() |
| 146 |
todayDateStr = datetime.datetime.strftime(todayDate, dateFormat) |
| 147 |
finalDateTime = parseDateTimeString(todayDateStr+" "+options.todayRunTill) |
| 148 |
|
| 149 |
if pauseFileExists(options.pauseFile): |
| 150 |
print("\nThe file " + options.pauseFile + " exists at start so deleting it!") |
| 151 |
os.remove(options.pauseFile) |
| 152 |
|
| 153 |
print("\nThe script will run iterations till = " + formatDateTime(finalDateTime) |
| 154 |
+ "\n") |
| 155 |
|
| 156 |
currentTime = datetime.datetime.now() |
| 157 |
iteration = 0 |
| 158 |
|
| 159 |
while currentTime < finalDateTime: |
| 160 |
print("*********************************************************************" |
| 161 |
"********\n" + str(iteration) + ":" + " current time = " + |
| 162 |
formatDateTime(currentTime) + ", final time = " + |
| 163 |
formatDateTime(finalDateTime)) |
| 164 |
if pauseFileExists(options.pauseFile): |
| 165 |
print("\nThe file " + options.pauseFile + " exists so skipping this iteration!") |
| 166 |
else: |
| 167 |
echoRunSysCmnd(options.command, throwExcept=False, timeCmnd=True) |
| 168 |
echoRunSysCmnd("sleep "+options.loopInterval) |
| 169 |
currentTime = datetime.datetime.now() |
| 170 |
iteration += 1 |