Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Fix install bug #13

Merged
merged 8 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions mim/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
from mim.commands.uninstall import uninstall
from mim.utils import (
DEFAULT_URL,
MODULENAME2PKG,
PKG2MODULENAME,
PKG2PROJECT,
PKG_ALIAS,
WHEEL_URL,
call_command,
echo_success,
Expand Down Expand Up @@ -115,10 +116,10 @@ def install(package: str,
target_pkg, target_version = split_package_version(package)

# mmcv-full -> mmcv
true_pkg = PKG_ALIAS.get(target_pkg, target_pkg)
# module_name = PKG2MODULENAME.get(target_pkg, target_pkg)

# whether install from local repo
is_install_local_repo = osp.isdir(osp.abspath(true_pkg)) and not find_url
is_install_local_repo = osp.isdir(osp.abspath(target_pkg)) and not find_url

# whether install master branch from github
is_install_master = bool(not target_version and find_url)
Expand Down Expand Up @@ -166,15 +167,16 @@ def install(package: str,

if is_install_local_repo:
is_record = True
repo_root = osp.abspath(true_pkg)
target_pkg, target_version = get_package_version(repo_root)

echo_success(f'installing {target_pkg} from local repo.')

if not target_pkg:
repo_root = osp.abspath(target_pkg)
module_name, target_version = get_package_version(repo_root)
if not module_name:
raise FileNotFoundError(
highlighted_error(f'version.py is missed in {repo_root}'))

target_pkg = MODULENAME2PKG.get(module_name, module_name)

echo_success(f'installing {target_pkg} from local repo.')

install_from_repo(
repo_root,
package=target_pkg,
Expand All @@ -189,7 +191,7 @@ def install(package: str,
else:
# if installing from wheel failed, it will try to install package by
# building from source if possible.
is_record = bool(target_pkg in PKG_ALIAS)
is_record = bool(target_pkg in PKG2MODULENAME)
try:
install_from_wheel(target_pkg, target_version, find_url, timeout,
is_user_dir)
Expand Down Expand Up @@ -360,8 +362,8 @@ def install_from_repo(repo_root: str,
if dependencies:
install_dependencies(dependencies, timeout, is_yes, is_user_dir)

true_pkg = PKG_ALIAS.get(package, package)
pkg_root = osp.join(repo_root, true_pkg)
module_name = PKG2MODULENAME.get(package, package)
pkg_root = osp.join(repo_root, module_name)
src_tool_root = osp.join(repo_root, 'tools')
dst_tool_root = osp.join(pkg_root, 'tools')
src_config_root = osp.join(repo_root, 'configs')
Expand Down Expand Up @@ -401,11 +403,11 @@ def install_from_repo(repo_root: str,
if is_user_dir:
install_cmd.append('--user')

os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'
zhouzaida marked this conversation as resolved.
Show resolved Hide resolved
# install mmcv with ops by default
if package in WHEEL_URL or os.getenv('MMCV_WITH_OPS', '1') == 1:
if package in WHEEL_URL or os.getenv('MMCV_WITH_OPS', '1') == '1':
echo_warning(f'compiling {package} with "MMCV_WITH_OPS=1"')
os.environ['MMCV_WITH_OPS'] = '1'
os.environ['MKL_SERVICE_FORCE_INTEL'] = '1'

call_command(install_cmd)

Expand Down
4 changes: 2 additions & 2 deletions mim/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click

from mim.click import get_installed_package, param2lowercase
from mim.utils import PKG_ALIAS, call_command, remove_installation_records
from mim.utils import PKG2MODULENAME, call_command, remove_installation_records


@click.command('uninstall')
Expand Down Expand Up @@ -43,6 +43,6 @@ def uninstall(package: str, confirm_yes=False) -> None:
# package in the same process, is_installed will give a wrong result
# because importlib.import_module will search package from sys.modules
# first.
sys.modules.pop(PKG_ALIAS.get(package, package), None)
sys.modules.pop(PKG2MODULENAME.get(package, package), None)

remove_installation_records(package)
6 changes: 4 additions & 2 deletions mim/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .default import (
DEFAULT_CACHE_DIR,
DEFAULT_URL,
MODULENAME2PKG,
PKG2MODULENAME,
PKG2PROJECT,
PKG_ALIAS,
RAW_GITHUB_URL,
USER,
WHEEL_URL,
Expand Down Expand Up @@ -59,7 +60,7 @@
'is_installed',
'parse_url',
'PKG2PROJECT',
'PKG_ALIAS',
'PKG2MODULENAME',
'RAW_GITHUB_URL',
'read_installation_records',
'write_installation_records',
Expand All @@ -82,4 +83,5 @@
'highlighted_error',
'extract_tar',
'get_release_version',
'MODULENAME2PKG',
]
8 changes: 7 additions & 1 deletion mim/utils/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@
'mmocr': 'mmocr',
'mmgen': 'mmgeneration',
}
PKG_ALIAS = {
PKG2MODULENAME = {
ZwwWayne marked this conversation as resolved.
Show resolved Hide resolved
'mmcv-full': 'mmcv',
'mmcv': 'mmcv',
'mmaction2': 'mmaction',
'mmsegmentation': 'mmseg',
}
MODULENAME2PKG = {
'mmaction': 'mmaction2',
'mmseg': 'mmsegmentation',
}

DEFAULT_CACHE_DIR = f'{os.environ["HOME"]}/.cache/mim'
Expand Down
26 changes: 12 additions & 14 deletions mim/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from requests.exceptions import InvalidURL, RequestException, Timeout
from requests.models import Response

from .default import DEFAULT_URL, MMPACKAGE_PATH, PKG2PROJECT, PKG_ALIAS
from .default import DEFAULT_URL, MMPACKAGE_PATH, PKG2MODULENAME, PKG2PROJECT


def parse_url(url: str) -> Tuple[str, str]:
Expand Down Expand Up @@ -152,8 +152,8 @@ def is_installed(package: str) -> Any:
Args:
package (str): Name of package to be checked.
"""
target_pkg = PKG_ALIAS.get(package, package)
return importlib.util.find_spec(target_pkg) # type: ignore
module_name = PKG2MODULENAME.get(package, package)
return importlib.util.find_spec(module_name) # type: ignore


def get_package_version(repo_root: str) -> Tuple[str, str]:
Expand All @@ -178,13 +178,12 @@ def get_installed_version(package: str) -> str:
Args:
package (str): Name of package.
"""
target_pkg = PKG_ALIAS.get(package, package)
module_name = PKG2MODULENAME.get(package, package)

if not is_installed(target_pkg):
raise RuntimeError(
highlighted_error(f'{target_pkg} is not installed.'))
if not is_installed(module_name):
raise RuntimeError(highlighted_error(f'{package} is not installed.'))

module = importlib.import_module(target_pkg)
module = importlib.import_module(module_name)
return module.__version__ # type: ignore


Expand Down Expand Up @@ -229,8 +228,8 @@ def get_commit_id(package: str) -> str:
Args:
package (str): Name of package.
"""
target_pkg = PKG_ALIAS.get(package, package)
module = importlib.import_module('..commit_id', f'{target_pkg}.subpkg')
module_name = PKG2MODULENAME.get(package, package)
module = importlib.import_module('..commit_id', f'{module_name}.subpkg')
return module.commit_id # type: ignore


Expand All @@ -244,8 +243,8 @@ def get_installed_path(package: str) -> str:
>>> get_installed_path('mmcls')
>>> '.../lib/python3.7/site-packages/mmcls'
"""
target_pkg = PKG_ALIAS.get(package, package)
module = importlib.import_module(target_pkg)
module_name = PKG2MODULENAME.get(package, package)
module = importlib.import_module(module_name)
return module.__path__[0] # type: ignore


Expand Down Expand Up @@ -296,7 +295,7 @@ def read_installation_records() -> list:
for pkg in pkg_resources.working_set:
pkg_name = pkg.project_name
if pkg_name not in seen and (pkg_name in PKG2PROJECT
or pkg_name in PKG_ALIAS):
or pkg_name in PKG2MODULENAME):
pkgs_info.append((pkg_name, pkg.version, ''))

return pkgs_info
Expand All @@ -312,7 +311,6 @@ def write_installation_records(package: str,
for _package, _version, _source in pkgs_info:
if _package != package:
fw.write(f'{_package},{_version},{_source}\n')

fw.write(f'{package},{version},{source}\n')


Expand Down