feat: add redist script
- add redist script for the convenience of building plugin package, especially for Blender 4.2
This commit is contained in:
@ -1,4 +1,12 @@
|
||||
import os, typing
|
||||
import os, typing, fnmatch, shutil
|
||||
|
||||
def get_plugin_folder() -> str:
|
||||
"""
|
||||
Get the absolute path to plugin root folder.
|
||||
|
||||
@return The absolute path to plugin root folder.
|
||||
"""
|
||||
return os.path.dirname(os.path.dirname(__file__))
|
||||
|
||||
def relative_to_folder(abs_path: str, src_parent: str, dst_parent: str) -> str:
|
||||
"""
|
||||
@ -55,3 +63,84 @@ def common_file_migrator(
|
||||
dst_file: str = relative_to_folder(src_file, from_folder, to_folder)
|
||||
# call handler
|
||||
fct_proc_file(src_file, dst_file)
|
||||
|
||||
def conditional_file_copy(
|
||||
from_folder: str, to_folder: str,
|
||||
only_copy: tuple[str, ...] | None = None,
|
||||
ignore_copy: tuple[str, ...] | None = None,
|
||||
recursively: bool = False) -> None:
|
||||
"""
|
||||
The enhanced file tree copy function used in redist script.
|
||||
|
||||
The name of file or folder will be checked by `only_copy` first,
|
||||
it it decide this file or folder should be copied, we then check whether
|
||||
it is in `ignore_copy`.
|
||||
|
||||
@param from_folder[in] The folder need to be redist.
|
||||
@param to_folder[in] The folder will be placed redist files.
|
||||
@param only_copy[in] An Unix style pathname pattern tuple to instruct which files or folders should be copied,
|
||||
or None if we want to copy every files and folders.
|
||||
@param ignore_copy[in] An Unix style pathname pattern tuple to instruct which files or folders should not be copied,
|
||||
or None if we want to copy every files and folders.
|
||||
@param recursively[in] Whether recursively copy sub-folders and their files.
|
||||
"""
|
||||
# build a helper functions
|
||||
def is_need_copy(checked_filename: str) -> bool:
|
||||
# if only_copy enabled, check it.
|
||||
# if no only_copy, pass the check because file should be copied in default.
|
||||
if only_copy is not None:
|
||||
for only_copy_item in only_copy:
|
||||
# matched, should copy it, break this for syntax
|
||||
if fnmatch.fnmatch(checked_filename, only_copy_item):
|
||||
break
|
||||
else:
|
||||
# no matched item, this entry should not be copied.
|
||||
return False
|
||||
|
||||
if ignore_copy is not None:
|
||||
# check whether given name is in ignore_copy
|
||||
for ignore_copy_item in ignore_copy:
|
||||
# matched, should not be copied
|
||||
if fnmatch.fnmatch(checked_filename, only_copy_item):
|
||||
return False
|
||||
# no matched, copy it
|
||||
return True
|
||||
else:
|
||||
# no ignore_copy, directly copy it
|
||||
return True
|
||||
|
||||
# iterate from_folder folder
|
||||
for root, dirs, files in os.walk(from_folder, topdown = True):
|
||||
# create self
|
||||
src_self: str = root
|
||||
dst_self: str = relative_to_folder(src_self, from_folder, to_folder)
|
||||
print(f'Creating: {src_self} -> {dst_self}')
|
||||
os.makedirs(dst_self, exist_ok=True)
|
||||
|
||||
# iterate files
|
||||
for name in files:
|
||||
# get source file path
|
||||
src_file: str = os.path.join(root, name)
|
||||
# check whether copy it
|
||||
if not is_need_copy(src_file):
|
||||
continue
|
||||
# build dst path and copy it
|
||||
dst_file: str = relative_to_folder(src_file, from_folder, to_folder)
|
||||
print(f'Copying: {src_file} -> {dst_file}')
|
||||
shutil.copy(src_file, dst_file)
|
||||
|
||||
# iterate folders when recursively flag enabled
|
||||
# if recursively:
|
||||
# for name in dirs:
|
||||
# # get source folder path
|
||||
# src_folder: str = os.path.join(root, name)
|
||||
# # build dst path and create it
|
||||
# dst_folder: str = relative_to_folder(src_folder, from_folder, to_folder)
|
||||
# print(f'Copying: {src_folder} -> {dst_folder}')
|
||||
# os.makedirs(dst_folder, exist_ok=True)
|
||||
|
||||
# if we don't have recursively flag,
|
||||
# we should exit at the end of first loop
|
||||
if not recursively:
|
||||
break
|
||||
|
||||
|
Reference in New Issue
Block a user