Skip to content

Commit

Permalink
Merge pull request #5544 from duohedron/boilerplate
Browse files Browse the repository at this point in the history
Get rid of python scripts in build process
  • Loading branch information
tstromberg committed Oct 23, 2019
2 parents 98b3aef + f9110b2 commit 989dc02
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 178 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Expand Up @@ -9,12 +9,10 @@ env:
- GOPROXY=https://proxy.golang.org
matrix:
include:
- language: python
name: Check Boilerplate
- language: go
name: Check Boilerplate
env:
- TESTSUITE=boilerplate
before_install:
- pip install flake8 && flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
script: make test

- language: go
Expand Down
164 changes: 164 additions & 0 deletions hack/boilerplate/boilerplate.go
@@ -0,0 +1,164 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.
Licensed 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.
*/

package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)

var (
boilerplatedir = flag.String("boilerplate-dir", ".", "Boilerplate directory for boilerplate files")
rootdir = flag.String("rootdir", "../../", "Root directory to examine")
verbose = flag.Bool("v", false, "Verbose")
skippedPaths = regexp.MustCompile(`Godeps|third_party|_gopath|_output|\.git|cluster/env.sh|vendor|test/e2e/generated/bindata.go|site/themes/docsy`)
windowdNewLine = regexp.MustCompile(`\r`)
txtExtension = regexp.MustCompile(`\.txt`)
goBuildTag = regexp.MustCompile(`(?m)^(// \+build.*\n)+\n`)
shebang = regexp.MustCompile(`(?m)^(#!.*\n)\n*`)
copyright = regexp.MustCompile(`Copyright YEAR`)
copyrightReal = regexp.MustCompile(`Copyright \d{4}`)
)

func main() {
flag.Parse()
refs, err := extensionToBoilerplate(*boilerplatedir)
if err != nil {
log.Fatal(err)
}
if len(refs) == 0 {
log.Fatal("no references in ", *boilerplatedir)
}
files, err := filesToCheck(*rootdir, refs)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
pass, err := filePasses(file, refs[filepath.Ext(file)])
if err != nil {
log.Println(err)
}
if !pass {
path, err := filepath.Abs(file)
if err != nil {
log.Println(err)
}
fmt.Println(path)
}
}

}

// extensionToBoilerplate returns a map of file extension to required boilerplate text.
func extensionToBoilerplate(dir string) (map[string][]byte, error) {
refs := make(map[string][]byte)
files, _ := filepath.Glob(dir + "/*.txt")
for _, filename := range files {
extension := strings.ToLower(filepath.Ext(txtExtension.ReplaceAllString(filename, "")))
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
refs[extension] = windowdNewLine.ReplaceAll(data, nil)
}
if *verbose {
dir, err := filepath.Abs(dir)
if err != nil {
return refs, err
}
fmt.Printf("Found %v boilerplates in %v for the following extensions:", len(refs), dir)
for ext := range refs {
fmt.Printf(" %v", ext)
}
fmt.Println()
}
return refs, nil
}

// filePasses checks whether the processed file is valid. Returning false means that the file does not the proper boilerplate template.
func filePasses(filename string, expectedBoilerplate []byte) (bool, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return false, err
}
data = windowdNewLine.ReplaceAll(data, nil)

extension := filepath.Ext(filename)

// remove build tags from the top of Go files
if extension == ".go" {
data = goBuildTag.ReplaceAll(data, nil)
}

// remove shebang from the top of shell files
if extension == ".sh" {
data = shebang.ReplaceAll(data, nil)
}

// if our test file is smaller than the reference it surely fails!
if len(data) < len(expectedBoilerplate) {
return false, nil
}

data = data[:len(expectedBoilerplate)]

// Search for "Copyright YEAR" which exists in the boilerplate, but shouldn't in the real thing
if copyright.Match(data) {
return false, nil
}

// Replace all occurrences of the regex "Copyright \d{4}" with "Copyright YEAR"
data = copyrightReal.ReplaceAll(data, []byte(`Copyright YEAR`))

return bytes.Equal(data, expectedBoilerplate), nil
}

// filesToCheck returns the list of the filers that will be checked for the boilerplate.
func filesToCheck(rootDir string, extensions map[string][]byte) ([]string, error) {
var outFiles []string
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
// remove current workdir from the beginig of the path in case it matches the skipped path
cwd, _ := os.Getwd()
// replace "\" with "\\" for windows style path
re := regexp.MustCompile(`\\`)
re = regexp.MustCompile(`^` + re.ReplaceAllString(cwd, `\\`))
if !info.IsDir() && !skippedPaths.MatchString(re.ReplaceAllString(filepath.Dir(path), "")) {
if extensions[strings.ToLower(filepath.Ext(path))] != nil {
outFiles = append(outFiles, path)
}
}
return nil
})
if err != nil {
return nil, err
}
if *verbose {
rootDir, err = filepath.Abs(rootDir)
if err != nil {
return outFiles, err
}
fmt.Printf("Found %v files to check in %v\n\n", len(outFiles), rootDir)
}
return outFiles, nil
}
170 changes: 0 additions & 170 deletions hack/boilerplate/boilerplate.py

This file was deleted.

4 changes: 3 additions & 1 deletion hack/boilerplate/fix.sh
Expand Up @@ -21,7 +21,9 @@ function prepend() {
local pattern=$1
local ref=$2
local headers=$3
local files=$(hack/boilerplate/boilerplate.py --rootdir ${ROOT_DIR} | grep -v "$ignore" | grep "$pattern")
pushd hack/boilerplate > /dev/null
local files=$(go run boilerplate.go -rootdir ${ROOT_DIR} -boilerplate-dir ${ROOT_DIR}/hack/boilerplate | grep -v "$ignore" | grep "$pattern")
popd > /dev/null
for f in ${files}; do
echo ${f};
local copyright="$(cat hack/boilerplate/boilerplate.${ref}.txt | sed s/YEAR/$(date +%Y)/g)"
Expand Down
7 changes: 4 additions & 3 deletions test.sh
Expand Up @@ -33,9 +33,10 @@ fi
if [[ "$TESTSUITE" = "boilerplate" ]] || [[ "$TESTSUITE" = "all" ]]
then
echo "= boilerplate ==========================================================="
readonly PYTHON=$(type -P python || echo docker run --rm -it -v $(pwd):/minikube -w /minikube python python)
readonly BDIR="./hack/boilerplate"
missing="$($PYTHON ${BDIR}/boilerplate.py --rootdir . --boilerplate-dir ${BDIR} | egrep -v '/assets.go|/translations.go|/site/themes/|/site/node_modules|\./out|/hugo/' || true)"
readonly ROOT_DIR=$(pwd)
readonly BDIR="${ROOT_DIR}/hack/boilerplate"
cd ${BDIR}
missing="$(go run boilerplate.go -rootdir ${ROOT_DIR} -boilerplate-dir ${BDIR} | egrep -v '/assets.go|/translations.go|/site/themes/|/site/node_modules|\./out|/hugo/' || true)"
if [[ -n "${missing}" ]]; then
echo "boilerplate missing: $missing"
echo "consider running: ${BDIR}/fix.sh"
Expand Down

0 comments on commit 989dc02

Please sign in to comment.