pyscript = dedent("""
import time
- f = open("{path}", 'w')
- f.write('content')
- f.flush()
- f.write('content2')
- while True:
- time.sleep(1)
+ with open("{path}", 'w') as f:
+ f.write('content')
+ f.flush()
+ f.write('content2')
+ while True:
+ time.sleep(1)
""").format(path=path)
else:
pyscript = dedent("""
import time
- f = open("{path}", 'r')
- while True:
- time.sleep(1)
+ with open("{path}", 'r') as f:
+ while True:
+ time.sleep(1)
""").format(path=path)
rproc = self._run_python(pyscript)
f1 = open("{path}-1", 'r')
try:
fcntl.flock(f1, fcntl.LOCK_EX | fcntl.LOCK_NB)
- except IOError, e:
+ except IOError as e:
if e.errno == errno.EAGAIN:
pass
else:
try:
lockdata = struct.pack('hhllhh', fcntl.F_WRLCK, 0, 0, 0, 0, 0)
fcntl.fcntl(f2, fcntl.F_SETLK, lockdata)
- except IOError, e:
+ except IOError as e:
if e.errno == errno.EAGAIN:
pass
else:
time.sleep(1)
if not {loop}:
break
- except IOError, e:
+ except IOError as e:
pass
os.close(fd)
""").format(path=path, loop=str(loop))
return self.run_python(dedent("""
import zlib
path = "{path}"
- f = open(path, 'w')
- for i in range(0, {size}):
- val = zlib.crc32("%s" % i) & 7
- f.write(chr(val))
- f.close()
+ with open(path, 'w') as f:
+ for i in range(0, {size}):
+ val = zlib.crc32(str(i).encode('utf-8')) & 7
+ f.write(chr(val))
""".format(
path=os.path.join(self.mountpoint, filename),
size=size
return self.run_python(dedent("""
import zlib
path = "{path}"
- f = open(path, 'r')
- bytes = f.read()
- f.close()
+ with open(path, 'r') as f:
+ bytes = f.read()
if len(bytes) != {size}:
raise RuntimeError("Bad length {{0}} vs. expected {{1}}".format(
len(bytes), {size}
))
for i, b in enumerate(bytes):
- val = zlib.crc32("%s" % i) & 7
+ val = zlib.crc32(str(i).encode('utf-8')) & 7
if b != chr(val):
raise RuntimeError("Bad data at offset {{0}}".format(i))
""".format(
for i in range(0, n):
fname = "{{0}}_{{1}}".format(abs_path, i)
- h = open(fname, 'w')
- h.write('content')
- if {sync}:
- h.flush()
- os.fsync(h.fileno())
- h.close()
+ with open(fname, 'w') as f:
+ f.write('content')
+ if {sync}:
+ f.flush()
+ os.fsync(f.fileno())
""").format(abs_path=abs_path, count=count, sync=str(sync))
self.run_python(pyscript)
size = {size}
file_count = {file_count}
os.mkdir(os.path.join(mount_path, subdir))
- for i in xrange(0, file_count):
+ for i in range(0, file_count):
filename = "{{0}}_{{1}}.bin".format(i, size)
- f = open(os.path.join(mount_path, subdir, filename), 'w')
- f.write(size * 'x')
- f.close()
+ with open(os.path.join(mount_path, subdir, filename), 'w') as f:
+ f.write(size * 'x')
""".format(
mount_path=self.mount_a.mountpoint,
size=1024,
size_unit = {size_unit}
file_multiplier = {file_multiplier}
os.mkdir(os.path.join(mount_path, subdir))
- for i in xrange(0, file_multiplier):
- for size in xrange(0, {size_range}*size_unit, size_unit):
+ for i in range(0, file_multiplier):
+ for size in range(0, {size_range}*size_unit, size_unit):
filename = "{{0}}_{{1}}.bin".format(i, size / size_unit)
- f = open(os.path.join(mount_path, subdir, filename), 'w')
- f.write(size * 'x')
- f.close()
+ with open(os.path.join(mount_path, subdir, filename), 'w') as f:
+ f.write(size * 'x')
""".format(
mount_path=self.mount_a.mountpoint,
size_unit=size_unit,
path = os.path.join("{path}", "subdir")
os.mkdir(path)
for n in range(0, {file_count}):
- open(os.path.join(path, "%s" % n), 'w').write("%s" % n)
+ with open(os.path.join(path, "%s" % n), 'w') as f:
+ f.write(str(n))
""".format(
path=self.mount_a.mountpoint,
file_count=LOW_LIMIT+1
path = os.path.join("{path}", "subdir2")
os.mkdir(path)
for n in range(0, {file_count}):
- open(os.path.join(path, "%s" % n), 'w').write("%s" % n)
+ with open(os.path.join(path, "%s" % n), 'w') as f:
+ f.write(str(n))
dfd = os.open(path, os.O_DIRECTORY)
os.fsync(dfd)
""".format(
import os
path = os.path.join("{path}", "subdir2")
for n in range({file_count}, ({file_count}*3)//2):
- open(os.path.join(path, "%s" % n), 'w').write("%s" % n)
+ with open(os.path.join(path, "%s" % n), 'w') as f:
+ f.write(str(n))
""".format(
path=self.mount_a.mountpoint,
file_count=LOW_LIMIT
os.mkdir(path)
for n in range({file_count}):
fpath = os.path.join(path, "%s" % n)
- f = open(fpath, 'w')
- f.write("%s" % n)
- f.close()
+ with open(fpath, 'w') as f:
+ f.write(str(n))
os.unlink(fpath)
""".format(
path=self.mount_a.mountpoint,
os.mkdir(path)
for n in range({file_count}):
fpath = os.path.join(path, "%s" % n)
- f = open(fpath, 'w')
- f.write("%s" % n)
- f.close()
+ with open(fpath, 'w') as f:
+ f.write(str(n))
os.unlink(fpath)
""".format(
path=self.mount_a.mountpoint,