diff options
| author | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-03-15 13:14:36 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-03-15 13:14:36 +0100 |
| commit | 4914800729158cfc6de90b3964c222c4ab225e8e (patch) | |
| tree | 84811e7127c5280bb6dfdfe495af3abcabbaa32a /repo/www | |
| parent | 039dc0e133229482ff93aac1eac07e1d06465d2f (diff) | |
| download | memory-leaks-4914800729158cfc6de90b3964c222c4ab225e8e.tar.xz | |
Use git-ls-files(1) to compute list of source files
It's not clear whether or not Python supports '\0' for null: the
reference[1] says nothing about this specific escape sequence, but
Python 3.7 seems to recognize it…
repr('\0') says "'\\x00'", so let's use that.
[1]: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Diffstat (limited to 'repo/www')
| -rw-r--r-- | repo/www/Makefile | 2 | ||||
| -rwxr-xr-x | repo/www/make-deps.py | 49 |
2 files changed, 17 insertions, 34 deletions
diff --git a/repo/www/Makefile b/repo/www/Makefile index ca50752..8f5bb2a 100644 --- a/repo/www/Makefile +++ b/repo/www/Makefile @@ -16,7 +16,7 @@ dependencies = deps.mk include $(dependencies) $(dependencies): make-deps.py $(text_folders) - python3 $< "$(TEXT_FILES)" $(TOP_DIR) $(OUT_DIR) + python3 $< "$(TEXT_FILES)" $(OUT_DIR) site: $(pages) diff --git a/repo/www/make-deps.py b/repo/www/make-deps.py index 189ccfd..c2a88e1 100755 --- a/repo/www/make-deps.py +++ b/repo/www/make-deps.py @@ -20,10 +20,11 @@ We want to compute: 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 +from git import Repo + @dataclass class Directory: @@ -32,45 +33,23 @@ class Directory: def parse_arguments(args): - if len(args) != 4: - exit(f'Usage: {argv[0]} EXTENSIONS TOP-DIR OUTPUT-DIR') - - return argv[1].split(), argv[2], argv[3] - - -def join(collections, joiner): - out = [] - - for c in collections[:-1]: - out.extend(c) - out.append(joiner) + if len(args) != 3: + exit(f'Usage: {argv[0]} EXTENSIONS OUTPUT-DIR') - out.extend(collections[-1]) - return tuple(out) + return argv[1].split(), argv[2] -def find_sources(extensions, top_dir): - filters = tuple(('-name', '*.'+ext) for ext in extensions) - - p = run( - # TODO: use git ls-files - ('find', top_dir) + join(filters, '-o'), - capture_output=True, check=True, text=True - ) - - return p.stdout.splitlines() +def find_sources(extensions, repository): + patterns = (f'**.{ext}' for ext in extensions) + zero = '\x00' + return repository.git.ls_files('-z', *patterns).strip(zero).split(zero) def compute_directories(files, top_dir): directories = defaultdict(Directory) for f in files: - fpath, fname = path.split(f) - fdir = ( - path.relpath(fpath, top_dir) - if fpath != top_dir - else '' - ) + fdir, fname = path.split(f) directories[fdir].files.append(fname) @@ -108,8 +87,12 @@ def write_dependencies(deps, directories, top_dir, out_dir): def main(arguments): - extensions, top_dir, out_dir = parse_arguments(arguments) - source_files = find_sources(extensions, top_dir) + extensions, out_dir = parse_arguments(arguments) + + repository = Repo(search_parent_directories=True) + top_dir = repository.working_dir + + source_files = find_sources(extensions, repository) directories = compute_directories(source_files, top_dir) |
