Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Python script that finds and runs other scripts, passing them each an open Database connection.
import glob
import sys
import os
import sqlite3 as sqlite
def LoadScripts(scriptFolder):
''' Returns a list of module references '''
# Find Script Files:
scriptFiles = glob.glob(os.path.join(scriptFolder, "Script*.py"))
print "Found %d Script Files" % (len(scriptFiles))
# Load scripts:
loadedScripts = []
for scriptFile in scriptFiles:
# Get full module name:
scriptName = os.path.basename(scriptFile)
scriptName = os.path.splitext(scriptName)[0]
folderName = os.path.basename(scriptFolder)
fullModuleName = "%s.%s" % (folderName, scriptName)
# Import Script:
script_module = __import__(fullModuleName, fromlist=["fullModuleName"])
# Store reference to Script:
except Exception, e:
print "Something went wrong while loading script file \'%s\'" % scriptFile
print e
return loadedScripts
def RunScripts(databaseFilename, loadedScripts):
# Open DB:
connection = sqlite.connect(databaseFilename)
# Keep a list of completed scripts so scripts can test if they are ready to run:
namesOfCompletedScripts = []
success = True
# Keep passing over list until everything has been run:
while (len(namesOfCompletedScripts) < len(loadedScripts)):
nothingExecutedOnThisPass = True
# Iterate over scripts:
for script in loadedScripts:
# Check if this script has been run or not:
if (script.Name not in namesOfCompletedScripts):
# Ask script if it is ready to run -
# - give it a list of completed scripts:
if (script.CanRun(connection, namesOfCompletedScripts)):
# Run script:
nothingExecutedOnThisPass = False
print "\n%s not ready to run" % script.Name
except Exception, e:
print "Something went wrong while running script %s" % script.Name
print e
success = False
if (nothingExecutedOnThisPass):
# If nothing executed on this pass, we're stuck -
# - the remaining scripts won't run
print "\nThe following scripts were successfully run:"
print namesOfCompletedScripts
if (len(namesOfCompletedScripts) < len(loadedScripts)):
print "Some scripts failed:"
for script in loadedScripts:
if (script.Name not in namesOfCompletedScripts):
print "\t" + script.Name
except Exception, e:
print "Something went wrong while applying scripts to DB"
print e
# Close DB:
if __name__ == "__main__":
if (len(sys.argv) != 3):
print "\n\tRequires two arguments:"
print "\n\t\ {databasefilename} {scriptfolder}\n\n"
databaseFilename = sys.argv[1]
scriptFolder = sys.argv[2]
# Load Scripts:
loadedScripts = LoadScripts(scriptFolder)
RunScripts(databaseFilename, loadedScripts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment