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 2.1 kB

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 mindinsight.datavisual.data_transform.summary_watcher import SummaryWatcher
  19. from mindinsight.lineagemgr.common.exceptions.exceptions import LineageParamTypeError
  20. from mindinsight.lineagemgr.common.log import logger as log
  21. def enum_to_list(enum):
  22. return [enum_ele.value for enum_ele in enum]
  23. def get_timestamp(filename):
  24. """Get timestamp from filename."""
  25. timestamp = int(re.search(SummaryWatcher().SUMMARY_FILENAME_REGEX, filename)[1])
  26. return timestamp
  27. def make_directory(path):
  28. """Make directory."""
  29. if path is None or not isinstance(path, str) or not path.strip():
  30. log.error("Invalid input path: %r.", path)
  31. raise LineageParamTypeError("Invalid path type")
  32. # convert relative path to abs path
  33. path = os.path.realpath(path)
  34. log.debug("The abs path is %r", path)
  35. # check path exist and its write permissions]
  36. if os.path.exists(path):
  37. real_path = path
  38. else:
  39. # All exceptions need to be caught because create directory maybe have some limit(permissions)
  40. log.debug("The directory(%s) doesn't exist, will create it", path)
  41. try:
  42. os.makedirs(path, exist_ok=True)
  43. real_path = path
  44. except PermissionError as err:
  45. log.error("No write permission on the directory(%r), error = %r", path, err)
  46. raise LineageParamTypeError("No write permission on the directory.")
  47. return real_path