Bug 1188468 - Allow script to force updating a generated file even if the file is actually not changed. r=gps

This commit is contained in:
Xidorn Quan 2015-08-25 10:07:43 +10:00
parent f3fbd95cf0
commit da613e7a51
3 changed files with 30 additions and 16 deletions

View File

@ -109,6 +109,25 @@ def ensureParentDir(path):
raise
def readFileContent(name, mode):
"""Read the content of file, returns tuple (file existed, file content)"""
existed = False
old_content = None
try:
existing = open(name, mode)
existed = True
except IOError:
pass
else:
try:
old_content = existing.read()
except IOError:
pass
finally:
existing.close()
return existed, old_content
class FileAvoidWrite(BytesIO):
"""File-like object that buffers output and only writes if content changed.
@ -127,6 +146,7 @@ class FileAvoidWrite(BytesIO):
self._capture_diff = capture_diff
self.diff = None
self.mode = mode
self.force_update = False
def write(self, buf):
if isinstance(buf, unicode):
@ -146,23 +166,11 @@ class FileAvoidWrite(BytesIO):
"""
buf = self.getvalue()
BytesIO.close(self)
existed = False
old_content = None
try:
existing = open(self.name, self.mode)
existed = True
except IOError:
pass
else:
try:
old_content = existing.read()
if old_content == buf:
return True, False
except IOError:
pass
finally:
existing.close()
existed, old_content = readFileContent(self.name, self.mode)
if not self.force_update and old_content == buf:
assert existed
return existed, False
ensureParentDir(self.name)
with open(self.name, 'w') as file:

View File

@ -589,6 +589,9 @@ class Certificate:
def main(output, inputPath):
with open(inputPath) as configStream:
output.write(Certificate(configStream).toPEM())
# Force updating the output file even if the content does not change
# so that we won't be called again simply because of the mtime.
output.force_update = True
# When run as a standalone program, this will read a specification from
# stdin and output the certificate as PEM to stdout.

View File

@ -700,6 +700,9 @@ def keyFromSpecification(specification):
def main(output, inputPath):
with open(inputPath) as configStream:
output.write(keyFromSpecification(configStream.read().strip()).toPEM())
# Force updating the output file even if the content does not change
# so that we won't be called again simply because of the mtime.
output.force_update = True
# When run as a standalone program, this will read a specification from
# stdin and output the certificate as PEM to stdout.