-
Notifications
You must be signed in to change notification settings - Fork 42
/
indent
executable file
·78 lines (64 loc) · 2.76 KB
/
indent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
## ---------------------------------------------------------------------
## This file is part of BurnMan - a thermoelastic and thermodynamic toolkit
## for the Earth and Planetary Sciences
## Copyright (C) 2012 - 2021 by the BurnMan team, released under the GNU
## GPL v2 or later.
## ---------------------------------------------------------------------
if test ! -d burnman -o ! -d tests -o ! -d examples ; then
echo "*** This script must be run from the top-level directory of BurnMan."
exit 1
fi
# collect all source files and process them in batches of 50 files
# with up to 10 in parallel
echo "--- Indenting all BurnMan header and source files"
# Run black autoformatter
find burnman contrib examples misc tests \( -name '*.py' \) -print | xargs -n 1 -P 10 -I {} bash -c 'black "$@"' _ {}
# remove execute permission on source files:
find burnman contrib examples misc tests \( -name '*.py' \) -print | xargs -n 50 -P 10 chmod -x
# convert dos formatted files to unix file format by stripping out
# carriage returns (15=0x0D):
dos_to_unix()
{
f=$1
tr -d '\015' <$f >$f.tmp
diff -q $f $f.tmp >/dev/null || mv $f.tmp $f
rm -f $f.tmp
}
export -f dos_to_unix
find burnman contrib examples misc tests \( -name '*.py' \) -print | xargs -n 1 -P 10 -I {} bash -c 'dos_to_unix "$@"' _ {}
# Remove trailing whitespace from files
remove_trailing_whitespace()
{
f=$1
# awkward tab replacement because of OSX sed, do not change unless you test it on OSX
TAB=$'\t'
sed -e "s/[ $TAB]*$//" $f >$f.tmp
diff -q $f $f.tmp >/dev/null || mv $f.tmp $f
rm -f $f.tmp
}
export -f remove_trailing_whitespace
find burnman contrib examples misc tests \( -name '*.bash' -o -name '*.py' -o -name '*.txt' -o -name '*.tex' \) -print | xargs -n 1 -P 10 -I {} bash -c 'remove_trailing_whitespace "$@"' _ {}
# Ensure only a single newline at end of files
ensure_single_trailing_newline()
{
f=$1
# Remove newlines at end of file
# Check that the current line only contains newlines
# If it doesn't match, print it
# If it does match and we're not at the end of the file,
# append the next line to the current line and repeat the check
# If it does match and we're at the end of the file,
# remove the line.
sed -e :a -e '/^\n*$/{$d;N;};/\n$/ba' $f >$f.tmpi
# Then add a newline to the end of the file
# '$' denotes the end of file
# 'a\' appends the following text (which in this case is nothing)
# on a new line
sed -e '$a\' $f.tmpi >$f.tmp
diff -q $f $f.tmp >/dev/null || mv $f.tmp $f
rm -f $f.tmp $f.tmpi
}
export -f ensure_single_trailing_newline
find burnman contrib examples misc tests \( -name '*.bash' -o -name '*.py' -o -name '*.txt' -o -name '*.tex' \) -print | xargs -n 1 -P 10 -I {} bash -c 'ensure_single_trailing_newline "$@"' _ {}
exit 0