hub_server.py 5.7 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 format is invalid, 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
        if not self._load_resource_list_file_if_valid():
            return None

75 76 77 78 79 80 81 82 83 84
        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 已提交
85

W
wuzewu 已提交
86 87 88 89 90
        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 已提交
91

W
wuzewu 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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 已提交
106 107
            self.request()

108
        if not self._load_resource_list_file_if_valid():
W
wuzewu 已提交
109
            return {}
110

W
wuzewu 已提交
111
        resource_index_list = [
W
wuzewu 已提交
112
            index
W
wuzewu 已提交
113 114 115 116
            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 已提交
117
        ]
W
wuzewu 已提交
118 119 120
        resource_version_list = [
            self.resource_list_file['version'][index]
            for index in resource_index_list
W
wuzewu 已提交
121 122
        ]
        #TODO(wuzewu): version sort method
W
wuzewu 已提交
123
        resource_version_list = sorted(resource_version_list)
W
wuzewu 已提交
124
        if not version:
W
wuzewu 已提交
125
            if not resource_version_list:
W
wuzewu 已提交
126
                return {}
W
wuzewu 已提交
127
            version = resource_version_list[-1]
W
wuzewu 已提交
128

W
wuzewu 已提交
129 130
        for index in resource_index_list:
            if self.resource_list_file['version'][index] == version:
W
wuzewu 已提交
131 132 133 134
                return {
                    'url': self.resource_list_file['url'][index],
                    'md5': self.resource_list_file['md5'][index]
                }
W
wuzewu 已提交
135

W
wuzewu 已提交
136
        return {}
W
wuzewu 已提交
137

W
wuzewu 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150
    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 已提交
151

W
wuzewu 已提交
152
    def request(self):
W
wuzewu 已提交
153 154
        file_url = self.server_url + RESOURCE_LIST_FILE
        result, tips, self.resource_list_file = default_downloader.download_file(
W
wuzewu 已提交
155 156 157
            file_url, save_path=hub.CACHE_HOME)
        if not result:
            return False
158
        return True
W
wuzewu 已提交
159

W
wuzewu 已提交
160 161

default_hub_server = HubServer()