downloader.py 4.0 KB
Newer Older
Z
Zeyu Chen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   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.
Z
Zeyu Chen 已提交
14
# coding=utf-8
Z
Zeyu Chen 已提交
15

Z
Zeyu Chen 已提交
16 17 18 19
from __future__ import print_function
from __future__ import division
from __future__ import print_function

Z
Zeyu Chen 已提交
20
import os
Z
Zeyu Chen 已提交
21 22 23
import sys
import hashlib
import requests
Z
Zeyu Chen 已提交
24
import tempfile
Z
Zeyu Chen 已提交
25
import tarfile
Z
Zeyu Chen 已提交
26

Z
Zeyu Chen 已提交
27
__all__ = ['MODULE_HOME', 'download', 'md5file', 'download_and_uncompress']
Z
Zeyu Chen 已提交
28

Z
Zeyu Chen 已提交
29
# TODO(ZeyuChen) add environment varialble to set MODULE_HOME
Z
Zeyu Chen 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
MODULE_HOME = os.path.expanduser('~/.cache/paddle/module')


# When running unit tests, there could be multiple processes that
# trying to create MODULE_HOME directory simultaneously, so we cannot
# use a if condition to check for the existence of the directory;
# instead, we use the filesystem as the synchronization mechanism by
# catching returned errors.
def must_mkdirs(path):
    try:
        os.makedirs(MODULE_HOME)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise
        pass


def md5file(fname):
    hash_md5 = hashlib.md5()
    f = open(fname, "rb")
    for chunk in iter(lambda: f.read(4096), b""):
        hash_md5.update(chunk)
    f.close()
    return hash_md5.hexdigest()


def download_and_uncompress(url, save_name=None):
    module_name = url.split("/")[-2]
    dirname = os.path.join(MODULE_HOME, module_name)
    print("download to dir", dirname)
    if not os.path.exists(dirname):
        os.makedirs(dirname)

Z
Zeyu Chen 已提交
63
    #TODO(ZeyuChen) add download md5 file to verify file completeness
Z
Zeyu Chen 已提交
64 65 66
    file_name = os.path.join(
        dirname,
        url.split('/')[-1] if save_name is None else save_name)
Z
Zeyu Chen 已提交
67 68 69 70 71 72 73 74 75

    retry = 0
    retry_limit = 3
    while not (os.path.exists(file_name)):
        if os.path.exists(file_name):
            print("file md5", md5file(file_name))
        if retry < retry_limit:
            retry += 1
        else:
Z
Zeyu Chen 已提交
76 77 78
            raise RuntimeError(
                "Cannot download {0} within retry limit {1}".format(
                    url, retry_limit))
Z
Zeyu Chen 已提交
79 80 81 82 83 84 85 86
        print("Cache file %s not found, downloading %s" % (file_name, url))
        r = requests.get(url, stream=True)
        total_length = r.headers.get('content-length')

        if total_length is None:
            with open(file_name, 'wb') as f:
                shutil.copyfileobj(r.raw, f)
        else:
Z
Zeyu Chen 已提交
87
            #TODO(ZeyuChen) upgrade to tqdm process
Z
Zeyu Chen 已提交
88 89 90 91 92 93 94
            with open(file_name, 'wb') as f:
                dl = 0
                total_length = int(total_length)
                for data in r.iter_content(chunk_size=4096):
                    dl += len(data)
                    f.write(data)
                    done = int(50 * dl / total_length)
Z
Zeyu Chen 已提交
95 96
                    sys.stdout.write(
                        "\r[%s%s]" % ('=' * done, ' ' * (50 - done)))
Z
Zeyu Chen 已提交
97 98 99
                    sys.stdout.flush()

    print("file download completed!", file_name)
Z
Zeyu Chen 已提交
100 101
    #TODO(ZeyuChen) add md5 check error and file incompleted error, then raise
    # them and catch them
Z
Zeyu Chen 已提交
102 103 104 105 106 107 108
    with tarfile.open(file_name, "r:gz") as tar:
        file_names = tar.getnames()
        print(file_names)
        module_dir = os.path.join(dirname, file_names[0])
        for file_name in file_names:
            tar.extract(file_name, dirname)

Z
Zeyu Chen 已提交
109
    return module_name, module_dir
Z
Zeyu Chen 已提交
110

Z
Zeyu Chen 已提交
111 112

if __name__ == "__main__":
Z
Zeyu Chen 已提交
113
    # TODO(ZeyuChen) add unit test
Z
Zeyu Chen 已提交
114 115 116 117 118 119 120
    link = "http://paddlehub.bj.bcebos.com/word2vec/word2vec-dim16-simple-example-1.tar.gz"

    module_path = download_and_uncompress(link)
    print("module path", module_path)

    # dl = DownloadManager()
    # dl.download_and_uncompress(link, "./tmp")