5 import fnmatch, os, string
9 def Walk( root, recurse=0, pattern='*', return_folders=0 ):
17 names = os.listdir(root)
22 pattern = pattern
or '*'
23 pat_list = string.splitfields( pattern ,
';' )
27 fullname = os.path.normpath(os.path.join(root, name))
31 if fnmatch.fnmatch(name, pat):
32 if os.path.isfile(fullname)
or (return_folders
and os.path.isdir(fullname)):
33 result.append(fullname)
38 if os.path.isdir(fullname)
and not os.path.islink(fullname):
39 result = result +
Walk( fullname, recurse, pattern, return_folders )
43 def arch2xml(root,filename,xsl_file='cfarches.xsl'):
44 files =
Walk(root, 1,
'*.arc', 1)
45 print 'searching for arch files in %s' %root
46 xml = open(filename,
'w')
47 xml.write(
'<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="%s"?>\n<ARCHES>'%xsl_file)
50 contents = arc.read().split('\n')
55 if mess == 1
and len(xp)>1:
56 str = string.join(xp[0:])
57 xml.write(
'%s\n' %str)
59 tag = string.lower(xp[0])
76 xml.write(
'%s' %(tag))
78 tag = string.lower(xp[0])
80 str = string.join(xp)[1:]
81 xml.write(
' <comment>%s</comment>\n' %(str))
83 str = string.join(xp[1:])
84 xml.write(
' <%s>%s</%s>\n' %(tag,str,tag))
85 xml.write(
'\n</arch>\n')
87 xml.write(
'\n</ARCHES>')
91 if __name__ ==
'__main__':
94 sys.stderr.write (
'Converts arc files in a directory and all sub directories to an xml file\nUsage: arch2xml.py <directory> <XML-filename>\n')