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

add check_server()

上级 fff66a11
......@@ -79,6 +79,9 @@ class DownloadCommand(BaseCommand):
url = search_result.get('url', None)
except_md5_value = search_result.get('md5', None)
if not url:
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
......
......@@ -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]
......
......@@ -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 = {}
......@@ -106,6 +106,7 @@ class HubServer(object):
if resource_type:
payload['type'] = resource_type
api_url = srv_utils.uri_path(self.get_server_url(), 'search')
print(payload)
r = srv_utils.hub_request(api_url, payload, extra=extra)
if r['status'] == 0 and len(r['data']) > 0:
return [(item['name'], item['type'], item['version'],
......@@ -227,12 +228,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 +246,28 @@ 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.")
default_hub_server = HubServer()
......@@ -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,6 +101,9 @@ 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):
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册