[Oval-commits] r306 - trunk/oval-server

Pavel Vinogradov blaze-guest at alioth.debian.org
Sun Nov 18 19:41:37 UTC 2007


Author: blaze-guest
Date: 2007-11-18 19:41:37 +0000 (Sun, 18 Nov 2007)
New Revision: 306

Modified:
   trunk/oval-server/dsa2oval.py
Log:
Optimize Walker class

Modified: trunk/oval-server/dsa2oval.py
===================================================================
--- trunk/oval-server/dsa2oval.py	2007-11-17 20:40:36 UTC (rev 305)
+++ trunk/oval-server/dsa2oval.py	2007-11-18 19:41:37 UTC (rev 306)
@@ -12,15 +12,27 @@
 import getopt
 import logging
 
+from oval.dsa2oval.definition import generator
+from oval.dsa2oval.parser import dsa
+from oval.dsa2oval.parser import wml
+
 assert sys.version_info >= (2,4), 'Requires Python 2.4 or better'
 
 class Walker:
-	from oval.dsa2oval.definition import generator
-	from oval.dsa2oval.parser import dsa
-	from oval.dsa2oval.parser import wml
-
+	
 	dsaref = {}
 	
+	def __init__ (self, dataExt = '.data', infoExt = '.wml'):
+		"""
+		@type dataExt: C(string)
+		@param dataExt: filename postfix of files which contains data of DSA		
+		@type infoExt: C(string)
+		@param infoExt: filename postfix of files which contains description of DSA
+		"""
+		
+		self.dataExt = dataExt
+		self.infoExt = infoExt
+		
 	def printDSA (self):
 		""" Generate and print OVAL Definitions for collected DSA information 
     
@@ -31,7 +43,7 @@
 		ovalDefinitions = generator.createOVALDefinitions (self.dsaref)
 		generator.printOVALDefinitions (ovalDefinitions)
 
-	def parseDir (self, directory, postfix, depth):
+	def parseDir (self, directory, depth):
 		""" Recursive search directory for DSA files contain postfix in their names.
 		
 		Starting from specified directory recursive parse all files which contain 
@@ -42,8 +54,6 @@
 	
 		@type directory: C(string)
 		@param directory: top of parsed filesystem hierarhy
-		@type postfix: C(string)
-		@param postfix: filename postfix of files which contains data of DSA
 		@type depth: C(integer)
 		@param depth: maximum recursion depth
 		"""
@@ -56,14 +66,14 @@
 			
 			path = "%s/%s" % (directory, file)
 		
-			logging.log (logging.DEBUG, "Checking %s (for %s at %s)" % (file, postfix, depth))
+			logging.log (logging.DEBUG, "Checking %s (for %s at %s)" % (file, self.dataExt, depth))
 		
 			if os.access(path, os.R_OK) and os.path.isdir (path) and not os.path.islink (path) and file[0] != '.':
 				logging.log(logging.DEBUG, "Entering directory " + path)
-				self.parseDir (path, postfix, depth-1)
+				self.parseDir (path, depth-1)
 		
         	#Parse DSA data files
-			if os.access(path, os.R_OK) and file.endswith(postfix) and file[0] != '.' and file[0] != '#':
+			if os.access(path, os.R_OK) and file.endswith(self.dataExt) and file[0] != '.' and file[0] != '#':
 				result = dsa.parseFile (path)
 				if result:
 					if self.dsaref.has_key (result[0]):
@@ -73,7 +83,7 @@
 						self.dsaref[result[0]] = result[1]
 		
         	#Parse DSA wml descriptions
-			if os.access(path, os.R_OK) and file.endswith(".wml") and file[0] != '.' and file[0] != '#':
+			if os.access(path, os.R_OK) and file.endswith(self.infoExt) and file[0] != '.' and file[0] != '#':
 				result = wml.parseFile(path)
 				if result:
 					if self.dsaref.has_key (result[0]):
@@ -95,12 +105,11 @@
 		@rtype: C(string)		
 		"""
 
-		datafile = filename
-		(path, ext) = os.path.splitext(datafile)
-		wmlfile = '.'.join((path, 'wml'))
+		(path, ext) = os.path.splitext(filename)
+		wmlfile = '.'.join((path, self.infoExt))
 	
 		#Parse data file
-		result = dsa.parseFile (datafile)
+		result = dsa.parseFile (filename)
 		if result:
 			if self.dsaref.has_key (result[0]):
 				for (k, v) in result[1].iteritems():
@@ -161,7 +170,7 @@
 	
 	walker = Walker();
 	if opts.has_key('-d'):
-		walker.parseDir(opts['-d'], '.data', 2)
+		walker.parseDir(opts['-d'], 2)
 	if opts.has_key('-f'):
 		walker.parseFile (opts['-f'])
 	walker.printDSA()
\ No newline at end of file




More information about the Oval-commits mailing list