Files
paper_db/paper_db_common/new_project.py
T
2026-07-30 10:51:43 +09:00

81 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""共通テンプレートから新しい論文DBプロジェクトを作成する。"""
from __future__ import annotations
import argparse
import shutil
from pathlib import Path
COMMON_ROOT = Path(__file__).resolve().parent
TEMPLATE_DIR = COMMON_ROOT / "templates" / "project"
TEMPLATE_FILES = (
"config.py",
"README.org",
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="新しい分野DBを作成する"
)
parser.add_argument(
"directory",
type=Path,
help="作成するプロジェクトディレクトリ",
)
parser.add_argument(
"--force",
action="store_true",
help="既存ファイルを上書きする",
)
return parser.parse_args()
def validate_destination(
destination: Path,
force: bool,
) -> None:
if not destination.exists():
return
if not destination.is_dir():
raise NotADirectoryError(destination)
if any(destination.iterdir()) and not force:
raise FileExistsError(
f"空でないディレクトリです: {destination}"
)
def copy_template_files(destination: Path) -> None:
for name in TEMPLATE_FILES:
source = TEMPLATE_DIR / name
if not source.is_file():
raise FileNotFoundError(source)
shutil.copy2(source, destination / name)
def create_directories(destination: Path) -> None:
(destination / "tmp" / "text").mkdir(
parents=True,
exist_ok=True,
)
(destination / "logs").mkdir(
parents=True,
exist_ok=True,
)
def main() -> int:
args = parse_args()
destination = args.directory.expanduser().resolve()
validate_destination(destination, args.force)
destination.mkdir(parents=True, exist_ok=True)
copy_template_files(destination)
create_directories(destination)
print(destination)
return 0
if __name__ == "__main__":
raise SystemExit(main())