Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

command-line script for professor

  • Loading branch information...
commit 4dd566cf9653b3defabdaddb28287b0136cbb8e1 1 parent d009e1a
@dcrosta authored
Showing with 105 additions and 0 deletions.
  1. +105 −0 professor/scripts.py
View
105 professor/scripts.py
@@ -0,0 +1,105 @@
+# Copyright (c) 2011, Daniel Crosta
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+# this list of conditions and the following disclaimer.
+#
+# * Redistributions in binary form must reproduce the above copyright notice,
+# this list of conditions and the following disclaimer in the documentation
+# and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+import argparse
+from pymongo import ASCENDING, DESCENDING
+import re
+
+from professor import db
+from professor.logic import *
+
+db_name = re.compile('(\w+)/(\w+)')
+
+def do_list(parser, args):
+ print 'Known Databases:'
+ for d in db.databases.find().sort([('hostname', ASCENDING), ('dbname', ASCENDING)]):
+ print " %s/%s" % (d['hostname'], d['dbname'])
+
+def get_database(parser, args):
+ m = db_name.match(args.database)
+ if not m:
+ parser.error('"%s" is not a valid database' % args.database)
+
+ hostname, dbname = m.groups()
+ database = db.databases.find_one({'hostname': hostname, 'dbname': dbname})
+ if not database:
+ parser.error('"%s" is not a known database' % args.database)
+
+ return database
+
+def do_update(parser, args):
+ database = get_database(parser, args)
+ count = update(database)
+ print "updated %d entries" % count
+
+def do_reset(parser, args):
+ database = get_database(parser, args)
+ db.profiles.remove({'database': database['_id']})
+ db.databases.update({'_id': database['_id']}, {'$set': {'timestamp': None}})
+ print "reset %s" % args.database
+
+def do_clean(parser, args):
+ database = get_database(parser, args)
+ db.profiles.remove({'database': database['_id']})
+ print "cleaned %s" % args.database
+
+def do_remove(parser, args):
+ database = get_database(parser, args)
+ db.databases.remove({'_id': database['_id']})
+ db.profiles.remove({'database': database['_id']})
+ print "removed %s" % args.database
+
+
+
+def profess():
+ parser = argparse.ArgumentParser(description='Painless MongoDB Profiling')
+ commands = parser.add_subparsers()
+
+ list = commands.add_parser('list', help='List databases known to professor')
+ list.set_defaults(cmd=do_list)
+
+ reset = commands.add_parser('reset', help='Erase profiling information and reset last sync timestamp')
+ reset.set_defaults(cmd=do_reset)
+ reset.add_argument('database', help='Database to clean')
+
+ update = commands.add_parser('update', help="Update a database's profiling information")
+ update.set_defaults(cmd=do_update)
+ update.add_argument('database', help='Database to update')
+
+ clean = commands.add_parser('clean', help='Delete existing profiling information for database')
+ clean.set_defaults(cmd=do_clean)
+ clean.add_argument('database', help='Database to clean')
+
+ remove = commands.add_parser('remove', help='Completely remove a database from professor')
+ remove.set_defaults(cmd=do_remove)
+ remove.add_argument('database', help='Database to remove')
+
+
+ args = parser.parse_args()
+ args.cmd(parser, args)
+
+if __name__ == '__main__':
+ profess()
+
Please sign in to comment.
Something went wrong with that request. Please try again.