Browse Source

[to #44857956]fix: disable git command username/password prompt

Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/10106602

    * [to #44857956]fix: disable git command username/password prompt
master
mulin.lyh 3 years ago
parent
commit
ff58300d09
2 changed files with 22 additions and 2 deletions
  1. +8
    -1
      modelscope/hub/git.py
  2. +14
    -1
      tests/hub/test_hub_private_repository.py

+ 8
- 1
modelscope/hub/git.py View File

@@ -39,14 +39,21 @@ class GitCommandWrapper(metaclass=Singleton):
subprocess.CompletedProcess: the command response
"""
logger.debug(' '.join(args))
git_env = os.environ.copy()
git_env['GIT_TERMINAL_PROMPT'] = '0'
response = subprocess.run(
[self.git_path, *args],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE) # compatible for python3.6
stderr=subprocess.PIPE,
env=git_env,
) # compatible for python3.6
try:
response.check_returncode()
return response
except subprocess.CalledProcessError as error:
logger.error(
'There are error run git command, you may need to login first.'
)
raise GitError(
'stdout: %s, stderr: %s' %
(response.stdout.decode('utf8'), error.stderr.decode('utf8')))


+ 14
- 1
tests/hub/test_hub_private_repository.py View File

@@ -10,7 +10,8 @@ from modelscope.hub.errors import GitError
from modelscope.hub.repository import Repository
from modelscope.utils.constant import ModelFile
from .test_utils import (TEST_ACCESS_TOKEN1, TEST_ACCESS_TOKEN2,
TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG)
TEST_MODEL_CHINESE_NAME, TEST_MODEL_ORG,
delete_credential)

DEFAULT_GIT_PATH = 'git'

@@ -65,6 +66,18 @@ class HubPrivateRepositoryTest(unittest.TestCase):
print(repo2.model_dir)
assert repo1.model_dir == repo2.model_dir

def test_clone_private_model_without_token(self):
delete_credential()
temporary_dir = tempfile.mkdtemp()
local_dir = os.path.join(temporary_dir, self.model_name)
with self.assertRaises(GitError) as cm:
Repository(local_dir, clone_from=self.model_id)

print(cm.exception)
assert not os.path.exists(os.path.join(local_dir, ModelFile.README))

self.api.login(TEST_ACCESS_TOKEN1) # re-login for delete


if __name__ == '__main__':
unittest.main()

Loading…
Cancel
Save