Skip to content

Instantly share code, notes, and snippets.

@swarminglogic
Last active December 17, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swarminglogic/5619183 to your computer and use it in GitHub Desktop.
Save swarminglogic/5619183 to your computer and use it in GitHub Desktop.
Monitors executables in a directory (passed as argument). Executables that are modified (based on md5sum) or introduced, are executed.
#!/usr/bin/env python
import sys, os, commands, time, errno
def getExecutablesAndMd5(directory):
"""
Returns a dictionary with the following.
Key: Paths to executable within specified directory.
Value: The md5sums of the executable.
"""
execs = {}
for d in os.listdir(directory):
filePath = os.path.join(directory,d)
if (os.path.isfile(filePath) and os.access(filePath, os.X_OK)):
execs[filePath] = commands.getoutput('md5sum ' + filePath)
return execs
def sanityCheck():
if len(sys.argv) < 2:
print 'Not enough arguments. Specify directory to monitor.'
exit(errno.EINVAL)
monitorDir = str(sys.argv[1])
if not os.path.isdir(monitorDir):
print 'Directory "' + monitorDir + '" doesn\'t exist!'
exit(errno.ENOTDIR)
def main():
"""
Monitors executables in a directory (passed as argument).
Executables that are modified (based on md5sum) or introduced,
are executed.
External requirements: md5sum
"""
sanityCheck()
os.system('clear')
print 'Waiting for executables.'
monitorDir = str(sys.argv[1])
execs = getExecutablesAndMd5(monitorDir)
loopsSinceClear = 0
while (True):
execsNow = getExecutablesAndMd5(monitorDir)
# Loop through to see if tests should be run.
areThereTestsToRun = False
for k in execsNow:
isNew = not execs.has_key(k)
isUpdated = execs.has_key(k) and execsNow[k] != execs[k]
if isNew or isUpdated:
areThereTestsToRun = True
break
if (areThereTestsToRun):
# Only clear screen if >6 iterations have passed by
# without tests being modified.
if loopsSinceClear > 6:
os.system('clear')
loopsSinceClear = 0
# Execute tests that are new or updated.
for k in execsNow:
isNew = not execs.has_key(k)
isUpdated = execs.has_key(k) and execsNow[k] != execs[k]
if isNew or isUpdated:
print commands.getoutput(k)
else:
loopsSinceClear += 1
execs = execsNow
time.sleep(0.5);
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment