def test_diff_outside_tree(self): tree = self.make_branch_and_tree('branch1') tree.commit('nothing') tree.commit('nothing') # A directory we can run commands from which we hope is not contained # in a bzr tree (though if there is one at or above $TEMPDIR, this is # false and may cause test failures). # Watch out for tricky test dir (on OSX /tmp -> /private/tmp) tmp_dir = osutils.realpath(osutils.mkdtemp()) self.addCleanup(osutils.rmtree, tmp_dir) # We expect a read-to-root attempt to occur. self.permit_url('file:///') expected_error = u'bzr: ERROR: Not a branch: "%s/branch2/".\n' % tmp_dir # -r X..Y out, err = self.run_bzr('diff -r revno:2:branch2..revno:1', retcode=3, working_dir=tmp_dir) self.assertEqual('', out) self.assertEqual(expected_error, err) # -r X out, err = self.run_bzr('diff -r revno:2:branch2', retcode=3, working_dir=tmp_dir) self.assertEqual('', out) self.assertEqual(expected_error, err) # -r X.. out, err = self.run_bzr('diff -r revno:2:branch2..', retcode=3, working_dir=tmp_dir) self.assertEqual('', out) self.assertEqual(expected_error, err) # no -r at all. out, err = self.run_bzr('diff', retcode=3, working_dir=tmp_dir) self.assertEqual('', out) self.assertEqual(u'bzr: ERROR: Not a branch: "%s/".\n' % tmp_dir, err)
def __init__(self, filename): super(_fcntl_WriteLock, self).__init__() # Check we can grab a lock before we actually open the file. self.filename = osutils.realpath(filename) if self.filename in _fcntl_WriteLock._open_locks: self._clear_f() raise errors.LockContention(self.filename) if self.filename in _fcntl_ReadLock._open_locks: if 'strict_locks' in debug.debug_flags: self._clear_f() raise errors.LockContention(self.filename) else: trace.mutter('Write lock taken w/ an open read lock on: %s' % (self.filename,)) self._open(self.filename, 'rb+') # reserve a slot for this lock - even if the lockf call fails, # at this point unlock() will be called, because self.f is set. # TODO: make this fully threadsafe, if we decide we care. _fcntl_WriteLock._open_locks.add(self.filename) try: # LOCK_NB will cause IOError to be raised if we can't grab a # lock right away. fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError, e: if e.errno in (errno.EAGAIN, errno.EACCES): # We couldn't grab the lock self.unlock() # we should be more precise about whats a locking # error and whats a random-other error raise errors.LockContention(self.filename, e)
def __init__(self, filename): super(_fcntl_WriteLock, self).__init__() # Check we can grab a lock before we actually open the file. self.filename = osutils.realpath(filename) if self.filename in _fcntl_WriteLock._open_locks: self._clear_f() raise errors.LockContention(self.filename) if self.filename in _fcntl_ReadLock._open_locks: if 'strict_locks' in debug.debug_flags: self._clear_f() raise errors.LockContention(self.filename) else: trace.mutter( 'Write lock taken w/ an open read lock on: %s' % (self.filename, )) self._open(self.filename, 'rb+') # reserve a slot for this lock - even if the lockf call fails, # at this point unlock() will be called, because self.f is set. # TODO: make this fully threadsafe, if we decide we care. _fcntl_WriteLock._open_locks.add(self.filename) try: # LOCK_NB will cause IOError to be raised if we can't grab a # lock right away. fcntl.lockf(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError, e: if e.errno in (errno.EAGAIN, errno.EACCES): # We couldn't grab the lock self.unlock() # we should be more precise about whats a locking # error and whats a random-other error raise errors.LockContention(self.filename, e)
def test_cwd_log(self): # Watch out for tricky test dir (on OSX /tmp -> /private/tmp) tmp_dir = osutils.realpath(osutils.mkdtemp()) # We expect a read-to-root attempt to occur. self.permit_url('file:///') self.addCleanup(osutils.rmtree, tmp_dir) out, err = self.run_bzr('log', retcode=3, working_dir=tmp_dir) self.assertEqual(u'bzr: ERROR: Not a branch: "%s/".\n' % (tmp_dir, ), err)
def test_relpath(self): """test for branch path lookups bzrlib.osutils._relpath do a simple but subtle job: given a path (either relative to cwd or absolute), work out if it is inside a branch and return the path relative to the base. """ import tempfile savedir = os.getcwdu() dtmp = tempfile.mkdtemp() # On Mac OSX, /tmp actually expands to /private/tmp dtmp = realpath(dtmp) def rp(p): return relpath(dtmp, p) try: # check paths inside dtmp while standing outside it self.assertEqual(rp(pathjoin(dtmp, 'foo')), 'foo') # root = nothing self.assertEqual(rp(dtmp), '') self.assertRaises(PathNotChild, rp, '/etc') # now some near-miss operations -- note that # os.path.commonprefix gets these wrong! self.assertRaises(PathNotChild, rp, dtmp.rstrip('\\/') + '2') self.assertRaises(PathNotChild, rp, dtmp.rstrip('\\/') + '2/foo') # now operations based on relpath of files in current # directory, or nearby os.chdir(dtmp) self.assertEqual(rp('foo/bar/quux'), 'foo/bar/quux') self.assertEqual(rp('foo'), 'foo') self.assertEqual(rp('./foo'), 'foo') self.assertEqual(rp(abspath('foo')), 'foo') self.assertRaises(PathNotChild, rp, '../foo') finally: os.chdir(savedir) osutils.rmtree(dtmp)
def test_cwd_log(self): # Watch out for tricky test dir (on OSX /tmp -> /private/tmp) tmp_dir = osutils.realpath(osutils.mkdtemp()) # We expect a read-to-root attempt to occur. self.permit_url('file:///') self.addCleanup(osutils.rmtree, tmp_dir) out, err = self.run_bzr('log', retcode=3, working_dir=tmp_dir) self.assertEqual(u'bzr: ERROR: Not a branch: "%s/".\n' % (tmp_dir,), err)
def _open(self, filename, access, share, cflags, pymode): self.filename = osutils.realpath(filename) try: self._handle = win32file_CreateFile( filename, access, share, None, win32file.OPEN_ALWAYS, win32file.FILE_ATTRIBUTE_NORMAL, None) except pywintypes.error, e: if e.args[0] == winerror.ERROR_ACCESS_DENIED: raise errors.LockFailed(filename, e) if e.args[0] == winerror.ERROR_SHARING_VIOLATION: raise errors.LockContention(filename, e) raise
def _open(self, filename, access, share, cflags, pymode): self.filename = osutils.realpath(filename) try: self._handle = win32file_CreateFile(filename, access, share, None, win32file.OPEN_ALWAYS, win32file.FILE_ATTRIBUTE_NORMAL, None) except pywintypes.error, e: if e.args[0] == winerror.ERROR_ACCESS_DENIED: raise errors.LockFailed(filename, e) if e.args[0] == winerror.ERROR_SHARING_VIOLATION: raise errors.LockContention(filename, e) raise
def _open(self, filename, access, share, cflags, pymode): self.filename = osutils.realpath(filename) handle = _CreateFile(filename, access, share, None, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0) if handle in (INVALID_HANDLE_VALUE, 0): e = ctypes.WinError() if e.args[0] == ERROR_ACCESS_DENIED: raise errors.LockFailed(filename, e) if e.args[0] == ERROR_SHARING_VIOLATION: raise errors.LockContention(filename, e) raise e fd = msvcrt.open_osfhandle(handle, cflags) self.f = os.fdopen(fd, pymode) return self.f
def __init__(self, filename): super(_fcntl_ReadLock, self).__init__() self.filename = osutils.realpath(filename) _fcntl_ReadLock._open_locks.setdefault(self.filename, 0) _fcntl_ReadLock._open_locks[self.filename] += 1 self._open(filename, 'rb') try: # LOCK_NB will cause IOError to be raised if we can't grab a # lock right away. fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB) except IOError, e: # we should be more precise about whats a locking # error and whats a random-other error raise errors.LockContention(e)
def _open(self, filename, filemode): self.filename = osutils.realpath(filename) try: self.f = open(self.filename, filemode) return self.f except IOError, e: if e.errno in (errno.EACCES, errno.EPERM): raise errors.LockFailed(self.filename, str(e)) if e.errno != errno.ENOENT: raise # maybe this is an old branch (before may 2005) trace.mutter("trying to create missing lock %r", self.filename) self.f = open(self.filename, 'wb+') return self.f
def __init__(self, filename): super(_fcntl_ReadLock, self).__init__() self.filename = osutils.realpath(filename) if self.filename in _fcntl_WriteLock._open_locks: if 'strict_locks' in debug.debug_flags: # We raise before calling _open so we don't need to # _clear_f raise errors.LockContention(self.filename) else: trace.mutter('Read lock taken w/ an open write lock on: %s' % (self.filename,)) _fcntl_ReadLock._open_locks.setdefault(self.filename, 0) _fcntl_ReadLock._open_locks[self.filename] += 1 self._open(filename, 'rb') try: # LOCK_NB will cause IOError to be raised if we can't grab a # lock right away. fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB) except IOError, e: # we should be more precise about whats a locking # error and whats a random-other error raise errors.LockContention(self.filename, e)
def __init__(self, filename): super(_fcntl_ReadLock, self).__init__() self.filename = osutils.realpath(filename) if self.filename in _fcntl_WriteLock._open_locks: if 'strict_locks' in debug.debug_flags: # We raise before calling _open so we don't need to # _clear_f raise errors.LockContention(self.filename) else: trace.mutter( 'Read lock taken w/ an open write lock on: %s' % (self.filename, )) _fcntl_ReadLock._open_locks.setdefault(self.filename, 0) _fcntl_ReadLock._open_locks[self.filename] += 1 self._open(filename, 'rb') try: # LOCK_NB will cause IOError to be raised if we can't grab a # lock right away. fcntl.lockf(self.f, fcntl.LOCK_SH | fcntl.LOCK_NB) except IOError, e: # we should be more precise about whats a locking # error and whats a random-other error raise errors.LockContention(self.filename, e)
# Copyright (C) 2007-2008 Canonical Ltd # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA """ssl_certs -- provides access to ssl keys and certificates needed by tests""" from bzrlib import ( osutils, ) # Directory containing all ssl files, keys or certificates base_dir = osutils.dirname(osutils.realpath(__file__)) def build_path(name): """Build and return a path in ssl_certs directory for name""" return osutils.pathjoin(base_dir, name)