--- /dev/null
+#!/usr/bin/env python
+
+from optparse import OptionParser
+import os
+import sys
+
+class Snippet:
+
+ def __init__(self, fname):
+ self.name = ""
+ self.legal = []
+ self.doc = []
+ self.code = []
+ self.read(fname)
+
+ def read(self, fname):
+ self.name = fname
+ f = open(fname, "r")
+ current = self.legal
+ counter = 0
+ for line in f:
+ if current == self.legal:
+ if line.strip() == "#": counter += 1
+ else: counter = 0
+ if counter == 2: current = self.doc
+ if line[0] != "#": current = self.code
+
+ current.append(line)
+
+ if not self.doc:
+ self.doc = self.legal
+ self.legal = []
+
+ f.close
+
+ def __str__(self):
+ s = "Snippet: %s\n" % self.name
+ s += " Legal: %d lines\n" % len(self.legal)
+ s += " Doc: %d lines\n" % len(self.doc)
+ s += " Code: %d lines\n" % len(self.code)
+ return s
+
+
+def parseCommandLine():
+ parser = OptionParser(usage = """
+ %prog [-o FILE] snippet1 snippet2 ... build Makefile.rules from snippets
+ %prog --split Makfile.rules
+ """)
+ parser.add_option("-s", "--split",
+ action="store", dest="split", default=False, metavar="RULES",
+ help="Split given Makefile.rules to the original snippets")
+ parser.add_option("-o", "--output",
+ action="store", dest="output", default=False, metavar="RULES",
+ help="Split given Makefile.rules to the original snippets")
+ (options, args) = parser.parse_args()
+ return options, args
+
+def splitRules(rules):
+ pass
+
+def buildRules(snippets, output):
+ if output:
+ f = open(output, "w+")
+ else:
+ f = sys.stdout
+
+ parts = []
+
+ for snip in snippets:
+ parts.append(Snippet(snip))
+
+ for type in ['legal', 'doc', 'code']:
+ for snip in parts:
+ f.writelines(snip.__dict__[type])
+ f.close()
+
+def main():
+ (options, args) = parseCommandLine()
+ if options.split:
+ splitRules(options.split)
+ else:
+ buildRules(args, options.output)
+
+
+if __name__ == "__main__": main()