def test_closed(self): local_warnings = [] def capture_warnings(msg, cls, stacklevel=None): self.assertEqual(cls, DeprecationWarning) local_warnings.append(msg) method = symbol_versioning.warn try: symbol_versioning.set_warning_method(capture_warnings) f = atomicfile.AtomicFile('test', mode='wb') self.assertEqual(False, f.closed) f.abort() self.assertEqual(True, f.closed) f = atomicfile.AtomicFile('test', mode='wb') f.close() self.assertEqual(True, f.closed) f = atomicfile.AtomicFile('test', mode='wb') f.commit() self.assertEqual(True, f.closed) finally: symbol_versioning.set_warning_method(method) txt = 'AtomicFile.closed deprecated in bzr 0.10' self.assertEqual([txt] * 4, local_warnings)
def tree_ignores_add_patterns(tree, name_pattern_list): """Retrieve a list of ignores from the ignore file in a tree. :param tree: Tree to retrieve the ignore list from. :return: """ ifn = tree.abspath(bzrlib.IGNORE_FILENAME) if tree.has_filename(ifn): f = open(ifn, 'rt') try: igns = f.read().decode('utf-8') finally: f.close() else: igns = "" # TODO: If the file already uses crlf-style termination, maybe # we should use that for the newly added lines? if igns and igns[-1] != '\n': igns += '\n' for name_pattern in name_pattern_list: igns += name_pattern + '\n' f = atomicfile.AtomicFile(ifn, 'wb') try: f.write(igns.encode('utf-8')) f.commit() finally: f.close() if not tree.path2id('.bzrignore'): tree.add(['.bzrignore'])
def _test_mode(self, mode): if not self.can_sys_preserve_mode(): raise TestSkipped("This test cannot be run on your platform") f = atomicfile.AtomicFile('test', mode='wb', new_mode=mode) f.write('foo\n') f.commit() st = os.lstat('test') self.assertEqualMode(mode, stat.S_IMODE(st.st_mode))
def test_no_mode(self): # The default file permissions should be based on umask umask = osutils.get_umask() f = atomicfile.AtomicFile('test', mode='wb') f.write('foo\n') f.commit() st = os.lstat('test') self.assertEqualMode(0666 & ~umask, stat.S_IMODE(st.st_mode))
def test_text_mode(self): f = atomicfile.AtomicFile('test', mode='wt') f.write('foo\n') f.commit() contents = open('test', 'rb').read() if sys.platform == 'win32': self.assertEqual('foo\r\n', contents) else: self.assertEqual('foo\n', contents)
def test_abort(self): f = atomicfile.AtomicFile('test') f.write('foo\n') f.abort() self.assertEqual([], os.listdir('.')) self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort) self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit) # close is re-entrant safe f.close()
def test_commit(self): f = atomicfile.AtomicFile('test') self.failIfExists('test') f.write('foo\n') f.commit() self.assertEqual(['test'], os.listdir('.')) self.check_file_contents('test', 'foo\n') self.assertRaises(errors.AtomicFileAlreadyClosed, f.commit) self.assertRaises(errors.AtomicFileAlreadyClosed, f.abort) # close is re-entrant safe f.close()
def write(self): """Write contents of cache to file.""" outf = atomicfile.AtomicFile(self.cache_file_name(), 'wb', new_mode=self._mode) try: outf.write(CACHE_HEADER) for path, c in self._cache.iteritems(): line_info = [path.encode('utf-8'), '// ', c[0], ' '] line_info.append(' '.join([str(fld) for fld in c[1]])) line_info.append('\n') outf.write(''.join(line_info)) outf.commit() self.needs_write = False ## mutter("write hash cache: %s hits=%d misses=%d stat=%d recent=%d updates=%d", ## self.cache_file_name(), self.hit_count, self.miss_count, ## self.stat_count, ## self.danger_count, self.update_count) finally: outf.close()