Skip to content

Commit

Permalink
Remove obsoleted output files (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutaj authored May 16, 2024
1 parent 43c8f84 commit 984459c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
31 changes: 29 additions & 2 deletions statements_manager/src/convert_task_runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import glob
import hashlib
import json
import os
import pathlib
import shutil
from enum import Enum
Expand Down Expand Up @@ -155,9 +157,10 @@ def copy_assets(
logger.info("copy assets file")
if assets_dst_path.exists():
logger.warning(
f"assets directory '{assets_dst_path}' already exists."
f"assets directory '{assets_dst_path}' already exists. overwriting..."
)
shutil.copytree(assets_src_path, assets_dst_path, dirs_exist_ok=True)
shutil.rmtree(assets_dst_path)
shutil.copytree(assets_src_path, assets_dst_path)

for path in assets_dst_path.glob("**/*"):
with open(path, "rb") as f:
Expand Down Expand Up @@ -293,6 +296,15 @@ def run(
reference_cache = json.load(open(output_dir / "cache.json"))
reference_cache.setdefault(output_ext, {})
reference_cache[output_ext].setdefault(problem_id, {})
problem_group = self.problemset_config.get_problem_group(problem_id)
for ext in reference_cache.keys():
obsoleted_ids = list(
filter(
lambda id: id not in problem_group, reference_cache[ext].keys()
)
)
for id in obsoleted_ids:
reference_cache[ext].pop(id)

problem_cache["assets"] = self.copy_assets(
problem_id, output_dir / "assets"
Expand All @@ -313,6 +325,21 @@ def run(
sort_keys=True,
)
dict_merge(problemset_cache, reference_cache[output_ext])

filenames = list(
filter(
lambda filename: pathlib.Path(filename).stem not in problem_group,
sum(
[
list(glob.glob(str(output_dir) + f"/*.{ext}"))
for ext in ["html", "pdf", "md"]
],
[],
),
)
)
for filename in filenames:
os.remove(filename)
logger.info("")

# 問題セットに対応するものを出力
Expand Down
9 changes: 9 additions & 0 deletions statements_manager/src/execute_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def __init__(self, problemset_filename: pathlib.Path, config: dict) -> None:
)
self.pdf_config = PDFRenderingConfig(problemset_filename, config.get("pdf", {}))
self.known_ids: set[str] = set()
self.id_groups: list[list[str]] = list()
self.problem_configs: dict[str, ProblemConfig] = dict()

dirname = problemset_filename.parent.resolve()
Expand All @@ -192,10 +193,18 @@ def get_problem(self, id: str) -> ProblemConfig:
def get_problem_ids(self) -> list[str]:
return sorted(list(self.problem_configs.keys()))

def get_problem_group(self, id: str) -> list[str]:
for group in self.id_groups:
if id in group:
return group
logger.error("problem id not found in any group")
raise ValueError("problem id not found in any group")

def add_problem_configs(self, filename: pathlib.Path) -> None:
raw_config = RawProblemConfig(filename, toml.load(filename))
self._check_id(raw_config)
numbered_ids = get_numbered_ids(raw_config)
self.id_groups.append(numbered_ids)
for id, statement_config in zip(numbered_ids, raw_config.statements):
self.problem_configs[id] = ProblemConfig(
filename, id, raw_config, statement_config
Expand Down

0 comments on commit 984459c

Please sign in to comment.