diff --git a/mim/commands/install.py b/mim/commands/install.py index f1ab503..21dbee0 100644 --- a/mim/commands/install.py +++ b/mim/commands/install.py @@ -129,7 +129,15 @@ def install(package: str, target_pkg, target_version = split_package_version(package) # whether install from local repo - is_install_local_repo = osp.isdir(osp.abspath(target_pkg)) and not find_url + if looks_like_path(target_pkg): + if is_installable_dir(target_pkg): + is_install_local_repo = True + else: + raise ValueError( + highlighted_error( + f'{target_pkg} is not a installable directory')) + else: + is_install_local_repo = False # whether install master branch from github is_install_master = bool(not target_version and find_url) @@ -240,6 +248,38 @@ def install(package: str, echo_success(f'Successfully installed {target_pkg}.') +def looks_like_path(name: str) -> bool: + """Checks whether the string "looks like" a path on the filesystem. + + This does not check whether the target actually exists, only judge from the + appearance. + + Args: + name (str): The string to be checked. + """ + if osp.sep in name: + return True + if osp.altsep is not None and osp.altsep in name: + return True + if name.startswith('.'): + return True + return False + + +def is_installable_dir(name: str) -> bool: + """Check whether path is a directory containing setup.py. + + Args: + name (str): The string to be checked. + """ + path = osp.abspath(name) + if osp.isdir(path): + setup_py = osp.join(path, 'setup.py') + return osp.isfile(setup_py) + else: + return False + + def infer_find_url(package: str) -> str: """Try to infer find_url if possible. diff --git a/tests/test_install.py b/tests/test_install.py index 2a23481..afc15f7 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -64,12 +64,8 @@ def test_mmrepo_install(): result = runner.invoke(install, ['./mmclassification', '--yes']) assert result.exit_code == 0 - # mim install mmclassification - result = runner.invoke(install, ['mmclassification', '--yes']) - assert result.exit_code == 0 - - # mim install -e mmclassification - result = runner.invoke(install, ['-e', 'mmclassification', '--yes']) + # mim install -e ./mmclassification + result = runner.invoke(install, ['-e', './mmclassification', '--yes']) assert result.exit_code == 0 os.chdir(current_root)