|
| 1 | +import argparse |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pygambit as gbt |
| 5 | + |
| 6 | +CATALOG_CSV = Path(__file__).parent.parent.parent / "doc" / "catalog.csv" |
| 7 | +CATALOG_DIR = Path(__file__).parent.parent.parent / "catalog" |
| 8 | +MAKEFILE_AM = Path(__file__).parent.parent.parent / "Makefile.am" |
| 9 | + |
| 10 | + |
| 11 | +def update_makefile(): |
| 12 | + """Update the Makefile.am with all games from the catalog.""" |
| 13 | + |
| 14 | + # Using rglob("*") to find files in all subdirectories |
| 15 | + slugs = [] |
| 16 | + for resource_path in sorted(CATALOG_DIR.rglob("*.efg")): |
| 17 | + if resource_path.is_file(): |
| 18 | + rel_path = resource_path.relative_to(CATALOG_DIR) |
| 19 | + slugs.append(str(rel_path)) |
| 20 | + for resource_path in sorted(CATALOG_DIR.rglob("*.nfg")): |
| 21 | + if resource_path.is_file(): |
| 22 | + rel_path = resource_path.relative_to(CATALOG_DIR) |
| 23 | + slugs.append(str(rel_path)) |
| 24 | + |
| 25 | + game_files = [] |
| 26 | + for slug in slugs: |
| 27 | + game_files.append(f"catalog/{slug}") |
| 28 | + game_files.sort() |
| 29 | + |
| 30 | + with open(MAKEFILE_AM, encoding="utf-8") as f: |
| 31 | + content = f.readlines() |
| 32 | + |
| 33 | + with open(MAKEFILE_AM, "w", encoding="utf-8") as f: |
| 34 | + in_gamefiles_section = False |
| 35 | + for line in content: |
| 36 | + # Add to the EXTRA_DIST after the README.rst line |
| 37 | + if line.startswith(" src/README.rst \\"): |
| 38 | + in_gamefiles_section = True |
| 39 | + f.write(" src/README.rst \\\n") |
| 40 | + for gf in game_files: |
| 41 | + if gf == game_files[-1]: |
| 42 | + f.write(f"\t{gf}\n") |
| 43 | + else: |
| 44 | + f.write(f"\t{gf} \\\n") |
| 45 | + f.write("\n") |
| 46 | + elif in_gamefiles_section: |
| 47 | + if line.strip() == "": |
| 48 | + in_gamefiles_section = False |
| 49 | + continue # Skip old gamefiles lines |
| 50 | + else: |
| 51 | + f.write(line) |
| 52 | + |
| 53 | + with open(MAKEFILE_AM, encoding="utf-8") as f: |
| 54 | + updated_content = f.readlines() |
| 55 | + |
| 56 | + if content != updated_content: |
| 57 | + print(f"Updated {str(MAKEFILE_AM)}") |
| 58 | + else: |
| 59 | + print(f"No changes to add to {str(MAKEFILE_AM)}") |
| 60 | + |
| 61 | + |
| 62 | +if __name__ == "__main__": |
| 63 | + |
| 64 | + parser = argparse.ArgumentParser() |
| 65 | + parser.add_argument("--build", action="store_true") |
| 66 | + args = parser.parse_args() |
| 67 | + |
| 68 | + # Create CSV used by RST docs page |
| 69 | + gbt.catalog.games().to_csv(CATALOG_CSV, index=False) |
| 70 | + print(f"Generated {CATALOG_CSV} for use in local docs build. DO NOT COMMIT.") |
| 71 | + |
| 72 | + # Update the Makefile.am with the current list of catalog files |
| 73 | + if args.build: |
| 74 | + update_makefile() |
0 commit comments