未验证 提交 78425c07 编写于 作者: B Bin Long 提交者: GitHub

Merge pull request #219 from ShenYuhan/check_server

Check server
......@@ -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
......@@ -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
......
......@@ -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 = {}
......@@ -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()
......@@ -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(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册