glocal_config patch
This patch contains the global_config class and the global_config.ini file (located in root dir). This patch also includes changes to tko/db.py so that rather than using tko/.database and tko/.priv_login, it reads the needed info from the global_config
travis miller
git-svn-id: http://test.kernel.org/svn/autotest/trunk@1271 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/client/common_lib/__init__.py b/client/common_lib/__init__.py
index 49276f1..51d13c4 100644
--- a/client/common_lib/__init__.py
+++ b/client/common_lib/__init__.py
@@ -1,4 +1,4 @@
-__all__ = ['error', 'logging', 'barrier', 'check_version', 'test', 'utils']
+__all__ = ['error', 'logging', 'barrier', 'check_version', 'test', 'utils', 'global_config']
import site_libraries
__all__.extend(site_libraries.libraries)
diff --git a/client/common_lib/global_config.py b/client/common_lib/global_config.py
new file mode 100644
index 0000000..d8eafe9
--- /dev/null
+++ b/client/common_lib/global_config.py
@@ -0,0 +1,44 @@
+"""A singleton class for accessing global config values
+
+provides access to global configuration file
+"""
+
+__author__ = 'raphtee@google.com (Travis Miller)'
+
+import os
+import ConfigParser
+import error
+
+class ConfigError(error.AutotestError):
+ pass
+
+
+class global_config(object):
+
+ config = None
+
+ def get_config_value(self, section, key, default=None):
+ if self.config == None:
+ self.parse_config_file()
+
+ try:
+ return self.config.get(section, key)
+ except:
+ if default == None:
+ raise ConfigError("config value not found")
+ else:
+ return default
+
+
+ def parse_config_file(self):
+ dirname = os.path.dirname(sys.modules[__name__].__file__)
+ root = os.path.abspath(os.path.join(dirname, "../../"))
+ config_file = os.path.join(root, "global_config.ini")
+ self.config = ConfigParser.ConfigParser()
+ self.config.read(config_file)
+
+
+
+# insure the class is a singleton. Now the symbol global_config
+# will point to the one and only one instace of the class
+global_config = global_config()
diff --git a/global_config.ini b/global_config.ini
new file mode 100644
index 0000000..544c375
--- /dev/null
+++ b/global_config.ini
@@ -0,0 +1,6 @@
+[TKO]
+host: localhost
+database: tko
+db_type: mysql
+user: nobody
+password:
diff --git a/server/autoserv b/server/autoserv
index 09b7783..1ede1d4 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -55,8 +55,6 @@
repair = parser.parse_opts('-R')
no_tee = parser.parse_opts('-n')
-if getattr(hosts.site_host, 'site_parse_options', None):
- hosts.site_host.site_parse_options(parser)
if len(parser.args) < 1 and not verify and not repair:
print usage
diff --git a/tko/common.py b/tko/common.py
new file mode 100644
index 0000000..51359a1
--- /dev/null
+++ b/tko/common.py
@@ -0,0 +1,26 @@
+"""\
+This abortion of a file is here so we can access stuff in
+client/common_lib
+"""
+
+__author__ = "raphtee@google.com (Travis Miller)"
+
+import os, sys
+
+dirname = os.path.dirname(sys.modules[__name__].__file__)
+client_dir = os.path.abspath(os.path.join(dirname, "..", 'client'))
+
+# insert client into top of path
+sys.path.insert(0, client_dir)
+import common_lib
+from common_lib import *
+
+# remove top of path
+del sys.path[0]
+
+for library in common_lib.__all__:
+ sys.modules['common.%s' % library] = eval(library)
+
+# clean up the namespace
+del dirname, client_dir, library
+del os, sys, common_lib
diff --git a/tko/db.py b/tko/db.py
index 92f2d2a..789517a 100644
--- a/tko/db.py
+++ b/tko/db.py
@@ -1,55 +1,30 @@
import re, os, sys, types
+from common import global_config
+
class db_sql:
def __init__(self, debug = False, autocommit=True, host = None,
database = None, user = None, password = None):
self.debug = debug
self.autocommit = autocommit
-
+
path = os.path.dirname(__file__)
- try:
- file = os.path.join(path, '.database')
- db_prefs = open(file, 'r')
- file_host = db_prefs.readline().rstrip()
- file_database = db_prefs.readline().rstrip()
- if not host:
- host = file_host
- if not database:
- database = file_database
- db_prefs.close()
- except:
- if not host:
- host = 'localhost'
- if not database:
- database = 'tko'
-
- try:
- file = os.path.join(path, '.priv_login')
- login = open(file, 'r')
- file_user = login.readline().rstrip()
- file_password = login.readline().rstrip()
- if not user:
- user = file_user
- if not password:
- password = file_password
- login.close()
- except:
- try:
- file = os.path.join(path, '.unpriv_login')
- login = open(file, 'r')
- file_user = login.readline().rstrip()
- file_password = login.readline().rstrip()
- if not user:
- user = file_user
- if not password:
- password = file_password
- login.close()
- except:
- if not user:
- user = 'nobody'
- if not password:
- password = ''
-
+
+ # grab the global config
+ c = global_config.global_config
+
+ # grab the host, database
+ if not host:
+ host = c.get_config_value("TKO", "host", 'localhost')
+ if not database:
+ database = c.get_config_value("TKO", "database", 'tko')
+
+ # grab the user and password
+ if not user:
+ user = c.get_config_value("TKO", "user", 'nobody')
+ if not password:
+ password = c.get_config_value("TKO", "password", '')
+
self.con = self.connect(host, database, user, password)
self.cur = self.con.cursor()
@@ -377,19 +352,12 @@
def db(*args, **dargs):
path = os.path.dirname(__file__)
db_type = None
- try:
- db_file = os.path.join(path, '.database')
- db_prefs = open(db_file, 'r')
- host = db_prefs.readline().rstrip()
- database = db_prefs.readline().rstrip()
- db_type = db_prefs.readline().rstrip()
- except:
- pass
-
- if not db_type:
- db_type = 'mysql'
-
+
+ # read db_type from global config
+ c = global_config.global_config
+ db_type = c.get_config_value("TKO", "db_type", 'mysql')
db_type = 'db_' + db_type
- exec 'import %s; db = %s.%s(*args, **dargs)' % (db_type, db_type, db_type)
+ exec ('import %s; db = %s.%s(*args, **dargs)'
+ % (db_type, db_type, db_type))
return db