1
# Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
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
7
# http://www.apache.org/licenses/LICENSE-2.0
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.
15
from __future__ import annotations
20
from tempfile import TemporaryDirectory
23
class LockFileTest(unittest.TestCase):
25
"https://bj.bcebos.com/paddlenlp/models/transformers/roformerv2/roformer_v2_chinese_char_small/vocab.txt"
28
def test_downloading_with_exist_file(self):
30
from paddlenlp.utils.downloader import get_path_from_url_with_filelock
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)
38
with open(lock_file_path, "w", encoding="utf-8") as f:
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)
45
def test_downloading_with_opened_exist_file(self):
47
from paddlenlp.utils.downloader import get_path_from_url_with_filelock
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)
55
with open(lock_file_path, "w", encoding="utf-8") as f:
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)