3 This script has two main functions:
5 1) Combine several snippets to form Makefile.rules file
6 2) Split Makefile.rules file to the originnal snippets
10 snippet ::= legal? documentation rules
12 legal ::= comment* empty-comment empty-comment
13 documentation := comment*
20 Makefile.rules policies:
22 * All the parts of the Makefile.rules are ordered in the same order
23 as they are in snippets i.e. copyrights, documentations and rules.
25 * On the first line of each part of the Makefile.rules, there is
26 special mart of the form #OMK@<snippet file name><EOL>. This mark
27 is used for splitting Makefile.rules back to the original
32 from optparse import OptionParser
39 def __init__(self, fname = None):
40 """Initializes the snippet and if fname is given, reads it
46 if fname: self.readFromFile(fname)
48 def readFromFile(self, fname):
49 """Loads snippet from file."""
53 self.readLines(f.readlines())
57 def readLines(self, lines):
58 """Parses the snippet given in the list and stores it in
60 currentPart = self.legal
64 if currentPart == self.legal:
65 if line.strip() == "#": counter += 1
67 if counter == 2: currentPart = self.doc
68 if line[0] != "#": currentPart = self.code
70 currentPart.append(line)
78 for type in ['legal', 'doc', 'code']:
79 for line in self.__dict__[type]:
85 lines = self.asLines()
86 for l in lines: s += l
90 s = "<Snippet: %s>" % self.name
93 def __cmp__(self, other):
94 ret = self.name.__cmp__(other.name)
95 if ret != 0: return ret
96 ret = self.legal.__cmp__(other.legal)
97 if ret != 0: return ret
98 ret = self.doc.__cmp__(other.doc)
99 if ret != 0: return ret
100 ret = self.code.__cmp__(other.code)
103 def parseCommandLine():
104 parser = OptionParser(usage = """
105 %prog [-o FILE] snippet1 snippet2 ... build Makefile.rules from snippets
106 %prog [-o - ] -s Makfile.rules
108 parser.add_option("-s", "--split",
109 action="store", dest="split", default=False, metavar="RULES",
110 help="Split given Makefile.rules to the original snippets")
111 parser.add_option("-o", "--output",
112 action="store", dest="output", default=False, metavar="RULES",
113 help="Output built Makefile.rules to file RULES")
114 (options, args) = parser.parse_args()
117 def splitToSnippets(rules):
118 """Split rules to the original snippets. The output is dictinary
121 snipBegin = re.compile("^(.*)#OMK@(.*)$")
123 currentLinesList = None
126 match = snipBegin.match(line)
128 line = match.group(1).rstrip() + "\n"
129 snipName = match.group(2)
130 if not snipName in snipDict:
131 snipDict[snipName] = []
132 currentLinesList = snipDict[snipName]
134 currentLinesList.append(line);
137 def convertSnipDict(snipDict):
138 """Takes dictionary of snippets, where each snippet is a lists of
139 lines, as the input argument and returns dictionary of snippets objects"""
145 snip.readLines(snipDict[s])
149 def readSnippets(fnames):
150 """Reads the snippets from several files and retuns them as a
151 dictionaly indexed by file name."""
155 snipDict[fn] = Snippet(fn)
160 def combineRules(snippets):
161 """Combine all snippents from the snippets dictionary to one list
166 for type in ['legal', 'doc', 'code']:
169 if len(snip.__dict__[type]) == 0: continue
170 firstLine = string.rstrip(snip.__dict__[type][0])
171 rules.append(firstLine.ljust(60)+" #OMK@%s\n"%snip.name)
172 rules.extend(snip.__dict__[type][1:]);
175 def assertSameSnippets(d1, d2):
178 # Generate an error message
181 sys.stderr.write("Consistency error: ")
184 for i in range(len(s1)):
186 sys.stderr.write("snippet %s, line %d differs!\n")
190 def buildRules(fnames, output):
191 snipDict = readSnippets(fnames)
192 rules = combineRules(snipDict)
193 snipDict2 = convertSnipDict(splitToSnippets(rules))
195 if assertSameSnippets(snipDict, snipDict2) == False:
199 if output: f = open(output,"w+")
207 def splitRules(rulesFN, output):
211 (options, args) = parseCommandLine()
213 splitRules(options.split, options.output)
215 buildRules(args, options.output)
218 if __name__ == "__main__": main()