]> rtime.felk.cvut.cz Git - omk.git/blob - omkbuild.py
Small enhancements
[omk.git] / omkbuild.py
1 #!/usr/bin/env python
2
3 from optparse import OptionParser
4 import os
5 import sys
6 import string
7
8 class Snippet:
9
10     def __init__(self, fname):
11         self.name = ""
12         self.legal = []
13         self.doc = []
14         self.code = []
15         self.read(fname)
16
17     def read(self, fname):
18         self.name = fname
19         f = open(fname, "r")
20         current = self.legal
21         counter = 0
22         for line in f:
23             if current == self.legal:
24                 if line.strip() == "#": counter += 1
25                 else:
26                     if counter == 2: current = self.doc
27                     counter = 0
28             if line[0] != "#": current = self.code
29             
30             current.append(line)
31
32         if not self.doc:
33             self.doc = self.legal
34             self.legal = []
35             
36         f.close
37
38     def __str__(self):
39         s = "Snippet: %s\n" % self.name
40         s += "  Legal: %d lines\n" % len(self.legal)
41         s += "  Doc:   %d lines\n" % len(self.doc)
42         s += "  Code:  %d lines\n" % len(self.code)
43         return s
44
45
46 def parseCommandLine():
47     parser = OptionParser(usage = """
48     %prog [-o FILE] snippet1 snippet2 ...      build Makefile.rules from snippets
49     %prog --split Makfile.rules
50     """)
51     parser.add_option("-s", "--split",
52                       action="store", dest="split", default=False, metavar="RULES",
53                       help="Split given Makefile.rules to the original snippets")
54     parser.add_option("-o", "--output",
55                       action="store", dest="output", default=False, metavar="RULES",
56                       help="Output built Makefile.rules to file RULES")
57     (options, args) = parser.parse_args()
58     return options, args
59
60 def splitRules(rules):
61     pass
62
63 def buildRules(snippets, output):
64     if output:
65         f = open(output, "w+")
66     else:
67         f = sys.stdout
68
69     parts = []
70         
71     for snip in snippets:
72         parts.append(Snippet(snip))
73
74     for type in ['legal', 'doc', 'code']:
75         for snip in parts:
76             if len(snip.__dict__[type]) == 0: continue
77             firstLine = string.rstrip(snip.__dict__[type][0])
78             f.write(firstLine.ljust(60)+" #@omk@%s\n"%snip.name)
79             f.writelines(snip.__dict__[type][1:])
80     f.close()
81
82 def main():
83     (options, args) = parseCommandLine()
84     if options.split:
85         splitRules(options.split)
86     else:
87         buildRules(args, options.output)
88
89
90 if __name__ == "__main__":    main()