diff --git a/paddlehub/__init__.py b/paddlehub/__init__.py index d4c0017fecc134009eb8dd3231f491cfab76a286..b940e22925c18fb6a050a85e5270331a73d03510 100644 --- a/paddlehub/__init__.py +++ b/paddlehub/__init__.py @@ -61,3 +61,5 @@ from .finetune.strategy import ULMFiTStrategy from .finetune.strategy import CombinedStrategy from .autofinetune.evaluator import report_final_result + +from .common.hub_server import server_check diff --git a/paddlehub/commands/download.py b/paddlehub/commands/download.py index a7db88918fa1918fd4252b28586934264330c55e..7c31fbe203e142ec373915cbc899e6a0fc2e583e 100644 --- a/paddlehub/commands/download.py +++ b/paddlehub/commands/download.py @@ -79,10 +79,13 @@ class DownloadCommand(BaseCommand): url = search_result.get('url', None) except_md5_value = search_result.get('md5', None) if not url: - tips = "PaddleHub can't find model/module named %s" % mod_name - if mod_version: - tips += " with version %s" % mod_version - tips += ". Please use the 'hub search' command to find the correct model/module name." + if default_hub_server._server_check() is False: + tips = "Request Hub-Server unsuccessfully, please check your network." + else: + tips = "PaddleHub can't find model/module named %s" % mod_name + if mod_version: + tips += " with version %s" % mod_version + tips += ". Please use the 'hub search' command to find the correct model/module name." print(tips) return True diff --git a/paddlehub/commands/search.py b/paddlehub/commands/search.py index 24699f52abcd8a873573d0df6c1efbeeba68a12d..80c526888330a80cc6f5f6d76ae9489fac303008 100644 --- a/paddlehub/commands/search.py +++ b/paddlehub/commands/search.py @@ -53,6 +53,11 @@ class SearchCommand(BaseCommand): tp = TablePrinter( titles=["ResourceName", "Type", "Version", "Summary"], placeholders=placeholders) + if len(resource_list) == 0: + if default_hub_server._server_check() is False: + print( + "Request Hub-Server unsuccessfully, please check your network." + ) for resource_name, resource_type, resource_version, resource_summary in resource_list: if resource_type == "Module": colors = ["yellow", None, None, None] diff --git a/paddlehub/common/hub_server.py b/paddlehub/common/hub_server.py index c47f775b57c6bb35a3a0ea6b80a7584ae470b69e..d4d5c0d0205e01eada81ccf51042fd93bb5ee1c0 100644 --- a/paddlehub/common/hub_server.py +++ b/paddlehub/common/hub_server.py @@ -31,7 +31,7 @@ from paddlehub.common.downloader import default_downloader from paddlehub.common.server_config import default_server_config from paddlehub.io.parser import yaml_parser from paddlehub.common.lock import lock -import paddlehub as hub +from paddlehub.common.dir import CONF_HOME, CACHE_HOME RESOURCE_LIST_FILE = "resource_list_file.yml" CACHE_TIME = 60 * 10 @@ -40,9 +40,9 @@ CACHE_TIME = 60 * 10 class HubServer(object): def __init__(self, config_file_path=None): if not config_file_path: - config_file_path = os.path.join(hub.CONF_HOME, 'config.json') - if not os.path.exists(hub.CONF_HOME): - utils.mkdir(hub.CONF_HOME) + config_file_path = os.path.join(CONF_HOME, 'config.json') + if not os.path.exists(CONF_HOME): + utils.mkdir(CONF_HOME) if not os.path.exists(config_file_path): with open(config_file_path, 'w+') as fp: lock.flock(fp, lock.LOCK_EX) @@ -69,7 +69,7 @@ class HubServer(object): return self.server_url[random.randint(0, len(self.server_url) - 1)] def resource_list_file_path(self): - return os.path.join(hub.CACHE_HOME, RESOURCE_LIST_FILE) + return os.path.join(CACHE_HOME, RESOURCE_LIST_FILE) def _load_resource_list_file_if_valid(self): self.resource_list_file = {} @@ -227,12 +227,12 @@ class HubServer(object): extra=extra) def request(self): - if not os.path.exists(hub.CACHE_HOME): - utils.mkdir(hub.CACHE_HOME) + if not os.path.exists(CACHE_HOME): + utils.mkdir(CACHE_HOME) try: r = requests.get(self.get_server_url() + '/' + 'search') data = json.loads(r.text) - cache_path = os.path.join(hub.CACHE_HOME, RESOURCE_LIST_FILE) + cache_path = os.path.join(CACHE_HOME, RESOURCE_LIST_FILE) with open(cache_path, 'w+') as fp: yaml.safe_dump({'resource_list': data['data']}, fp) return True @@ -245,12 +245,32 @@ class HubServer(object): file_url = self.config[ 'resource_storage_server_url'] + RESOURCE_LIST_FILE result, tips, self.resource_list_file = default_downloader.download_file( - file_url, save_path=hub.CACHE_HOME, replace=True) + file_url, save_path=CACHE_HOME, replace=True) if not result: return False except: return False return True + def _server_check(self): + try: + r = requests.get(self.get_server_url() + '/search') + if r.status_code == 200: + return True + else: + return False + except: + return False + + def server_check(self): + if self._server_check() is True: + print("Request Hub-Server successfully.") + else: + print("Request Hub-Server unsuccessfully.") + + +def server_check(): + default_hub_server.server_check() + default_hub_server = HubServer() diff --git a/paddlehub/module/manager.py b/paddlehub/module/manager.py index 821f32f753bc4837de3c4c352c140fd9a7e20db2..e8f5d653f6ebee8c9a9f07af657d471d99057c4f 100644 --- a/paddlehub/module/manager.py +++ b/paddlehub/module/manager.py @@ -23,6 +23,7 @@ import shutil 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.module import module_desc_pb2 import paddlehub as hub @@ -100,11 +101,14 @@ class LocalModuleManager(object): installed_module_version = search_result.get('version', None) if not url or (module_version is not None and installed_module_version != module_version) or (name != module_name): - tips = "Can't find module %s" % module_name - if module_version: - tips += " with version %s" % module_version - module_tag = module_name if not module_version else '%s-%s' % ( - module_name, module_version) + if default_hub_server._server_check() is False: + tips = "Request Hub-Server unsuccessfully, please check your network." + else: + tips = "Can't find module %s" % module_name + if module_version: + tips += " with version %s" % module_version + module_tag = module_name if not module_version else '%s-%s' % ( + module_name, module_version) return False, tips, None result, tips, module_zip_file = default_downloader.download_file(