File: find-german-comments

package info (click to toggle)
libreoffice 1:6.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,699,584 kB
  • sloc: cpp: 4,078,558; xml: 324,485; java: 281,935; python: 48,236; ansic: 36,476; perl: 32,383; sh: 13,671; yacc: 10,820; makefile: 9,085; cs: 6,600; lex: 2,184; objc: 1,904; awk: 978; pascal: 949; asm: 866; php: 79; csh: 20; sed: 5
file content (402 lines) | stat: -rwxr-xr-x 14,721 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#!/usr/bin/env python3
########################################################################
#
#  Copyright (c) 2010 Jonas Jensen, Miklos Vajna
#
#  Permission is hereby granted, free of charge, to any person
#  obtaining a copy of this software and associated documentation
#  files (the "Software"), to deal in the Software without
#  restriction, including without limitation the rights to use,
#  copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the
#  Software is furnished to do so, subject to the following
#  conditions:
#
#  The above copyright notice and this permission notice shall be
#  included in all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
#  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#  OTHER DEALINGS IN THE SOFTWARE.
#
########################################################################


import sys
import re
import subprocess
import os
import argparse
import string

class Parser:
    """
    This parser extracts comments from source files, tries to guess
    their language and then prints out the German ones.
    """
    def __init__(self):
        self.strip = string.punctuation + " \n"
        self.text_cat = self.start_text_cat()
        parser = argparse.ArgumentParser(description='Searches for German comments in cxx/hxx source files inside a given root directory recursively.')
        parser.add_argument("-f", "--filenames-only", action="store_true",
            help="Only print the filenames of files containing German comments")
        parser.add_argument("-v", "--verbose", action="store_true",
            help="Turn on verbose mode (print only positives progress to stderr)")
        parser.add_argument("-l", "--line-numbers", action="store_true",
            help="Prints the filenames and line numbers only.")
        parser.add_argument("-L", "--line-numbers-pos", action="store_true",
            help="Prints the filenames and line numbers only (if positive).")
        parser.add_argument("-t", "--threshold", action="store", default=0, type=int,
            help="When used with '--line-numbers', only bothers outputting comment info if there are more than X number of flagged comments. Useful for weeding out false positives.")
        parser.add_argument("directory", nargs='?', default='.', type=str, help='Give a directory to search in')
        self.args = parser.parse_args()
        self.check_source_files(self.args.directory)

    def get_comments(self, filename):
        """
        Extracts the source code comments.
        """
        linenum = 0
        if self.args.verbose:
            print("processing file '%s'...\n" % filename)
        sock = open(filename)
        # add an empty line to trigger the output of collected oneliner
        # comment group
        lines = sock.readlines() + ["\n"]
        sock.close()

        in_comment = False
        buf = []
        count = 1
        for i in lines:
            if "//" in i and not in_comment:
                # if we find a new //-style comment, then we
                # just append it to a previous one if: there is
                # only whitespace before the // mark that is
                # necessary to make comments longer, giving
                # more reliable output
                if not len(re.sub("(.*)//.*", r"\1", i).strip(self.strip)):
                    s = re.sub(".*// ?", "", i).strip(self.strip)
                    if len(s):
                        buf.append(s)
                else:
                    # otherwise it's an independent //-style comment in the next line
                    yield (count, "\n    ".join(buf))
                    buf = [re.sub(".*// ?", "", i.strip(self.strip))]
            elif "//" not in i and not in_comment and len(buf) > 0:
                # first normal line after a // block
                yield (count, "\n    ".join(buf))
                buf = []
            elif "/*" in i and "*/" not in i and not in_comment:
                # start of a real multiline comment
                in_comment = True
                linenum = count
                s = re.sub(".*/\*+", "", i.strip(self.strip))
                if len(s):
                    buf.append(s.strip(self.strip))
            elif in_comment and not "*/" in i:
                # in multiline comment
                s = re.sub("^( |\|)*\*?", "", i)
                if len(s.strip(self.strip)):
                    buf.append(s.strip(self.strip))
            elif "*/" in i and in_comment:
                # end of multiline comment
                in_comment = False
                s = re.sub(r"\*+/.*", "", i.strip(self.strip))
                if len(s):
                    buf.append(s)
                yield (count, "\n    ".join(buf))
                buf = []
            elif "/*" in i and "*/" in i:
                # c-style oneliner comment
                yield (count, re.sub(".*/\*(.*)\*/.*", r"\1", i).strip(self.strip))
            count += 1

    def start_text_cat(self):
        cwd = os.getcwd()
        # change to our directory
        os.chdir(os.path.split(os.path.abspath(sys.argv[0]))[0])
        sock = subprocess.Popen(["text_cat/text_cat", "-s", "-d", "text_cat/LM"], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        os.chdir(cwd)
        return sock

    def get_lang(self, s):
        """ the output is 'german' or 'english' or 'german or english'. when
        unsure, just don't warn, there are strings where you just can't
        teremine the results reliably, like '#110680#' """

        self.text_cat.stdin.write(bytes(s, 'utf-8'))
        self.text_cat.stdin.write(bytes("\n", 'utf-8'))
        self.text_cat.stdin.flush()
        lang = self.text_cat.stdout.readline().strip()
        return lang

    def is_german(self, s):
        """
        determines if a string is German or not
        """
        # for short strings we can't do reliable recognition, so skip
        # short strings and less than 4 words
        s = s.replace('\n', ' ')
        if len(s) < 32 or len(s.split()) < 4:
            return False
        return self.get_lang(s) == b"german"

    def check_file(self, path):
        """
        checks each comment in a file
        """
        def tab_calc(path):
            START = 40 #Default of 10 tabs
            if len(path) >= START:
                return 1
            diff = START - len(path)
            if diff % 4 is not 0:
                padding = 1
            else:
                padding = 0
            return (diff/4)+padding

        if self.args.line_numbers or self.args.line_numbers_pos:
            TABS = "\t"*10
            path_linenums = []
            for linenum, s in self.get_comments(path):
                if self.is_german(s):
                    path_linenums.append(linenum)
            valid = len(path_linenums) > int(self.args.threshold)
            if self.args.line_numbers:
                print("%s ... %s positives -- %s\n" % (path, str(len(path_linenums)), str(valid)))
            if valid:
                if self.args.line_numbers_pos:
                    print("%s ... %s positives\n" % (path, str(len(path_linenums))))
                    return
                if len(path) + (len(path_linenums)*4) > 75:
                    print("%s:\n" % path)
                    while path_linenums:
                        i = 0
                        numline = []
                        while i < 10:
                            try:
                                numline.append(path_linenums[0])
                                path_linenums.remove(path_linenums[0])
                            except IndexError:
                                i = 10
                            i += 1
                        numline = [str(i) for i in numline]
                        print("%s%s" % (TABS, ",".join(numline)))
                else:
                    if self.args.line_numbers:
                        path_linenums = [str(i) for i in path_linenums]
                        print("%s:%s%s" % (path, "\t"*int(tab_calc(path)), ",".join(path_linenums)))

        elif not self.args.filenames_only:
            for linenum, s in self.get_comments(path):
                if self.is_german(s):
                    print("%s:%s: %s" % (path, linenum, s))
        else:
            fnames = set([])
            for linenum, s in self.get_comments(path):
                if self.is_german(s):
                    # Make sure we print each filename only once
                    fnames.add(path)
            # Print the filenames
            for f in fnames:
                print(f)

    def first_elem(self, path):
        """
        Returns the root directory in our repo of a given path, so we can check against the whitelist.
        """
        lastElem = os.path.dirname(path)
        done = False
        while not done:
            nextElem = os.path.split(lastElem)[0]
            if nextElem is not '':
                lastElem = nextElem
            else:
                done = True
        return lastElem

    def check_source_files(self, directory):
        """
        checks each _tracked_ file in a directory recursively
        """

	# top-level project directory -> use whitelist.
        globalscan = False
        if os.path.exists(directory + "/.git/config"):
           globalscan = True

        # Change into the given dir, so "git ls-tree" does work.
        os.chdir(directory)

        sock = os.popen(r"git ls-tree -r HEAD --name-only |egrep '\.(c|cc|cpp|cxx|h|hxx|mm)$'")
        lines = sock.readlines()
        sock.close()

        # Helps to speedup a global scan
        directory_whitelist = {
            "ure" : 1,
            "ios" : 1,
            "bean" : 1,
            "apple_remote" : 1,
            "UnoControls" : 1,
            "accessibility" : 1,
            "android" : 1,
            "animations" : 1,
            "avmedia" : 1,
            "basctl" : 1,
            "basegfx" : 1,
            "basic" : 1,
            "binaryurp" : 1,
            "bridges" : 1,
            "canvas" : 1,
            "chart2" : 1,
            "cli_ure" : 1,
            "codemaker" : 1,
            "comphelper" : 1,
            "compilerplugins" : 1,
            "configmgr" : 1,
            "connectivity" : 1,
            "cppcanvas" : 1,
            "cppu" : 1,
            "cppuhelper" : 1,
            "cpputools" : 1,
            "cui" : 1,
            "dbaccess" : 1,
            "desktop" : 1,
            "drawinglayer" : 1,
            "dtrans" : 1,
            "editeng" : 1,
            "embeddedobj" : 1,
            "embedserv" : 1,
            "eventattacher" : 1,
            "extensions" : 1,
            "external" : 1,
            "filter" : 1,
            "forms" : 1,
            "formula" : 1,
            "fpicker" : 1,
            "framework" : 1,
            "helpcompiler" : 1,
            "hwpfilter" : 1,
            "i18npool" : 1,
            "i18nlangtag" : 1,
            "i18nutil" : 1,
            "idl" : 1,
            "idlc" : 1,
            "include" : 1,
            "io" : 1,
            "javaunohelper" : 1,
            "jvmaccess" : 1,
            "jvmfwk" : 1,
            "jurt" : 1,
            "l10ntools" : 1,
            "libreofficekit" : 1,
            "lingucomponent" : 1,
            "linguistic" : 1,
            "lotuswordpro" : 1,
            "mysqlc" : 1,
            "o3tl" : 1,
            "odk" : 1,
            "officecfg" : 1,
            "onlineupdate" : 1,
            "opencl" : 1,
            "oox" : 1,
            "package" : 1,
            "postprocess" : 1,
            "pyuno" : 1,
            "registry" : 1,
            "remotebridges" : 1,
            "reportdesign" : 1,
            "rsc" : 1,
            "sal" : 1,
            "salhelper" : 1,
            "sax" : 1,
            "sc" : 1,
            "scaddins" : 1,
            "sccomp" : 1,
            "scripting" : 1,
            "sd" : 1,
            "sdext" : 1,
            "sfx2" : 1,
            "shell" : 1,
            "setup_native" : 1,
            "sot" : 1,
            "slideshow" : 1,
            "smoketest" : 1,
            "solenv" : 1,
            "soltools" : 1,
            "starmath" : 1,
            "stoc" : 1,
            "store" : 1,
            "svgio" : 1,
            "svl" : 1,
            "svtools" : 1,
            "svx" : 1,
            "sw" : 1,
            "test" : 1,
            "testtools" : 1,
            "toolkit" : 1,
            "tools" : 1,
            "touch" : 1,
            "ucb" : 1,
            "ucbhelper" : 1,
            "unodevtools" : 1,
            "unotest" : 1,
            "unoidl" : 1,
            "unotools" : 1,
            "unoxml" : 1,
            "uui" : 1,
            "vbahelper" : 1,
            "vcl" : 1,
            "winaccessibility" : 1,
            "writerfilter" : 1,
            "writerperfect" : 1,
            "xmlhelp" : 1,
            "xmloff" : 1,
            "xmlreader" : 1,
            "xmlsecurity" : 1,
            "xmlscript" : 1,
        }

        if globalscan:
            print("Scanning all files globally:")
        elif directory == '.':
            print("Scanning all files in our current directory:")
        else:
            print("Scanning all files in", directory + ":")

        num_checked = 0

        for path in lines:
            baseDir = self.first_elem(path)
            # If we have an globalscan use the whitelist.
            if globalscan:
                if not baseDir in directory_whitelist:
                    sys.stderr.write("\n - Error: Missing path %s -\n\n" % baseDir)
                    sys.exit(1)
                elif directory_whitelist[baseDir] is 0:
                    self.check_file(path.strip())
                    num_checked = num_checked + 1
                elif directory_whitelist[baseDir] is 1:
                    sys.stderr.write("Skipping whitelisted directory %s\n" % baseDir)
                    directory_whitelist[baseDir] = 2
            elif not globalscan:
                self.check_file(path.strip())
                num_checked = num_checked + 1

        print("Scanned %s files\n" % num_checked)

try:
    Parser()
except KeyboardInterrupt:
    print("Interrupted!")
    sys.exit(0)

# vim:set shiftwidth=4 softtabstop=4 expandtab: