Skip to content

Commit

Permalink
Use a temporary file for FileUpload, and then rename once complete.
Browse files Browse the repository at this point in the history
Fixes: #161
  • Loading branch information
Chris AtLee committed Aug 18, 2010
1 parent 053d43a commit 3f3edec
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
7 changes: 6 additions & 1 deletion master/buildbot/steps/transfer.py
Expand Up @@ -27,7 +27,8 @@ def __init__(self, destfile, maxsize, mode):
os.makedirs(dirname)

self.destfile = destfile
self.fp = open(destfile, "wb")
fd, self.tmpname = tempfile.mkstemp(dir=dirname)
self.fp = os.fdopen(fd, 'wb')
if mode is not None:
os.chmod(destfile, mode)
self.remaining = maxsize
Expand All @@ -54,6 +55,8 @@ def remote_close(self):
"""
self.fp.close()
self.fp = None
os.rename(self.tmpname, self.destfile)
self.tmpname = None

def __del__(self):
# unclean shutdown, the file is probably truncated, so delete it
Expand All @@ -62,6 +65,8 @@ def __del__(self):
if fp:
fp.close()
os.unlink(self.destfile)
if self.tmpname and os.path.exists(self.tmpname):
os.unlink(self.tmpname)


def _extractall(self, path=".", members=None):
Expand Down
42 changes: 41 additions & 1 deletion master/buildbot/test/unit/test_steps_transfer.py
@@ -1,10 +1,50 @@
import tempfile, os
from twisted.trial import unittest

from mock import Mock

from buildbot.process.properties import Properties
from buildbot.util import json
from buildbot.steps.transfer import StringDownload, JSONStringDownload, JSONPropertiesDownload
from buildbot.steps.transfer import StringDownload, JSONStringDownload, JSONPropertiesDownload, \
FileUpload

class TestFileUpload(unittest.TestCase):
def setUp(self):
self.fd, self.destfile = tempfile.mkstemp()
os.unlink(self.destfile)

def tearDown(self):
os.close(self.fd)
os.unlink(self.destfile)

def testBasic(self):
s = FileUpload(slavesrc=__file__, masterdest=self.destfile)
s.build = Mock()
s.build.getProperties.return_value = Properties()
s.build.getSlaveCommandVersion.return_value = 1

s.step_status = Mock()
s.buildslave = Mock()
s.remote = Mock()

s.start()

for c in s.remote.method_calls:
name, command, args = c
commandName = command[3]
kwargs = command[-1]
if commandName == 'uploadFile':
self.assertEquals(kwargs['slavesrc'], __file__)
writer = kwargs['writer']
writer.remote_write(open(__file__, "rb").read())
self.assert_(not os.path.exists(self.destfile))
writer.remote_close()
break
else:
self.assert_(False, "No uploadFile command found")

self.assertEquals(open(self.destfile, "rb").read(),
open(__file__, "rb").read())

class TestStringDownload(unittest.TestCase):
def testBasic(self):
Expand Down

0 comments on commit 3f3edec

Please sign in to comment.