hub_server.py 12.1 KB
Newer Older
S
Steffy-zxf 已提交
1
#coding:utf-8
W
wuzewu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
W
wuzewu 已提交
19

W
wuzewu 已提交
20
import os
21
import time
22
import re
B
BinLong 已提交
23 24 25
import requests
import json
import yaml
B
BinLong 已提交
26
import random
27
import threading
W
wuzewu 已提交
28

29
from paddlehub.common import utils, srv_utils
W
wuzewu 已提交
30
from paddlehub.common.downloader import default_downloader
走神的阿圆's avatar
走神的阿圆 已提交
31
from paddlehub.common.decorator_utils import singleton
B
BinLong 已提交
32
from paddlehub.common.server_config import default_server_config
W
wuzewu 已提交
33
from paddlehub.io.parser import yaml_parser
S
shenyuhan 已提交
34
from paddlehub.common.lock import lock
S
shenyuhan 已提交
35
from paddlehub.common.dir import CONF_HOME, CACHE_HOME
走神的阿圆's avatar
走神的阿圆 已提交
36
from paddlehub.common.utils import ConfigInfo
W
wuzewu 已提交
37

W
wuzewu 已提交
38
RESOURCE_LIST_FILE = "resource_list_file.yml"
39
CACHE_TIME = 60 * 10
W
wuzewu 已提交
40 41


K
kinghuin 已提交
42
@singleton
W
wuzewu 已提交
43
class HubServer(object):
B
BinLong 已提交
44 45
    def __init__(self, config_file_path=None):
        if not config_file_path:
S
shenyuhan 已提交
46 47 48
            config_file_path = os.path.join(CONF_HOME, 'config.json')
        if not os.path.exists(CONF_HOME):
            utils.mkdir(CONF_HOME)
B
BinLong 已提交
49 50
        if not os.path.exists(config_file_path):
            with open(config_file_path, 'w+') as fp:
S
shenyuhan 已提交
51
                lock.flock(fp, lock.LOCK_EX)
B
BinLong 已提交
52
                fp.write(json.dumps(default_server_config))
S
shenyuhan 已提交
53
                lock.flock(fp, lock.LOCK_UN)
B
BinLong 已提交
54

走神的阿圆's avatar
走神的阿圆 已提交
55
        with open(config_file_path, "r") as fp:
B
BinLong 已提交
56
            self.config = json.load(fp)
走神的阿圆's avatar
走神的阿圆 已提交
57

S
shenyuhan 已提交
58 59
        fp_lock = open(config_file_path)
        lock.flock(fp_lock, lock.LOCK_EX)
B
BinLong 已提交
60

B
BinLong 已提交
61 62
        utils.check_url(self.config['server_url'])
        self.server_url = self.config['server_url']
B
BinLong 已提交
63
        self.request()
W
wuzewu 已提交
64
        self._load_resource_list_file_if_valid()
S
shenyuhan 已提交
65
        lock.flock(fp_lock, lock.LOCK_UN)
66

B
BinLong 已提交
67
    def get_server_url(self):
B
BinLong 已提交
68
        random.seed(int(time.time()))
B
BinLong 已提交
69 70 71
        HS_ENV = os.environ.get('HUB_SERVER')
        if HS_ENV:
            HUB_SERVERS = HS_ENV.split(';')
B
BinLong 已提交
72 73
            return HUB_SERVERS[random.randint(0, len(HUB_SERVERS) - 1)]
        return self.server_url[random.randint(0, len(self.server_url) - 1)]
B
BinLong 已提交
74

W
wuzewu 已提交
75
    def resource_list_file_path(self):
S
shenyuhan 已提交
76
        return os.path.join(CACHE_HOME, RESOURCE_LIST_FILE)
77

W
wuzewu 已提交
78 79 80
    def _load_resource_list_file_if_valid(self):
        self.resource_list_file = {}
        if not os.path.exists(self.resource_list_file_path()):
W
wuzewu 已提交
81
            return False
W
wuzewu 已提交
82
        file_create_time = os.path.getctime(self.resource_list_file_path())
83 84 85 86
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
W
wuzewu 已提交
87
            os.remove(self.resource_list_file_path())
88
            return False
W
wuzewu 已提交
89
        for resource in yaml_parser.parse(
W
wuzewu 已提交
90 91 92 93 94
                self.resource_list_file_path())['resource_list']:
            for key in resource:
                if key not in self.resource_list_file:
                    self.resource_list_file[key] = []
                self.resource_list_file[key].append(resource[key])
95

96
        # if file format is invalid, remove it
W
wuzewu 已提交
97 98 99
        if "version" not in self.resource_list_file or "name" not in self.resource_list_file:
            self.resource_list_file = {}
            os.remove(self.resource_list_file_path())
100 101
            return False
        return True
W
wuzewu 已提交
102

S
shenyuhan 已提交
103 104 105 106 107
    def search_resource(self,
                        resource_key,
                        resource_type=None,
                        update=False,
                        extra=None):
B
BinLong 已提交
108 109 110 111
        try:
            payload = {'word': resource_key}
            if resource_type:
                payload['type'] = resource_type
112
            api_url = srv_utils.uri_path(self.get_server_url(), 'search')
S
shenyuhan 已提交
113
            r = srv_utils.hub_request(api_url, payload, extra=extra)
B
BinLong 已提交
114
            if r['status'] == 0 and len(r['data']) > 0:
B
BinLong 已提交
115 116
                return [(item['name'], item['type'], item['version'],
                         item['summary']) for item in r['data']]
B
BinLong 已提交
117
        except:
B
BinLong 已提交
118 119 120 121
            if self.config.get('debug', False):
                raise
            else:
                pass
B
BinLong 已提交
122

W
wuzewu 已提交
123
        if update or not self.resource_list_file:
W
wuzewu 已提交
124 125
            self.request()

126 127 128
        if not self._load_resource_list_file_if_valid():
            return None

129 130 131
        match_resource_index_list = []
        for index, resource in enumerate(self.resource_list_file['name']):
            try:
W
wuzewu 已提交
132
                is_match = re.search(resource_key, resource)
133 134 135 136 137 138
                if is_match and (resource_type is None
                                 or self.resource_list_file['type'][index] ==
                                 resource_type):
                    match_resource_index_list.append(index)
            except:
                pass
W
wuzewu 已提交
139

W
wuzewu 已提交
140 141 142 143 144
        return [(self.resource_list_file['name'][index],
                 self.resource_list_file['type'][index],
                 self.resource_list_file['version'][index],
                 self.resource_list_file['summary'][index])
                for index in match_resource_index_list]
W
wuzewu 已提交
145

W
wuzewu 已提交
146 147 148 149 150 151 152 153
    def search_module(self, module_key, update=False):
        self.search_resource(
            resource_key=module_key, resource_type="Module", update=update)

    def search_model(self, module_key, update=False):
        self.search_resource(
            resource_key=module_key, resource_type="Model", update=update)

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    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

W
wuzewu 已提交
169 170 171 172
    def get_resource_url(self,
                         resource_name,
                         resource_type=None,
                         version=None,
S
shenyuhan 已提交
173 174
                         update=False,
                         extra=None):
B
BinLong 已提交
175 176 177 178 179 180
        try:
            payload = {'word': resource_name}
            if resource_type:
                payload['type'] = resource_type
            if version:
                payload['version'] = version
181
            api_url = srv_utils.uri_path(self.get_server_url(), 'search')
S
shenyuhan 已提交
182
            r = srv_utils.hub_request(api_url, payload, extra)
B
BinLong 已提交
183
            if r['status'] == 0 and len(r['data']) > 0:
184 185 186
                for item in r['data']:
                    if resource_name.lower() == item['name'].lower():
                        return item
B
BinLong 已提交
187 188
                return r['data'][0]
        except:
B
BinLong 已提交
189 190 191 192
            if self.config.get('debug', False):
                raise
            else:
                pass
B
BinLong 已提交
193

W
wuzewu 已提交
194
        if update or not self.resource_list_file:
W
wuzewu 已提交
195 196
            self.request()

197
        if not self._load_resource_list_file_if_valid():
W
wuzewu 已提交
198
            return {}
199

W
wuzewu 已提交
200
        resource_index_list = [
W
wuzewu 已提交
201
            index
W
wuzewu 已提交
202 203 204 205
            for index, resource in enumerate(self.resource_list_file['name'])
            if resource == resource_name and (
                resource_type is None
                or self.resource_list_file['type'][index] == resource_type)
W
wuzewu 已提交
206
        ]
W
wuzewu 已提交
207 208 209
        resource_version_list = [
            self.resource_list_file['version'][index]
            for index in resource_index_list
W
wuzewu 已提交
210
        ]
W
wuzewu 已提交
211
        resource_version_list = sorted(resource_version_list)
W
wuzewu 已提交
212
        if not version:
W
wuzewu 已提交
213
            if not resource_version_list:
W
wuzewu 已提交
214
                return {}
W
wuzewu 已提交
215
            version = resource_version_list[-1]
W
wuzewu 已提交
216

W
wuzewu 已提交
217 218
        for index in resource_index_list:
            if self.resource_list_file['version'][index] == version:
W
wuzewu 已提交
219 220
                return {
                    'url': self.resource_list_file['url'][index],
221 222
                    'md5': self.resource_list_file['md5'][index],
                    'version': version
W
wuzewu 已提交
223
                }
W
wuzewu 已提交
224

W
wuzewu 已提交
225
        return {}
W
wuzewu 已提交
226

S
shenyuhan 已提交
227 228 229 230 231
    def get_module_url(self,
                       module_name,
                       version=None,
                       update=False,
                       extra=None):
W
wuzewu 已提交
232 233 234 235
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Module",
            version=version,
S
shenyuhan 已提交
236 237
            update=update,
            extra=extra)
W
wuzewu 已提交
238

S
shenyuhan 已提交
239 240
    def get_model_url(self, module_name, version=None, update=False,
                      extra=None):
W
wuzewu 已提交
241 242 243 244
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Model",
            version=version,
S
shenyuhan 已提交
245 246
            update=update,
            extra=extra)
W
wuzewu 已提交
247

W
wuzewu 已提交
248
    def request(self):
S
shenyuhan 已提交
249 250
        if not os.path.exists(CACHE_HOME):
            utils.mkdir(CACHE_HOME)
B
BinLong 已提交
251
        try:
B
BinLong 已提交
252
            r = requests.get(self.get_server_url() + '/' + 'search')
B
BinLong 已提交
253
            data = json.loads(r.text)
S
shenyuhan 已提交
254
            cache_path = os.path.join(CACHE_HOME, RESOURCE_LIST_FILE)
B
BinLong 已提交
255
            with open(cache_path, 'w+') as fp:
B
BinLong 已提交
256
                yaml.safe_dump({'resource_list': data['data']}, fp)
B
BinLong 已提交
257 258
            return True
        except:
B
BinLong 已提交
259 260 261 262
            if self.config.get('debug', False):
                raise
            else:
                pass
263 264 265 266
        try:
            file_url = self.config[
                'resource_storage_server_url'] + RESOURCE_LIST_FILE
            result, tips, self.resource_list_file = default_downloader.download_file(
S
shenyuhan 已提交
267
                file_url, save_path=CACHE_HOME, replace=True)
268 269 270
            if not result:
                return False
        except:
W
wuzewu 已提交
271
            return False
272
        return True
W
wuzewu 已提交
273

S
shenyuhan 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    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.")

W
wuzewu 已提交
290

291
class CacheUpdater(threading.Thread):
走神的阿圆's avatar
走神的阿圆 已提交
292 293 294 295 296
    def __init__(self,
                 command="update_cache",
                 module=None,
                 version=None,
                 addition=None):
297
        threading.Thread.__init__(self)
走神的阿圆's avatar
走神的阿圆 已提交
298
        self.command = command
299 300
        self.module = module
        self.version = version
走神的阿圆's avatar
走神的阿圆 已提交
301
        self.addition = addition
302

走神的阿圆's avatar
走神的阿圆 已提交
303 304 305 306 307
    def update_resource_list_file(self,
                                  command="update_cache",
                                  module=None,
                                  version=None,
                                  addition=None):
308 309 310
        payload = {'word': module}
        if version:
            payload['version'] = version
K
kinghuin 已提交
311
        api_url = srv_utils.uri_path(HubServer().get_server_url(), 'search')
312
        cache_path = os.path.join(CACHE_HOME, RESOURCE_LIST_FILE)
走神的阿圆's avatar
走神的阿圆 已提交
313
        hub_name = ConfigInfo().get_hub_name()
314 315
        if os.path.exists(cache_path):
            extra = {
走神的阿圆's avatar
走神的阿圆 已提交
316 317 318
                "command": command,
                "mtime": os.stat(cache_path).st_mtime,
                "hub_name": hub_name
319 320 321
            }
        else:
            extra = {
走神的阿圆's avatar
走神的阿圆 已提交
322 323 324
                "command": command,
                "mtime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                "hub_name": hub_name
325
            }
走神的阿圆's avatar
走神的阿圆 已提交
326 327
        if addition is not None:
            extra.update({"addition": addition})
328
        try:
走神的阿圆's avatar
走神的阿圆 已提交
329
            r = srv_utils.hub_request(api_url, payload, extra, timeout=0.1)
330 331 332
            if r.get("update_cache", 0) == 1:
                with open(cache_path, 'w+') as fp:
                    yaml.safe_dump({'resource_list': r['data']}, fp)
333 334
        except Exception as err:
            pass
335 336

    def run(self):
走神的阿圆's avatar
走神的阿圆 已提交
337 338
        self.update_resource_list_file(self.command, self.module, self.version,
                                       self.addition)
339 340


S
shenyuhan 已提交
341
def server_check():
K
kinghuin 已提交
342
    HubServer().server_check()