From 5b0b54633b0babe9aafa566c2a26f6dc505e9beb Mon Sep 17 00:00:00 2001 From: "zhangzhicheng.zzc" Date: Wed, 24 Aug 2022 13:35:42 +0800 Subject: [PATCH] [to #42322933]compatible with windows path on only core parts Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/9855254 --- modelscope/hub/constants.py | 4 +++- modelscope/msdatasets/config.py | 4 ++-- modelscope/utils/ast_utils.py | 4 +++- modelscope/utils/file_utils.py | 5 ++--- modelscope/utils/import_utils.py | 3 ++- tests/utils/test_ast.py | 8 ++++---- 6 files changed, 16 insertions(+), 12 deletions(-) diff --git a/modelscope/hub/constants.py b/modelscope/hub/constants.py index 702251e3..014a1e59 100644 --- a/modelscope/hub/constants.py +++ b/modelscope/hub/constants.py @@ -1,3 +1,5 @@ +from pathlib import Path + MODELSCOPE_URL_SCHEME = 'http://' DEFAULT_MODELSCOPE_DOMAIN = 'www.modelscope.cn' DEFAULT_MODELSCOPE_DATA_ENDPOINT = MODELSCOPE_URL_SCHEME + DEFAULT_MODELSCOPE_DOMAIN @@ -6,7 +8,7 @@ DEFAULT_MODELSCOPE_GROUP = 'damo' MODEL_ID_SEPARATOR = '/' FILE_HASH = 'Sha256' LOGGER_NAME = 'ModelScopeHub' -DEFAULT_CREDENTIALS_PATH = '~/.modelscope/credentials' +DEFAULT_CREDENTIALS_PATH = Path.home().joinpath('.modelscope', 'credentials') API_RESPONSE_FIELD_DATA = 'Data' API_RESPONSE_FIELD_GIT_ACCESS_TOKEN = 'AccessToken' API_RESPONSE_FIELD_USERNAME = 'Username' diff --git a/modelscope/msdatasets/config.py b/modelscope/msdatasets/config.py index 0357e823..bafe3f99 100644 --- a/modelscope/msdatasets/config.py +++ b/modelscope/msdatasets/config.py @@ -4,9 +4,9 @@ from pathlib import Path # Cache location from modelscope.hub.constants import DEFAULT_MODELSCOPE_DATA_ENDPOINT -DEFAULT_CACHE_HOME = '~/.cache' +DEFAULT_CACHE_HOME = Path.home().joinpath('.cache') CACHE_HOME = os.getenv('CACHE_HOME', DEFAULT_CACHE_HOME) -DEFAULT_MS_CACHE_HOME = os.path.join(CACHE_HOME, 'modelscope/hub') +DEFAULT_MS_CACHE_HOME = os.path.join(CACHE_HOME, 'modelscope', 'hub') MS_CACHE_HOME = os.path.expanduser( os.getenv('MS_CACHE_HOME', DEFAULT_MS_CACHE_HOME)) diff --git a/modelscope/utils/ast_utils.py b/modelscope/utils/ast_utils.py index 759bd447..2d2f61d8 100644 --- a/modelscope/utils/ast_utils.py +++ b/modelscope/utils/ast_utils.py @@ -7,6 +7,7 @@ import os.path as osp import time import traceback from functools import reduce +from pathlib import Path from typing import Generator, Union import gast @@ -24,9 +25,10 @@ from modelscope.utils.registry import default_group logger = get_logger() storage = LocalStorage() +p = Path(__file__) # get the path of package 'modelscope' -MODELSCOPE_PATH = '/'.join(os.path.dirname(__file__).split('/')[:-1]) +MODELSCOPE_PATH = p.resolve().parents[1] REGISTER_MODULE = 'register_module' IGNORED_PACKAGES = ['modelscope', '.'] SCAN_SUB_FOLDERS = [ diff --git a/modelscope/utils/file_utils.py b/modelscope/utils/file_utils.py index a04d890f..9b82f8d2 100644 --- a/modelscope/utils/file_utils.py +++ b/modelscope/utils/file_utils.py @@ -1,7 +1,7 @@ # Copyright (c) Alibaba, Inc. and its affiliates. import inspect -import os +from pathlib import Path # TODO: remove this api, unify to flattened args @@ -33,6 +33,5 @@ def get_default_cache_dir(): """ default base dir: '~/.cache/modelscope' """ - default_cache_dir = os.path.expanduser( - os.path.join('~/.cache', 'modelscope')) + default_cache_dir = Path.home().joinpath('.cache', 'modelscope') return default_cache_dir diff --git a/modelscope/utils/import_utils.py b/modelscope/utils/import_utils.py index 85f442a7..c9bea020 100644 --- a/modelscope/utils/import_utils.py +++ b/modelscope/utils/import_utils.py @@ -10,6 +10,7 @@ from collections import OrderedDict from functools import wraps from importlib import import_module from itertools import chain +from pathlib import Path from types import ModuleType from typing import Any @@ -43,7 +44,7 @@ def import_modules_from_file(py_file: str): """ dirname, basefile = os.path.split(py_file) if dirname == '': - dirname == './' + dirname = Path.cwd() module_name = osp.splitext(basefile)[0] sys.path.insert(0, dirname) validate_py_syntax(py_file) diff --git a/tests/utils/test_ast.py b/tests/utils/test_ast.py index c144c4fe..de99a7b8 100644 --- a/tests/utils/test_ast.py +++ b/tests/utils/test_ast.py @@ -5,13 +5,13 @@ import shutil import tempfile import time import unittest - -import gast +from pathlib import Path from modelscope.utils.ast_utils import AstScaning, FilesAstScaning, load_index -MODELSCOPE_PATH = '/'.join( - os.path.dirname(__file__).split('/')[:-2]) + '/modelscope' +p = Path(__file__) + +MODELSCOPE_PATH = p.resolve().parents[2].joinpath('modelscope') class AstScaningTest(unittest.TestCase):