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.

_event_writer.py 2.8 kB

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. """Event writer to record lineage message to summary log."""
  16. import os
  17. import stat
  18. import struct
  19. from tests.utils import crc32
  20. class EventWriter:
  21. """
  22. Lineage summary record.
  23. Recording train lineage and evaluation lineage to summary log.
  24. Args:
  25. file_path (str): Summary log path.
  26. override (bool): If override the summary log exist.
  27. Raises:
  28. IOError: Write to summary log failed or file_path is a dir.
  29. Examples:
  30. >>> content = b'\x01\x02\x03\x04'
  31. >>> event_writer = EventWriter("./test.log", True)
  32. >>> event_writer.write_event_to_file(content)
  33. """
  34. def __init__(self, file_path, override=False):
  35. """
  36. Init EventWriter, get the type of writing.
  37. Args:
  38. file_path (str): The file path to writing.
  39. override (bool): The type of writing.
  40. """
  41. if os.path.exists(file_path):
  42. if not os.path.isfile(file_path):
  43. raise IOError("The file_path is not a normal file.")
  44. self.file_path = file_path
  45. if override:
  46. self.write_type = 'wb'
  47. else:
  48. self.write_type = 'ab'
  49. def write_event_to_file(self, content):
  50. """
  51. Write event to file.
  52. Args:
  53. content (bytes): Content to write.
  54. """
  55. length = struct.pack("<Q", len(content))
  56. header_crc = EventWriter.get_crc(length)
  57. crc = EventWriter.get_crc(content)
  58. content = length + header_crc + content + crc
  59. try:
  60. with open(self.file_path, self.write_type) as log_file:
  61. os.chmod(self.file_path, stat.S_IRUSR | stat.S_IWUSR)
  62. log_file.write(content)
  63. except IOError:
  64. raise IOError("There are some error when writing summary log.")
  65. @staticmethod
  66. def get_crc(content):
  67. """
  68. Calculate crc value of the content.
  69. Args:
  70. content (bytes): Content to be Calculated.
  71. Returns:
  72. bytes, crc of content, 4 bytes.
  73. """
  74. crc_value = crc32.get_mask_from_string(content)
  75. return struct.pack("<L", crc_value)