You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_utils.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Profiler utils."""
  16. import os
  17. import re
  18. def fwrite_format(output_data_path, data_source=None, is_print=False, is_start=False):
  19. """
  20. Write data to the output file.
  21. Args:
  22. output_data_path (str): The output file path of the data.
  23. data_source (list): The data to write.
  24. is_print (bool): whether to print the data to stdout.
  25. is_start (bool): Whether is the first line of the output file, will remove the old file if True."
  26. """
  27. if is_start is True and os.path.exists(output_data_path):
  28. os.remove(output_data_path)
  29. if data_source.startswith("title:"):
  30. title_label = '=' * 20
  31. data_source = title_label + data_source[6:] + title_label
  32. with open(output_data_path, 'a+') as f:
  33. f.write(data_source)
  34. f.write("\n")
  35. if is_print:
  36. print(data_source)
  37. def get_log_slice_id(file_name):
  38. pattern = re.compile(r'(?<=slice_)\d+')
  39. slice_list = pattern.findall(file_name)
  40. index = re.findall(r'\d+', slice_list[0])
  41. return int(index[0])
  42. def get_file_join_name(input_path, file_name):
  43. """
  44. Search files under the special path, and will join all the files to one file.
  45. Args:
  46. input_path (str): The source path, will search files under it.
  47. file_name (str): The target of the filename, such as 'hwts.log.data.45.dev'.
  48. Returns:
  49. str, the join file name.
  50. """
  51. name_list = []
  52. file_join_name = ''
  53. input_path = os.path.realpath(input_path)
  54. if os.path.exists(input_path):
  55. files = os.listdir(input_path)
  56. for f in files:
  57. if file_name in f and not f.endswith('.done') and not f.endswith('.join') \
  58. and not f.endswith('.zip'):
  59. name_list.append(f)
  60. # resort name_list
  61. name_list.sort(key=get_log_slice_id)
  62. if len(name_list) == 1:
  63. file_join_name = os.path.join(input_path, name_list[0])
  64. elif len(name_list) > 1:
  65. file_join_name = os.path.join(input_path, '%s.join' % file_name)
  66. if os.path.exists(file_join_name):
  67. os.remove(file_join_name)
  68. with open(file_join_name, 'ab') as bin_data:
  69. for i in name_list:
  70. file = input_path + os.sep + i
  71. with open(file, 'rb') as txt:
  72. bin_data.write(txt.read())
  73. return file_join_name
  74. def get_file_names(input_path, file_name):
  75. """
  76. Search files under the special path.
  77. Args:
  78. input_path (str): The source path, will search files under it.
  79. file_name (str): The target of the filename, such as 'host_start_log'.
  80. Returns:
  81. list, file name list.
  82. """
  83. input_path = os.path.realpath(input_path)
  84. name_list = []
  85. if os.path.exists(input_path):
  86. files = os.listdir(input_path)
  87. for f in files:
  88. if file_name in f and not f.endswith('.done') \
  89. and not f.endswith('.zip'):
  90. name_list.append(f)
  91. break
  92. return name_list