]> git.proxmox.com Git - mirror_frr.git/blobdiff - python/xrelfo.py
python: use ujson if available
[mirror_frr.git] / python / xrelfo.py
index 0ecd0085798f440e15f3a663984c4e2f9b49f624..09455ea9b43d926ad3ec6af15c45ddfcea622563 100644 (file)
@@ -21,12 +21,21 @@ import os
 import struct
 import re
 import traceback
-import json
+
+json_dump_args = {}
+
+try:
+    import ujson as json
+
+    json_dump_args["escape_forward_slashes"] = False
+except ImportError:
+    import json
+
 import argparse
 
 from clippy.uidhash import uidhash
 from clippy.elf import *
-from clippy import frr_top_src
+from clippy import frr_top_src, CmdAttr
 from tiabwarfo import FieldApplicator
 
 try:
@@ -196,8 +205,6 @@ Xref.containers[XREFT_LOGMSG] = XrefLogmsg
 class CmdElement(ELFDissectStruct, XrelfoJson):
     struct = 'cmd_element'
 
-    cmd_attrs = { 0: None, 1: 'deprecated', 2: 'hidden'}
-
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
 
@@ -207,10 +214,14 @@ class CmdElement(ELFDissectStruct, XrelfoJson):
         jsobj.update({
             'string': self.string,
             'doc': self.doc,
-            'attr': self.cmd_attrs.get(self.attr, self.attr),
         })
-        if jsobj['attr'] is None:
-            del jsobj['attr']
+        if self.attr:
+            jsobj['attr'] = attr = self.attr
+            for attrname in CmdAttr.__members__:
+                val = CmdAttr[attrname]
+                if attr & val:
+                    jsobj.setdefault('attrs', []).append(attrname.lower())
+                    attr &= ~val
 
         jsobj['defun'] = dict([(i, getattr(self.xref, i)) for i in ['file', 'line', 'func']])
 
@@ -357,6 +368,7 @@ def main():
     argp.add_argument('--out-by-file',     type=str, help='write by-file JSON output')
     argp.add_argument('-Wlog-format',      action='store_const', const=True)
     argp.add_argument('-Wlog-args',        action='store_const', const=True)
+    argp.add_argument('-Werror',           action='store_const', const=True)
     argp.add_argument('--profile',         action='store_const', const=True)
     argp.add_argument('binaries', metavar='BINARY', nargs='+', type=str, help='files to read (ELF files or libtool objects)')
     args = argp.parse_args()
@@ -380,9 +392,12 @@ def _main(args):
             traceback.print_exc()
 
     for option in dir(args):
-        if option.startswith('W'):
+        if option.startswith('W') and option != 'Werror':
             checks = sorted(xrelfo.check(args))
             sys.stderr.write(''.join([c[-1] for c in checks]))
+
+            if args.Werror and len(checks) > 0:
+                errors += 1
             break
 
 
@@ -412,12 +427,12 @@ def _main(args):
 
     if args.output:
         with open(args.output + '.tmp', 'w') as fd:
-            json.dump(out, fd, indent=2, sort_keys=True)
+            json.dump(out, fd, indent=2, sort_keys=True, **json_dump_args)
         os.rename(args.output + '.tmp', args.output)
 
     if args.out_by_file:
         with open(args.out_by_file + '.tmp', 'w') as fd:
-            json.dump(outbyfile, fd, indent=2, sort_keys=True)
+            json.dump(outbyfile, fd, indent=2, sort_keys=True, **json_dump_args)
         os.rename(args.out_by_file + '.tmp', args.out_by_file)
 
 if __name__ == '__main__':