diff options
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/notsd-fixup | 32 | ||||
-rwxr-xr-x | tools/notsd-fixup--includes | 46 |
2 files changed, 39 insertions, 39 deletions
diff --git a/tools/notsd-fixup b/tools/notsd-fixup index 1d4287b259..4bd4b3f94c 100755 --- a/tools/notsd-fixup +++ b/tools/notsd-fixup @@ -1,11 +1,9 @@ #!/usr/bin/env bash # Copyright (C) 2015-2016 Luke Shumaker -# The reason we do `find`/`while read`-loops instead of `find -exec` commands -# is that we want errors from the inner loop to bubble up. - fixup_makefiles() ( find "$@" -type f -name Makefile | while read -r filename; do + >&2 printf ' => fixup %q\n' "$filename" { <"$filename" sed -r \ -e "s|(/\.\.)*/config.mk|/$(realpath -ms --relative-to="$(dirname -- "$filename")" config.mk)|" \ @@ -34,27 +32,13 @@ fixup_includes() ( done fi - find "$@" \( -name '*.h' -o -name '*.c' -o -name '*.gperf' \) -type f | while read -r filename; do - false - # We copy the write-ifchanged logic to here, because we have a - # higher-than usual chance of the main command failing. In a - # Makefile we would handle this by setting .DELETE_ON_ERROR:, - # but we can't do that here, so we have to inter-mingle the - # logics. - local outfile="$filename" - tmpfile="$(dirname "$outfile")/.tmp.${outfile##*/}.tmp" - local r=0 - "$0"--includes "$filename" > "$tmpfile" || r=$? - if [[ $r != 0 ]]; then - rm -f "$tmpfile" || : - (exit $r) - fi - if cmp -s "$tmpfile" "$outfile"; then - rm -f "$tmpfile" || : - else - mv -f "$tmpfile" "$outfile" - fi - done + # We wrap the $0--includes program with `sh` because xargs only exits + # early if the status is 255, but we want to exit early for all + # non-zero statuses. We use xargs instead of -exec because -exec won't + # do much of anything useful with the exit status. + rm -rf -- "$0"--includes.cache + find "$@" \( -name '*.h' -o -name '*.c' -o -name '*.gperf' -o -name '*.gperf.m4' \) -type f -print0 | + xargs -r0 sh -c "$0--includes \"\$@\" || exit 255" -- rm -rf -- "$0"--includes.cache ) diff --git a/tools/notsd-fixup--includes b/tools/notsd-fixup--includes index 196fd488a3..d16a3e6500 100755 --- a/tools/notsd-fixup--includes +++ b/tools/notsd-fixup--includes @@ -10,6 +10,8 @@ # (easy since `tools/notsd-move` runs it on the entire repo and # puts the results in git history). +import atexit +import filecmp import json import os import re @@ -225,7 +227,7 @@ class IncludeSection: ################################################################ # The main program loop -def phase0(cache, filename, line): +def phase0(cache, filename, line, file=sys.stdout): global phase phase = phase0 @@ -234,9 +236,9 @@ def phase0(cache, filename, line): includes = IncludeSection() phase1(cache, filename, line) else: - print(line) + print(line, file=file) -def phase1(cache, filename, line): +def phase1(cache, filename, line, file=sys.stdout): global phase, includes phase = phase1 @@ -254,25 +256,39 @@ def phase1(cache, filename, line): includes.trailing_nl = '' includes.typedef.append(line) else: - includes.print() + includes.print(file=file) includes = None - phase0(cache, filename, line) + phase0(cache, filename, line, file=file) includes = None phase = phase0 def main(argv): - filename = argv[1] - print(' => {0} {1}'.format( - shlex.quote(__file__), - shlex.quote(filename), - ), file=sys.stderr) cache = Cache(__file__+'.cache') - with open(filename) as f: - for line in f: - phase(cache, filename, line.rstrip('\n')) - if includes: - includes.print() + tmpfilename = '' + def cleanup(): + if tmpfilename != '': + try: + os.unlink(tmpfilename) + except FileNotFoundError: + pass + atexit.register(cleanup) + for filename in argv[1:]: + tmpfilename = os.path.join(os.path.dirname(filename), '.tmp.'+os.path.basename(filename)+'.tmp') + print(' => {0} {1}'.format( + shlex.quote(__file__), + shlex.quote(filename), + ), file=sys.stderr) + with open(tmpfilename, 'w') as tmpfile: + with open(filename) as f: + for line in f: + phase(cache, filename, line.rstrip('\n'), file=tmpfile) + if includes: + includes.print(file=tmpfile) + if not filecmp.cmp(filename, tmpfilename): + os.rename(tmpfilename, filename) + cleanup() + tmpfilename = '' cache.save(__file__+'.cache') if __name__ == '__main__': |