From e0723a4c724ec0204ef5f1f801859b8e568c80b7 Mon Sep 17 00:00:00 2001 From: Talha Gulbudak Date: Mon, 8 Jan 2024 01:41:39 -0800 Subject: [PATCH 1/2] Replace the `>` and `:` characters in the file name --- diagnostic_analysis/diagnostic_analysis/exporter.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/diagnostic_analysis/diagnostic_analysis/exporter.py b/diagnostic_analysis/diagnostic_analysis/exporter.py index 4f764d635..c5ab1e76a 100644 --- a/diagnostic_analysis/diagnostic_analysis/exporter.py +++ b/diagnostic_analysis/diagnostic_analysis/exporter.py @@ -164,8 +164,9 @@ 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) output_file.close() From 30cc59fbf24a1a74f4628a92d7242cb956be68f5 Mon Sep 17 00:00:00 2001 From: Talha Gulbudak Date: Thu, 25 Jan 2024 07:55:53 -0800 Subject: [PATCH 2/2] Use file object to copy data to output file instead cat command --- diagnostic_analysis/diagnostic_analysis/exporter.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/diagnostic_analysis/diagnostic_analysis/exporter.py b/diagnostic_analysis/diagnostic_analysis/exporter.py index c5ab1e76a..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 @@ -169,12 +169,16 @@ def finish_logfile(self): 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