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.

validate_path.py 3.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. """Validate the input path."""
  16. import os
  17. from typing import Union, List
  18. from marshmallow import ValidationError
  19. def safe_normalize_path(
  20. path,
  21. raise_key,
  22. safe_prefixes: Union[None, List[str]],
  23. check_absolute_path=False,
  24. allow_parent_dir=False,
  25. ):
  26. """
  27. Returns safe normalized path.
  28. This func validates given path, and returns its normalized form. If
  29. safe_prefixes is given, this func will check whether the path is safe.
  30. Note:
  31. This func is not compatible with windows.
  32. Caller should check returned path to ensure safety according to
  33. business logic.
  34. File scheme (rfc8089) is currently not supported.
  35. Args:
  36. path (str): Path to be normalized.
  37. raise_key (str): The exception raise key
  38. safe_prefixes (list[str]): If not none, path must startswith one of the
  39. safe_prefixes. Set this arg to [] will cause all paths considered
  40. unsafe. Normally, prefix in this arg should end with "/".
  41. check_absolute_path (bool): Whether check path is absolute.
  42. allow_parent_dir (bool): Whether allow parent dir in path.
  43. Returns:
  44. str, normalized path.
  45. """
  46. normalized_path = validate_and_normalize_path(
  47. path,
  48. raise_key=raise_key,
  49. check_absolute_path=check_absolute_path,
  50. allow_parent_dir=allow_parent_dir,
  51. )
  52. if safe_prefixes is None:
  53. return normalized_path
  54. normalized_str = str(normalized_path)
  55. for prefix in safe_prefixes:
  56. if normalized_str.startswith(prefix):
  57. return normalized_path
  58. raise ValidationError({raise_key: {"The path is invalid!"}})
  59. def validate_and_normalize_path(
  60. path,
  61. raise_key,
  62. check_absolute_path=False,
  63. allow_parent_dir=False,
  64. ):
  65. """
  66. Validates path and returns its normalized form.
  67. If path has a valid scheme, treat path as url, otherwise consider path a
  68. unix local path.
  69. Note:
  70. File scheme (rfc8089) is currently not supported.
  71. Args:
  72. path (str): Path to be normalized.
  73. raise_key (str): The exception raise key.
  74. check_absolute_path (bool): Whether check path scheme is supported.
  75. allow_parent_dir (bool): Whether allow parent dir in path.
  76. Returns:
  77. str, normalized path.
  78. """
  79. if not path:
  80. raise ValidationError({raise_key: {"The path is invalid!"}})
  81. path_str = str(path)
  82. if not allow_parent_dir:
  83. path_components = path_str.split("/")
  84. if ".." in path_components:
  85. raise ValidationError({raise_key: {"The path is invalid!"}})
  86. # path does not have valid schema, treat it as unix local path.
  87. if check_absolute_path:
  88. if not path_str.startswith("/"):
  89. raise ValidationError({raise_key: {"The path is invalid!"}})
  90. try:
  91. # most unix systems allow
  92. normalized_path = os.path.realpath(path)
  93. except ValueError:
  94. raise ValidationError({raise_key: {"The path is invalid!"}})
  95. return normalized_path

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。

Contributors (1)