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

--HG--
extra : source : 47b56f2495030d77c446215d8822c31fc32f23b7
This commit is contained in:
Xidorn Quan 2015-08-25 10:07:43 +10:00
Родитель dbc240166a
Коммит dbaa85ce62
3 изменённых файлов: 30 добавлений и 16 удалений

Просмотреть файл

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

Просмотреть файл

@ -589,6 +589,9 @@ class Certificate:
def main(output, inputPath): def main(output, inputPath):
with open(inputPath) as configStream: with open(inputPath) as configStream:
output.write(Certificate(configStream).toPEM()) 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 # When run as a standalone program, this will read a specification from
# stdin and output the certificate as PEM to stdout. # stdin and output the certificate as PEM to stdout.

Просмотреть файл

@ -700,6 +700,9 @@ def keyFromSpecification(specification):
def main(output, inputPath): def main(output, inputPath):
with open(inputPath) as configStream: with open(inputPath) as configStream:
output.write(keyFromSpecification(configStream.read().strip()).toPEM()) 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 # When run as a standalone program, this will read a specification from
# stdin and output the certificate as PEM to stdout. # stdin and output the certificate as PEM to stdout.