-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathyapf.sh
executable file
·132 lines (115 loc) · 4.17 KB
/
yapf.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#! /usr/bin/env bash
#
# Simple wrapper to run yapf on a directory.
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Update these VERSION variables with the new desired yapf tag when a new
# yapf version is desired.
# See:
# https://github.com/google/yapf/tags
YAPF_VERSION="v0.43.0"
VERSION="yapf 0.43.0"
function main() {
# check for python3
python3 - << _END_
import sys
if sys.version_info.major < 3 or sys.version_info.minor < 8:
exit(1)
_END_
if [ $? = 1 ]; then
echo "Python 3.8 or newer is not installed/enabled."
exit 1
fi
set -e # exit on error
if command -v pip3 &> /dev/null; then
PIP_CMD="pip3"
elif command -v pip &> /dev/null; then
PIP_CMD="pip"
else
echo "pip is not installed."
exit 1
fi
if ! type virtualenv >/dev/null 2>/dev/null
then
${PIP_CMD} install -q virtualenv
fi
if python3 -m venv --help &> /dev/null; then
VENV_LIB="venv"
elif python3 -m virtualenv --help &> /dev/null; then
VENV_LIB="virtualenv"
else
echo "Neither venv nor virtualenv is available."
exit 1
fi
REPO_ROOT=$(cd $(dirname $0) && git rev-parse --show-toplevel)
GIT_DIR=$(git rev-parse --absolute-git-dir)
YAPF_VENV=${YAPF_VENV:-${GIT_DIR}/fmt/yapf_${YAPF_VERSION}_venv}
if [ ! -e ${YAPF_VENV} ]
then
python3 -m ${VENV_LIB} ${YAPF_VENV}
fi
source ${YAPF_VENV}/bin/activate
${PIP_CMD} install -q --upgrade pip
${PIP_CMD} install -q "yapf==${YAPF_VERSION}"
ver=$(yapf --version 2>&1)
if [ "$ver" != "$VERSION" ]
then
echo "Wrong version of yapf!"
echo "Expected: \"${VERSION}\", got: \"${ver}\""
exit 1
fi
DIR=${@:-.}
# Only run yapf on tracked files. This saves time and possibly avoids
# formatting files the user doesn't want formatted.
tmp_dir=$(mktemp -d -t tracked-git-files.XXXXXXXXXX)
files=${tmp_dir}/git_files.txt
files_filtered=${tmp_dir}/git_files_filtered.txt
git ls-tree -r HEAD --name-only ${DIR} | grep -vE "lib/yamlcpp" > ${files}
# Add to the above any newly added staged files.
git diff --cached --name-only --diff-filter=A >> ${files}
# Keep this list of Python extensions the same with the list of
# extensions searched for in the tools/git/pre-commit hook.
grep -E '\.py$|\.cli.ext$|\.test.ext$' ${files} > ${files_filtered}
# Add back in the tools Python scripts without a .py extension.
grep -rl '#!.*python' "${REPO_ROOT}/tools" | grep -vE '(yapf.sh|.py)' | sed "s:${REPO_ROOT}/::g" >> ${files_filtered}
# Prepend the filenames with "./" to make the modified file output consistent
# with the clang-format target output.
sed -i'.bak' 's:^:\./:' ${files_filtered}
rm -f ${files_filtered}.bak
# Efficiently retrieving modification timestamps in a platform
# independent way is challenging. We use find's -newer argument, which
# seems to be broadly supported. The following file is created and has a
# timestamp just before running yapf. Any file with a timestamp
# after this we assume was modified by yapf.
start_time_file=${tmp_dir}/format_start.$$
touch ${start_time_file}
YAPF_CONFIG=${REPO_ROOT}/.style.yapf
yapf \
--style ${YAPF_CONFIG} \
--parallel \
--in-place \
$(cat ${files_filtered})
find $(cat ${files_filtered}) -newer ${start_time_file}
rm -rf ${tmp_dir}
deactivate
}
if [[ "$(basename -- "$0")" == 'yapf.sh' ]]; then
main "$@"
else
GIT_DIR=$(git rev-parse --absolute-git-dir)
YAPF_VENV=${YAPF_VENV:-${GIT_DIR}/fmt/yapf_${YAPF_VERSION}_venv}
fi