paddlenlp

Форк
0
/
test_downloader.py 
62 строки · 2.4 Кб
1
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#     http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

15
from __future__ import annotations
16

17
import hashlib
18
import os
19
import unittest
20
from tempfile import TemporaryDirectory
21

22

23
class LockFileTest(unittest.TestCase):
24
    test_url = (
25
        "https://bj.bcebos.com/paddlenlp/models/transformers/roformerv2/roformer_v2_chinese_char_small/vocab.txt"
26
    )
27

28
    def test_downloading_with_exist_file(self):
29

30
        from paddlenlp.utils.downloader import get_path_from_url_with_filelock
31

32
        with TemporaryDirectory() as tempdir:
33
            lock_file_name = hashlib.md5((self.test_url + tempdir).encode("utf-8")).hexdigest()
34
            lock_file_path = os.path.join(tempdir, ".lock", lock_file_name)
35
            os.makedirs(os.path.dirname(lock_file_path), exist_ok=True)
36

37
            # create lock file
38
            with open(lock_file_path, "w", encoding="utf-8") as f:
39
                f.write("temp test")
40

41
            # downloading with exist lock file
42
            config_file = get_path_from_url_with_filelock(self.test_url, root_dir=tempdir)
43
            self.assertIsNotNone(config_file)
44

45
    def test_downloading_with_opened_exist_file(self):
46

47
        from paddlenlp.utils.downloader import get_path_from_url_with_filelock
48

49
        with TemporaryDirectory() as tempdir:
50
            lock_file_name = hashlib.md5((self.test_url + tempdir).encode("utf-8")).hexdigest()
51
            lock_file_path = os.path.join(tempdir, ".lock", lock_file_name)
52
            os.makedirs(os.path.dirname(lock_file_path), exist_ok=True)
53

54
            # create lock file
55
            with open(lock_file_path, "w", encoding="utf-8") as f:
56
                f.write("temp test")
57

58
            # downloading with opened lock file
59
            open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC
60
            _ = os.open(lock_file_path, open_mode)
61
            config_file = get_path_from_url_with_filelock(self.test_url, root_dir=tempdir)
62
            self.assertIsNotNone(config_file)
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.