From e1a97d377394b17ad76eb8d7e4eba5b5ebe47a8e Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Sun, 15 Mar 2020 12:12:35 +0100 Subject: Refactor dependency writer to make it easier to reason about indices --- repo/www/make-deps.py | 88 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 21 deletions(-) diff --git a/repo/www/make-deps.py b/repo/www/make-deps.py index 4ca4e6e..26a4037 100755 --- a/repo/www/make-deps.py +++ b/repo/www/make-deps.py @@ -1,8 +1,34 @@ #!/usr/bin/env python3 +"""Write dependencies for all website pages in makefile syntax. + +We want to compute: + +- a list of leaf pages, +- a list of auto-generated indices, + +- dependencies for leaf pages: + OUTPUT/foo/bar.html: foo/bar.txt | OUTPUT/foo + + - special case for READMEs: + OUTPUT/foo/index.html: foo/README.txt foo | OUTPUT/foo + +- dependencies for auto-generated indices: + OUTPUT/foo/index.html: foo | OUTPUT/foo +""" + +from collections import defaultdict +from dataclasses import dataclass, field from os import path from subprocess import run from sys import argv, exit +from typing import List, Set + + +@dataclass +class Directory: + files: List[str] = field(default_factory=list) + subfolders: Set[str] = field(default_factory=set) def parse_arguments(args): @@ -14,6 +40,7 @@ def parse_arguments(args): def find_sources(top_dir): p = run( + # TODO: use git ls-files ('find', top_dir, '-name', '*.md', '-o', '-name', '*.org'), capture_output=True, check=True, text=True ) @@ -21,41 +48,60 @@ def find_sources(top_dir): return p.stdout.splitlines() -def html_path(source_path, top_dir, out_dir): - fname = path.basename(source_path) - dname = path.dirname(source_path) +def compute_directories(files, top_dir): + directories = defaultdict(Directory) - name, _ = path.splitext(fname) + for f in files: + fpath, fname = path.split(f) + fdir = ( + path.relpath(fpath, top_dir) + if fpath != top_dir + else '' + ) - if name == 'README': - name = 'index' + directories[fdir].files.append(fname) - return path.join( - out_dir, path.relpath(dname, top_dir), f'{name}.html' - ) + if fdir: + parent, child = path.split(fdir) + directories[parent].subfolders.add(child) + + return directories -def write_dependencies(output, sources, top_dir, out_dir): - pages = [] +def write_dependencies(deps, directories, top_dir, out_dir): + pages = list() - for src in sources: - html = html_path(src, top_dir, out_dir) - html_dir = path.dirname(html) + for dpath, d in directories.items(): - print(f'{html}: {src} | {html_dir}', file=output) + for f in d.files: + name, _ = path.splitext(f) + if name == 'README': + name = 'index' - pages.append(html) + html_dir = ( + path.join(out_dir, dpath) + if dpath + else out_dir + ) - print(file=output) - print(f'pages = {" ".join(pages)}', file=output) + html_path = path.join(html_dir, name+'.html') + src_path = path.join(top_dir, dpath, f) + print(f'{html_path}: {src_path} | {html_dir}', file=deps) + pages.append(html_path) -def main(argv): - top_dir, out_dir = parse_arguments(argv) + print(file=deps) + print(f'pages = {" ".join(pages)}', file=deps) + + +def main(arguments): + top_dir, out_dir = parse_arguments(arguments) source_files = find_sources(top_dir) + directories = compute_directories(source_files, top_dir) + with open('deps.mk', 'w') as deps: - write_dependencies(deps, source_files, top_dir, out_dir) + write_dependencies(deps, directories, top_dir, out_dir) if __name__ == '__main__': -- cgit v1.2.3