-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathglob-replace.py
executable file
·165 lines (132 loc) · 5.71 KB
/
glob-replace.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
#!/usr/bin/env python
# Sebastian Raschka 2014-2018
# A command line tool for global replacements of strings in files.
import glob
import fileinput
import sys
import os
searched_files = 0
replaced_instances = 0
TEXTCHARS = bytearray({7, 8, 9, 10, 12, 13, 27} |
set(range(0x20, 0x100)) - {0x7f})
def check_extension(extensions, filename):
for x in extensions:
if filename.endswith(x):
return True
return False
def is_binary(filepath):
with open(filepath, 'rb') as f:
content = f.read(1024)
return bool(content.translate(None, TEXTCHARS))
def is_invisible(path):
fp = os.path.basename(path)
return fp.startswith((".", "__"))
def replace_indir(extensions, search, replace,
cur_dir, print_out, skip_binary):
filenames = glob.glob(os.path.join(cur_dir, '*'))
global searched_files
global replaced_instances
for f in filenames:
found = 0
if not os.path.isfile(f):
continue
if skip_binary:
if is_binary(f):
searched_files += 1
continue
if not extensions or check_extension(extensions, f):
if print_out:
with open(f, 'r', encoding="ISO-8859-1") as in_file:
try:
for line in in_file:
if search in line:
found += 1
except UnicodeDecodeError as e:
print('ERROR: File %s may be binary. You can try to'
' skip binary by using the --skip_binary flag' %
f)
raise e
else:
for line in fileinput.input(f, inplace=True, mode='rU'):
if search in line:
found += 1
try:
sys.stdout.write(line.replace(search, replace))
except UnicodeDecodeError as e:
print('ERROR: File %s may be binary. You can try to'
' skip binary by using the --skip_binary flag' %
f)
raise e
replaced_instances += found
searched_files += 1
if print_out and found:
print('%d x in %s' % (found, f))
def run(extensions, search, replace, cur_dir,
recursive, print_out, skip_binary, skip_invisible):
if recursive:
tree = os.walk(cur_dir)
for d in tree:
if skip_invisible:
if is_invisible(os.path.basename(d[0])):
continue
replace_indir(extensions, search, replace,
cur_dir=d[0], print_out=print_out,
skip_binary=skip_binary)
else:
replace_indir(extensions, search, replace,
cur_dir=cur_dir, print_out=print_out,
skip_binary=skip_binary)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description=('A command line tool for global '
'replacements of strings in files.'),
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument('start_dir')
parser.add_argument('-s', '--search', help='String to be replaced.')
parser.add_argument('-r', '--replace', help='String to replace the search '
'query with.')
parser.add_argument('-f', '--replace_from_file', help='Link to a text '
'file that contains the text to replace the'
' query with')
parser.add_argument('-w', '--walk', action='store_true', default=False,
help='Applies the global replacement recursively to '
'sub-directorires.')
parser.add_argument('-e', '--extensions', help='Only process files with '
'particular extensions. '
'Comma separated, e.g., ".txt,.py"')
parser.add_argument('--skip_invisible', help='Skips files and folders '
'starting with a leading period or two leading '
'underscores (e.g., `.git` or `__pycache__`).',
action='store_true', default=False)
parser.add_argument('-p', '--print', action='store_true',
help='Prints what it would rename.')
parser.add_argument('-b', '--skip_binary', action='store_true',
help=('Skips binary files if enabled '
'(may result in false positives'
' and negatives).'))
parser.add_argument('-v', '--version', action='version', version='v. 1.2')
args = parser.parse_args()
if args.replace and args.replace_from_file:
raise AttributeError("Can't have both -r/--replace and "
"--replace_from_file/-f flags at the same time.")
if args.replace:
replace = args.replace
elif args.replace_from_file:
with open(args.replace_from_file, 'r', encoding="ISO-8859-1") as f:
replace = f.read()
else:
raise AttributeError("Must use either the -r/--replace or "
"--replace_from_file/-f flag.")
extensions = False
if args.extensions:
extensions = args.extensions.split(',')
run(extensions, args.search, replace,
args.start_dir, args.walk, args.print, args.skip_binary,
skip_invisible=args.skip_invisible)
would = 'replaced'
if args.print:
would = 'would replace'
print('Searched %s file(s) and %s %s instance(s) of %s' % (
searched_files, would, replaced_instances, args.search))