From 1e076698ac2e3e5fe18b1c7e389a1ca70212d100 Mon Sep 17 00:00:00 2001 From: Christie Wilson Date: Tue, 17 Jul 2018 12:00:21 -0700 Subject: [PATCH] Fail if python script not found; run from any dir If the script was run from anywhere other than the directory above hack, the python script could not be found, however since the error code was ignored, this would pass. Finding files and ignoring failed files was being done in one line. We wanted to ignore errors from grep, however this has the side effect of ignoring errors from the python script, e.g. if this script is run from an unexpected directory and the python script is not found. Lastly, filtering of dirs was being done in both `boilerplate.sh` and `boilerplate.py` so now it will just be done in `boilerplate.sh`. --- hack/boilerplate.sh | 13 ++++++------- hack/boilerplate/boilerplate.py | 25 ++++++++++--------------- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/hack/boilerplate.sh b/hack/boilerplate.sh index 22bf8fb660d..c46232c68b2 100755 --- a/hack/boilerplate.sh +++ b/hack/boilerplate.sh @@ -13,14 +13,13 @@ # 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. - -# Ignore these paths in the following tests. -ignore="vendor\|out\|testdata" -BOILERPLATEDIR=./hack/boilerplate -# Grep returns a non-zero exit code if we don't match anything, which is good in this case. -set +e -files=$(python ${BOILERPLATEDIR}/boilerplate.py --rootdir . --boilerplate-dir ${BOILERPLATEDIR} | grep -v $ignore) set -e + +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) +BOILERPLATEDIR=$DIR/boilerplate + +files=$(python ${BOILERPLATEDIR}/boilerplate.py --rootdir . --boilerplate-dir ${BOILERPLATEDIR}) + if [[ ! -z ${files} ]]; then echo "Boilerplate missing in:" echo "${files}" diff --git a/hack/boilerplate/boilerplate.py b/hack/boilerplate/boilerplate.py index 3567c90e33f..a67ec1687ad 100644 --- a/hack/boilerplate/boilerplate.py +++ b/hack/boilerplate/boilerplate.py @@ -22,6 +22,10 @@ import re import sys + +SKIPPED_DIRS = ["Godeps", "third_party", ".git", "vendor", "examples", "testdata"] +SKIPPED_FILES = ["install_golint.sh"] + parser = argparse.ArgumentParser() parser.add_argument("filenames", help="list of files to check, all files if unspecified", nargs='*') @@ -103,17 +107,11 @@ def file_passes(filename, refs, regexs): def file_extension(filename): return os.path.splitext(filename)[1].split(".")[-1].lower() -skipped_dirs = ['Godeps', 'third_party', '.git', "vendor", "examples", './hack/install_golint.sh'] - def normalize_files(files): newfiles = [] - for pathname in files: - if any(x in pathname for x in skipped_dirs): - continue - newfiles.append(pathname) - for i, pathname in enumerate(newfiles): + for i, pathname in enumerate(files): if not os.path.isabs(pathname): - newfiles[i] = os.path.join(args.rootdir, pathname) + newfiles.append(os.path.join(args.rootdir, pathname)) return newfiles def get_files(extensions): @@ -122,17 +120,14 @@ def get_files(extensions): files = args.filenames else: for root, dirs, walkfiles in os.walk(args.rootdir): - # don't visit certain dirs. This is just a performance improvement - # as we would prune these later in normalize_files(). But doing it - # cuts down the amount of filesystem walking we do and cuts down - # the size of the file list - for d in skipped_dirs: + for d in SKIPPED_DIRS: if d in dirs: dirs.remove(d) for name in walkfiles: - pathname = os.path.join(root, name) - files.append(pathname) + if name not in SKIPPED_FILES: + pathname = os.path.join(root, name) + files.append(pathname) files = normalize_files(files) outfiles = []