diff options
author | noelallen@google.com <noelallen@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-03 01:37:06 +0000 |
---|---|---|
committer | noelallen@google.com <noelallen@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-03 01:37:06 +0000 |
commit | be688adaaf4a4180debd6b52261e8a1f550fbcac (patch) | |
tree | 58e03d91aeaecd3be6babc267f48d586939c5e09 /ppapi/generators | |
parent | 3e42a9ff25493008a39ec8a018e2b6ae4a104b2d (diff) | |
download | chromium_src-be688adaaf4a4180debd6b52261e8a1f550fbcac.zip chromium_src-be688adaaf4a4180debd6b52261e8a1f550fbcac.tar.gz chromium_src-be688adaaf4a4180debd6b52261e8a1f550fbcac.tar.bz2 |
IDL cleanup, added IDLOutFile
Added the ability to create output objects for storing the output
until the object is closed. By default, closing the object will
not write or modify the timestamp if a file with identical data
already exists.
BUG=77551
TEST= python idl_outfile.py
Review URL: http://codereview.chromium.org/6901108
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@83837 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ppapi/generators')
-rw-r--r-- | ppapi/generators/idl_outfile.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/ppapi/generators/idl_outfile.py b/ppapi/generators/idl_outfile.py new file mode 100644 index 0000000..a9e4506 --- /dev/null +++ b/ppapi/generators/idl_outfile.py @@ -0,0 +1,115 @@ +#!/usr/bin/python +# +# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +""" Output file objects for generator. """ + +import os +import time +import sys + +from idl_log import ErrOut, InfoOut, WarnOut +from stat import * + +# +# IDLOutFile +# +# IDLOutFile provides a temporary output file. By default, the object will +# not write the output if the file already exists, and matches what will be +# written. This prevents the timestamp from changing to optimize cases where +# the output files are used by a timestamp dependent build system +# +class IDLOutFile(object): + def __init__(self, filename, always_write = False): + self.filename = filename + self.always_write = always_write + self.outlist = [] + self.open = True + + # Append to the output if the file is still open + def Write(self, string): + if not self.open: + raise RuntimeError('Could not write to closed file %s.' % self.filename) + self.outlist.append(string) + + # Close the file + def Close(self): + self.open = False + outtext = ''.join(self.outlist) + if not self.always_write: + intext = open(filename, 'r').read() + if intext == outtext: + InfoOut.Log('Output %s unchanged.' % self.filename) + return False + + try: + outfile = open(filename, 'w') + outfile.write(''.join(self.outlist)) + InfoOut.Log('Output %s written.' % self.filename) + return True + + except IOError as (errno, strerror): + ErrOut.Log("I/O error(%d): %s" % (errno, strerror)) + except: + ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0]) + raise + + return False + +def TestFile(name, stringlist, force, update): + errors = 0 + + # Get the old timestamp + if os.path.exists(name): + old_time = os.stat(filename)[ST_MTIME] + else: + old_time = 'NONE' + + # Create the file and write to it + out = IDLOutFile(filename, force) + for item in stringlist: + out.Write(item) + + # We wait for flush to force the timestamp to change + time.sleep(2) + + wrote = out.Close() + cur_time = os.stat(filename)[ST_MTIME] + if update: + if not wrote: + ErrOut.Log('Failed to write output %s.' % filename) + return 1 + if cur_time == old_time: + ErrOut.Log('Failed to update timestamp for %s.' % filename) + return 1 + else: + if wrote: + ErrOut.Log('Should not have writen output %s.' % filename) + return 1 + if cur_time != old_time: + ErrOut.Log('Should not have modified timestamp for %s.' % filename) + return 1 + return 0 + +if __name__ == '__main__': + + errors = 0 + stringlist = ['Test', 'Testing\n', 'Test'] + filename = 'outtest.txt' + + # Test forcibly writing a file + errors += TestFile(filename, stringlist, force=True, update=True) + + # Test conditionally writing the file skipping + errors += TestFile(filename, stringlist, force=False, update=False) + + # Test conditionally writing the file updating + errors += TestFile(filename, stringlist + ['X'], force=False, update=True) + + # Clean up file + os.remove(filename) + if not errors: InfoOut.Log('All tests pass.') + sys.exit(errors) + |