(no commit message)
authorSapan Bhatia <sapanb@cs.princeton.edu>
Wed, 2 Sep 2009 03:14:00 +0000 (03:14 +0000)
committerSapan Bhatia <sapanb@cs.princeton.edu>
Wed, 2 Sep 2009 03:14:00 +0000 (03:14 +0000)
sfatables/commands/Add.py
sfatables/sfatables
sfatables/xmlextension.py

index ef2b26e..c13c714 100644 (file)
@@ -1,7 +1,7 @@
 import os, time
+import libxml2
 from sfatables.command import Command
-
-import sfatables.globals
+from sfatables.globals import *
 
 class Add(Command):
     options = [('-A','--add')]
@@ -12,15 +12,49 @@ class Add(Command):
     def __init__(self):
         return
 
+    def getnextfilename(self,type,chain):
+        dir = sfatables_config + chain;
+        last_rule_number = 1
+
+        for file in os.walk(dir):
+            if (file.startswith('sfatables-')):
+                number_str = file.split('-')[1]
+                number = int(number_str)
+                if (number>last_rule_number):
+                    last_rule_number = number
+
+        return "sfatables-%d-%s"%(last_rule_number,type)
+
+
+
     def call(self, command_options, match_options, target_options):
-        filename = match_dir + "/"+match_options.name+".xml"
+        import pdb
+        filename = match_dir + "/"+match_options.match_name+".xml"
         xmldoc = libxml2.parseFile(filename)
     
-        p = self.xmldoc.xpathNewContext()
+        p = xmldoc.xpathNewContext()
+
+        supplied_arguments = match_options.arguments
+        for option in supplied_arguments:
+            option_name = option['name']
+            option_value = getattr(match_options,option_name)
+
+            if (hasattr(match_options,option_name)):
+                context = p.xpathEval("//rule/argument[name='%s']"%option_name)
+                if (not context):
+                    raise Exception('Unknown option %s for match %s'%(option,option['name']))
+                else:
+                    # Add the value of option
+                    valueNode = libxml2.newNode('value')
+                    valueNode.addContent(option_value)
+                    context[0].addChild(valueNode)
 
-        context = p.xpathEval("//rule/argument[name='user-hrn']")
         pdb.set_trace()
-
+        chain = command_options.args[0]
+        filename = self.getnextfilename('match',chain)
+        xmldoc.saveFile(filename)
+        p.xpathFreeContext()
+        xmldoc.freeDoc()
 
 
         return True
index 59628e7..f4bb8a8 100755 (executable)
@@ -78,7 +78,8 @@ def main():
 
     command_dict = load_commands("sfatables.commands",commands.all)
     command_parser = create_parser(command_dict)
-    (options, args) = command_parser.parse_args()
+    (options, args) = command_parser.parse_args(pargs[0])
+    setattr(options, 'args', args)
 
     command = command_dict[options.command]
 
@@ -89,8 +90,17 @@ def main():
         match_parser = create_parser_xml_ext(match_dict)
         matches_str = ",".join(match_dict.keys())
         match_parser.add_option('-m','--match',dest='match_name',help='Match name (one of %s)'%matches_str, metavar = 'MATCH')
-
         (match_options, args) = match_parser.parse_args(pargs[1])
+        try:
+            match_name = match_options.match_name
+        except Exception:
+            print "Must specify match name with -m"
+
+        if (match_dict.has_key(match_name)):
+            setattr(match_options, 'arguments', match_dict[match_name].arguments)
+        else:
+            raise Exception('Match %s not found'%match_name)
+
     else:
         match_options=None
 
@@ -102,6 +112,16 @@ def main():
         targets_str = ",".join(target_dict.keys())
         target_parser.add_option('-j','--jump',dest='target_name',help='Target name (one of %s)'%targets, metavar = 'TARGET')
         (target_options, args) = target_parser.parse_args(pargs[2])
+        try:
+            target_name = target_options.target_name
+        except Exception:
+            print "Must specify target name with -m"
+
+#        if (target_dict.has_key(target_name)):
+#            setattr(target_options, 'arguments', target_dict[target_name].arguments)
+#        else:
+#            raise Exception('Target %s not found'%target_name)
+
     else:
         target_options = None
 
index c852a26..84e336f 100644 (file)
@@ -5,8 +5,7 @@
 #   - The parameters that the processor needs to evaluate the context
 
 import libxml2
-
-match_dir = 'matches'
+from sfatables.globals import *
 
 class Xmlextension:
     context = ""