OLD | NEW |
1 # pylint: disable=W0622 | 1 # pylint: disable=W0622 |
2 # Copyright (c) 2004-2013 LOGILAB S.A. (Paris, FRANCE). | 2 # Copyright (c) 2004-2013 LOGILAB S.A. (Paris, FRANCE). |
3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr | 3 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
4 # | 4 # |
5 # This program is free software; you can redistribute it and/or modify it under | 5 # This program is free software; you can redistribute it and/or modify it under |
6 # the terms of the GNU General Public License as published by the Free Software | 6 # the terms of the GNU General Public License as published by the Free Software |
7 # Foundation; either version 2 of the License, or (at your option) any later | 7 # Foundation; either version 2 of the License, or (at your option) any later |
8 # version. | 8 # version. |
9 # | 9 # |
10 # This program is distributed in the hope that it will be useful, but WITHOUT | 10 # This program is distributed in the hope that it will be useful, but WITHOUT |
(...skipping 24 matching lines...) Expand all Loading... |
35 def __init__(self, min_lines=4, ignore_comments=False, | 35 def __init__(self, min_lines=4, ignore_comments=False, |
36 ignore_docstrings=False, ignore_imports=False): | 36 ignore_docstrings=False, ignore_imports=False): |
37 self.min_lines = min_lines | 37 self.min_lines = min_lines |
38 self.ignore_comments = ignore_comments | 38 self.ignore_comments = ignore_comments |
39 self.ignore_docstrings = ignore_docstrings | 39 self.ignore_docstrings = ignore_docstrings |
40 self.ignore_imports = ignore_imports | 40 self.ignore_imports = ignore_imports |
41 self.linesets = [] | 41 self.linesets = [] |
42 | 42 |
43 def append_stream(self, streamid, stream, encoding=None): | 43 def append_stream(self, streamid, stream, encoding=None): |
44 """append a file to search for similarities""" | 44 """append a file to search for similarities""" |
45 stream.seek(0) # XXX may be removed with astroid > 0.23 | |
46 if encoding is None: | 45 if encoding is None: |
47 readlines = stream.readlines | 46 readlines = stream.readlines |
48 else: | 47 else: |
49 readlines = lambda: [line.decode(encoding) for line in stream] | 48 readlines = lambda: [line.decode(encoding) for line in stream] |
50 try: | 49 try: |
51 self.linesets.append(LineSet(streamid, | 50 self.linesets.append(LineSet(streamid, |
52 readlines(), | 51 readlines(), |
53 self.ignore_comments, | 52 self.ignore_comments, |
54 self.ignore_docstrings, | 53 self.ignore_docstrings, |
55 self.ignore_imports)) | 54 self.ignore_imports)) |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 self.stats = self.linter.add_stats(nb_duplicated_lines=0, | 292 self.stats = self.linter.add_stats(nb_duplicated_lines=0, |
294 percent_duplicated_lines=0) | 293 percent_duplicated_lines=0) |
295 | 294 |
296 def process_module(self, node): | 295 def process_module(self, node): |
297 """process a module | 296 """process a module |
298 | 297 |
299 the module's content is accessible via the stream object | 298 the module's content is accessible via the stream object |
300 | 299 |
301 stream must implement the readlines method | 300 stream must implement the readlines method |
302 """ | 301 """ |
303 self.append_stream(self.linter.current_name, node.file_stream, node.file
_encoding) | 302 with node.stream() as stream: |
| 303 self.append_stream(self.linter.current_name, |
| 304 stream, |
| 305 node.file_encoding) |
304 | 306 |
305 def close(self): | 307 def close(self): |
306 """compute and display similarities on closing (i.e. end of parsing)""" | 308 """compute and display similarities on closing (i.e. end of parsing)""" |
307 total = sum([len(lineset) for lineset in self.linesets]) | 309 total = sum([len(lineset) for lineset in self.linesets]) |
308 duplicated = 0 | 310 duplicated = 0 |
309 stats = self.stats | 311 stats = self.stats |
310 for num, couples in self._compute_sims(): | 312 for num, couples in self._compute_sims(): |
311 msg = [] | 313 msg = [] |
312 for lineset, idx in couples: | 314 for lineset, idx in couples: |
313 msg.append("==%s:%s" % (lineset.name, idx)) | 315 msg.append("==%s:%s" % (lineset.name, idx)) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 elif opt in ('-i', '--ignore-comments'): | 356 elif opt in ('-i', '--ignore-comments'): |
355 ignore_comments = True | 357 ignore_comments = True |
356 elif opt in ('--ignore-docstrings',): | 358 elif opt in ('--ignore-docstrings',): |
357 ignore_docstrings = True | 359 ignore_docstrings = True |
358 elif opt in ('--ignore-imports',): | 360 elif opt in ('--ignore-imports',): |
359 ignore_imports = True | 361 ignore_imports = True |
360 if not args: | 362 if not args: |
361 usage(1) | 363 usage(1) |
362 sim = Similar(min_lines, ignore_comments, ignore_docstrings, ignore_imports) | 364 sim = Similar(min_lines, ignore_comments, ignore_docstrings, ignore_imports) |
363 for filename in args: | 365 for filename in args: |
364 sim.append_stream(filename, open(filename)) | 366 with open(filename) as stream: |
| 367 sim.append_stream(filename, stream) |
365 sim.run() | 368 sim.run() |
366 sys.exit(0) | 369 sys.exit(0) |
367 | 370 |
368 if __name__ == '__main__': | 371 if __name__ == '__main__': |
369 Run() | 372 Run() |
OLD | NEW |