import os, sys import re import shutil import tempfile import smo import recreate_db import script_db import depends_db def drop (db, dependencies): for type, (schema, name) in reversed (list (dependencies)): sql = "DROP %s %s.%s" % (type, schema, name) print sql db.ExecuteNonQuery (sql) def main (argv=[]): ARGS = CONNECTION_STRING, NAMES= ["", ".*"] connection_string, names = [i or j for (i, j) in zip (argv + [None for _ in ARGS], [None for _ in ARGS])] if connection_string is None: connection_string = raw_input ("Connection ([user:pwd@]SVR/DB): [%s] " % CONNECTION_STRING) or CONNECTION_STRING if names is None: names = raw_input ("Names: [%s] " % NAMES) or NAMES print "You are about to drop all objects matching %s from database %s" % (names, connection_string) if raw_input ("If you are sure, type YES. Otherwise stop now: ") <> "YES": raise RuntimeError, "Stopped before it was too late" username, password, server, database = re.match (r"^(?:(\w+):?(\w+)?@)?(\w+)/(\w+)$", connection_string).groups () db = smo.connection (server, database, username, password) root = tempfile.mkdtemp () script_db.script (db, names, root) try: dependencies = depends_db.dependencies (root) drop (db, dependencies) finally: shutil.rmtree (root) if __name__ == '__main__': main (sys.argv[1:])