From ccde7d4abed75dfdde2f079b4eccabb7bc8786be Mon Sep 17 00:00:00 2001 From: TakWolf Date: Wed, 3 Jul 2024 17:35:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=20fs=5Futil.delete=5Fdir()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/build.py | 8 +++----- tools/services/update_service.py | 18 ++++++++++++------ tools/utils/fs_util.py | 6 ------ 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/tools/build.py b/tools/build.py index 7d935a31..88168b0c 100644 --- a/tools/build.py +++ b/tools/build.py @@ -1,4 +1,5 @@ import itertools +import shutil from tools import configs from tools.configs import path_define @@ -7,14 +8,11 @@ from tools.configs.font import FontConfig from tools.services import update_service, dump_service, publish_service, info_service, template_service, image_service from tools.services.font_service import DesignContext, FontContext -from tools.utils import fs_util def main(): - fs_util.delete_dir(path_define.dump_dir) - fs_util.delete_dir(path_define.fallback_glyphs_dir) - fs_util.delete_dir(path_define.outputs_dir) - fs_util.delete_dir(path_define.releases_dir) + if path_define.build_dir.exists(): + shutil.rmtree(path_define.build_dir) update_service.setup_ark_pixel_glyphs() diff --git a/tools/services/update_service.py b/tools/services/update_service.py index 7b198998..21315c37 100644 --- a/tools/services/update_service.py +++ b/tools/services/update_service.py @@ -69,12 +69,14 @@ def setup_ark_pixel_glyphs(): logger.info("Already downloaded: '%s'", source_file_path) source_unzip_dir = downloads_dir.joinpath(f'ark-pixel-font-{sha}') - fs_util.delete_dir(source_unzip_dir) + if source_unzip_dir.exists(): + shutil.rmtree(source_unzip_dir) with zipfile.ZipFile(source_file_path) as file: file.extractall(downloads_dir) logger.info("Unzip: '%s'", source_unzip_dir) - fs_util.delete_dir(path_define.ark_pixel_glyphs_dir) + if path_define.ark_pixel_glyphs_dir.exists(): + shutil.rmtree(path_define.ark_pixel_glyphs_dir) path_define.ark_pixel_glyphs_dir.mkdir(parents=True) for font_size in configs.font_sizes: source_glyphs_dir_from = source_unzip_dir.joinpath('assets', 'glyphs', str(font_size)) @@ -95,7 +97,8 @@ def setup_ark_pixel_glyphs(): license_path_to = font_ark_pixel_dir.joinpath('LICENSE.txt') shutil.copyfile(license_path_from, license_path_to) - fs_util.delete_dir(source_unzip_dir) + if source_unzip_dir.exists(): + shutil.rmtree(source_unzip_dir) fs_util.write_json(version_info, cache_version_file_path) logger.info("Update glyphs: '%s'", sha) @@ -120,7 +123,8 @@ def update_fonts(update_config: UpdateConfig): downloads_dir = path_define.downloads_dir.joinpath(update_config.repository_name, tag_name) downloads_dir.mkdir(parents=True, exist_ok=True) - fs_util.delete_dir(fonts_dir) + if fonts_dir.exists(): + shutil.rmtree(fonts_dir) fonts_dir.mkdir(parents=True) for asset_config in update_config.asset_configs: @@ -138,7 +142,8 @@ def update_fonts(update_config: UpdateConfig): logger.info("Already downloaded: '%s'", asset_file_path) asset_unzip_dir = asset_file_path.with_suffix('') - fs_util.delete_dir(asset_unzip_dir) + if asset_unzip_dir.exists(): + shutil.rmtree(asset_unzip_dir) with zipfile.ZipFile(asset_file_path) as file: file.extractall(asset_unzip_dir) logger.info("Unzip: '%s'", asset_unzip_dir) @@ -148,7 +153,8 @@ def update_fonts(update_config: UpdateConfig): to_path = fonts_dir.joinpath(copy_info[1].format(version=version)) shutil.copyfile(from_path, to_path) logger.info("Copy from '%s' to '%s'", from_path, to_path) - fs_util.delete_dir(asset_unzip_dir) + if asset_unzip_dir.exists(): + shutil.rmtree(asset_unzip_dir) version_info = { 'repository_url': repository_url, diff --git a/tools/utils/fs_util.py b/tools/utils/fs_util.py index ed766e7d..99e79e1d 100644 --- a/tools/utils/fs_util.py +++ b/tools/utils/fs_util.py @@ -1,16 +1,10 @@ import json -import shutil from pathlib import Path from typing import Any import yaml -def delete_dir(path: Path): - if path.exists(): - shutil.rmtree(path) - - def is_empty_dir(path: Path) -> bool: for item_path in path.iterdir(): if item_path.name == '.DS_Store':