check_docs.py 6.17 KB
Newer Older
1
#!/usr/bin/env python
2

3 4 5 6 7 8
import sys, glob

sys.path.append("../modules/python/src2/")
import hdr_parser as hp

opencv_hdr_list = [
9 10 11 12 13
"../modules/core/include/opencv2/core.hpp",
"../modules/ml/include/opencv2/ml.hpp",
"../modules/imgproc/include/opencv2/imgproc.hpp",
"../modules/calib3d/include/opencv2/calib3d.hpp",
"../modules/features2d/include/opencv2/features2d.hpp",
14 15
"../modules/video/include/opencv2/video/tracking.hpp",
"../modules/video/include/opencv2/video/background_segm.hpp",
16 17
"../modules/objdetect/include/opencv2/objdetect.hpp",
"../modules/highgui/include/opencv2/highgui.hpp",
18 19 20
]

opencv_module_list = [
21
"core",
22 23 24 25 26 27 28
"imgproc",
"calib3d",
"features2d",
"video",
"objdetect",
"highgui",
"ml"
29 30 31
]

class RSTParser(object):
32

33 34
    def __init__(self):
        self.read_whitelist()
35

36 37 38 39 40 41 42 43
    # reads the file containing functions and classes that do not need to be documented
    def read_whitelist(self):
        self.whitelist = {}
        try:
            wf = open("check_docs_whitelist.txt", "rt")
        except IOError:
            return
        self.parser = hp.CppHeaderParser()
44

45 46 47 48 49 50 51 52 53
        for l in wf.readlines():
            cpos = l.find("#")
            if cpos >= 0:
                l = l[:cpos]
            l = l.strip()
            if not l:
                continue
            rst_decl = None
            if "(" in l:
54
                l = l.replace("cv::", "")
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                rst_decl = self.parser.parse_func_decl_no_wrap(l)
                fname = rst_decl[0]
            else:
                fname = l.replace("::", ".")
            complist = fname.split(".")
            prefix = ""
            alreadyListed = False
            wl = []
            for c in complist:
                prefix = (prefix + "." + c).lstrip(".")
                wl = self.whitelist.get(prefix, [])
                if wl == "*":
                    break
            if wl == "*":
                continue
            if not rst_decl:
                self.whitelist[fname] = "*"
            else:
                wl.append(rst_decl)
                self.whitelist[fname] = wl
        wf.close()
76

77 78 79 80 81
    def process_rst(self, docname):
        df = open(docname, "rt")
        fdecl = ""
        balance = 0
        lineno = 0
82

83 84 85 86
        for l in df.readlines():
            lineno += 1
            ll = l.strip()
            if balance == 0:
87 88
                if not ll.startswith(".. c:function::") and \
                   not ll.startswith(".. cpp:function::") and \
89 90
                   not ll.startswith(".. ocv:function::") and \
                   not ll.startswith(".. ocv:cfunction::"):
91 92 93 94 95 96 97 98 99
                    continue
                fdecl = ll[ll.find("::") + 3:]
            elif balance > 0:
                fdecl += ll
            balance = fdecl.count("(") - fdecl.count(")")
            assert balance >= 0
            if balance > 0:
                continue
            rst_decl = self.parser.parse_func_decl_no_wrap(fdecl)
100 101
            fname = rst_decl[0]
            hdr_decls = self.fmap.get(fname, [])
102
            if not hdr_decls:
103 104
                fname = fname.replace("cv.", "")
                hdr_decls = self.fmap.get(fname, [])
105
            if not hdr_decls:
106
                print "Documented function %s (%s) in %s:%d is not in the headers" % (fdecl, rst_decl[0].replace(".", "::"), docname, lineno)
107 108 109 110
                continue
            decl_idx = 0
            for hd in hdr_decls:
                if len(hd[3]) != len(rst_decl[3]):
111
                    decl_idx += 1
112 113 114
                    continue
                idx = 0
                for a in hd[3]:
115
                    if a[0] != rst_decl[3][idx][0] and a[0].replace("cv::", "") != rst_decl[3][idx][0]:
116 117 118 119 120 121
                        break
                    idx += 1
                if idx == len(hd[3]):
                    break
                decl_idx += 1
            if decl_idx < len(hdr_decls):
122
                self.fmap[fname] = hdr_decls[:decl_idx] + hdr_decls[decl_idx+1:]
123 124
                continue
            print "Documented function %s in %s:%d does not have a match" % (fdecl, docname, lineno)
125
        df.close()
126

127 128 129
    def decl2str(self, decl):
        return "%s %s(%s)" % (decl[1], decl[0], ", ".join([a[0] + " " + a[1] for a in decl[3]]))

130 131 132 133 134 135 136 137
    def check_module_docs(self, name):
        self.parser = hp.CppHeaderParser()
        decls = []
        self.fmap = {}

        for hname in opencv_hdr_list:
            if hname.startswith("../modules/" + name):
                decls += self.parser.parse(hname, wmode=False)
138

139 140
        for d in decls:
            fname = d[0]
141
            if not fname.startswith("struct") and not fname.startswith("class") and not fname.startswith("const"):
142 143
                dlist = self.fmap.get(fname, [])
                dlist.append(d)
144
                self.fmap[fname] = dlist
145

146
        self.missing_docfunc_list = []
147

148 149 150
        doclist = glob.glob("../modules/" + name + "/doc/*.rst")
        for d in doclist:
            self.process_rst(d)
151

152
        print "\n\n########## The list of undocumented functions: ###########\n\n"
153
        misscount = 0
154 155
        fkeys = sorted(self.fmap.keys())
        for f in fkeys:
156 157 158
            # skip undocumented destructors
            if "~" in f:
                continue
159
            decls = self.fmap[f]
160 161 162 163 164 165 166 167 168 169 170
            fcomps = f.split(".")
            prefix = ""
            wlist_decls = []
            for c in fcomps:
                prefix = (prefix + "." + c).lstrip(".")
                wlist_decls = self.whitelist.get(prefix, [])
                if wlist_decls == "*":
                    break
            if wlist_decls == "*":
                continue
            wlist_decls = [self.decl2str(d) for d in wlist_decls]
171

172
            for d in decls:
173
                dstr = self.decl2str(d)
174 175 176
                # special hack for ML: skip old variants of the methods
                if name == "ml" and ("CvMat" in dstr):
                    continue
177 178 179
                if dstr not in wlist_decls:
                    misscount += 1
                    print "%s %s(%s)" % (d[1], d[0].replace(".", "::"), ", ".join([a[0] + " " + a[1] for a in d[3]]))
180
        print "\n\n\nundocumented functions in %s: %d" % (name, misscount)
181 182


183 184 185 186
p = RSTParser()
for m in opencv_module_list:
    print "\n\n*************************** " + m + " *************************\n"
    p.check_module_docs(m)