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 )
60 esc_me = esc_me.replace(
'&',
'&', 4)
61 esc_me = esc_me.replace(
'<',
'<', 3)
62 esc_me = esc_me.replace(
'>',
'>', 3)
66 def arch2xml(root,filename,xsl_file='cfarches.xsl'):
67 files =
Walk(root, 1,
'*.arc', 1)
68 print 'searching for arch files in %s' %root
69 xml =
open(filename,
'w')
70 xml.write(
'<?xml version="1.0"?>\n<?xml-stylesheet type="text/xsl" href="%s"?>\n<ARCHES>'%xsl_file)
73 contents = arc.read().
split(
'\n')
81 if mess == 1
and len(xp)>1:
82 str =
escape(string.join(xp[0:]))
83 xml.write(
'%s\n' %str)
85 tag = string.lower(xp[0])
111 xml.write(
'%s\n' %(tag))
114 tag = string.lower(xp[0])
116 str = string.join(xp)[1:]
117 xml.write(
' <comment>%s</comment>\n' %(
escape(str)))
127 if need_new_arch == 1:
128 xml.write(
'\n</arch>\n<arch>\n')
131 str = string.join(xp[1:])
132 xml.write(
' <%s>%s</%s>\n' %(tag,str,tag))
133 xml.write(
'\n</arch>\n')
135 xml.write(
'\n</ARCHES>')
139 if __name__ ==
'__main__':
141 if len(sys.argv) < 3:
142 sys.stderr.write (
'Converts arc files in a directory and all sub directories to an xml file\nUsage: arch2xml.py <directory> <XML-filename>\n')