hub_server.py 3.8 KB
Newer Older
W
wuzewu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# 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
from paddle_hub.tools import utils
from paddle_hub.tools.downloader import default_downloader
from paddle_hub.data.reader import csv_reader
import os
22
import time
W
wuzewu 已提交
23 24
import paddle_hub as hub

25 26
MODULE_LIST_FILE = "module_list_file.csv"
CACHE_TIME = 60 * 10
W
wuzewu 已提交
27 28 29 30 31 32 33 34


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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        self._load_module_list_file_if_valid()

    def module_list_file_path(self):
        return os.path.join(hub.CACHE_HOME, MODULE_LIST_FILE)

    def _load_module_list_file_if_valid(self):
        self.module_list_file = {}
        if not os.path.exists(self.module_list_file_path()):
            return False
        file_create_time = os.path.getctime(self.module_list_file_path())
        now_time = time.time()

        # if file is out of date, remove it
        if now_time - file_create_time >= CACHE_TIME:
            os.remove(self.module_list_file_path())
            return False
        self.module_list_file = csv_reader.read(self.module_list_file_path())

        # if file do not contain necessary data, remove it
        if not "version" in self.module_list_file or not "module_name" in self.module_list_file:
            self.module_list_file = {}
            os.remove(self.module_list_file_path())
            return False
        return True
W
wuzewu 已提交
59 60

    def search_module(self, module_key, update=False):
61
        if update or not self.module_list_file:
W
wuzewu 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
            self.request()

        match_module_index_list = [
            index
            for index, module in enumerate(self.module_list_file['module_name'])
            if module_key in module
        ]

        return [(self.module_list_file['module_name'][index],
                 self.module_list_file['version'][index])
                for index in match_module_index_list]

    def get_module_url(self, module_name, version=None, update=False):
75
        if update or not self.module_list_file:
W
wuzewu 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            self.request()

        module_index_list = [
            index
            for index, module in enumerate(self.module_list_file['module_name'])
            if module == module_name
        ]
        module_version_list = [
            self.module_list_file['version'][index]
            for index in module_index_list
        ]
        #TODO(wuzewu): version sort method
        module_version_list = sorted(module_version_list)
        if not version:
            if not module_version_list:
                return None
            version = module_version_list[-1]

        for index in module_index_list:
            if self.module_list_file['version'][index] == version:
                return self.module_list_file['url'][index]

        return None

    def request(self):
        file_url = self.server_url + MODULE_LIST_FILE
W
wuzewu 已提交
102
        result, tips, self.module_list_file = default_downloader.download_file(
W
wuzewu 已提交
103
            file_url, save_path=hub.CACHE_HOME)
W
wuzewu 已提交
104 105
        if not result:
            return False
106
        return self._load_module_list_file_if_valid()
W
wuzewu 已提交
107 108 109


default_hub_server = HubServer()