forked from PyCQA/eradicate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheradicate.py
183 lines (149 loc) · 6.26 KB
/
eradicate.py
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
# Copyright (C) 2012-2013 Steven Myint
#
# 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.
"""Removes commented-out Python code."""
from __future__ import print_function
from io import StringIO
import os
import re
import tokenize
__version__ = '0.1.1'
try:
unicode
except NameError:
unicode = str
def comment_contains_code(line):
"""Return True comment contains code."""
line = line.lstrip()
if not line.startswith('#'):
return False
line = line.lstrip(' \t\v\n#').strip()
# Ignore non-comment related hashes. For example, "# Issue #999".
if re.search('#[0-9]', line):
return False
# Check that this is possibly code.
for symbol in list('()[]{}:=') + ['print', 'return', 'break', 'continue',
'import']:
if symbol in line:
break
else:
return False
# Handle multi-line cases manually.
for ending in ')]}':
if line.endswith(ending + ':'):
return True
for symbol in ['else', 'try', 'finally']:
if re.match(r'^\s*' + symbol + r'\s*:\s*$', line):
return True
for remove_beginning in ['print', 'return']:
line = re.sub('^' + remove_beginning + r'\b\s*', '', line)
try:
compile(line, '<string>', 'exec')
return True
except (SyntaxError, TypeError, UnicodeDecodeError):
return False
def commented_out_code_line_numbers(source):
"""Yield line numbers of commented-out code."""
sio = StringIO(source)
try:
for token in tokenize.generate_tokens(sio.readline):
token_type = token[0]
start_row = token[2][0]
line = token[4]
if (token_type == tokenize.COMMENT and
line.lstrip().startswith('#') and
comment_contains_code(line)):
yield start_row
except (tokenize.TokenError, IndentationError):
pass
def filter_commented_out_code(source):
"""Yield code with commented out code removed."""
marked_lines = list(commented_out_code_line_numbers(source))
sio = StringIO(source)
previous_line = ''
for line_number, line in enumerate(sio.readlines(), start=1):
if (line_number not in marked_lines or
previous_line.rstrip().endswith('\\')):
yield line
previous_line = line
def fix_file(filename, args, standard_out):
"""Run filter_commented_out_code() on file."""
encoding = detect_encoding(filename)
with open_with_encoding(filename, encoding=encoding) as input_file:
source = input_file.read()
filtered_source = unicode().join(filter_commented_out_code(source))
if source != filtered_source:
if args.in_place:
with open_with_encoding(filename, mode='w',
encoding=encoding) as output_file:
output_file.write(filtered_source)
else:
import difflib
diff = difflib.unified_diff(
StringIO(source).readlines(),
StringIO(filtered_source).readlines(),
'before/' + filename,
'after/' + filename)
standard_out.write(unicode().join(diff))
def open_with_encoding(filename, encoding, mode='r'):
"""Return opened file with a specific encoding."""
import io
return io.open(filename, mode=mode, encoding=encoding,
newline='') # Preserve line endings
def detect_encoding(filename):
"""Return file encoding."""
try:
with open(filename, 'rb') as input_file:
from lib2to3.pgen2 import tokenize as lib2to3_tokenize
encoding = lib2to3_tokenize.detect_encoding(input_file.readline)[0]
# Check for correctness of encoding.
with open_with_encoding(filename, encoding) as input_file:
input_file.read()
return encoding
except (SyntaxError, LookupError, UnicodeDecodeError):
return 'latin-1'
def main(argv, standard_out, standard_error):
"""Main entry point."""
import argparse
parser = argparse.ArgumentParser(description=__doc__, prog='eradicate')
parser.add_argument('-i', '--in-place', action='store_true',
help='make changes to files instead of printing diffs')
parser.add_argument('-r', '--recursive', action='store_true',
help='drill down directories recursively')
parser.add_argument('--version', action='version',
version='%(prog)s ' + __version__)
parser.add_argument('files', nargs='+', help='files to format')
args = parser.parse_args(argv[1:])
filenames = list(set(args.files))
while filenames:
name = filenames.pop(0)
if args.recursive and os.path.isdir(name):
for root, directories, children in os.walk(name):
filenames += [os.path.join(root, f) for f in children
if f.endswith('.py') and
not f.startswith('.')]
for d in directories:
if d.startswith('.'):
directories.remove(d)
else:
try:
fix_file(name, args=args, standard_out=standard_out)
except IOError as exception:
print(exception, file=standard_error)