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