提交 8a91c860 编写于 作者: S shenyuhan

add check_server()

上级 fff66a11
...@@ -79,10 +79,13 @@ class DownloadCommand(BaseCommand): ...@@ -79,10 +79,13 @@ class DownloadCommand(BaseCommand):
url = search_result.get('url', None) url = search_result.get('url', None)
except_md5_value = search_result.get('md5', None) except_md5_value = search_result.get('md5', None)
if not url: if not url:
tips = "PaddleHub can't find model/module named %s" % mod_name if default_hub_server._server_check() is False:
if mod_version: tips = "Request Hub-Server unsuccessfully, please check your network."
tips += " with version %s" % mod_version else:
tips += ". Please use the 'hub search' command to find the correct model/module name." 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) print(tips)
return True return True
......
...@@ -53,6 +53,11 @@ class SearchCommand(BaseCommand): ...@@ -53,6 +53,11 @@ class SearchCommand(BaseCommand):
tp = TablePrinter( tp = TablePrinter(
titles=["ResourceName", "Type", "Version", "Summary"], titles=["ResourceName", "Type", "Version", "Summary"],
placeholders=placeholders) 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: for resource_name, resource_type, resource_version, resource_summary in resource_list:
if resource_type == "Module": if resource_type == "Module":
colors = ["yellow", None, None, None] colors = ["yellow", None, None, None]
......
...@@ -31,7 +31,7 @@ from paddlehub.common.downloader import default_downloader ...@@ -31,7 +31,7 @@ from paddlehub.common.downloader import default_downloader
from paddlehub.common.server_config import default_server_config from paddlehub.common.server_config import default_server_config
from paddlehub.io.parser import yaml_parser from paddlehub.io.parser import yaml_parser
from paddlehub.common.lock import lock 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" RESOURCE_LIST_FILE = "resource_list_file.yml"
CACHE_TIME = 60 * 10 CACHE_TIME = 60 * 10
...@@ -40,9 +40,9 @@ CACHE_TIME = 60 * 10 ...@@ -40,9 +40,9 @@ CACHE_TIME = 60 * 10
class HubServer(object): class HubServer(object):
def __init__(self, config_file_path=None): def __init__(self, config_file_path=None):
if not config_file_path: if not config_file_path:
config_file_path = os.path.join(hub.CONF_HOME, 'config.json') config_file_path = os.path.join(CONF_HOME, 'config.json')
if not os.path.exists(hub.CONF_HOME): if not os.path.exists(CONF_HOME):
utils.mkdir(hub.CONF_HOME) utils.mkdir(CONF_HOME)
if not os.path.exists(config_file_path): if not os.path.exists(config_file_path):
with open(config_file_path, 'w+') as fp: with open(config_file_path, 'w+') as fp:
lock.flock(fp, lock.LOCK_EX) lock.flock(fp, lock.LOCK_EX)
...@@ -69,7 +69,7 @@ class HubServer(object): ...@@ -69,7 +69,7 @@ class HubServer(object):
return self.server_url[random.randint(0, len(self.server_url) - 1)] return self.server_url[random.randint(0, len(self.server_url) - 1)]
def resource_list_file_path(self): 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): def _load_resource_list_file_if_valid(self):
self.resource_list_file = {} self.resource_list_file = {}
...@@ -106,6 +106,7 @@ class HubServer(object): ...@@ -106,6 +106,7 @@ class HubServer(object):
if resource_type: if resource_type:
payload['type'] = resource_type payload['type'] = resource_type
api_url = srv_utils.uri_path(self.get_server_url(), 'search') api_url = srv_utils.uri_path(self.get_server_url(), 'search')
print(payload)
r = srv_utils.hub_request(api_url, payload, extra=extra) r = srv_utils.hub_request(api_url, payload, extra=extra)
if r['status'] == 0 and len(r['data']) > 0: if r['status'] == 0 and len(r['data']) > 0:
return [(item['name'], item['type'], item['version'], return [(item['name'], item['type'], item['version'],
...@@ -227,12 +228,12 @@ class HubServer(object): ...@@ -227,12 +228,12 @@ class HubServer(object):
extra=extra) extra=extra)
def request(self): def request(self):
if not os.path.exists(hub.CACHE_HOME): if not os.path.exists(CACHE_HOME):
utils.mkdir(hub.CACHE_HOME) utils.mkdir(CACHE_HOME)
try: try:
r = requests.get(self.get_server_url() + '/' + 'search') r = requests.get(self.get_server_url() + '/' + 'search')
data = json.loads(r.text) 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: with open(cache_path, 'w+') as fp:
yaml.safe_dump({'resource_list': data['data']}, fp) yaml.safe_dump({'resource_list': data['data']}, fp)
return True return True
...@@ -245,12 +246,28 @@ class HubServer(object): ...@@ -245,12 +246,28 @@ class HubServer(object):
file_url = self.config[ file_url = self.config[
'resource_storage_server_url'] + RESOURCE_LIST_FILE 'resource_storage_server_url'] + RESOURCE_LIST_FILE
result, tips, self.resource_list_file = default_downloader.download_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: if not result:
return False return False
except: except:
return False return False
return True 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.")
default_hub_server = HubServer() default_hub_server = HubServer()
...@@ -23,6 +23,7 @@ import shutil ...@@ -23,6 +23,7 @@ import shutil
from paddlehub.common import utils from paddlehub.common import utils
from paddlehub.common import srv_utils from paddlehub.common import srv_utils
from paddlehub.common.downloader import default_downloader 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.dir import MODULE_HOME
from paddlehub.module import module_desc_pb2 from paddlehub.module import module_desc_pb2
import paddlehub as hub import paddlehub as hub
...@@ -100,11 +101,14 @@ class LocalModuleManager(object): ...@@ -100,11 +101,14 @@ class LocalModuleManager(object):
installed_module_version = search_result.get('version', None) installed_module_version = search_result.get('version', None)
if not url or (module_version is not None and installed_module_version if not url or (module_version is not None and installed_module_version
!= module_version) or (name != module_name): != module_version) or (name != module_name):
tips = "Can't find module %s" % module_name if default_hub_server._server_check() is False:
if module_version: tips = "Request Hub-Server unsuccessfully, please check your network."
tips += " with version %s" % module_version else:
module_tag = module_name if not module_version else '%s-%s' % ( tips = "Can't find module %s" % module_name
module_name, module_version) 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 return False, tips, None
result, tips, module_zip_file = default_downloader.download_file( result, tips, module_zip_file = default_downloader.download_file(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册