[ctypes-commit] ctypes/sandbox/tools/codegen xml2py.py,NONE,1.1 h2xml.py,NONE,1.1
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2004-11-23 20:06:09
|
Update of /cvsroot/ctypes/ctypes/sandbox/tools/codegen In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12240 Added Files: xml2py.py h2xml.py Log Message: Two scripts with command line interface: create an XML file from one or more H files, and create a Python module from an XML file. --- NEW FILE: xml2py.py --- # bugs: # packing of structures/unions with bitfields? See '##XXX FIXME' import sys from codegenerator import generate_code ################################################################ def main(): from optparse import OptionParser parser = OptionParser("usage: %prog [options] xmlfile") parser.add_option("-s", dest="symbols", help="comma separated list of symbols to include " "(if not specified, all symbols will be included)", default=None) parser.add_option("-o", dest="python_file", help="output filename (if not specified, standard output will be used)", default="-") options, files = parser.parse_args() if len(files) != 1: parser.error("Only one input file can be specified") if options.python_file == "-": stream = sys.stdout else: stream = open(options.python_file, "w") generate_code(files[0], stream, symbols=options.symbols) if __name__ == "__main__": import sys if len(sys.argv) == 1: ## sys.argv.append("win32.xml") ## sys.argv.append("-sCoCreateInstance") sys.argv.append("win32.xml") sys.argv.append("-sCLSIDFromString,StringFromGUID2,IsEqualGUID") ## sys.argv.append("-owin32.py") main() --- NEW FILE: h2xml.py --- """h2xml - convert C include file(s) into an xml file by running gccxml.""" import sys, os, tempfile os.environ["PATH"] = r"c:\sf\buildgcc\bin\release" ################################################################ class CompilerError(Exception): pass # Create a C file containing #includes to the specified filenames. # Run GCCXML to create an XML file, and return the xml filename. def run_gccxml(fnames, options, verbose=0, xml_file=None): # fnames is the sequence of include files # options is seuqence of strings containing command line options for GCCXML # verbose - integer specifying the verbosity # # returns the filename of the generated XML file # write a temporary C file handle, c_file = tempfile.mkstemp(suffix=".c", text=True) if verbose: print >> sys.stderr, "writing temporary C source file %s" % c_file ## os.write(handle, 'extern "C" {\n'); for fname in fnames: os.write(handle, '#include <%s>\n' % fname) ## os.write(handle, '}'); os.close(handle) if xml_file is None: handle, xml_file = tempfile.mkstemp(suffix=".xml", text=True) os.close(handle) if options: options = " ".join(options) else: options = "" try: if verbose: print >> sys.stderr, r"gccxml.exe %s %s -fxml=%s" % (options, c_file, xml_file) i, o = os.popen4(r"gccxml.exe %s %s -fxml=%s" % (options, c_file, xml_file)) i.close() sys.stderr.write(o.read()) retval = o.close() if retval: raise CompilerError, "gccxml returned error %s" % retval return xml_file finally: if verbose: print >> sys.stderr, "Deleting temporary file %s" % c_file os.remove(c_file) ################################################################ def main(): from optparse import OptionParser def add_option(option, opt, value, parser): parser.values.gccxml_options.append("%s %s" % (opt, value)) parser = OptionParser() ## parser.add_option("-h", action="help") parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=False) parser.add_option("-D", type="string", action="callback", callback=add_option, dest="gccxml_options", help="macros to define", metavar="defines", default=[]) parser.add_option("-U", type="string", action="callback", callback=add_option, help="macros to undefine", metavar="defines") parser.add_option("-o", dest="xml_file", help="XML output filename", default=None) options, files = parser.parse_args() if not files: print "Error: no files to process" print >> sys.stderr, __doc__ return 1 try: xmlfile = run_gccxml(files, options.gccxml_options, options.verbose, options.xml_file) except CompilerError, detail: sys.exit(1) if options.xml_file is None: if options.verbose: print >> sys.stderr, "Deleting temporary file %s" % xmlfile os.remove(xmlfile) if __name__ == "__main__": main() |