4444 import argparse
4545 import traceback
4646 from hw_management_lib import HW_Mgmt_Logger as Logger
47+ from hw_management_lib import atomic_file_write
4748 from collections import Counter
4849
4950 from hw_management_redfish_client import RedfishClient , BMCAccessor
@@ -680,9 +681,8 @@ def asic_temp_populate(arg_list, arg):
680681 # Write the temperature data to files
681682 for suffix , value in file_paths .items ():
682683 f_name = "/var/run/hw-management/thermal/{}{}" .format (asic_name , suffix )
683- with open (f_name , 'w' , encoding = "utf-8" ) as f :
684- f .write ("{}\n " .format (value ))
685- LOGGER .debug (f"Write { asic_name } { suffix } : { value } " )
684+ atomic_file_write (f_name , str (value ) + "\n " )
685+ LOGGER .debug (f"Write { asic_name } { suffix } : { value } " )
686686
687687 asic_chipup_completed_fname = os .path .join ("/var/run/hw-management/config" , "asic_chipup_completed" )
688688 asic_num_fname = os .path .join ("/var/run/hw-management/config" , "asic_num" )
@@ -704,11 +704,8 @@ def asic_temp_populate(arg_list, arg):
704704 else :
705705 asics_init_done = 0
706706
707- with open (asics_init_done_fname , 'w+' , encoding = "utf-8" ) as f :
708- f .write (str (asics_init_done ) + "\n " )
709-
710- with open (asic_chipup_completed_fname , 'w' , encoding = "utf-8" ) as f :
711- f .write (str (asic_chipup_completed ) + "\n " )
707+ atomic_file_write (asics_init_done_fname , str (asics_init_done ) + "\n " )
708+ atomic_file_write (asic_chipup_completed_fname , str (asic_chipup_completed ) + "\n " )
712709
713710# ----------------------------------------------------------------------
714711
@@ -801,15 +798,13 @@ def module_temp_populate(arg_list, _dummy):
801798 for suffix , value in file_paths .items ():
802799 f_name = "/var/run/hw-management/thermal/{}{}" .format (module_name , suffix )
803800 if value is not None :
804- with open (f_name , 'w' , encoding = "utf-8" ) as f :
805- f .write ("{}\n " .format (value ))
806- LOGGER .debug (f"Write { module_name } { suffix } : { value } " )
801+ atomic_file_write (f_name , str (value ) + "\n " )
802+ LOGGER .debug (f"Write { module_name } { suffix } : { value } " )
807803 module_updated = True
808804
809805 if module_updated :
810806 LOGGER .debug ("{} module_counter ({}) updated" .format (module_name , module_count ))
811- with open ("/var/run/hw-management/config/module_counter" , 'w+' , encoding = "utf-8" ) as f :
812- f .write ("{}\n " .format (module_count ))
807+ atomic_file_write ("/var/run/hw-management/config/module_counter" , str (module_count ) + "\n " )
813808 return
814809
815810# ----------------------------------------------------------------------
0 commit comments