[Oval-commits] r184 - in trunk/oval-agent: . ovalHttpClient

Pavel Vinogradov blaze-guest at alioth.debian.org
Tue Aug 21 07:32:12 UTC 2007


Author: blaze-guest
Date: 2007-08-21 07:32:12 +0000 (Tue, 21 Aug 2007)
New Revision: 184

Modified:
   trunk/oval-agent/oval-agent.cfg
   trunk/oval-agent/oval-agent.py
   trunk/oval-agent/ovalHttpClient/__init__.py
   trunk/oval-agent/ovalHttpClient/ovalHttpClient.py
Log:
Add documentation, exception handling, cmp options parse

Modified: trunk/oval-agent/oval-agent.cfg
===================================================================
--- trunk/oval-agent/oval-agent.cfg	2007-08-20 19:31:42 UTC (rev 183)
+++ trunk/oval-agent/oval-agent.cfg	2007-08-21 07:32:12 UTC (rev 184)
@@ -8,6 +8,7 @@
 [general]
 log_dir = /home/blaze/tmp/oval/agent/log
 log_file = oval-agent.log
-log_level = info
+#log_level = CRITICAL | ERROR | WARNING (default) | INFO | DEBUG | NOTSET
+log_level = INFO
 workdir = /home/blaze/tmp/oval/agent/definitions
 

Modified: trunk/oval-agent/oval-agent.py
===================================================================
--- trunk/oval-agent/oval-agent.py	2007-08-20 19:31:42 UTC (rev 183)
+++ trunk/oval-agent/oval-agent.py	2007-08-21 07:32:12 UTC (rev 184)
@@ -1,17 +1,45 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#                                                                                                                                              
+# Written by Pavel Vinogradov
+# Licensed under the GNU General Public License version 2.
+"""  Agent package that interact with oval-server and evaluate local machine with received definitions.
+
+    Interact with oval-server over HTTP protocol and use ovaldi package for evaluate definitions. 
+    Don't present any statistic.                                                                                                                                                                                                        
+"""
+ 
 from ConfigParser import SafeConfigParser
+import os, logging, sys
+import getopt
 from ovalHttpClient import ovalHttpClient
-import os, logging
 
+class configNotFoundError (Exception):
+	pass
+
+logLevels = {'CRITICAL' : 50, 'ERROR' : 40, 'WARNING' : 30, 'INFO' : 20, 'DEBUG' : 10, 'NOTSET' : 0}
+
+def usage (prog = 'oval-agent.py'):
+	"""Print information about script flags and options"""
+
+	print """usage: python %s [-h] [-c <config>]
+\t-h\tthis help
+\t-d\tpath to config file (by default oval-agent.cfg""" % prog
+
 class agentThread:
 	config = SafeConfigParser()
 	logger = logging.getLogger()
 	
 	def __init__(self, cfgfile):
 		try:
-			self.config.read(cfgfile)
+			if not self.config.read(cfgfile):
+				raise configNotFoundError, 'Config file %s not found.\n' % cfgfile 
+			
+			# Read general options from config file
 			self.workdir = self.config.get('general', 'workdir')
 			logdirname = self.config.get('general', 'log_dir')
 			logfilename = self.config.get('general', 'log_file')
+			self.log_level = self.config.get('general', 'log_level')
 			outfilename = os.path.join(logdirname, logfilename)
 			
 			# Create the root handler (removing any others)
@@ -20,15 +48,29 @@
 			for h in logging.root.handlers:
 				logging.root.removeHandler(h)
 			logging.root.addHandler(hdlr)
-			logging.root.setLevel(logging.DEBUG)
+			
+			if logLevels.has_key(self.log_level):
+				logging.root.setLevel(logLevels[self.log_level])
+			else:
+				logging.root.setLevel(logging.WARNING)
+				self.logger.warning('Wrong value of log_level key in config.')
+				
 			self.logger.info('Logging begins')
 
 		except IOError, e:
-			logger.exception('error: ' + str(e))
+			self.logger.exception('error: ' + str(e))
 			logging.shutdown()
 			exit(2)
-	
+			
 	def readConfigSection (self, section):
+		""" Parse section of config file into dict
+		 
+		@type section: C(string)
+		@param section: name of config file section
+		@rtype: C(dict)
+		@return: content of config section in dict format
+		
+		"""
 		conf = {}
 		
 		if self.config.has_section(section):
@@ -38,16 +80,45 @@
 		return conf
 		
 	def run(self):
+		"""Run agent instance
+		
+		Create instance of OvalHttpClient class and run it
+		
+		"""
 		self.logger.info('clientThread.RUN')
+		
 		config = self.readConfigSection('server')
 		config['workdir'] = self.workdir
 		
 		client = ovalHttpClient.OvalHttpClient(config, self.logger)
 		
 		client.run()
-			
+
 if __name__ == "__main__":
-	client = agentThread('oval-agent.cfg')
-	client.run ()
+	#Parse command line options. 
+	#By default we search for config file in current directory 
+	opts = {'-c' : 'oval-agent.cfg'}
 	
+	try:
+		opt, args = getopt.getopt (sys.argv[1:], 'hc:')
+	except getopt.GetoptError:
+		usage (sys.argv[0])
+		sys.exit(1)
+	
+	for key, value in opt: 
+		opts[key] = value
 
+	if opts.has_key ('-h'):
+		usage(sys.argv[0])
+		sys.exit(0)
+	
+	#Crate agent instance and run it
+	try:
+		client = agentThread(opts['-c'])
+		client.run ()
+	except configNotFoundError, e:
+		sys.stderr.write (str(e))
+	except KeyboardInterrupt, e:
+		sys.stderr.write ('Execution interrupted by keyboard.')
+	except Exception:
+		sys.stderr.write('Error in config file.')
\ No newline at end of file

Modified: trunk/oval-agent/ovalHttpClient/__init__.py
===================================================================
--- trunk/oval-agent/ovalHttpClient/__init__.py	2007-08-20 19:31:42 UTC (rev 183)
+++ trunk/oval-agent/ovalHttpClient/__init__.py	2007-08-21 07:32:12 UTC (rev 184)
@@ -0,0 +1,12 @@
+#
+# __init__.py: defines this directory as the 'oval-agent' package
+#
+######################################################################
+#
+# (c) 2007 Pavel Vinogradov                                                                                                       
+# Licensed under the GNU General Public License version 2.
+#
+######################################################################
+
+__all__ = ['ovalHttpClient']
+__version__ = 0.5

Modified: trunk/oval-agent/ovalHttpClient/ovalHttpClient.py
===================================================================
--- trunk/oval-agent/ovalHttpClient/ovalHttpClient.py	2007-08-20 19:31:42 UTC (rev 183)
+++ trunk/oval-agent/ovalHttpClient/ovalHttpClient.py	2007-08-21 07:32:12 UTC (rev 184)
@@ -1,3 +1,7 @@
+# -*- coding: utf-8 -*-
+#                                                                                                                                              
+# Written by Pavel Vinogradov
+# Licensed under the GNU General Public License version 2.
 import urllib, urllib2, time
 import httplib, mimetypes
 import os




More information about the Oval-commits mailing list