From 109d529394899b847dc4063c6fbd03f481497b7b Mon Sep 17 00:00:00 2001 From: ShenYuhan Date: Fri, 20 Dec 2019 18:25:16 +0800 Subject: [PATCH] add tips if paddle or hub version cannot match module --- paddlehub/commands/list.py | 2 +- paddlehub/commands/search.py | 2 +- paddlehub/commands/show.py | 2 +- paddlehub/{commands => common}/cml_utils.py | 0 paddlehub/common/hub_server.py | 15 +++++++++ paddlehub/common/utils.py | 35 +++++++++++++++++++++ paddlehub/module/manager.py | 35 +++++++++++++++++++++ 7 files changed, 88 insertions(+), 3 deletions(-) rename paddlehub/{commands => common}/cml_utils.py (100%) diff --git a/paddlehub/commands/list.py b/paddlehub/commands/list.py index 8ccccb67..5ee94ac2 100644 --- a/paddlehub/commands/list.py +++ b/paddlehub/commands/list.py @@ -21,7 +21,7 @@ from paddlehub.common import utils from paddlehub.common.downloader import default_downloader from paddlehub.module.manager import default_module_manager from paddlehub.commands.base_command import BaseCommand -from paddlehub.commands.cml_utils import TablePrinter +from paddlehub.common.cml_utils import TablePrinter class ListCommand(BaseCommand): diff --git a/paddlehub/commands/search.py b/paddlehub/commands/search.py index 80c52688..4a55cf40 100644 --- a/paddlehub/commands/search.py +++ b/paddlehub/commands/search.py @@ -22,7 +22,7 @@ import argparse from paddlehub.common import utils from paddlehub.common.hub_server import default_hub_server from paddlehub.commands.base_command import BaseCommand, ENTRY -from paddlehub.commands.cml_utils import TablePrinter +from paddlehub.common.cml_utils import TablePrinter class SearchCommand(BaseCommand): diff --git a/paddlehub/commands/show.py b/paddlehub/commands/show.py index e160da02..1c3f4543 100644 --- a/paddlehub/commands/show.py +++ b/paddlehub/commands/show.py @@ -22,7 +22,7 @@ import argparse from paddlehub.common import utils from paddlehub.commands.base_command import BaseCommand, ENTRY -from paddlehub.commands.cml_utils import TablePrinter +from paddlehub.common.cml_utils import TablePrinter from paddlehub.module.manager import default_module_manager from paddlehub.module.module import Module from paddlehub.io.parser import yaml_parser diff --git a/paddlehub/commands/cml_utils.py b/paddlehub/common/cml_utils.py similarity index 100% rename from paddlehub/commands/cml_utils.py rename to paddlehub/common/cml_utils.py diff --git a/paddlehub/common/hub_server.py b/paddlehub/common/hub_server.py index 4b7841c0..7039a2db 100644 --- a/paddlehub/common/hub_server.py +++ b/paddlehub/common/hub_server.py @@ -148,6 +148,21 @@ class HubServer(object): self.search_resource( resource_key=module_key, resource_type="Model", update=update) + def search_module_info(self, module_key): + try: + payload = {'name': module_key} + api_url = srv_utils.uri_path(self.get_server_url(), 'info') + r = srv_utils.hub_request(api_url, payload) + if r['status'] == 0 and len(r['data']) > 0: + return [(item['raw_name'], item['version'], + item['paddle_version'], item["hub_version"]) + for item in r['data']["info"]] + except: + if self.config.get('debug', False): + raise + else: + pass + def get_resource_url(self, resource_name, resource_type=None, diff --git a/paddlehub/common/utils.py b/paddlehub/common/utils.py index f4c7e0c1..8c291792 100644 --- a/paddlehub/common/utils.py +++ b/paddlehub/common/utils.py @@ -257,3 +257,38 @@ def sys_stdout_encoding(): if encoding is None: encoding = get_platform_default_encoding() return encoding + + +def version_sum(version): + """ + get sum(version), eg: version_sum(1.4.5) = 1*100*100*100 + 4*100*100 + 5*100 + :param version: string("1.3.6") + :return: + """ + sum = 0 + version_list = version.split(".") + for i in version_list: + sum = (sum + int(i)) * 100 + return sum + + +def sort_version_key(version_a, version_b): + if version_sum(version_a[1]) > version_sum(version_b[1]): + return -1 + elif version_sum(version_a[1]) == version_sum(version_b[1]): + return 0 + else: + return 1 + + +def strflist_version(version_list): + version_list = version_list[1:-1].split(",") + result = "" + if version_list[0] != "-1.0.0": + result = ">" + version_list[0] + if version_list[1] != "99.0.0": + if result != "": + result = result + ", " + "<" + version_list[1] + else: + result = "<" + version_list[1] + return result if result != "" else "-" diff --git a/paddlehub/module/manager.py b/paddlehub/module/manager.py index e8f5d653..433ccdaa 100644 --- a/paddlehub/module/manager.py +++ b/paddlehub/module/manager.py @@ -19,12 +19,14 @@ from __future__ import print_function import os import shutil +from functools import cmp_to_key from paddlehub.common import utils from paddlehub.common import srv_utils from paddlehub.common.downloader import default_downloader from paddlehub.common.hub_server import default_hub_server from paddlehub.common.dir import MODULE_HOME +from paddlehub.common.cml_utils import TablePrinter from paddlehub.module import module_desc_pb2 import paddlehub as hub from paddlehub.common.logger import logger @@ -103,6 +105,39 @@ class LocalModuleManager(object): != module_version) or (name != module_name): if default_hub_server._server_check() is False: tips = "Request Hub-Server unsuccessfully, please check your network." + return False, tips, None + module_versions_info = default_hub_server.search_module_info( + module_name) + if module_versions_info is not None and len( + module_versions_info) > 0: + + if utils.is_windows(): + placeholders = [20, 8, 14, 14] + else: + placeholders = [30, 8, 16, 16] + tp = TablePrinter( + titles=[ + "ResourceName", "Version", "PaddlePaddle", "PaddleHub" + ], + placeholders=placeholders) + module_versions_info.sort( + key=cmp_to_key(utils.sort_version_key)) + for resource_name, resource_version, paddle_version, \ + hub_version in module_versions_info: + colors = ["yellow", None, None, None] + + tp.add_line( + contents=[ + resource_name, resource_version, + utils.strflist_version(paddle_version), + utils.strflist_version(hub_version) + ], + colors=colors) + tips = "The version of PaddlePaddle or PaddleHub " \ + "can not match module, please upgrade your " \ + "PaddlePaddle or PaddleHub according to the form " \ + "below." + tp.get_text() + else: tips = "Can't find module %s" % module_name if module_version: -- GitLab