Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d68a4bd

Browse files
committed
Use re instead of regex.
Don't rewrite the file in place. (Reported by Andy Dustman.)
1 parent 2e1094e commit d68a4bd

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

Tools/scripts/fixps.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
#! /usr/bin/env python
1+
#!/usr/bin/env python
22

33
# Fix Python script(s) to reference the interpreter via /usr/bin/env python.
4+
# Warning: this overwrites the file without making a backup.
45

56
import sys
6-
import regex
7-
import regsub
7+
import re
88

99

1010
def main():
1111
for file in sys.argv[1:]:
1212
try:
13-
f = open(file, 'r+')
14-
except IOError:
15-
print file, ': can\'t open for update'
13+
f = open(file, 'r')
14+
except IOError, msg:
15+
print file, ': can\'t open :', msg
1616
continue
1717
line = f.readline()
18-
if regex.match('^#! */usr/local/bin/python', line) < 0:
18+
if not re.match('^#! */usr/local/bin/python', line):
1919
print file, ': not a /usr/local/bin/python script'
2020
f.close()
2121
continue
2222
rest = f.read()
23-
line = regsub.sub('/usr/local/bin/python',
24-
'/usr/bin/env python', line)
23+
f.close()
24+
line = re.sub('/usr/local/bin/python',
25+
'/usr/bin/env python', line)
2526
print file, ':', `line`
26-
f.seek(0)
27+
f = open(file, "w")
2728
f.write(line)
2829
f.write(rest)
2930
f.close()

0 commit comments

Comments
 (0)