]> rtime.felk.cvut.cz Git - omk.git/blobdiff - omkbuild.py
Added news to web page
[omk.git] / omkbuild.py
index 5dc7c3a9982ee996cfc89a5799c39f50217655f6..facccc21d39e42edc433f298a85e9480ef6c5cb5 100755 (executable)
@@ -13,9 +13,9 @@ Snippet syntax:
   documentation := comment*
   rules ::= text
   
-  comment ::= '#' text
-  empty-comment ::= '#'
-  text ::= [^#] ...
+  comment ::= '#' text '\n'
+  empty-comment ::= '#' '\n'
+  text ::= [^#] ... '\n'
 
  Makefile.rules policies:
 
@@ -23,23 +23,32 @@ Snippet syntax:
      as they are in snippets i.e. copyrights, documentations and rules.
 
    * On the first line of each part of the Makefile.rules, there is
-     special mart of the form #OMK@<snippet file name><EOL>. This mark
-     is used for splitting Makefile.rules back to the original
-     snippets.
-
+     special mark of the form #OMK@<snippet file name><EOL>. This mark
+     is used for splitting modified Makefile.rules back to the
+     original snippets. If <snippet file name> starts with __, it is
+     ignored during splitting.
 """
 
 from optparse import OptionParser
 import os
+import os.path
 import sys
 import string
 import re
 
+rulesDir = "rules"
+snippetsDir = "snippets"
+
 class LineList(list):
     """List of text lines"""
     def getDiff(self, other):
         s = ''
         for i in range(len(self)):
+            if i >= len(other):
+                s += ("  Line %d differs!\n" % i)
+                s += "  -"+self[i].rstrip() + "\n"
+                s += "  +\n"
+                break
             if self[i] != other[i]:
                 s += ("  Line %d differs!\n" % i)
                 s += "  -"+self[i].rstrip() + "\n"
@@ -54,15 +63,20 @@ class LineList(list):
 
     def loadFromFile(self, fname):
         """Loads itself from file."""
-        f = open(fname, "r")
-        self.expand(f.readlines())
+        try:
+            f = open(fname, "r")
+        except IOError:
+            sys.stderr.write("Cannot open %s\n" % fname)
+            sys.exit(1)
+            
+        self.extend(f.readlines())
         f.close
 
 class Snippet:
-    def __init__(self, fname = None):
+    def __init__(self, fname = None, name = ""):
         """Initializes the snippet and if fname is given, reads it
         from file"""
-        self.name = ""
+        self.name = name
         self.legal = LineList()
         self.doc = LineList()
         self.code = LineList()
@@ -77,6 +91,9 @@ class Snippet:
          
         f.close
 
+    def addCodeLine(self, line):
+        self.code.append(line)
+
     def readLines(self, lines):
         """Parses the snippet given as a list and stores it in itself."""
         currentPart = self.legal
@@ -172,6 +189,7 @@ class Snippets:
         assert isinstance(other, Snippets)
         s = ''
         for snip in self:
+            if (snip.name[0:2] == '__'): continue
             if (snip != other[snip.name]):
                 s += "Snippet %s:\n" % snip.name
                 s += snip.getDiff(other[snip.name])
@@ -216,8 +234,10 @@ class MakefileRules(LineList):
                 if not snipName in snipDict:
                     snipDict[snipName] = LineList()
                 currentLinesList = snipDict[snipName]
-
-            currentLinesList.append(line);
+            
+            if currentLinesList != None:
+                currentLinesList.append(line);
+                
         return snipDict
 
 
@@ -231,12 +251,18 @@ def parseCommandLine():
                       help="Split given Makefile.rules to the original snippets")
     parser.add_option("-o", "--output",
                       action="store", dest="output", default=False, metavar="RULES",
-                      help="Output built Makefile.rules to file RULES")
+                      help="Write Makefile.rules to file RULES")
+    parser.add_option("-a", "--all",
+                      action="store_true", dest="all",
+                      help="Rebuild all rules acording to rulesdef.py")
     (options, args) = parser.parse_args()
     return options, args
 
-def buildRules(fnames, output):
+def buildRules(fnames, output, name):
     rules = MakefileRules()
+    snip_rule_type = Snippet(name="__type")
+    snip_rule_type.addCodeLine("OMK_RULES_TYPE=%s\n" % name)
+    rules.snippets += snip_rule_type
     rules.snippets.loadFromFiles(fnames)
     rules.combine()
 
@@ -264,17 +290,38 @@ def splitRules(rulesFN, output):
     rulesCheck.snippets = rules.snippets
     rulesCheck.combine()
 
-    if rules.rules != rulesCheck.rules:
-        sys.stderr.write("Consistency error:\n")
-        diff = rules.rules.getDiff(rulesCheck.rules)
-        sys.stderr.write(diff)
-        sys.exit(1)
-
-    #TODO: Store snippets to files
+    # The comparsion is not that simple. The order of rules might be
+    # different.
+#     if rules.rules != rulesCheck.rules:
+#         sys.stderr.write("Consistency error:\n")
+#         diff = rules.rules.getDiff(rulesCheck.rules)
+#         sys.stderr.write(diff)
+#         sys.exit(1)
+
+    for snip in rules.snippets:
+        if snip.name[0:2] == "__":
+            continue
+        print snip.name
+        f = None
+        if output == "-": f = sys.stdout
+        else: f = open(snip.name, "w+")
+        f.writelines(snip.asLinesList())
+        f.close()
+
+def buildAllRules():
+    import rulesdef
+    os.chdir(snippetsDir)
+    for rules in rulesdef.rules:
+        print 'Building rules: %s' % rules
+        outputDir = os.path.join(sys.path[0], rulesDir, rules)
+        if not os.path.isdir(outputDir): os.makedirs(outputDir)
+        buildRules(rulesdef.rules[rules], os.path.join(outputDir, 'Makefile.rules'), rules)
 
 def main():
     (options, args) = parseCommandLine()
-    if options.split:
+    if options.all:
+        buildAllRules()
+    elif options.split:
         splitRules(options.split, options.output)
     else:
         buildRules(args, options.output)