hub_server.py 5.5 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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 已提交
18

W
wuzewu 已提交
19
import os
20
import time
21
import re
W
wuzewu 已提交
22 23 24

from paddlehub.common import utils
from paddlehub.common.downloader import default_downloader
W
wuzewu 已提交
25
from paddlehub.io.parser import yaml_parser
W
wuzewu 已提交
26
import paddlehub as hub
W
wuzewu 已提交
27

W
wuzewu 已提交
28
RESOURCE_LIST_FILE = "resource_list_file.yml"
29
CACHE_TIME = 60 * 10
W
wuzewu 已提交
30 31 32 33 34 35 36 37


class HubServer:
    def __init__(self, server_url=None):
        if not server_url:
            server_url = "https://paddlehub.bj.bcebos.com/"
        utils.check_url(server_url)
        self.server_url = server_url
W
wuzewu 已提交
38
        self._load_resource_list_file_if_valid()
39

W
wuzewu 已提交
40 41
    def resource_list_file_path(self):
        return os.path.join(hub.CACHE_HOME, RESOURCE_LIST_FILE)
42

W
wuzewu 已提交
43 44 45
    def _load_resource_list_file_if_valid(self):
        self.resource_list_file = {}
        if not os.path.exists(self.resource_list_file_path()):
W
wuzewu 已提交
46
            return False
W
wuzewu 已提交
47
        file_create_time = os.path.getctime(self.resource_list_file_path())
48 49 50 51
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
W
wuzewu 已提交
52
            os.remove(self.resource_list_file_path())
53
            return False
W
wuzewu 已提交
54
        for resource in yaml_parser.parse(
W
wuzewu 已提交
55 56 57 58 59
                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])
60 61

        # if file do not contain necessary data, remove it
W
wuzewu 已提交
62 63 64
        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())
65 66
            return False
        return True
W
wuzewu 已提交
67

W
wuzewu 已提交
68 69
    def search_resource(self, resource_key, resource_type=None, update=False):
        if update or not self.resource_list_file:
W
wuzewu 已提交
70 71
            self.request()

72 73 74 75 76 77 78 79 80 81
        match_resource_index_list = []
        for index, resource in enumerate(self.resource_list_file['name']):
            try:
                is_match = re.match(resource_key, resource)
                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 已提交
82

W
wuzewu 已提交
83 84 85 86 87
        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 已提交
88

W
wuzewu 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    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)

    def get_resource_url(self,
                         resource_name,
                         resource_type=None,
                         version=None,
                         update=False):
        if update or not self.resource_list_file:
W
wuzewu 已提交
103 104
            self.request()

W
wuzewu 已提交
105
        resource_index_list = [
W
wuzewu 已提交
106
            index
W
wuzewu 已提交
107 108 109 110
            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 已提交
111
        ]
W
wuzewu 已提交
112 113 114
        resource_version_list = [
            self.resource_list_file['version'][index]
            for index in resource_index_list
W
wuzewu 已提交
115 116
        ]
        #TODO(wuzewu): version sort method
W
wuzewu 已提交
117
        resource_version_list = sorted(resource_version_list)
W
wuzewu 已提交
118
        if not version:
W
wuzewu 已提交
119
            if not resource_version_list:
W
wuzewu 已提交
120
                return None
W
wuzewu 已提交
121
            version = resource_version_list[-1]
W
wuzewu 已提交
122

W
wuzewu 已提交
123 124 125
        for index in resource_index_list:
            if self.resource_list_file['version'][index] == version:
                return self.resource_list_file['url'][index]
W
wuzewu 已提交
126 127 128

        return None

W
wuzewu 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141
    def get_module_url(self, module_name, version=None, update=False):
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Module",
            version=version,
            update=update)

    def get_model_url(self, module_name, version=None, update=False):
        return self.get_resource_url(
            resource_name=module_name,
            resource_type="Model",
            version=version,
            update=update)
W
wuzewu 已提交
142

W
wuzewu 已提交
143
    def request(self):
W
wuzewu 已提交
144 145
        file_url = self.server_url + RESOURCE_LIST_FILE
        result, tips, self.resource_list_file = default_downloader.download_file(
W
wuzewu 已提交
146 147 148
            file_url, save_path=hub.CACHE_HOME)
        if not result:
            return False
W
wuzewu 已提交
149
        return self._load_resource_list_file_if_valid()
W
wuzewu 已提交
150

W
wuzewu 已提交
151 152

default_hub_server = HubServer()