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.2 kB

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2019 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. """Lineage utils."""
  16. import os
  17. import re
  18. from pathlib import Path
  19. from mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
  20. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageParamValueError, LineageParamTypeError, \
  21. LineageDirNotExistError, LineageParamSummaryPathError
  22. from mindinsight.lineagemgr.common.log import logger as log
  23. from mindinsight.lineagemgr.common.validator.validate import validate_path
  24. def enum_to_list(enum):
  25. return [enum_ele.value for enum_ele in enum]
  26. def normalize_summary_dir(summary_dir):
  27. """Normalize summary dir."""
  28. try:
  29. summary_dir = validate_path(summary_dir)
  30. except (LineageParamValueError, LineageDirNotExistError) as error:
  31. log.error(str(error))
  32. log.exception(error)
  33. raise LineageParamSummaryPathError(str(error.message))
  34. return summary_dir
  35. def get_timestamp(filename):
  36. """Get timestamp from filename."""
  37. timestamp = int(re.search(SummaryWatcher().SUMMARY_FILENAME_REGEX, filename)[1])
  38. return timestamp
  39. def make_directory(path):
  40. """Make directory."""
  41. if path is None or not isinstance(path, str) or not path.strip():
  42. log.error("Invalid input path: %r.", path)
  43. raise LineageParamTypeError("Invalid path type")
  44. # convert relative path to abs path
  45. path = os.path.realpath(path)
  46. log.debug("The abs path is %r", path)
  47. # check path exist and its write permissions]
  48. if os.path.exists(path):
  49. real_path = path
  50. else:
  51. # All exceptions need to be caught because create directory maybe have some limit(permissions)
  52. log.debug("The directory(%s) doesn't exist, will create it", path)
  53. try:
  54. os.makedirs(path, exist_ok=True)
  55. real_path = path
  56. except PermissionError as err:
  57. log.error("No write permission on the directory(%r), error = %r", path, err)
  58. raise LineageParamTypeError("No write permission on the directory.")
  59. return real_path
  60. def get_relative_path(path, base_path):
  61. """
  62. Get relative path based on base_path.
  63. Args:
  64. path (str): absolute path.
  65. base_path: absolute base path.
  66. Returns:
  67. str, relative path based on base_path.
  68. """
  69. try:
  70. r_path = str(Path(path).relative_to(Path(base_path)))
  71. except ValueError:
  72. raise LineageParamValueError("The path %r does not start with %r." % (path, base_path))
  73. if r_path == ".":
  74. r_path = ""
  75. return os.path.join("./", r_path)