!24255 _make_directory master 0927

Merge pull request !24255 from mindspore_ding/mkdir_master_0924
This commit is contained in:
i-robot 2021-10-08 06:33:20 +00:00 committed by Gitee
commit 298bf9fc48
1 changed files with 34 additions and 1 deletions

View File

@ -18,6 +18,7 @@ log module
import warnings
import sys
import os
import re
import stat
import time
import logging
@ -271,6 +272,38 @@ def _get_env_config():
config_dict[key] = value.strip()
return config_dict
def _check_directory_by_regular(target, reg=None, flag=re.ASCII, prim_name=None):
"""Check whether directory is legitimate."""
if not isinstance(target, str):
raise ValueError("Args directory {} must be string, please check it".format(target))
if reg is None:
reg = r"^[\/0-9a-zA-Z\_\-\.\:\\]+$"
if re.match(reg, target, flag) is None:
prim_name = f'in `{prim_name}`' if prim_name else ""
raise ValueError("'{}' {} is illegal, it should be match regular'{}' by flags'{}'".format(
target, prim_name, reg, flag))
def _make_directory(path: str):
"""Make directory."""
if path is None or not isinstance(path, str) or path.strip() == "":
raise TypeError("Input path '{}' is invalid type".format(path))
path = os.path.realpath(path)
_check_directory_by_regular(path)
if os.path.exists(path):
real_path = path
else:
try:
permissions = os.R_OK | os.W_OK | os.X_OK
os.umask(permissions << 3 | permissions)
mode = permissions << 6
os.makedirs(path, mode=mode, exist_ok=True)
real_path = path
except PermissionError:
raise TypeError("No write permission on the directory `{path}`.")
return real_path
def _verify_config(kwargs):
@ -313,7 +346,7 @@ def _verify_config(kwargs):
if console == _std_off and file_path is not None:
file_real_path = os.path.realpath(file_path)
if not os.path.exists(file_real_path):
os.makedirs(file_real_path, exist_ok=True)
_make_directory(file_real_path)
# Check the input value of maxBytes
max_bytes = kwargs.get('maxBytes', None)
if console == _std_off and max_bytes is not None: