//go:build tools // +build tools /* Copyright (c) 2019, Percona LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ // check-license checks that the license header in all files matches the copyright text below. package main import ( "flag" "fmt" "io" "log" "os" "path/filepath" "regexp" ) var ( generatedHeader = regexp.MustCompile(`^// Code generated .* DO NOT EDIT\.`) copyrightText = `/* Copyright (c) 2019, Percona LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ ` copyrightPattern = regexp.MustCompile(`^/\* Copyright \(c\) 20\d{2}, Percona LLC\. All rights reserved\. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: \* Redistributions of source code must retain the above copyright notice, this {2}list of conditions and the following disclaimer. \* Redistributions in binary form must reproduce the above copyright notice, {2}this list of conditions and the following disclaimer in the documentation {2}and/or other materials provided with the distribution. \* Neither the name of the copyright holder nor the names of its {2}contributors may be used to endorse or promote products derived from {2}this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES \(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION\) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \(INCLUDING NEGLIGENCE OR OTHERWISE\) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\. \*/ `, ) ) func checkHeader(path string) bool { f, err := os.Open(path) if err != nil { log.Fatal(err) } defer f.Close() actual := make([]byte, len(copyrightText)) _, err = io.ReadFull(f, actual) if err == io.ErrUnexpectedEOF { err = nil // some files are shorter than license header } if err != nil { log.Printf("%s - %s", path, err) return false } if string(copyrightText) == string(actual) { return true } if generatedHeader.Match(actual) { return true } if !copyrightPattern.Match(actual) { if !checkBuildIgnoreHeader(path) { log.Print(path) return false } return true } return true } func checkBuildIgnoreHeader(path string) bool { f, err := os.Open(path) if err != nil { log.Fatal(err) } defer f.Close() headerPattern := regexp.MustCompile(`^//go:build tools // \+build tools ` + copyrightPattern.String()) headerText := `//go:build tools // +build tools ` + copyrightText actual := make([]byte, len(headerText)) _, err = io.ReadFull(f, actual) if err == io.ErrUnexpectedEOF { err = nil // some files are shorter than license header } if err != nil { log.Printf("%s - %s", path, err) return false } if string(headerText) == string(actual) { return true } if !headerPattern.Match(actual) { log.Print(path) return false } return true } func main() { log.SetFlags(0) flag.Usage = func() { fmt.Fprintln(flag.CommandLine.Output(), "Usage: go run .github/check-license.go") flag.CommandLine.PrintDefaults() } flag.Parse() ok := true filepath.Walk(".", func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { switch info.Name() { case ".git", "vendor": return filepath.SkipDir default: return nil } } if filepath.Ext(info.Name()) == ".go" { if !checkHeader(path) { ok = false } } return nil }) if ok { os.Exit(0) } log.Print("Please update license header in those files.") os.Exit(1) }