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. """
13 from idl_log
import ErrOut
, InfoOut
, WarnOut
14 from idl_option
import GetOption
, Option
, ParseOptions
17 Option('diff', 'Generate a DIFF when saving the file.')
23 # IDLOutFile provides a temporary output file. By default, the object will
24 # not write the output if the file already exists, and matches what will be
25 # written. This prevents the timestamp from changing to optimize cases where
26 # the output files are used by a timestamp dependent build system
28 class IDLOutFile(object):
29 def __init__(self
, filename
, always_write
= False, create_dir
= True):
30 self
.filename
= filename
31 self
.always_write
= always_write
32 self
.create_dir
= create_dir
36 # Compare the old text to the current list of output lines.
37 def IsEquivalent_(self
, oldtext
):
38 if not oldtext
: return False
40 oldlines
= oldtext
.split('\n')
41 curlines
= (''.join(self
.outlist
)).split('\n')
43 # If number of lines don't match, it's a mismatch
44 if len(oldlines
) != len(curlines
):
47 for index
in range(len(oldlines
)):
48 oldline
= oldlines
[index
]
49 curline
= curlines
[index
]
51 if oldline
== curline
: continue
53 curwords
= curline
.split()
54 oldwords
= oldline
.split()
56 # Unmatched lines must be the same length
57 if len(curwords
) != len(oldwords
):
60 # If it's not a comment then it's a mismatch
61 if curwords
[0] not in ['*', '/*', '//']:
64 # Ignore changes to the Copyright year which is autogenerated
65 # /* Copyright (c) 2011 The Chromium Authors. All rights reserved.
66 if len(curwords
) > 4 and curwords
[1] == 'Copyright':
67 if curwords
[4:] == oldwords
[4:]: continue
69 # Ignore changes to auto generation timestamp when line unwrapped
70 # // From FILENAME.idl modified DAY MON DATE TIME YEAR.
71 # /* From FILENAME.idl modified DAY MON DATE TIME YEAR. */
72 if len(curwords
) > 8 and curwords
[1] == 'From':
73 if curwords
[0:4] == oldwords
[0:4]: continue
75 # Ignore changes to auto generation timestamp when line is wrapped
76 # * modified DAY MON DATE TIME YEAR.
77 if len(curwords
) > 6 and curwords
[1] == 'modified':
83 # Return the file name
87 # Append to the output if the file is still open
88 def Write(self
, string
):
90 raise RuntimeError('Could not write to closed file %s.' % self
.filename
)
91 self
.outlist
.append(string
)
93 # Close the file, flushing it to disk
95 filename
= os
.path
.realpath(self
.filename
)
97 outtext
= ''.join(self
.outlist
)
100 if not self
.always_write
:
101 if os
.path
.isfile(filename
):
102 oldtext
= open(filename
, 'rb').read()
103 if self
.IsEquivalent_(oldtext
):
104 if GetOption('verbose'):
105 InfoOut
.Log('Output %s unchanged.' % self
.filename
)
108 if GetOption('diff'):
109 for line
in difflib
.unified_diff(oldtext
.split('\n'), outtext
.split('\n'),
110 'OLD ' + self
.filename
,
111 'NEW ' + self
.filename
,
116 # If the directory does not exit, try to create it, if we fail, we
117 # still get the exception when the file is openned.
118 basepath
, leafname
= os
.path
.split(filename
)
119 if basepath
and not os
.path
.isdir(basepath
) and self
.create_dir
:
120 InfoOut
.Log('Creating directory: %s\n' % basepath
)
121 os
.makedirs(basepath
)
123 if not GetOption('test'):
124 outfile
= open(filename
, 'wb')
125 outfile
.write(outtext
)
126 InfoOut
.Log('Output %s written.' % self
.filename
)
129 except IOError as (errno
, strerror
):
130 ErrOut
.Log("I/O error(%d): %s" % (errno
, strerror
))
132 ErrOut
.Log("Unexpected error: %s" % sys
.exc_info()[0])
138 def TestFile(name
, stringlist
, force
, update
):
141 # Get the old timestamp
142 if os
.path
.exists(name
):
143 old_time
= os
.stat(filename
)[ST_MTIME
]
147 # Create the file and write to it
148 out
= IDLOutFile(filename
, force
)
149 for item
in stringlist
:
152 # We wait for flush to force the timestamp to change
156 cur_time
= os
.stat(filename
)[ST_MTIME
]
159 ErrOut
.Log('Failed to write output %s.' % filename
)
161 if cur_time
== old_time
:
162 ErrOut
.Log('Failed to update timestamp for %s.' % filename
)
166 ErrOut
.Log('Should not have writen output %s.' % filename
)
168 if cur_time
!= old_time
:
169 ErrOut
.Log('Should not have modified timestamp for %s.' % filename
)
176 stringlist
= ['Test', 'Testing\n', 'Test']
177 filename
= 'outtest.txt'
179 # Test forcibly writing a file
180 errors
+= TestFile(filename
, stringlist
, force
=True, update
=True)
182 # Test conditionally writing the file skipping
183 errors
+= TestFile(filename
, stringlist
, force
=False, update
=False)
185 # Test conditionally writing the file updating
186 errors
+= TestFile(filename
, stringlist
+ ['X'], force
=False, update
=True)
190 if not errors
: InfoOut
.Log('All tests pass.')
194 if __name__
== '__main__':