Skip to content

Commit

Permalink
Improve building resources code (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
5yutan5 authored Jan 6, 2022
1 parent 9d625f8 commit 771ceea
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
12 changes: 0 additions & 12 deletions qdarktheme/qtpy/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion qdarktheme/qtpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Package containing Qt compat."""
"""Package applying Qt compat of PyQt6, PySide6, PyQt5 and PySide2."""
from qdarktheme.qtpy.qt_compat import QtImportError
from qdarktheme.qtpy.qt_version import __version__

Expand Down
23 changes: 16 additions & 7 deletions tools/build_resources/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from rich.console import Console

from tools.build_resources.main import DIST_DIR_PATH, build_resources, compare_all_files
from tools.build_resources.main import DIST_DIR_PATH, build_resources, compare_all_files, compare_rc_files

ROOT_INIT_DOC = '''"""Package including resources.
Expand Down Expand Up @@ -78,23 +78,32 @@ def _main() -> None:
with TemporaryDirectory() as temp_dir:
build_resources(Path(temp_dir), color_schemes, ROOT_INIT_DOC)
# Refresh dist dir
changed_files = compare_all_files(DIST_DIR_PATH, Path(temp_dir))
if not changed_files:
files_changed = compare_all_files(DIST_DIR_PATH, Path(temp_dir))
if not files_changed:
_console.log("There is no change")
return
if only_check:
_console.log("You can change following files: ", changed_files)
_console.log("You should change following files: ", files_changed)
raise Exception(
"""You need to change 'qdarktheme/themes' directory. You can use pre-commit command or run
'tools/build_resources/__main__.py' file
"""You can change './qdarktheme/themes' directory, by running pre-commit command or tools.build_resources
pre-commit : Run 'pre-commit install' and commit the changes
python script : Run 'poetry run python -m tools.build_resources'"""
) from None

rc_files_changed = compare_rc_files((DIST_DIR_PATH, Path(temp_dir)))
for rc_file in [file for file in files_changed if "rc_icons.py" in file]:
if rc_file in rc_files_changed:
continue
temp_rc_file = Path(temp_dir) / rc_file
dist_rc_file = DIST_DIR_PATH / rc_file
temp_rc_file.write_text(dist_rc_file.read_text())
files_changed.remove(rc_file)

shutil.rmtree(DIST_DIR_PATH, ignore_errors=True)
shutil.copytree(temp_dir, DIST_DIR_PATH)

_console.log("Build finished!", style="green")
_console.log("Changed contents: ", changed_files)
_console.log("Changed contents: ", files_changed)


if __name__ == "__main__":
Expand Down
42 changes: 31 additions & 11 deletions tools/build_resources/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from importlib import resources
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Sequence

from qdarktheme.util import get_qdarktheme_root_path, multi_replace
from tools.build_resources.color import RGBA
Expand Down Expand Up @@ -175,6 +176,26 @@ def build_resources(build_path: Path, theme_file_paths: list[Path], root_init_fi
_generate_root_init_file(build_path, themes, root_init_file_doc)


def compare_rc_files(dirs: Sequence[Path]) -> list[str]:
"""Check if the contents of the qt resource files with the same name in the two directories are the same.
Args:
dirs: The directories.
Returns:
list[str]: A list of file names with different contents.
"""
rc_files_changed: list[str] = []
exclude_pattern = re.compile(r"qt_resource_struct = b\"[\s\S]*?\"\n")
# Exclude rc_icon.py when the text other than random hash is the same
for rc_path in dirs[1].glob("**/rc_icons.py"):
rc_path = str(rc_path).replace(str(dirs[1]), "")[1:]
targets = {exclude_pattern.sub("", (dir / rc_path).read_text()) for dir in dirs}
if len(targets) != 1:
rc_files_changed.append(rc_path)
return rc_files_changed


def compare_all_files(dir1: Path, dir2: Path) -> list[str]:
"""Check if the contents of the files with the same name in the two directories are the same.
Expand All @@ -183,7 +204,7 @@ def compare_all_files(dir1: Path, dir2: Path) -> list[str]:
dir2: The directory containing files.
Returns:
list[str]: A list of file names with different contents
list[str]: A list of file names with different contents.
"""
target_files = set()
for file in dir2.glob("**/*"):
Expand All @@ -193,14 +214,13 @@ def compare_all_files(dir1: Path, dir2: Path) -> list[str]:
_, mismatch, err = cmpfiles(dir1, dir2, target_files)

files_changed = mismatch + err
rc_files_removed: list[str] = []

# Exclude rc_icon.py when the text other than random hash is the same
for rc_path in [file for file in files_changed if "rc_icons.py" in file]:
resource_code_1 = (dir1 / rc_path).read_text()
resource_code_2 = (dir2 / rc_path).read_text()
target1 = re.sub(r"qt_resource_struct = b\"[\s\S]*?\"\n", "", resource_code_1)
target2 = re.sub(r"qt_resource_struct = b\"[\s\S]*?\"\n", "", resource_code_2)
if target1 == target2:
files_changed.remove(rc_path)

return [str(dir1.relative_to(Path.cwd()) / file) for file in files_changed]
rc_files_changed = compare_rc_files((dir1, dir2))
for rc_file in [file for file in files_changed if "rc_icons.py" in file]:
if rc_file in rc_files_changed:
continue
files_changed.remove(rc_file)
rc_files_removed.append(rc_file)

return [str(file) for file in files_changed]

0 comments on commit 771ceea

Please sign in to comment.