2025-07-23 22:35:47 +08:00
|
|
|
import os, typing, logging
|
|
|
|
from pathlib import Path
|
2024-05-21 16:19:13 +08:00
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
def get_plugin_folder() -> Path:
|
2024-05-21 16:19:13 +08:00
|
|
|
"""
|
|
|
|
Get the absolute path to plugin root folder.
|
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
:return: The absolute path to plugin root folder.
|
2024-05-21 16:19:13 +08:00
|
|
|
"""
|
2025-07-23 22:35:47 +08:00
|
|
|
return Path(__file__).resolve().parent.parent
|
2023-12-25 10:49:25 +08:00
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
def relative_to_folder(abs_path: Path, src_parent: Path, dst_parent: Path) -> Path:
|
2023-12-25 10:49:25 +08:00
|
|
|
"""
|
|
|
|
Rebase one path to another path.
|
|
|
|
|
|
|
|
Give a absolute file path and folder path, and compute the relative path of given file to given folder.
|
|
|
|
Then applied the computed relative path to another given folder path.
|
|
|
|
Thus it seems like the file was rebased to from a folder to another folder with keeping the folder hierarchy.
|
|
|
|
|
|
|
|
For example, given `/path/to/file` and `/path`, it will compute relative path `to/file`.
|
|
|
|
Then it was applied to another folder path `/new` and got `/new/to/file`.
|
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
:param abs_path: The absolute path to a folder or file.
|
|
|
|
:param src_parent: The absolute path to folder which the `abs_path` will have relative path to.
|
|
|
|
:param dst_parent: The absolute path to folder which the relative path will be applied to.
|
2023-12-25 10:49:25 +08:00
|
|
|
"""
|
2025-07-23 22:35:47 +08:00
|
|
|
return dst_parent / (abs_path.relative_to(src_parent))
|
2023-12-25 10:49:25 +08:00
|
|
|
|
|
|
|
def common_file_migrator(
|
2025-07-23 22:35:47 +08:00
|
|
|
from_folder: Path, to_folder: Path,
|
|
|
|
fct_proc_folder: typing.Callable[[str, Path, Path], None],
|
|
|
|
fct_proc_file: typing.Callable[[str, Path, Path], None]) -> None:
|
2023-12-25 10:49:25 +08:00
|
|
|
"""
|
|
|
|
Common file migrator used by some build script.
|
|
|
|
|
|
|
|
This function receive 2 absolute folder path. `from_folder` indicate the file migrated out,
|
|
|
|
and `to_folder` indicate the file migrated in.
|
|
|
|
`fct_proc_folder` is a function pointer from caller which handle folder migration in detail.
|
|
|
|
`fct_proc_file` is same but handle file migration.
|
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
`fct_proc_folder` will receive 3 args.
|
|
|
|
First is the name of this folder which can be shown for end user.
|
|
|
|
Second is the source folder and third is expected dest folder.
|
2023-12-25 10:49:25 +08:00
|
|
|
`fct_proc_file` is same, but receive the file path instead.
|
|
|
|
Both of these function pointer should do the migration in detail. This function will only just iterate
|
|
|
|
folder and give essential args and will not do any migration operations such as copying or moving.
|
|
|
|
|
2025-07-23 22:35:47 +08:00
|
|
|
:param from_folder: The folder need to be migrated.
|
|
|
|
:param to_folder: The folder will be migrated to.
|
|
|
|
:param fct_proc_folder: Folder migration detail handler.
|
|
|
|
:param fct_proc_file: File migration detail handler.
|
2023-12-25 10:49:25 +08:00
|
|
|
"""
|
2025-07-23 22:35:47 +08:00
|
|
|
# TODO: If we have Python 3.12, use Path.walk instead of current polyfill.
|
|
|
|
|
2023-12-25 10:49:25 +08:00
|
|
|
# iterate from_folder folder
|
2025-07-23 22:35:47 +08:00
|
|
|
for root, dirs, files in os.walk(from_folder, topdown=True):
|
|
|
|
root = Path(root)
|
|
|
|
|
2023-12-25 10:49:25 +08:00
|
|
|
# iterate folders
|
|
|
|
for name in dirs:
|
|
|
|
# prepare handler args
|
2025-07-23 22:35:47 +08:00
|
|
|
src_folder = root / name
|
|
|
|
dst_folder = relative_to_folder(src_folder, from_folder, to_folder)
|
2023-12-25 10:49:25 +08:00
|
|
|
# call handler
|
2025-01-15 14:18:27 +08:00
|
|
|
fct_proc_folder(name, src_folder, dst_folder)
|
2025-07-23 22:35:47 +08:00
|
|
|
|
2023-12-25 10:49:25 +08:00
|
|
|
# iterate files
|
|
|
|
for name in files:
|
|
|
|
# prepare handler args
|
2025-07-23 22:35:47 +08:00
|
|
|
src_file = root / name
|
|
|
|
dst_file = relative_to_folder(src_file, from_folder, to_folder)
|
2023-12-25 10:49:25 +08:00
|
|
|
# call handler
|
2025-01-15 14:18:27 +08:00
|
|
|
fct_proc_file(name, src_file, dst_file)
|
2025-07-23 22:35:47 +08:00
|
|
|
|
|
|
|
def setup_logging() -> None:
|
|
|
|
"""
|
|
|
|
Setup uniform style for logging module.
|
|
|
|
"""
|
|
|
|
logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO)
|