Update of /cvsroot/cpptool/rfta/bin
In directory sc8-pr-cvs1:/tmp/cvs-serv5788/bin
Added Files:
lister.py
Log Message:
* astdump now takes the input and output file path as argument, and parse the whole file.
* added python script to recursively generate ast dump for all files contained in testdata/
--- NEW FILE: lister.py ---
# Recursively generate ast
# Path.split_all function from http://www.jorendorff.com/articles/python/path
import os
class Path(str):
def split_all( self ):
""" Return a list of the path components in this path.
The first item in the list will be a path. Its value will be
either os.curdir, os.pardir, empty, or the root directory of
this path (for example, '/' or 'C:\\'). The other items in
the list will be strings.
path.path.joinpath(*result) will yield the original path.
"""
parts = []
loc = self
while loc != os.curdir and loc != os.pardir:
prev = loc
loc, child = prev.split_path()
if loc == prev:
break
parts.append(child)
parts.append(loc)
parts.reverse()
return parts
def make_relative_to( self, base_path ):
components = Path(os.path.normcase(self)).split_all()
base_components = Path(os.path.normcase(base_path)).split_all()
return Path( os.path.join( *components[len(base_components):] ) )
def change_extension( self, extension ):
return Path( os.path.splitext( self )[0] + "." + extension )
def split_path( self ):
""" p.splitpath() -> Return (p.parent, p.name). """
parent, child = os.path.split(self)
return Path(parent), child
def ensure_dir_exists( self ):
directory = os.path.dirname( self )
if not os.path.exists( directory ):
os.makedirs( directory )
class ASTGenerator:
def __init__( self, source_dir, output_dir ):
self.source_dir = source_dir
self.output_dir = output_dir
def generate( self, source_path ):
relative_source_path = Path(source_path).make_relative_to( self.source_dir )
output_filename = Path( relative_source_path ).change_extension( "html" )
output_path = os.path.join( self.output_dir, output_filename )
print "Generating AST for ", source_path, " in ", output_path
Path(output_path).ensure_dir_exists()
exit_code = os.spawnl( os.P_WAIT, "astdump", "astdump", source_path, output_path )
if exit_code != 0:
print "Failed to generate AST for ", source_path
def listFilesIgnoreCVSDir( generator, dirName, fileNames ):
if ( fileNames.count( 'CVS' ) > 0 ):
fileNames.remove( 'CVS' )
for file in fileNames:
file_path = os.path.join( dirName, file )
if os.path.isfile( file_path ):
generator.generate( file_path )
#if __name__ == '__main__':
# os.path.walk(sys.argv[1], lister, None) # dir name in cmdline
input_dir = "testdata"
output_dir = "ast"
x = Path( "tata" )
generator = ASTGenerator( input_dir, output_dir )
os.path.walk( input_dir, listFilesIgnoreCVSDir, generator )
|