gump-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosim...@apache.org
Subject svn commit: r156273 - in gump/branches/Gump3/pygump/python/gump: plugins/dynagumper.py test/testDynagumper.py test/testMain.py util/sysinfo.py
Date Sat, 05 Mar 2005 20:00:07 GMT
Author: leosimons
Date: Sat Mar  5 12:00:04 2005
New Revision: 156273

URL: http://svn.apache.org/viewcvs?view=rev&rev=156273
Log:
* Start working on pushing more info on the machine to dynagump
* fix the testMain script

* extract a _command method (currently in the new sysinfo module) for input/output/result
value mangling and the like

Added:
    gump/branches/Gump3/pygump/python/gump/util/sysinfo.py
      - copied, changed from r156261, gump/branches/Gump3/pygump/python/gump/util/mysql.py
Modified:
    gump/branches/Gump3/pygump/python/gump/plugins/dynagumper.py
    gump/branches/Gump3/pygump/python/gump/test/testDynagumper.py
    gump/branches/Gump3/pygump/python/gump/test/testMain.py

Modified: gump/branches/Gump3/pygump/python/gump/plugins/dynagumper.py
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/plugins/dynagumper.py?view=diff&r1=156272&r2=156273
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/plugins/dynagumper.py (original)
+++ gump/branches/Gump3/pygump/python/gump/plugins/dynagumper.py Sat Mar  5 12:00:04 2005
@@ -18,14 +18,18 @@
 __license__   = "http://www.apache.org/licenses/LICENSE-2.0"
 
 import platform
+import os
 
 from gump.plugins import AbstractPlugin
+from gump.util.sysinfo import amount_of_memory
+from gump.util.sysinfo import amount_of_cpu_mhz
+from gump.util.sysinfo import number_of_cpus
 
 class Dynagumper(AbstractPlugin):
     """
     Populate the DynaGump run metadata database.
     """    
-    def __init__(self, db, log, startdate_property_name='startdate', enddate_property_name='enddate'):
+    def __init__(self, db, log):
         AbstractPlugin.__init__(self,log)
         """Set up the Dynagumper.
 
@@ -36,23 +40,27 @@
         self.db = db
         self.log = log
         
-        self.startdate_property_name = startdate_property_name
-        self.enddate_property_name = enddate_property_name
-        
     def initialize(self):
-        #TODO call ensureThisHostIsInDatabase
-        pass
+        self.ensureThisHostIsInDatabase()
     
     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.db.execute(cmd)
         
+        cmd = "SELECT * FROM %s WHERE address = '%s' AND name = '%s';"
+        (rows, result) = self.db.execute(cmd)
+        if rows == 0:
+            memory = amount_of_memory()
+            mhz = amount_of_cpu_mhz()
+            cpus = number_of_cpus()
+            
+            description = "%s (%s,%s,%s,%s,%s)" % (host, system, release, version, machine,
processor)
+            cmd = """INSERT INTO %s (address, name, cpu_arch, cpu_number, cpu_speed_Mhz,
memory, description)
+                     VALUES ('%s', '%s', '%s', %s, %s, %s, '%s')""" \
+                % (tablename, host, host, processor, cpus, mhz, memory, description)
+            self.db.execute(cmd)
+    
     def visit_workspace(self, workspace):
         """Add information about the workspace to the database."""
         pass
@@ -66,15 +74,12 @@
     def visit_project(self, project):    
         """Add information about a project to the database."""
         tablename = "projects"
-        startdate = getattr(project, self.startdate_property_name)
-        enddate = getattr(project, self.enddate_property_name)
+        startdate = project.startdate
+        enddate = project.enddate
         name = project.name
         
         cmd = "INSERT INTO %s (project_name, start_date, end_date) VALUES ('%s', '%s', '%s')"
\
             % (tablename, name, startdate, enddate)
-        
         #TODO make query fit database model
-        self.log.debug("Executing SQL: %s" % cmd)
         # self.db.execute(cmd)
-        
         #TODO do the actual work right here...

Modified: gump/branches/Gump3/pygump/python/gump/test/testDynagumper.py
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/test/testDynagumper.py?view=diff&r1=156272&r2=156273
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/test/testDynagumper.py (original)
+++ gump/branches/Gump3/pygump/python/gump/test/testDynagumper.py Sat Mar  5 12:00:04 2005
@@ -23,6 +23,8 @@
 
 from gump.plugins.dynagumper import Dynagumper
 
+from main import _Logger
+
 class DynagumperTestCase(pmock.MockTestCase):
     def setUp(self):
         self.log = self.mock()
@@ -39,7 +41,7 @@
     def test_ensureThisHostIsInDatabase(self):
         #TODO actual tests
         db = self.mock()
-        db.expects(once()).method("execute")
+        db.expects(at_least_once()).method("execute").will(return_value((0,None)))
         dynagumper = Dynagumper(db,self.log)
         dynagumper.ensureThisHostIsInDatabase()
 

Modified: gump/branches/Gump3/pygump/python/gump/test/testMain.py
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/test/testMain.py?view=diff&r1=156272&r2=156273
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/test/testMain.py (original)
+++ gump/branches/Gump3/pygump/python/gump/test/testMain.py Sat Mar  5 12:00:04 2005
@@ -26,9 +26,14 @@
 import unittest
 from unittest import TestCase
 
+import os
 import StringIO
 
 from main import print_help
+from main import _Logger
+from main import DEBUG
+from main import CRITICAL
+from main import _check_version
 
 class MainTestCase(TestCase):
     def test_print_help(self):
@@ -36,7 +41,56 @@
         print_help(file)
         result = file.getvalue()
         file.close()
-        self.assert_(len(result) > 10) # for want of a better idea...
+        self.assert_(len(result) > 100) # for want of a better idea...
+
+    def test_logger(self):
+        try:
+            l = _Logger("gump-maintestcase-logdir-tmp", CRITICAL-1, CRITICAL-1)
+            l.debug("blah")
+            l.log(DEBUG,"blahblah")
+            l.info("blahblahblah")
+            l.warning("blahblahblahblah")
+            l.error("ehm")
+            try:
+                raise Exception
+            except:
+                l.exception("ehmehm")
+            l.critical("whoops!")
+            l.close()
+        finally:
+            try:
+                os.remove(l.filename)
+            except:
+                pass
+            try:
+                os.rmdir(l.logdir)
+            except:
+                pass
+    
+    def test_check_version(self):
+        try:
+            _check_version()
+        except:
+            pass
+    
+    # TODO: refactor the main module to make it easier testable!
+    
+    # don't test _parse_workspace since that should be removed anyway
+    
+    # don't test _svn_update since that should be removed as well and
+    # handled elsewhere
+    
+    # don't test _send_email because that would entail actually sending
+    # e-mail, which is way more trouble than its worth to test
+    
+    # dont't test send_error_email because that would entail actually
+    # sending e-mail, which is way more trouble than its worth to test
+    
+    # don't test _start_engine because that would basically be running
+    # an integration test
+    
+    # don't test main because that would basically be running
+    # an integration test
 
 # this is used by testrunner.py to determine what tests to run
 def test_suite():

Copied: gump/branches/Gump3/pygump/python/gump/util/sysinfo.py (from r156261, gump/branches/Gump3/pygump/python/gump/util/mysql.py)
URL: http://svn.apache.org/viewcvs/gump/branches/Gump3/pygump/python/gump/util/sysinfo.py?view=diff&rev=156273&p1=gump/branches/Gump3/pygump/python/gump/util/mysql.py&r1=156261&p2=gump/branches/Gump3/pygump/python/gump/util/sysinfo.py&r2=156273
==============================================================================
--- gump/branches/Gump3/pygump/python/gump/util/mysql.py (original)
+++ gump/branches/Gump3/pygump/python/gump/util/sysinfo.py Sat Mar  5 12:00:04 2005
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-# Copyright 2004-2005 The Apache Software Foundation
+# Copyright 2005 The Apache Software Foundation
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -14,98 +14,87 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-"""This module provides a thin wrapper around the MySQLdb library."""
+"""This module provides system introspection tools based on all sorts of ugly shell-based
stuff."""
 
-__copyright__ = "Copyright (c) 2004-2005 The Apache Software Foundation"
+__copyright__ = "Copyright (c) 2005 The Apache Software Foundation"
 __license__   = "http://www.apache.org/licenses/LICENSE-2.0"
 
-import types
+import os
+from tempfile import mkstemp
 
-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, log, host=None, user=None, password=None, db=None):
-        self.log = log
-        self.host = host
-        self.user = user
-        self.password = password
-        self.db = db
-        self._conn = None
-        
-    def __del__(self):
-        self.close()
-    
-    def commit(self):
-        """
-        See PEP 249.
-        """
+RESULT_IF_SYSTEM_CALL_FAILS = 10000
+
+def _safe_close(fd):
+    try:
+        if isinstance(fd,int):
+            os.close(fd)
+        else:
+            fd.close()
+    except:
         pass
-    
-    def rollback(self):
-        """
-        See PEP 249.
-        """
+
+def _safe_rm(filename):
+    try:
+        os.remove(filename)
+    except:
         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. The result is a tuple containing the number of
-        rows affected and the result set as a tuple of tuples, if there was a
-        result set, or None otherwise.
-        """
-        self.log.debug("Executing SQL statement: %s" % statement)
-        cursor = None
+def _system(command):
+    (f, scriptname) = mkstemp()
+    os.write(f,command)
+    _safe_close(f)
+    (f, soutname) = mkstemp()
+    _safe_close(f)
+    (f, serrname) = mkstemp()
+    _safe_close(f)
+    
+    try:
+        cmd = "sh -e '%s' >%s 2>%s" % (scriptname, soutname, serrname)
+    
+        result = RESULT_IF_SYSTEM_CALL_FAILS
         try:
-            cursor = self._connection().cursor()
-            cursor.execute(statement)
-            
-            affected = cursor.rowcount
-            self.log.debug("   ...%s rows affected." % affected)
-            if statement.lower().startswith("select"):
-                if affected > 0:
-                    result = cursor.fetchall()
-                    return (affected, result)
-                
-            return (affected, None)
-        finally:
-            if cursor: cursor.close()
-    
-    def _connection(self):
-        """
-        Get a connection to the actual database, setting one up if neccessary.
-        """
-        if not self._conn:
-            import MySQLdb
-            import MySQLdb.cursors
-            self._conn = MySQLdb.Connect(
-                    host=self.host, 
-                    user=self.user,
-                    passwd=self.password, 
-                    db=self.db,
-                    compress=1,
-                    cursorclass=MySQLdb.cursors.DictCursor)
+            result = os.system(cmd)
+        except:
+            pass
+        if not os.name == 'dos' and not os.name == 'nt':
+            result = (((result & 0xFF00) >> 8) & 0xFF)
         
-        return self._conn
+        f = open(soutname)
+        output = f.read()
+        _safe_close(f)
+        f = open(serrname)
+        error = f.read()
+        _safe_close(f)
+    
+        return (result, output, error)
+    finally:
+        _safe_rm(scriptname)
+        _safe_rm(soutname)
+        _safe_rm(serrname)
+
+def amount_of_memory():
+    amount = 0 # i.e., we don't know
+    cmd = 'cat /proc/meminfo | grep MemTotal | sed -e "s/[^0-9]//g"'
+    (result, output, error) = _system(cmd)
+    if not result: # exit status 0 is good!
+        amount = int(output)
+    
+    return amount
+    
+def amount_of_cpu_mhz():
+    amount = 0 # i.e., we don't know
+    cmd = "cat /proc/cpuinfo | grep MHz | sed -e 's/[^0-9]//g' | awk '!x[$0]++'"
+    (result, output, error) = _system(cmd)
+    if not result: # exit status 0 is good!
+        amount = int(output)
+    
+    return amount
+
+def number_of_cpus():
+    amount = 0 # i.e., we don't know
+    cmd = 'cat /proc/cpuinfo | grep "^processor" | sed -e "s/[^0-9]//g" | grep -c ".*"'
+    (result, output, error) = _system(cmd)
+    if not result: # exit status 0 is good!
+        amount = int(output)
+    
+    return amount



Mime
View raw message