Skip to content

Commit

Permalink
Use context-managers to close open file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigc committed Feb 25, 2017
1 parent afeaef2 commit 467bcd1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 20 deletions.
9 changes: 6 additions & 3 deletions worker/buildbot_worker/test/unit/test_bot.py
Expand Up @@ -80,9 +80,12 @@ def check(vers):
def test_getWorkerInfo(self):
infodir = os.path.join(self.basedir, "info")
os.makedirs(infodir)
open(os.path.join(infodir, "admin"), "w").write("testy!")
open(os.path.join(infodir, "foo"), "w").write("bar")
open(os.path.join(infodir, "environ"), "w").write("something else")
with open(os.path.join(infodir, "admin"), "w") as f:
f.write("testy!")
with open(os.path.join(infodir, "foo"), "w") as f:
f.write("bar")
with open(os.path.join(infodir, "environ"), "w") as f:
f.write("something else")

d = self.bot.callRemote("getWorkerInfo")

Expand Down
18 changes: 12 additions & 6 deletions worker/buildbot_worker/test/unit/test_commands_fs.py
Expand Up @@ -176,7 +176,8 @@ def test_existing_file(self):
self.make_command(fs.MakeDirectory, dict(
dir='test-file',
), True)
open(os.path.join(self.basedir, 'test-file'), "w")
with open(os.path.join(self.basedir, 'test-file'), "w"):
pass
d = self.run_command()

def check(_):
Expand Down Expand Up @@ -228,7 +229,8 @@ def test_file(self):
self.make_command(fs.StatFile, dict(
file='test-file',
), True)
open(os.path.join(self.basedir, 'test-file'), "w")
with open(os.path.join(self.basedir, 'test-file'), "w"):
pass

d = self.run_command()

Expand All @@ -248,7 +250,8 @@ def test_file_workdir(self):
workdir='wd'
), True)
os.mkdir(os.path.join(self.basedir, 'wd'))
open(os.path.join(self.basedir, 'wd', 'test-file'), "w")
with open(os.path.join(self.basedir, 'wd', 'test-file'), "w"):
pass

d = self.run_command()

Expand Down Expand Up @@ -304,7 +307,8 @@ def test_file(self):
self.make_command(fs.GlobPath, dict(
path='t*-file',
), True)
open(os.path.join(self.basedir, 'test-file'), "w")
with open(os.path.join(self.basedir, 'test-file'), "w"):
pass

d = self.run_command()

Expand Down Expand Up @@ -344,8 +348,10 @@ def test_dir(self):
dir='workdir',
), True)
workdir = os.path.join(self.basedir, 'workdir')
open(os.path.join(workdir, 'file1'), "w")
open(os.path.join(workdir, 'file2'), "w")
with open(os.path.join(workdir, 'file1'), "w"):
pass
with open(os.path.join(workdir, 'file2'), "w"):
pass

d = self.run_command()

Expand Down
14 changes: 9 additions & 5 deletions worker/buildbot_worker/test/unit/test_commands_transfer.py
Expand Up @@ -122,7 +122,8 @@ def setUp(self):
self.datafile = os.path.join(self.datadir, 'data')
# note: use of 'wb' here ensures newlines aren't translated on the
# upload
open(self.datafile, mode="wb").write(b"this is some data\n" * 10)
with open(self.datafile, mode="wb") as f:
f.write(b"this is some data\n" * 10)

def tearDown(self):
self.tearDownCommand()
Expand Down Expand Up @@ -297,9 +298,10 @@ def setUp(self):
if os.path.exists(self.datadir):
shutil.rmtree(self.datadir)
os.makedirs(self.datadir)
open(os.path.join(self.datadir, "aa"), mode="wb").write(b"lots of a" * 100)
open(os.path.join(self.datadir, "bb"), mode="wb").write(
b"and a little b" * 17)
with open(os.path.join(self.datadir, "aa"), mode="wb") as f:
f.write(b"lots of a" * 100)
with open(os.path.join(self.datadir, "bb"), mode="wb") as f:
f.write(b"and a little b" * 17)

def tearDown(self):
self.tearDownCommand()
Expand Down Expand Up @@ -505,7 +507,9 @@ def check(_):
])
datafile = os.path.join(self.basedir, 'data')
self.assertTrue(os.path.exists(datafile))
self.assertEqual(open(datafile, mode="rb").read(), test_data[:50])
with open(datafile, mode="rb") as f:
data = f.read()
self.assertEqual(data, test_data[:50])
d.addCallback(check)
return d

Expand Down
12 changes: 8 additions & 4 deletions worker/buildbot_worker/test/unit/test_commands_utils.py
Expand Up @@ -102,11 +102,14 @@ def setUp(self):

# fill it with some files
os.mkdir(os.path.join(self.target))
open(os.path.join(self.target, "a"), "w")
with open(os.path.join(self.target, "a"), "w"):
pass
os.mkdir(os.path.join(self.target, "d"))
open(os.path.join(self.target, "d", "a"), "w")
with open(os.path.join(self.target, "d", "a"), "w"):
pass
os.mkdir(os.path.join(self.target, "d", "d"))
open(os.path.join(self.target, "d", "d", "a"), "w")
with open(os.path.join(self.target, "d", "d", "a"), "w"):
pass

def tearDown(self):
try:
Expand All @@ -127,7 +130,8 @@ def test_rmdirRecursive_symlink(self):
if runtime.platformType == 'win32':
raise unittest.SkipTest("no symlinks on this platform")
os.mkdir("noperms")
open("noperms/x", "w")
with open("noperms/x", "w"):
pass
os.chmod("noperms/x", 0)
try:
os.symlink("../noperms", os.path.join(self.target, "link"))
Expand Down
6 changes: 4 additions & 2 deletions worker/buildbot_worker/test/unit/test_runprocess.py
Expand Up @@ -512,7 +512,8 @@ def poll():
return
if os.path.exists(pidfile):
try:
pid = int(open(pidfile).read())
with open(pidfile) as f:
pid = int(f.read())
except (IOError, TypeError, ValueError):
pid = None

Expand Down Expand Up @@ -814,7 +815,8 @@ def test_statFile_missing(self):

def test_statFile_exists(self):
rp = self.makeRP()
open('statfile.log', 'w').write('hi')
with open('statfile.log', 'w') as f:
f.write('hi')
lf = runprocess.LogFileWatcher(rp, 'test', 'statfile.log', False)
st = lf.statFile()
self.assertEqual(
Expand Down

0 comments on commit 467bcd1

Please sign in to comment.