diff --git a/diagnostic_analysis/diagnostic_analysis/exporter.py b/diagnostic_analysis/diagnostic_analysis/exporter.py index 4f764d635..a0e07e41a 100644 --- a/diagnostic_analysis/diagnostic_analysis/exporter.py +++ b/diagnostic_analysis/diagnostic_analysis/exporter.py @@ -129,7 +129,7 @@ def _update(self, topic, msg): # Use named temp file, will cat this to header on close datafile, tmp_name = tempfile.mkstemp() - self._stats[name]['data_file'] = os.fdopen(datafile, 'w') + self._stats[name]['data_file'] = os.fdopen(datafile, 'w+') self._stats[name]['data_name'] = tmp_name @@ -164,16 +164,21 @@ def finish_logfile(self): header_line = ','.join(['Timestamp'] + ['Level', 'Message', 'Hardware ID'] + [f.replace(',','').replace('\n', ' ') for f in fields]) + '\n' - file_name = os.path.join(self.output_dir, name.replace(' ', '_').replace('(', '').replace(')', '').replace('/', '__').replace('.', '').replace('#', '') + '.csv') - + file_name = os.path.join(self.output_dir, + name.replace(' ', '_').replace('(', '').replace(')', '').replace('/', '__').replace('\\', '__').replace('.', '').replace('#', '').replace('>','-').replace(':','-') + '.csv') + output_file = open(file_name, 'w') output_file.write(header_line) + + # Move cursor to beginning of the data file to read all data + self._stats[name]['data_file'].seek(0) + # Append the tmp data file to the header + output_file.write(self._stats[name]['data_file'].read()) + output_file.close() self._stats[name]['data_file'].close() # Close data - # Append the tmp data file to the header - subprocess.call("cat %s >> %s" % (self._stats[name]['data_name'], file_name), shell=True) # Remove tmp file os.remove(self._stats[name]['data_name']) # Store filename