Browse Source

[2157] apply review comments

Yoshitaka Aharen 12 years ago
parent
commit
9e0f9e4df2
1 changed files with 101 additions and 78 deletions
  1. 101 78
      src/bin/auth/gen-statisticsitems.py.pre.in

+ 101 - 78
src/bin/auth/gen-statisticsitems.py.pre.in

@@ -24,6 +24,7 @@ import re
 import sys
 import sys
 import json
 import json
 from xml.etree import ElementTree
 from xml.etree import ElementTree
+from xml.dom.minidom import parseString
 
 
 item_list = []
 item_list = []
 localstatedir = '@@LOCALSTATEDIR@@'
 localstatedir = '@@LOCALSTATEDIR@@'
@@ -33,7 +34,7 @@ pre_suffix = '.pre'
 
 
 xmldocument_command_name = 'b10-auth'
 xmldocument_command_name = 'b10-auth'
 
 
-def need_generate(filepath, mtime):
+def need_generate(filepath, prepath, mtime):
     '''Check if we need to generate the specified file.
     '''Check if we need to generate the specified file.
 
 
     To avoid unnecessary compilation, we skip (re)generating the file when
     To avoid unnecessary compilation, we skip (re)generating the file when
@@ -42,7 +43,7 @@ def need_generate(filepath, mtime):
     '''
     '''
     if os.path.exists(filepath) and\
     if os.path.exists(filepath) and\
         (os.path.getmtime(filepath) > mtime and
         (os.path.getmtime(filepath) > mtime and
-         os.path.getmtime(filepath) > os.path.getmtime(filepath+pre_suffix)):
+         os.path.getmtime(filepath) > os.path.getmtime(prepath)):
         return False
         return False
     return True
     return True
 
 
@@ -72,29 +73,31 @@ def import_definitions():
     global item_list
     global item_list
 
 
     items_definition_file = srcdir + os.sep + 'statistics_msg_items.def'
     items_definition_file = srcdir + os.sep + 'statistics_msg_items.def'
-    item_definition = open(items_definition_file, 'r')
+    with open(items_definition_file, 'r') as item_definition:
-
+        re_splitter = re.compile('\t+')
-    re_splitter = re.compile('\t+')
+        l = item_list
-    l = item_list
+        lp = None
-    lp = None
+        for line in item_definition.readlines():
-    for line in item_definition.readlines():
+            element = re_splitter.split(line.rstrip())
-        element = re_splitter.split(line.rstrip())
+            # pop first element if it is empty to skip indent
-        if element[0] == '':
+            if element[0] == '':
-            element.pop(0)
+                element.pop(0)
-
+
-        if element[-1] == '=':
+            # last element is '=': a branch node definition.
-            l.append({'name': element[0], 'child': [], 'index': element[1],
+            if element[-1] == '=':
-                      'description': element[2], 'parent': lp})
+                l.append({'name': element[0], 'child': [], 'index': element[1],
-            lp = l
+                          'description': element[2], 'parent': lp})
-            l = l[-1]['child']
+                lp = l
-        elif element[-1] == ';':
+                l = l[-1]['child']
-            l = lp
+            # last element is ';': end of a branch node.
-            lp = l[-1]['parent']
+            elif element[-1] == ';':
-        else:
+                l = lp
-            l.append({'name': element[0], 'child': None, 'index': element[1],
+                lp = l[-1]['parent']
-                      'description': element[2], 'parent': lp})
+            # otherwise, a leaf node definition.
-
+            else:
-    item_definition.close()
+                l.append({'name': element[0], 'child': None,
+                          'index': element[1], 'description': element[2],
+                          'parent': lp})
     return os.path.getmtime(items_definition_file)
     return os.path.getmtime(items_definition_file)
 
 
 def generate_specfile(specfile, def_mtime):
 def generate_specfile(specfile, def_mtime):
@@ -102,13 +105,12 @@ def generate_specfile(specfile, def_mtime):
     If the specfile is newer than both skeleton and def_mtime, file generation
     If the specfile is newer than both skeleton and def_mtime, file generation
     will be skipped.
     will be skipped.
 
 
-    This method reads the content of skeleton and replaces
+    This method reads the content of skeleton and append statistics items
-    <!-- ### STATISTICS DATA PLACEHOLDER ### --> with statistics items
+    definition into { "module_spec": { "statistics": } }. 
-    definition. LOCALSTATEDIR is also expanded.
+    LOCALSTATEDIR is also expanded.
 
 
     Returns nothing.
     Returns nothing.
     '''
     '''
-    global item_list
 
 
     def convert_list(items, prefix=''):
     def convert_list(items, prefix=''):
         spec_list = []
         spec_list = []
@@ -160,18 +162,18 @@ def generate_specfile(specfile, def_mtime):
             },
             },
         }]
         }]
 
 
-    if need_generate(builddir+os.sep+specfile, def_mtime):
+    if need_generate(builddir+os.sep+specfile,
-        stats_pre = open(builddir+os.sep+specfile+pre_suffix, 'r')
+                     builddir+os.sep+specfile+pre_suffix, def_mtime):
-        stats_pre_json = \
+        with open(builddir+os.sep+specfile+pre_suffix, 'r') as stats_pre:
-            json.loads(stats_pre.read().replace('@@LOCAL'+'STATEDIR@@',
+            # split LOCALSTATEDIR to avoid substitution
-                                                localstatedir))
+            stats_pre_json = \
-        stats_pre.close()
+                json.loads(stats_pre.read().replace('@@LOCAL'+'STATEDIR@@',
+                                                    localstatedir))
         stats_pre_json['module_spec']['statistics'] = statistics_spec_list
         stats_pre_json['module_spec']['statistics'] = statistics_spec_list
         statistics_spec_json = json.dumps(stats_pre_json, sort_keys=True,
         statistics_spec_json = json.dumps(stats_pre_json, sort_keys=True,
                                           indent=2)
                                           indent=2)
-        stats_spec = open(builddir+os.sep+specfile, 'w')
+        with open(builddir+os.sep+specfile, 'w') as stats_spec:
-        stats_spec.write(statistics_spec_json)
+            stats_spec.write(statistics_spec_json)
-        stats_spec.close()
     else:
     else:
         print('skip generating ' + specfile)
         print('skip generating ' + specfile)
     return
     return
@@ -187,12 +189,24 @@ def generate_docfile(docfile, def_mtime):
 
 
     Returns nothing.
     Returns nothing.
     '''
     '''
-    global item_list
-
     def convert_list(items, tree, prefix=''):
     def convert_list(items, tree, prefix=''):
+        '''
+        Build XML tree from items.
+            <varlistentry>
+              <term>##item_full_name##</term>
+              <listitem>
+              <simpara>
+              ##item_description##
+              </simpara>
+              </listitem>
+            <varlistentry>
+        xmldocument_command_name in item_description is surrounded with
+        <command>.
+        '''
         for item in items:
         for item in items:
             full_item_name = prefix + item['name']
             full_item_name = prefix + item['name']
             if item['child'] is None:
             if item['child'] is None:
+                # the item is a leaf node: build varlistentry
                 child_element = ElementTree.SubElement(tree, 'varlistentry')
                 child_element = ElementTree.SubElement(tree, 'varlistentry')
                 term = ElementTree.SubElement(child_element, 'term')
                 term = ElementTree.SubElement(child_element, 'term')
                 term.text = full_item_name
                 term.text = full_item_name
@@ -200,35 +214,47 @@ def generate_docfile(docfile, def_mtime):
                 sim_para = ElementTree.SubElement(list_item, 'simpara')
                 sim_para = ElementTree.SubElement(list_item, 'simpara')
                 sim_para.text = ''
                 sim_para.text = ''
                 prev = None
                 prev = None
+                # put xmldocument_command_name in <command> node
                 for word in item['description'].split():
                 for word in item['description'].split():
                     if word == xmldocument_command_name:
                     if word == xmldocument_command_name:
                         command = ElementTree.SubElement(sim_para, 'command')
                         command = ElementTree.SubElement(sim_para, 'command')
                         command.text = word
                         command.text = word
-                        para_tail = command
+                        command.tail = ''
-                        command.tail = ' '
                         prev = command
                         prev = command
                     else:
                     else:
                         if prev is None:
                         if prev is None:
                             sim_para.text += word + ' '
                             sim_para.text += word + ' '
                         else:
                         else:
                             prev.tail += word + ' '
                             prev.tail += word + ' '
+                sim_para.text = sim_para.text.rstrip()
+                if not prev is None:
+                    prev.tail = prev.tail.rstrip()
             else:
             else:
+                # the item is a branch node: call myself for child nodes
                 convert_list(item['child'], tree, full_item_name + '.')
                 convert_list(item['child'], tree, full_item_name + '.')
         return
         return
 
 
-    if need_generate(builddir+os.sep+docfile, def_mtime):
+    if need_generate(builddir+os.sep+docfile,
-        doc_pre = open(srcdir+os.sep+docfile+pre_suffix, 'r')
+                     srcdir+os.sep+docfile+pre_suffix, def_mtime):
-        doc_pre_xml = doc_pre.read().replace('@@LOCAL'+'STATEDIR@@',
+        with open(srcdir+os.sep+docfile+pre_suffix, 'r') as doc_pre:
-                                             localstatedir)
+            # split LOCALSTATEDIR to avoid substitution
-        doc_pre.close
+            doc_pre_xml = doc_pre.read().replace('@@LOCAL'+'STATEDIR@@',
+                                                 localstatedir)
 
 
         variable_tree = ElementTree.Element('variablelist')
         variable_tree = ElementTree.Element('variablelist')
         convert_list(item_list, variable_tree)
         convert_list(item_list, variable_tree)
-        doc = open(builddir+os.sep+docfile, 'w')
+        pretty_xml = \
-        doc.write(doc_pre_xml.replace(
+            parseString(ElementTree.tostring(variable_tree)).toprettyxml()
-            '<!-- ### STATISTICS DATA PLACEHOLDER ### -->',
+        # remove XML declaration
-            str(ElementTree.tostring(variable_tree))))
+        pretty_xml = re.sub('<\?xml[^?]+\?>', '', pretty_xml)
-        doc.close()
+        # remove extra whitespaces inside <command> and <term>
+        pretty_xml = \
+            re.sub(r'<(command|term)>\s+([^<\s]+)\s+</\1>', r'<\1>\2</\1>',
+                   pretty_xml)
+        with open(builddir+os.sep+docfile, 'w') as doc:
+            doc.write(doc_pre_xml.replace(
+                '<!-- ### STATISTICS DATA PLACEHOLDER ### -->',
+                pretty_xml))
     else:
     else:
         print('skip generating ' + docfile)
         print('skip generating ' + docfile)
     return
     return
@@ -247,8 +273,6 @@ def generate_cxx(itemsfile, ccfile, utfile, def_mtime):
 
 
     Returns nothing.
     Returns nothing.
     '''
     '''
-    global item_list
-
     msg_counter_types = 'enum MSGCounterType {\n'
     msg_counter_types = 'enum MSGCounterType {\n'
     item_names =  ['// using -1 as counter_id to state it is not a '
     item_names =  ['// using -1 as counter_id to state it is not a '
                    + 'counter item\n']
                    + 'counter item\n']
@@ -290,39 +314,38 @@ def generate_cxx(itemsfile, ccfile, utfile, def_mtime):
     item_decls = msg_counter_types
     item_decls = msg_counter_types
     item_defs = ''.join(item_names)
     item_defs = ''.join(item_names)
 
 
-    if need_generate(builddir+os.sep+itemsfile, def_mtime):
+    if need_generate(builddir+os.sep+itemsfile,
-        statistics_items_h_pre = open(srcdir+os.sep+itemsfile+pre_suffix, 'r')
+                     srcdir+os.sep+itemsfile+pre_suffix, def_mtime):
-        items_pre = statistics_items_h_pre.read()
+        with open(srcdir+os.sep+itemsfile+pre_suffix, 'r') \
-        statistics_items_h_pre.close
+            as statistics_items_h_pre:
+            items_pre = statistics_items_h_pre.read()
 
 
-        statistics_items_h = open(builddir+os.sep+itemsfile, 'w')
+        with open(builddir+os.sep+itemsfile, 'w') as statistics_items_h:
-        statistics_items_h.write(items_pre.replace(
+            statistics_items_h.write(items_pre.replace(
-            '// ### STATISTICS ITEMS DECLARATION ###', item_decls))
+                '// ### STATISTICS ITEMS DECLARATION ###', item_decls))
-        statistics_items_h.close()
     else:
     else:
         print('skip generating ' + itemsfile)
         print('skip generating ' + itemsfile)
 
 
-    if need_generate(builddir+os.sep+ccfile, def_mtime):
+    if need_generate(builddir+os.sep+ccfile,
-        statistics_cc_pre = open(srcdir+os.sep+ccfile+pre_suffix, 'r')
+                     srcdir+os.sep+ccfile+pre_suffix, def_mtime):
-        items_pre = statistics_cc_pre.read()
+        with open(srcdir+os.sep+ccfile+pre_suffix, 'r') as statistics_cc_pre:
-        statistics_cc_pre.close
+            items_pre = statistics_cc_pre.read()
 
 
-        statistics_cc = open(builddir+os.sep+ccfile, 'w')
+        with open(builddir+os.sep+ccfile, 'w') as statistics_cc:
-        statistics_cc.write(items_pre.replace(
+            statistics_cc.write(items_pre.replace(
-            '// ### STATISTICS ITEMS DEFINITION ###', item_defs))
+                '// ### STATISTICS ITEMS DEFINITION ###', item_defs))
-        statistics_cc.close()
     else:
     else:
         print('skip generating ' + ccfile)
         print('skip generating ' + ccfile)
 
 
-    if need_generate(builddir+os.sep+utfile, def_mtime):
+    if need_generate(builddir+os.sep+utfile,
-        statistics_ut_cc_pre = open(srcdir+os.sep+utfile+pre_suffix, 'r')
+                     srcdir+os.sep+utfile+pre_suffix, def_mtime):
-        items_pre = statistics_ut_cc_pre.read()
+        with open(srcdir+os.sep+utfile+pre_suffix, 'r') \
-        statistics_ut_cc_pre.close
+            as statistics_ut_cc_pre:
+            items_pre = statistics_ut_cc_pre.read()
 
 
-        statistics_ut_cc = open(builddir+os.sep+utfile, 'w')
+        with open(builddir+os.sep+utfile, 'w') as statistics_ut_cc:
-        statistics_ut_cc.write(items_pre.replace(
+            statistics_ut_cc.write(items_pre.replace(
-            '// ### STATISTICS ITEMS DEFINITION ###', item_defs))
+                '// ### STATISTICS ITEMS DEFINITION ###', item_defs))
-        statistics_ut_cc.close()
     else:
     else:
         print('skip generating ' + utfile)
         print('skip generating ' + utfile)