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

Pavel Vinogradov blaze-guest at alioth.debian.org
Sat Nov 17 20:40:37 UTC 2007


Author: blaze-guest
Date: 2007-11-17 20:40:36 +0000 (Sat, 17 Nov 2007)
New Revision: 305

Modified:
   trunk/oval-server/dsa2oval.py
Log:
Move most functions in object

Modified: trunk/oval-server/dsa2oval.py
===================================================================
--- trunk/oval-server/dsa2oval.py	2007-11-17 20:38:03 UTC (rev 304)
+++ trunk/oval-server/dsa2oval.py	2007-11-17 20:40:36 UTC (rev 305)
@@ -4,168 +4,164 @@
 # (c) 2007 Pavel Vinogradov
 # (c) 2004 Javier Fernandez-Sanguino
 # Licensed under the GNU General Public License version 2.
-
+#
 # Extract data from DSA files and create OVAL queries witch can 
 # be used with the OVAL query interpreter (see http://oval.mitre.org) 
-
 import os
 import sys
 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'
 
-dsaref = {}
+class Walker:
+	from oval.dsa2oval.definition import generator
+	from oval.dsa2oval.parser import dsa
+	from oval.dsa2oval.parser import wml
 
-def usage (prog = "dsa2oval"):
-	"""Print information about script flags and options
+	dsaref = {}
 	
-	@type prog: C(string)
-	@param prog: name of executable
-	"""
-
-	print """
-usage: %s [vh] [-d <directory> | -f <path to file>]
-\t-d\twhich directory use for dsa definition search
-\t-f\twhich file use for dsa definition generation
-\t-v\tverbose mode
-\t-h\tthis help
-	""" % prog
-   
-def printdsas (dsaref):
-    """ Generate and print OVAL Definitions for collected DSA information 
+	def printDSA (self):
+		""" Generate and print OVAL Definitions for collected DSA information 
     
-    Use generator from dsa2oval package for convert all DSA stored in dict
-    to proper formated XML file.
-
-    @type dsaref: C(dict)
-    @param dsaref: Dict with information about DSA
-    """
+    	Use generator from dsa2oval package for convert all DSA stored in dict
+    	to proper formated XML file.
+    	"""
     
-    ovalDefinitions = generator.createOVALDefinitions (dsaref)
-    generator.printOVALDefinitions (ovalDefinitions)
+		ovalDefinitions = generator.createOVALDefinitions (self.dsaref)
+		generator.printOVALDefinitions (ovalDefinitions)
 
-def parsedirs (directory, postfix, depth):
-	""" Recursive search directory for DSA files contain postfix in their names.
+	def parseDir (self, directory, postfix, depth):
+		""" Recursive search directory for DSA files contain postfix in their names.
+		
+		Starting from specified directory recursive parse all files which contain 
+		postfix or wml in their name.
+		For this files called dsa.parseFile() or wml.parseFile() (from parser package)
+		for extracting DSA information.
+		Results stored in global dict dsaref
 	
-	Starting from specified directory recursive parse all files which contain 
-	postfix or wml in their name.
-	For this files called dsa.parseFile() or wml.parseFile() (from parser package)
-	for extracting DSA information.
-	Results stored in global dict dsaref
-
-	@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
-	"""
-
-	if depth == 0:
-		logging.log(logging.DEBUG, "Maximum depth reached at directory " + directory)
-		return (0)
-	
-	for file in os.listdir (directory):
+		@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
+		"""
 		
-		path = "%s/%s" % (directory, file)
+		if depth == 0:
+			logging.log(logging.DEBUG, "Maximum depth reached at directory " + directory)
+			return (0)
 		
-		logging.log (logging.DEBUG, "Checking %s (for %s at %s)" % (file, postfix, depth))
+		for file in os.listdir (directory):
+			
+			path = "%s/%s" % (directory, file)
 		
-		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)
-			parsedirs (path, postfix, depth-1)
+			logging.log (logging.DEBUG, "Checking %s (for %s at %s)" % (file, postfix, 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)
+		
         	#Parse DSA data files
-		if os.access(path, os.R_OK) and file.endswith(postfix) and file[0] != '.' and file[0] != '#':
-			result = dsa.parseFile (path)
-			if result:
-				if dsaref.has_key (result[0]):
-					for (k, v) in result[1].iteritems():
-						dsaref[result[0]][k] = v
-				else:
-					dsaref[result[0]] = result[1]
+			if os.access(path, os.R_OK) and file.endswith(postfix) and file[0] != '.' and file[0] != '#':
+				result = dsa.parseFile (path)
+				if result:
+					if self.dsaref.has_key (result[0]):
+						for (k, v) in result[1].iteritems():
+							self.dsaref[result[0]][k] = v
+					else:
+						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] != '#':
-			result = wml.parseFile(path)
-			if result:
-				if dsaref.has_key (result[0]):
-					for (k, v) in result[1].iteritems():
-						dsaref[result[0]][k] = v
-				else:
-					dsaref[result[0]] = result[1]
+			if os.access(path, os.R_OK) and file.endswith(".wml") and file[0] != '.' and file[0] != '#':
+				result = wml.parseFile(path)
+				if result:
+					if self.dsaref.has_key (result[0]):
+						for (k, v) in result[1].iteritems():
+							self.dsaref[result[0]][k] = v
+					else:
+						self.dsaref[result[0]] = result[1]
 									
-	return 0
+		return 0
 
-def parsefile (filename):
-	""" Parse specifi DSA data and wml file.
-	
-	Parse specified DSA data file and according wml file.
-	Create OVAl definition for this DSA and return it.
+	def parseFile (self, filename):
+		""" Parse specifi DSA data and wml file.
+			
+		Parse specified DSA data file and according wml file.
+		Create OVAl definition for this DSA and return it.
 
-	@type filename: C(string)
-	@param filename: path to DSA datafile
-	@rtype: C(string)
-	@return: Generated OVAL definition XML
-	"""
+		@type filename: C(string)
+		@param filename: path to DSA datafile
+		@rtype: C(string)		
+		"""
 
-	datafile = filename
-	(path, ext) = os.path.splitext(datafile)
-	wmlfile = '.'.join((path, 'wml'))
+		datafile = filename
+		(path, ext) = os.path.splitext(datafile)
+		wmlfile = '.'.join((path, 'wml'))
 	
-	#Parse data file
-	result = dsa.parseFile (datafile)
-	if result:
-		if dsaref.has_key (result[0]):
-			for (k, v) in result[1].iteritems():
-				dsaref[result[0]][k] = v
-		else:
-			dsaref[result[0]] = result[1]
+		#Parse data file
+		result = dsa.parseFile (datafile)
+		if result:
+			if self.dsaref.has_key (result[0]):
+				for (k, v) in result[1].iteritems():
+					self.dsaref[result[0]][k] = v
+			else:
+				self.dsaref[result[0]] = result[1]
 
-	#Parse wml file
-	result = wml.parseFile(wmlfile)
-	if result:
-		if dsaref.has_key (result[0]):
-			for (k, v) in result[1].iteritems():
-				dsaref[result[0]][k] = v
-		else:
-			dsaref[result[0]] = result[1]
+		#Parse wml file
+		result = wml.parseFile(wmlfile)
+		if result:
+			if self.dsaref.has_key (result[0]):
+				for (k, v) in result[1].iteritems():
+					self.dsaref[result[0]][k] = v
+			else:
+				self.dsaref[result[0]] = result[1]
 
-	return generator.createOVALDefinitions(dsaref)
+		return 0
 
+def usage (prog = "dsa2oval"):
+	"""Print information about script flags and options
+	
+	@type prog: C(string)
+	@param prog: name of executable
+	"""
+
+	print """
+usage: %s [vh] [-d <directory> | -f <path to file>]
+\t-d\twhich directory use for dsa definition search
+\t-f\twhich file use for dsa definition generation
+\t-v\tverbose mode
+\t-h\tthis help
+	""" % prog
+
 if __name__ == "__main__":
-    
-    # Parse cmd options with getopt
-    opts = {}
-    
-    #By default we search dsa definitions from current directory, but -d option override this
-    opts['-d'] = "./"
-    
-    try:
-        opt, args = getopt.getopt (sys.argv[1:], 'vhd:f:')
-    except getopt.GetoptError:
-        usage ()
-        sys.exit(1)
-    
-    for key, value in opt:
-        opts[key] = value
-    
-    if opts.has_key ('-h'):
-        usage()
-        sys.exit(0)
-        
-    if opts.has_key('-v'):
-        logging.basicConfig(level=logging.WARNING)
-    else:
-    	logging.basicConfig(level=logging.ERROR)
 	
+	# Parse cmd options with getopt
+	opts = {}	
+	#By default we search dsa definitions from current directory, but -d option override this
+	opts['-d'] = "./"
+	
+	try:
+		opt, args = getopt.getopt (sys.argv[1:], 'vhd:f:')
+	except getopt.GetoptError:
+		usage ()
+		sys.exit(1)
+	
+	for key, value in opt:
+		opts[key] = value
+	
+	if opts.has_key ('-h'):
+		usage()
+		sys.exit(0)
+	
+	if opts.has_key('-v'):
+		logging.basicConfig(level=logging.WARNING)
+	else:
+		logging.basicConfig(level=logging.ERROR)
+	
+	walker = Walker();
 	if opts.has_key('-d'):
-		parsedirs (opts['-d'], '.data', 2)
+		walker.parseDir(opts['-d'], '.data', 2)
 	if opts.has_key('-f'):
-		parsefile (opts['-f'])
-    printdsas(dsaref)
+		walker.parseFile (opts['-f'])
+	walker.printDSA()
\ No newline at end of file




More information about the Oval-commits mailing list