本文整理汇总了Python中mozbuild.util.FileAvoidWrite.write方法的典型用法代码示例。如果您正苦于以下问题:Python FileAvoidWrite.write方法的具体用法?Python FileAvoidWrite.write怎么用?Python FileAvoidWrite.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mozbuild.util.FileAvoidWrite
的用法示例。
在下文中一共展示了FileAvoidWrite.write方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_diff_not_default
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_diff_not_default(self):
"""Diffs are not produced by default."""
faw = FileAvoidWrite('doesnotexist')
faw.write('dummy')
faw.close()
self.assertIsNone(faw.diff)
示例2: test_file_avoid_write
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_file_avoid_write(self):
with MockedOpen({'file': 'content'}):
# Overwriting an existing file replaces its content
with FileAvoidWrite('file') as file:
file.write('bazqux')
self.assertEqual(open('file', 'r').read(), 'bazqux')
# Creating a new file (obviously) stores its content
with FileAvoidWrite('file2') as file:
file.write('content')
self.assertEqual(open('file2').read(), 'content')
class MyMockedOpen(MockedOpen):
'''MockedOpen extension to raise an exception if something
attempts to write in an opened file.
'''
def __call__(self, name, mode):
if 'w' in mode:
raise Exception, 'Unexpected open with write mode'
return MockedOpen.__call__(self, name, mode)
with MyMockedOpen({'file': 'content'}):
# Validate that MyMockedOpen works as intended
file = FileAvoidWrite('file')
file.write('foobar')
self.assertRaises(Exception, file.close)
# Check that no write actually happens when writing the
# same content as what already is in the file
with FileAvoidWrite('file') as file:
file.write('content')
示例3: test_diff_not_default
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_diff_not_default(self):
"""Diffs are not produced by default."""
with MockedOpen({'file': 'old'}):
faw = FileAvoidWrite('file')
faw.write('dummy')
faw.close()
self.assertIsNone(faw.diff)
示例4: test_diff_update
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_diff_update(self):
"""Diffs are produced on file update."""
with MockedOpen({'file': 'old'}):
faw = FileAvoidWrite('file', capture_diff=True)
faw.write('new')
faw.close()
diff = '\n'.join(faw.diff)
self.assertIn('-old', diff)
self.assertIn('+new', diff)
示例5: _maybe_write_file
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def _maybe_write_file(self, path, content, result):
fh = FileAvoidWrite(path)
fh.write(content)
existed, updated = fh.close()
if not existed:
result[0].add(path)
elif updated:
result[1].add(path)
else:
result[2].add(path)
示例6: test_diff_create
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_diff_create(self):
"""Diffs are produced when files are created."""
tmpdir = tempfile.mkdtemp()
try:
path = os.path.join(tmpdir, 'file')
faw = FileAvoidWrite(path, capture_diff=True)
faw.write('new')
faw.close()
diff = '\n'.join(faw.diff)
self.assertIn('+new', diff)
finally:
shutil.rmtree(tmpdir)
示例7: install_from_file
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def install_from_file(self, filename, distdir):
self.log(logging.INFO, 'artifact',
{'filename': filename},
'Installing from {filename}')
# Copy all .so files to dist/bin, avoiding modification where possible.
ensureParentDir(os.path.join(distdir, 'bin', '.dummy'))
with zipfile.ZipFile(filename) as zf:
for info in zf.infolist():
if not info.filename.endswith('.so'):
continue
n = os.path.join(distdir, 'bin', os.path.basename(info.filename))
fh = FileAvoidWrite(n, mode='r')
shutil.copyfileobj(zf.open(info), fh)
fh.write(zf.open(info).read())
file_existed, file_updated = fh.close()
self.log(logging.INFO, 'artifact',
{'updating': 'Updating' if file_updated else 'Not updating', 'filename': n},
'{updating} {filename}')
return 0
示例8: test_file_avoid_write
# 需要导入模块: from mozbuild.util import FileAvoidWrite [as 别名]
# 或者: from mozbuild.util.FileAvoidWrite import write [as 别名]
def test_file_avoid_write(self):
with MockedOpen({"file": "content"}):
# Overwriting an existing file replaces its content
faw = FileAvoidWrite("file")
faw.write("bazqux")
self.assertEqual(faw.close(), (True, True))
self.assertEqual(open("file", "r").read(), "bazqux")
# Creating a new file (obviously) stores its content
faw = FileAvoidWrite("file2")
faw.write("content")
self.assertEqual(faw.close(), (False, True))
self.assertEqual(open("file2").read(), "content")
with MockedOpen({"file": "content"}):
with FileAvoidWrite("file") as file:
file.write("foobar")
self.assertEqual(open("file", "r").read(), "foobar")
class MyMockedOpen(MockedOpen):
"""MockedOpen extension to raise an exception if something
attempts to write in an opened file.
"""
def __call__(self, name, mode):
if "w" in mode:
raise Exception, "Unexpected open with write mode"
return MockedOpen.__call__(self, name, mode)
with MyMockedOpen({"file": "content"}):
# Validate that MyMockedOpen works as intended
file = FileAvoidWrite("file")
file.write("foobar")
self.assertRaises(Exception, file.close)
# Check that no write actually happens when writing the
# same content as what already is in the file
faw = FileAvoidWrite("file")
faw.write("content")
self.assertEqual(faw.close(), (True, False))