gump-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosim...@apache.org
Subject svn commit: r123508 - /gump/trunk/python/gump/actor/mysql/dynagumper.py /gump/trunk/python/gump/core/runner/runner.py /gump/trunk/python/gump/test/mockobjects.py /gump/trunk/python/gump/test/testDynagumper.py /gump/trunk/python/gump/util/mysql.py
Date Tue, 28 Dec 2004 13:59:16 GMT
Author: leosimons
Date: Tue Dec 28 05:59:15 2004
New Revision: 123508

URL: http://svn.apache.org/viewcvs?view=rev&rev=123508
Log:
Refactoring dynagump stuff. Create a new Database object to feed into the dynagumper actor
so that it can forget about MySQL alltogether and the same holds true for the runner.
Modified:
   gump/trunk/python/gump/actor/mysql/dynagumper.py
   gump/trunk/python/gump/core/runner/runner.py
   gump/trunk/python/gump/test/mockobjects.py
   gump/trunk/python/gump/test/testDynagumper.py
   gump/trunk/python/gump/util/mysql.py

Modified: gump/trunk/python/gump/actor/mysql/dynagumper.py
Url: http://svn.apache.org/viewcvs/gump/trunk/python/gump/actor/mysql/dynagumper.py?view=diff&rev=123508&p1=gump/trunk/python/gump/actor/mysql/dynagumper.py&r1=123507&p2=gump/trunk/python/gump/actor/mysql/dynagumper.py&r2=123508
==============================================================================
--- gump/trunk/python/gump/actor/mysql/dynagumper.py	(original)
+++ gump/trunk/python/gump/actor/mysql/dynagumper.py	Tue Dec 28 05:59:15 2004
@@ -17,12 +17,8 @@
 __copyright__ = "Copyright (c) 2004 The Apache Software Foundation"
 __license__   = "http://www.apache.org/licenses/LICENSE-2.0"
 
-import MySQLdb
-import MySQLdb.cursors
-
 import platform
 
-from gump import log
 from gump.core.run.gumprun import *
 import gump.core.run.actor
 
@@ -31,57 +27,55 @@
     Populate the DynaGump run metadata database.
     """
     
-    def __init__(self,run,conn):
+    def __init__(self,run,db, log = None):
+        """
+        Set up the Dynagumper:
+            Dynagumper(run,database)
+        
+        Run is an instance of GumpRun, db is an instance of Database.
+        Optional argument: log, instance of logging.logger or similar.
+        Will use log from gump.logging if not provided.
+        """
         gump.core.run.actor.AbstractRunActor.__init__(self,run)    
-        self.conn = conn
-
-    def _execute(self, cmd):
-        cursor = self.conn.cursor()
-        cursor.execute(cmd)
-        #try:
-        #    cursor = None    
-        #    try:
-        #        cursor = self.conn.cursor()
-        #        try:
-        #            cursor.execute(cmd)
-        #        except Exception, details:
-        #            log.error('SQL Error on [%s] : %s' % (cmd, details), exc_info=1)
-        #    finally:
-        #        if cursor: cursor.close()
-        #except Exception, details:
-        #    log.error('SQL Connection Error: %s' % (details), exc_info=1)
+        self.db = db
+        
+        if not log: from gump import log
+        self.log = log
     
     def ensureThisHostIsInDatabase(self):
+        """
+        Adds information about this server to the hosts table.
+        """
         (system, host, release, version, machine, processor) = platform.uname()
         tablename = "hosts"
         description = "%s (%s,%s,%s,%s,%s)" % (host, system, release, version, machine, processor)
         cmd = "INSERT INTO %s (address, name, cpu_arch, description) VALUES ('%s', '%s',
'%s', '%s')" \
                 % (tablename, host, host, processor, description)
         
-        self._execute(cmd)
+        self.db.execute(cmd)
         
-    def processOtherEvent(self,event):   
-        # do the actual work right here...
-        log.warning('dynagumper.py processOtherEvent(): need to implement event processing')
+    def processOtherEvent(self,event):
+        #TODO do the actual work right here...
+        self.log.warning('dynagumper.py processOtherEvent: need to implement event processing')
                       
     def processWorkspace(self):
         """
         Add information about the workspace to the database.
         """
-        # do the actual work right here...
-        self.ensureThisHostIsInDatabase()
-        log.warning('dynagumper.py processWorkspace: need to implement workspace event processing')
+        #TODO do the actual work right here...
+        #self.ensureThisHostIsInDatabase()
+        self.log.warning('dynagumper.py processWorkspace: need to implement workspace event
processing')
     
     def processModule(self,module):    
         """
-        Add information about the module to the database.
+        Add information about a module to the database.
         """
-        # do the actual work
-        log.warning('dynagumper.py processModule: need to implement module event processing')
+        #TODO do the actual work
+        self.log.warning('dynagumper.py processModule: need to implement module event processing')
     
     def processProject(self,project):    
         """
-        Add information about the project to the database.
+        Add information about a project to the database.
         """
-        # do the actual work right here...
-        log.warning('dynagumper.py processProject: need to implement project event processing')
+        #TODO do the actual work right here...
+        self.log.warning('dynagumper.py processProject: need to implement project event processing')

Modified: gump/trunk/python/gump/core/runner/runner.py
Url: http://svn.apache.org/viewcvs/gump/trunk/python/gump/core/runner/runner.py?view=diff&rev=123508&p1=gump/trunk/python/gump/core/runner/runner.py&r1=123507&p2=gump/trunk/python/gump/core/runner/runner.py&r2=123508
==============================================================================
--- gump/trunk/python/gump/core/runner/runner.py	(original)
+++ gump/trunk/python/gump/core/runner/runner.py	Tue Dec 28 05:59:15 2004
@@ -157,28 +157,14 @@
 
         # Add Dynagump database populator
         if self.run.getWorkspace().hasDatabaseInformation():
-            try:
-                conn = None
-                try:
-                    # create the database connection
-                    dbInfo = self.run.getWorkspace().getDatabaseInformation()
-                    import MySQLdb
-                    conn = MySQLdb.Connect(
-                        host=dbInfo.getHost(), 
-                        user=dbInfo.getUser(),
-                        passwd=dbInfo.getPasswd(), 
-                        db=dbInfo.getDatabase(),
-                        compress=1,
-                        cursorclass=MySQLdb.cursors.DictCursor)
-    
-                    # now create the Dynagumper
-                    import gump.actor.mysql.dynagumper
-                    self.run.registerActor(gump.actor.mysql.dynagumper.Dynagumper(self.run,conn))
-                finally:
-                    if conn: conn.close()
-            except Exception, details:
-                log.warning('Unable to register Dynagump Database Actor :  %s ' % details,
-                            exc_info=1)
+            # create the database helper
+            dbInfo = self.run.getWorkspace().getDatabaseInformation()
+            from gump.util.mysql import Database
+            database = new Database(dbInfo)
+
+            # now create the Dynagumper using that database
+            import gump.actor.mysql.dynagumper
+            self.run.registerActor(gump.actor.mysql.dynagumper.Dynagumper(self.run,database))
         
         # Document..
         # Use XDOCS if not overridden...

Modified: gump/trunk/python/gump/test/mockobjects.py
Url: http://svn.apache.org/viewcvs/gump/trunk/python/gump/test/mockobjects.py?view=diff&rev=123508&p1=gump/trunk/python/gump/test/mockobjects.py&r1=123507&p2=gump/trunk/python/gump/test/mockobjects.py&r2=123508
==============================================================================
--- gump/trunk/python/gump/test/mockobjects.py	(original)
+++ gump/trunk/python/gump/test/mockobjects.py	Tue Dec 28 05:59:15 2004
@@ -17,6 +17,22 @@
 __copyright__ = "Copyright (c) 2004 The Apache Software Foundation"
 __license__   = "http://www.apache.org/licenses/LICENSE-2.0"
 
+class MockLog:
+    def __init__(self):
+        pass
+    
+    def debug(self,msg):
+        print "DEBUG: %s" % (msg)
+
+    def info(self,msg):
+        print "INFO: %s" % (msg)
+
+    def warning(self,msg):
+        print "WARNING: %s" % (msg)
+
+    def error(self,msg):
+        print "ERROR: %s" % (msg)
+        
 class MockConnection:
     def __init__(self,cursor):
         self._cursor = cursor
@@ -57,13 +73,20 @@
     def getGumpSet(self):
         return self.gumpSet
 
+class MockDatabase:
+    def __init__(self):
+        self.lastCommand = ''
+    
+    def execute(self,command):
+        self.lastCommand = command
+
 class MockObjects:
     def __init__(self):
+        self.log = MockLog()
         self.workspace = MockWorkspace()
         self.options = MockOptions()
         self.gumpSet = MockGumpSet()
         self.run = MockRun(self.workspace,self.options,self.gumpSet)
         self.cursor = MockCursor()
         self.conn = MockConnection(self.cursor)
-
-mocks = MockObjects()
+        self.database = MockDatabase()

Modified: gump/trunk/python/gump/test/testDynagumper.py
Url: http://svn.apache.org/viewcvs/gump/trunk/python/gump/test/testDynagumper.py?view=diff&rev=123508&p1=gump/trunk/python/gump/test/testDynagumper.py&r1=123507&p2=gump/trunk/python/gump/test/testDynagumper.py&r2=123508
==============================================================================
--- gump/trunk/python/gump/test/testDynagumper.py	(original)
+++ gump/trunk/python/gump/test/testDynagumper.py	Tue Dec 28 05:59:15 2004
@@ -25,31 +25,31 @@
 from gump.actor.mysql.dynagumper import *
 from gump.core.model.workspace import *
 
+mock = MockObjects()
 
 class DynagumperTestCase(TestCase):
     def setUp(self):
-        self.workspace = MockWorkspace()
-        self.options = MockOptions()
-        self.gumpSet = MockGumpSet()
-        self.run = MockRun(self.workspace,self.options,self.gumpSet)
-        self.cursor = MockCursor()
-        self.conn = MockConnection(self.cursor)
-        self.dynagumper = Dynagumper(self.run,self.conn)
+        self.dynagumper = Dynagumper(mock.run,mock.database,log=mock.log)
     
-    def tearDown(self):
-        self.cleanupDatabaseMess()
-        
-    def testExecute(self):
-        self.dynagumper._execute("blah")
-        self.assertEquals( self.cursor.lastCommand, "blah" )
-        # you can do anything inside a test
-        # use the assertXXX methods on TestCase
-        # to check conditions
-        #self.assert_( True )
-        #self.assertEquals( type({}), type({}) )
+    def testEnsureThisHostIsInDatabase(self):
+        #TODO actual tests
+        self.dynagumper.ensureThisHostIsInDatabase()
+
+    def testProcessOtherEvent(self):
+        #TODO
+        self.dynagumper.processOtherEvent("blah")
+    
+    def testProcessWorkSpace(self):
+        #TODO
+        self.dynagumper.processWorkspace()
+    
+    def testProcessModule(self):
+        #TODO
+        self.dynagumper.processModule("blah")
     
-    def cleanupDatabaseMess(self):
-        pass # TODO
+    def testProcessProject(self):
+        #TODO
+        self.dynagumper.processProject("blah")
 
 # this is used by testrunner.py to determine what tests to run
 def test_suite():

Modified: gump/trunk/python/gump/util/mysql.py
Url: http://svn.apache.org/viewcvs/gump/trunk/python/gump/util/mysql.py?view=diff&rev=123508&p1=gump/trunk/python/gump/util/mysql.py&r1=123507&p2=gump/trunk/python/gump/util/mysql.py&r2=123508
==============================================================================
--- gump/trunk/python/gump/util/mysql.py	(original)
+++ gump/trunk/python/gump/util/mysql.py	Tue Dec 28 05:59:15 2004
@@ -24,10 +24,82 @@
 import MySQLdb.cursors
 
 from gump import log
-  
+
+class Database:
+    """
+    Very simple database abstraction layer, basically adding some utilities
+    around MySQLdb and ability to parse the gump DatabaseInformation object.
+    
+    See http://www.python.org/peps/pep-0249.html for more on python and databases.
+    This class adheres to the PEP 249 Connection interface.
+    """
+    def __init__(self,dbInfo):
+        self._dbInfo=dbInfo
+        self._conn=None
+        
+    def __del__(self):
+        self.close()
+    
+    def commit(self):
+        """
+        See PEP 249.
+        """
+        pass
+    
+    def rollback(self):
+        """
+        See PEP 249.
+        """
+        pass
+    
+    def cursor(self):
+        """
+        See PEP 249.
+        """
+        return self._connection().cursor()
+
+    def close(self):
+        """
+        See PEP 249.
+        """
+        if self._conn:
+            self._conn.close()
+            self._conn=None
+
+    def execute(self, statement):
+        """
+        Simple helper method to execute SQL statements that isolates its users
+        from cursor handling.
+        
+        Pass in any SQL command. Retrieve back the results of having a cursor
+        execute that command (normally the number of affected rows).
+        """
+        cursor = None
+        try:
+            cursor = self._connection().cursor()
+            result = cursor.execute(cmd)
+            return result
+        finally:
+            if cursor: cursor.close()
+    
+    def _connection(self):
+        """
+        Get a connection to the actual database, setting one up if neccessary.
+        """
+        if not self._conn:
+            self._conn = MySQLdb.Connect(
+                    host=self._dbInfo.getHost(), 
+                    user=self._dbInfo.getUser(),
+                    passwd=self._dbInfo.getPasswd(), 
+                    db=self._dbInfo.getDatabase(),
+                    compress=1,
+                    cursorclass=MySQLdb.cursors.DictCursor)
+        
+        return self._conn
+
 class DbHelper:
     """
-    	MySQL Statistics Database Interface
+    	MySQL Statistics Database Helper
     """
 
     def __init__(self,conn,database='gump'):

Mime
View raw message