Skip to content

Commit

Permalink
Fail if python script not found; run from any dir
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
bobcatfish committed Jul 17, 2018
1 parent e6f37af commit 1e07669
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
13 changes: 6 additions & 7 deletions hack/boilerplate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
25 changes: 10 additions & 15 deletions hack/boilerplate/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='*')

Expand Down Expand Up @@ -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):
Expand All @@ -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 = []
Expand Down

0 comments on commit 1e07669

Please sign in to comment.