diff options
author | Michi Henning <michi@zeroc.com> | 2004-11-26 05:08:22 +0000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2004-11-26 05:08:22 +0000 |
commit | a4edc14806a14d3bc38b8ae0f24833a79cfbd775 (patch) | |
tree | 6b1b51b4e6c7f0216deee27fbd88288077975981 /cpp/config/makeprops.py | |
parent | .dsp files for IcePatch2 (diff) | |
download | ice-a4edc14806a14d3bc38b8ae0f24833a79cfbd775.tar.bz2 ice-a4edc14806a14d3bc38b8ae0f24833a79cfbd775.tar.xz ice-a4edc14806a14d3bc38b8ae0f24833a79cfbd775.zip |
*** empty log message ***
Diffstat (limited to 'cpp/config/makeprops.py')
-rw-r--r-- | cpp/config/makeprops.py | 276 |
1 files changed, 276 insertions, 0 deletions
diff --git a/cpp/config/makeprops.py b/cpp/config/makeprops.py new file mode 100644 index 00000000000..452b1130c60 --- /dev/null +++ b/cpp/config/makeprops.py @@ -0,0 +1,276 @@ +#!/usr/bin/env python +# ********************************************************************** +# +# Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved. +# +# This copy of Ice is licensed to you under the terms described in the +# ICE_LICENSE file included in this distribution. +# +# ********************************************************************** + +import os, sys, re, signal, time + +progname = os.path.basename(sys.argv[0]) +infile = "" +lineNum = 0 +errors = 0 +outputFiles = [] + +# +# Program usage. +# +def usage(): + global progname + print >> sys.stderr, "Usage: " + progname + " -{cpp|java|cs} file" + +def fileError(msg): + global progname, infile, lineNum, errors + print >> sys.stderr, progname + ": " + infile + ':' + str(lineNum) + ": " + msg + errors += 1 + +def progError(msg): + global progname + print >> sys.stderr, progname + ": " + msg + +def removeOutputFiles(): + global outputFiles + for entry in outputFiles: + try: + if os.path.exists(entry[0]): + os.remove(entry[0]) + except EnvironmentError, ex: + progError("warning: could not unlink `" + entry[0] + "': " + ex.strerror + \ + " -- generated file contains errors"); + +def handler(signum, frame): + removeOutputFiles() + sys.exit(128 + signum) + +def openOutputFile(filename): + global outputFiles + try: + outfile = file(filename, 'w') + outputFiles.append([filename, outfile]) + except IOError, ex: + progError("cannot open `" + filename + "' for writing: " + ex.strerror) + removeOutputFiles() + sys.exit(1) + +def writePreamble(lang): + global progname + global infile + global outputFiles + global classname + for entry in outputFiles: + file = entry[1] + file.write("// **********************************************************************\n") + file.write("//\n") + file.write("// Copyright (c) 2003-2004 ZeroC, Inc. All rights reserved.\n") + file.write("//\n") + file.write("// This copy of Ice is licensed to you under the terms described in the\n") + file.write("// ICE_LICENSE file included in this distribution.\n") + file.write("//\n") + file.write("// **********************************************************************\n") + file.write("\n") + file.write("// Generated by " + progname + " from file `" + infile + "', " + time.ctime() + "\n") + file.write("\n") + file.write("// IMPORTANT: Do not edit this file -- any edits made here will be lost!\n"); + if lang == "cpp": + continue + if lang == "java": + file.write("\n"); + file.write("package IceInternal;\n") + file.write("\n") + file.write("public final class " + classname + '\n'); + file.write("{\n") + continue + if lang == "cs": + file.write("\n"); + file.write("namespace IceInternal\n") + file.write("{\n") + file.write(" public class " + classname + '\n') + file.write(" {\n"); + continue + progError("Internal error: impossible language: `" + lang + "'") + sys.exit(1) + + if lang == "cpp": + header = outputFiles[1][1] + header.write("\n"); + header.write("namespace IceInternal\n") + header.write("{\n") + header.write("\n") + header.write("public class " + classname + '\n') + header.write("{\n") + header.write("public:\n") + header.write("\n") + +def writePostamble(lang): + file = outputFiles[0][1] + if lang == "cpp": + file = outputFiles[1][1] + file.write("};\n") + file.write("\n") + file.write("}\n") + return + if lang == "java": + return + if lang == "cs": + return + +def startSection(lang, label): + if lang == "cpp": + header = outputFiles[1][1] + header.write(" static const char* " + label + "Props[];\n") + + file = outputFiles[0][1] + file.write("\n"); + if lang == "cpp": + file.write("const char* IceInternal::" + classname + "::" + label + "Props[] =\n") + file.write("{\n"); + return + if lang == "java": + return + if lang == "cs": + return + +def endSection(lang): + file = outputFiles[0][1] + if lang == "cpp": + file.write("\n};\n"); + return + if lang == "java": + return + if lang == "cs": + return + +def writeEntry(entry, first): + file = outputFiles[0][1] + if not first: + file.write(",\n") + file.write(" \"" + entry + "\"") +# +# Check arguments. +# +if len(sys.argv) != 3: + usage(progname) + sys.exit(1) + +option = sys.argv[1] +if option == "-cpp": + lang = "cpp" +elif option == "-java": + lang = "java" +elif option == "-cs": + lang = "cs" +elif option == "-h" or option == "-?": + usage(progname) + sys.exit(0) +else: + usage(progname) + sys.exit(1) + +# +# Open input file. +# +try: + infile = sys.argv[2] + f = file(infile, 'r') +except IOError, ex: + progError("cannot open `" + infile + "': " + ex.strerror) + sys.exit(1) + +# +# Set up regular expressions for empty and comment lines, section headings, and entry lines. +# +ignore = re.compile("^\s*(?:#.*)?$") # Empty line or comment line +heading = re.compile("^\s*([a-zA-z_]\w*)\s*:\s*$") # Section heading +entry = re.compile("^\s*([^ \t\n\r\f\v#]+)(?:\s*#.*)?$") # Any non-whitespace character sequence, except for # + +# +# Install signal handler so we can remove the output files if we are interrupted. +# +signal.signal(signal.SIGINT, handler) +signal.signal(signal.SIGHUP, handler) +signal.signal(signal.SIGTERM, handler) + +# +# Open output files. +# +classname, ext = os.path.splitext(os.path.basename(infile)) +openOutputFile(classname + '.' + lang) +if(lang == "cpp"): + openOutputFile(classname + ".h") + +labels = {} # Records the line number on which each label is defined +sectionStart = 0 # True for the line on which a label is defined +inSection = 0 # Set to true (and the remains as true) once the first labels is defined +numEntries = 0 # Number of entries within a section +errors = 0 # Number of syntax errors in the input file + +# +# Write preamble. +# +writePreamble(lang) + +# +# Loop over lines in input file. +# +lines = f.readlines() +for l in lines: + lineNum += 1 + + if ignore.match(l) != None: + continue + + labelMatch = heading.match(l) + if labelMatch != None: + if sectionStart: + fileError("section `" + label + "' must have at least one entry") + label = labelMatch.group(1) + try: + badLine = labels[label] + fileError("duplicate section heading: `" + label + "'") + except KeyError: + pass + labels[label] = lineNum + if inSection: + endSection(lang) + numEntries = 0 + startSection(lang, label) + inSection = 1 + sectionStart = 1 + continue + + entryMatch = entry.match(l) + if entryMatch != None: + writeEntry(entryMatch.group(1), sectionStart) + sectionStart = 0 + numEntries += 1 + continue + + fileError("syntax error") + +if len(labels) == 0: + fileError("input must define at least one section"); + +# +# End the final section. +# +if numEntries == 0: + fileError("section `" + label + "' must have at least one entry") +endSection(lang) + +# +# End the source files. +# +writePostamble(lang) + +# +# Remove the output files if anything went wrong, so we don't leave partically written files behind. +# +if errors != 0: + removeOutputFiles() + sys.exit(1) + +sys.exit(0) |