| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
import subprocess |
| 7 |
import tempfile |
| 8 |
import unittest |
| 9 |
|
| 10 |
from util import out_of_date |
| 11 |
|
| 12 |
|
| 13 |
def get_resource(name: str) -> Path: |
| 14 |
dir = Path(__file__).parent |
| 15 |
decompressed_path = dir / name |
| 16 |
compressed_path = dir / (name + ".zst") |
| 17 |
if out_of_date(decompressed_path, compressed_path): |
| 18 |
tmp_file = tempfile.NamedTemporaryFile(dir=dir, prefix=name, delete=False) |
| 19 |
try: |
| 20 |
try: |
| 21 |
subprocess.check_call( |
| 22 |
[ |
| 23 |
"zstd", |
| 24 |
"--quiet", |
| 25 |
"--force", |
| 26 |
"--decompress", |
| 27 |
"--stdout", |
| 28 |
str(compressed_path), |
| 29 |
], |
| 30 |
stdout=tmp_file, |
| 31 |
) |
| 32 |
except FileNotFoundError: |
| 33 |
raise unittest.SkipTest("zstd not found") |
| 34 |
except BaseException: |
| 35 |
os.unlink(tmp_file.name) |
| 36 |
raise |
| 37 |
else: |
| 38 |
os.rename(tmp_file.name, decompressed_path) |
| 39 |
return decompressed_path |