72def _WriteFile(path, lines):
73 (dir, file) = os.path.split(path)
74
75
76 if dir:
77 if not os.path.isdir(dir):
78 _logger.info('Mkdir - %s' % dir)
79 os.makedirs(dir)
80
81
82 new_contents = ''.join(lines)
83 if os.path.exists(path):
84 with open(path) as fd:
85 contents = fd.read()
86 if new_contents == contents:
87 _logger.info('Unchanged file %s' % path)
88 return
89
90
91 num_attempts = 4
92 for i in range(num_attempts):
93 try:
94 _logger.info('Writing (attempt %d) - %s' % (i + 1, path))
95 with open(path, 'w') as fd:
96 fd.write(new_contents)
97 return
98 except IOError as error:
99 last_attempt = (i == (num_attempts - 1))
100 if not last_attempt:
101
102 time.sleep(0.05)
103 else:
104 _logger.info('Got exception (%s) ' % error)
105 raise error