Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Tools/demo/markov.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def test():
continue
else:
f = open(filename, 'r')
if debug: print('processing', filename, '...')
text = f.read()
f.close()
with f:
if debug: print('processing', filename, '...')
text = f.read()
paralist = text.split('\n\n')
for para in paralist:
if debug > 1: print('feeding ...')
Expand Down
23 changes: 11 additions & 12 deletions Tools/demo/rpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ def main():
port = int(port[i+1:])
host = host[:i]
command = ' '.join(sys.argv[2:])
s = socket(AF_INET, SOCK_STREAM)
s.connect((host, port))
s.send(command.encode())
s.shutdown(SHUT_WR)
reply = b''
while True:
data = s.recv(BUFSIZE)
if not data:
break
reply += data
print(reply.decode(), end=' ')
s.close()
with socket(AF_INET, SOCK_STREAM) as s:
s.connect((host, port))
s.send(command.encode())
s.shutdown(SHUT_WR)
reply = b''
while True:
data = s.recv(BUFSIZE)
if not data:
break
reply += data
print(reply.decode(), end=' ')

main()
20 changes: 10 additions & 10 deletions Tools/demo/rpythond.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ def main():
s.listen(1)
while True:
conn, (remotehost, remoteport) = s.accept()
print('connection from', remotehost, remoteport)
request = b''
while 1:
data = conn.recv(BUFSIZE)
if not data:
break
request += data
reply = execute(request.decode())
conn.send(reply.encode())
conn.close()
with conn:
print('connection from', remotehost, remoteport)
request = b''
while 1:
data = conn.recv(BUFSIZE)
if not data:
break
request += data
reply = execute(request.decode())
conn.send(reply.encode())

def execute(request):
stdout = sys.stdout
Expand Down
3 changes: 2 additions & 1 deletion Tools/freeze/checkextensions_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ def parse_dsp(dsp):
ret = []
dsp_path, dsp_name = os.path.split(dsp)
try:
lines = open(dsp, "r").readlines()
with open(dsp, "r") as fp:
lines = fp.readlines()
except IOError as msg:
sys.stderr.write("%s: %s\n" % (dsp, msg))
return None
Expand Down
3 changes: 2 additions & 1 deletion Tools/freeze/freeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ def main():
# last option can not be "-i", so this ensures "pos+1" is in range!
if sys.argv[pos] == '-i':
try:
options = open(sys.argv[pos+1]).read().split()
with open(sys.argv[pos+1]) as infp:
options = infp.read().split()
except IOError as why:
usage("File name '%s' specified with the -i option "
"can not be read - %s" % (sys.argv[pos+1], why) )
Expand Down
5 changes: 2 additions & 3 deletions Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,8 @@ class Options:
# initialize list of strings to exclude
if options.excludefilename:
try:
fp = open(options.excludefilename)
options.toexclude = fp.readlines()
fp.close()
with open(options.excludefilename) as fp:
options.toexclude = fp.readlines()
except IOError:
print(_(
"Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr)
Expand Down
15 changes: 7 additions & 8 deletions Tools/scripts/cleanfuture.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def check(file):
errprint("%r: I/O Error: %s" % (file, str(msg)))
return

ff = FutureFinder(f, file)
changed = ff.run()
if changed:
ff.gettherest()
f.close()
with f:
ff = FutureFinder(f, file)
changed = ff.run()
if changed:
ff.gettherest()
if changed:
if verbose:
print("changed.")
Expand All @@ -122,9 +122,8 @@ def check(file):
os.rename(file, bak)
if verbose:
print("renamed", file, "to", bak)
g = open(file, "w")
ff.write(g)
g.close()
with open(file, "w") as g:
ff.write(g)
if verbose:
print("wrote new", file)
else:
Expand Down
9 changes: 5 additions & 4 deletions Tools/scripts/combinerefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ def read(fileiter, pat, whilematch):
else:
break

def combine(fname):
f = open(fname)

def combinefile(f):
fi = iter(f)

for line in read(fi, re.compile(r'^Remaining objects:$'), False):
Expand Down Expand Up @@ -121,8 +119,11 @@ def combine(fname):
print('[%s->%s]' % (addr2rc[addr], rc), end=' ')
print(guts, addr2guts[addr])

f.close()
print("%d objects before, %d after" % (before, after))

def combine(fname):
with open(fname) as f:
combinefile(f)

if __name__ == '__main__':
combine(sys.argv[1])
22 changes: 11 additions & 11 deletions Tools/scripts/dutree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import os, sys, errno

def main():
p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r')
total, d = None, {}
for line in p.readlines():
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
filename = line[i:-1]
comps = filename.split('/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
with os.popen('du ' + ' '.join(sys.argv[1:])) as p:
for line in p:
i = 0
while line[i] in '0123456789': i = i+1
size = eval(line[:i])
while line[i] in ' \t': i = i+1
filename = line[i:-1]
comps = filename.split('/')
if comps[0] == '': comps[0] = '/'
if comps[len(comps)-1] == '': del comps[len(comps)-1]
total, d = store(size, comps, total, d)
try:
display(total, d)
except IOError as e:
Expand Down
37 changes: 19 additions & 18 deletions Tools/scripts/eptags.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,30 @@ def treat_file(filename, outfp):
except OSError:
sys.stderr.write('Cannot open %s\n'%filename)
return
charno = 0
lineno = 0
tags = []
size = 0
while 1:
line = fp.readline()
if not line:
break
lineno = lineno + 1
m = matcher.search(line)
if m:
tag = m.group(0) + '\177%d,%d\n' % (lineno, charno)
tags.append(tag)
size = size + len(tag)
charno = charno + len(line)
with fp:
charno = 0
lineno = 0
tags = []
size = 0
while 1:
line = fp.readline()
if not line:
break
lineno = lineno + 1
m = matcher.search(line)
if m:
tag = m.group(0) + '\177%d,%d\n' % (lineno, charno)
tags.append(tag)
size = size + len(tag)
charno = charno + len(line)
outfp.write('\f\n%s,%d\n' % (filename,size))
for tag in tags:
outfp.write(tag)

def main():
outfp = open('TAGS', 'w')
for filename in sys.argv[1:]:
treat_file(filename, outfp)
with open('TAGS', 'w') as outfp:
for filename in sys.argv[1:]:
treat_file(filename, outfp)

if __name__=="__main__":
main()
22 changes: 11 additions & 11 deletions Tools/scripts/finddiv.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ def process(filename, listnames):
except IOError as msg:
sys.stderr.write("Can't open: %s\n" % msg)
return 1
g = tokenize.generate_tokens(fp.readline)
lastrow = None
for type, token, (row, col), end, line in g:
if token in ("/", "/="):
if listnames:
print(filename)
break
if row != lastrow:
lastrow = row
print("%s:%d:%s" % (filename, row, line), end=' ')
fp.close()
with fp:
g = tokenize.generate_tokens(fp.readline)
lastrow = None
for type, token, (row, col), end, line in g:
if token in ("/", "/="):
if listnames:
print(filename)
break
if row != lastrow:
lastrow = row
print("%s:%d:%s" % (filename, row, line), end=' ')

def processdir(dir, listnames):
try:
Expand Down
20 changes: 8 additions & 12 deletions Tools/scripts/fixnotice.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,19 @@ def main():
elif opt == '--dry-run':
DRYRUN = 1
elif opt == '--oldnotice':
fp = open(arg)
OLD_NOTICE = fp.read()
fp.close()
with open(arg) as fp:
OLD_NOTICE = fp.read()
elif opt == '--newnotice':
fp = open(arg)
NEW_NOTICE = fp.read()
fp.close()
with open(arg) as fp:
NEW_NOTICE = fp.read()

for arg in args:
process(arg)


def process(file):
f = open(file)
data = f.read()
f.close()
with open(file) as f:
data = f.read()
i = data.find(OLD_NOTICE)
if i < 0:
if VERBOSE:
Expand All @@ -102,9 +99,8 @@ def process(file):
data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):]
new = file + ".new"
backup = file + ".bak"
f = open(new, "w")
f.write(data)
f.close()
with open(new, "w") as f:
f.write(data)
os.rename(file, backup)
os.rename(new, file)

Expand Down
20 changes: 9 additions & 11 deletions Tools/scripts/fixps.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@ def main():
except IOError as msg:
print(filename, ': can\'t open :', msg)
continue
line = f.readline()
if not re.match('^#! */usr/local/bin/python', line):
print(filename, ': not a /usr/local/bin/python script')
f.close()
continue
rest = f.read()
f.close()
with f:
line = f.readline()
if not re.match('^#! */usr/local/bin/python', line):
print(filename, ': not a /usr/local/bin/python script')
continue
rest = f.read()
line = re.sub('/usr/local/bin/python',
'/usr/bin/env python', line)
print(filename, ':', repr(line))
f = open(filename, "w")
f.write(line)
f.write(rest)
f.close()
with open(filename, "w") as f:
f.write(line)
f.write(rest)

if __name__ == '__main__':
main()
15 changes: 6 additions & 9 deletions Tools/scripts/get-remote-certificate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,26 @@ def strip_to_x509_cert(certfile_contents, outfile=None):
return None
else:
tn = tempfile.mktemp()
fp = open(tn, "wb")
fp.write(m.group(1) + b"\n")
fp.close()
with open(tn, "wb") as fp:
fp.write(m.group(1) + b"\n")
try:
tn2 = (outfile or tempfile.mktemp())
status, output = subproc(r'openssl x509 -in "%s" -out "%s"' %
(tn, tn2))
if status != 0:
raise RuntimeError('OpenSSL x509 failed with status %s and '
'output: %r' % (status, output))
fp = open(tn2, 'rb')
data = fp.read()
fp.close()
with open(tn2, 'rb') as fp:
data = fp.read()
os.unlink(tn2)
return data
finally:
os.unlink(tn)

if sys.platform.startswith("win"):
tfile = tempfile.mktemp()
fp = open(tfile, "w")
fp.write("quit\n")
fp.close()
with open(tfile, "w") as fp:
fp.write("quit\n")
try:
status, output = subproc(
'openssl s_client -connect "%s:%s" -showcerts < "%s"' %
Expand Down
Loading