Invalidate scans when the host volume is unmounted.
[chromium-blink-merge.git] / ppapi / generators / idl_outfile.py
blob053cf3ea05b95e549a7ee80075904c3638d55546
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """ Output file objects for generator. """
8 import difflib
9 import os
10 import time
11 import subprocess
12 import sys
14 from idl_log import ErrOut, InfoOut, WarnOut
15 from idl_option import GetOption, Option, ParseOptions
16 from stat import *
18 Option('diff', 'Generate a DIFF when saving the file.')
22 # IDLOutFile
24 # IDLOutFile provides a temporary output file. By default, the object will
25 # not write the output if the file already exists, and matches what will be
26 # written. This prevents the timestamp from changing to optimize cases where
27 # the output files are used by a timestamp dependent build system
29 class IDLOutFile(object):
30 def __init__(self, filename, always_write = False, create_dir = True):
31 self.filename = filename
32 self.always_write = always_write
33 self.create_dir = create_dir
34 self.outlist = []
35 self.open = True
37 # Compare the old text to the current list of output lines.
38 def IsEquivalent_(self, oldtext):
39 if not oldtext: return False
41 oldlines = oldtext.split('\n')
42 curlines = (''.join(self.outlist)).split('\n')
44 # If number of lines don't match, it's a mismatch
45 if len(oldlines) != len(curlines):
46 return False
48 for index in range(len(oldlines)):
49 oldline = oldlines[index]
50 curline = curlines[index]
52 if oldline == curline: continue
54 curwords = curline.split()
55 oldwords = oldline.split()
57 # Unmatched lines must be the same length
58 if len(curwords) != len(oldwords):
59 return False
61 # If it's not a comment then it's a mismatch
62 if curwords[0] not in ['*', '/*', '//']:
63 return False
65 # Ignore changes to the Copyright year which is autogenerated
66 # /* Copyright (c) 2011 The Chromium Authors. All rights reserved.
67 if len(curwords) > 4 and curwords[1] == 'Copyright':
68 if curwords[4:] == oldwords[4:]: continue
70 # Ignore changes to auto generation timestamp when line unwrapped
71 # // From FILENAME.idl modified DAY MON DATE TIME YEAR.
72 # /* From FILENAME.idl modified DAY MON DATE TIME YEAR. */
73 if len(curwords) > 8 and curwords[1] == 'From':
74 if curwords[0:4] == oldwords[0:4]: continue
76 # Ignore changes to auto generation timestamp when line is wrapped
77 # * modified DAY MON DATE TIME YEAR.
78 if len(curwords) > 6 and curwords[1] == 'modified':
79 continue
81 return False
82 return True
84 # Return the file name
85 def Filename(self):
86 return self.filename
88 # Append to the output if the file is still open
89 def Write(self, string):
90 if not self.open:
91 raise RuntimeError('Could not write to closed file %s.' % self.filename)
92 self.outlist.append(string)
94 # Run clang-format on the buffered file contents.
95 def ClangFormat(self):
96 clang_format = subprocess.Popen(['clang-format', '-style=Chromium'],
97 stdin=subprocess.PIPE,
98 stdout=subprocess.PIPE)
99 new_output = clang_format.communicate("".join(self.outlist))[0]
100 self.outlist = [new_output]
102 # Close the file, flushing it to disk
103 def Close(self):
104 filename = os.path.realpath(self.filename)
105 self.open = False
106 outtext = ''.join(self.outlist)
107 oldtext = ''
109 if not self.always_write:
110 if os.path.isfile(filename):
111 oldtext = open(filename, 'rb').read()
112 if self.IsEquivalent_(oldtext):
113 if GetOption('verbose'):
114 InfoOut.Log('Output %s unchanged.' % self.filename)
115 return False
117 if GetOption('diff'):
118 for line in difflib.unified_diff(oldtext.split('\n'), outtext.split('\n'),
119 'OLD ' + self.filename,
120 'NEW ' + self.filename,
121 n=1, lineterm=''):
122 ErrOut.Log(line)
124 try:
125 # If the directory does not exit, try to create it, if we fail, we
126 # still get the exception when the file is openned.
127 basepath, leafname = os.path.split(filename)
128 if basepath and not os.path.isdir(basepath) and self.create_dir:
129 InfoOut.Log('Creating directory: %s\n' % basepath)
130 os.makedirs(basepath)
132 if not GetOption('test'):
133 outfile = open(filename, 'wb')
134 outfile.write(outtext)
135 outfile.close();
136 InfoOut.Log('Output %s written.' % self.filename)
137 return True
139 except IOError as (errno, strerror):
140 ErrOut.Log("I/O error(%d): %s" % (errno, strerror))
141 except:
142 ErrOut.Log("Unexpected error: %s" % sys.exc_info()[0])
143 raise
145 return False
148 def TestFile(name, stringlist, force, update):
149 errors = 0
151 # Get the old timestamp
152 if os.path.exists(name):
153 old_time = os.stat(filename)[ST_MTIME]
154 else:
155 old_time = 'NONE'
157 # Create the file and write to it
158 out = IDLOutFile(filename, force)
159 for item in stringlist:
160 out.Write(item)
162 # We wait for flush to force the timestamp to change
163 time.sleep(2)
165 wrote = out.Close()
166 cur_time = os.stat(filename)[ST_MTIME]
167 if update:
168 if not wrote:
169 ErrOut.Log('Failed to write output %s.' % filename)
170 return 1
171 if cur_time == old_time:
172 ErrOut.Log('Failed to update timestamp for %s.' % filename)
173 return 1
174 else:
175 if wrote:
176 ErrOut.Log('Should not have writen output %s.' % filename)
177 return 1
178 if cur_time != old_time:
179 ErrOut.Log('Should not have modified timestamp for %s.' % filename)
180 return 1
181 return 0
184 def main():
185 errors = 0
186 stringlist = ['Test', 'Testing\n', 'Test']
187 filename = 'outtest.txt'
189 # Test forcibly writing a file
190 errors += TestFile(filename, stringlist, force=True, update=True)
192 # Test conditionally writing the file skipping
193 errors += TestFile(filename, stringlist, force=False, update=False)
195 # Test conditionally writing the file updating
196 errors += TestFile(filename, stringlist + ['X'], force=False, update=True)
198 # Clean up file
199 os.remove(filename)
200 if not errors: InfoOut.Log('All tests pass.')
201 return errors
204 if __name__ == '__main__':
205 sys.exit(main())