github.com/mem/u-root@v2.0.1-0.20181004165302-9b18b4636a33+incompatible/scripts/checklicenses/checklicenses.go (about)

     1  // Copyright 2017-2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Run with `go run checklicenses.go`. This script has one drawback:
     6  // - It does not correct the licenses; it simply outputs a list of files which
     7  //   do not conform and returns 1 if the list is non-empty.
     8  package main
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"log"
    15  	"os"
    16  	"os/exec"
    17  	"regexp"
    18  	"strings"
    19  )
    20  
    21  var absPath = flag.Bool("a", false, "Print absolute paths")
    22  
    23  const uroot = "$GOPATH/src/github.com/u-root/u-root"
    24  
    25  // The first few lines of every go file is expected to contain this license.
    26  var license = regexp.MustCompile(
    27  	`^// Copyright [\d\-, ]+ the u-root Authors\. All rights reserved
    28  // Use of this source code is governed by a BSD-style
    29  // license that can be found in the LICENSE file\.
    30  `)
    31  
    32  type rule struct {
    33  	*regexp.Regexp
    34  	invert bool
    35  }
    36  
    37  func accept(s string) rule {
    38  	return rule{
    39  		regexp.MustCompile("^" + s + "$"),
    40  		false,
    41  	}
    42  }
    43  
    44  func reject(s string) rule {
    45  	return rule{
    46  		regexp.MustCompile("^" + s + "$"),
    47  		true,
    48  	}
    49  }
    50  
    51  // A file is checked iff all the accepts and none of the rejects match.
    52  var rules = []rule{
    53  	accept(`.*\.go`),
    54  	reject(`vendor/.*`),       // Various authors
    55  	reject(`cmds/dhcp/.*`),    // Graham King
    56  	reject(`cmds/elvish/.*`),  // elvish developers and contributors
    57  	reject(`cmds/ldd/.*`),     // Go authors
    58  	reject(`cmds/ping/.*`),    // Go authors
    59  	reject(`xcmds/ectool/.*`), // Chromium authors
    60  
    61  	reject(`pkg/diskboot/entrytype_string.go`), // generated
    62  }
    63  
    64  func main() {
    65  	flag.Parse()
    66  	uroot := os.ExpandEnv(uroot)
    67  	incorrect := []string{}
    68  
    69  	// List files added to u-root.
    70  	out, err := exec.Command("git", "ls-files").Output()
    71  	if err != nil {
    72  		log.Fatalln("error running git ls-files:", err)
    73  	}
    74  	files := strings.Fields(string(out))
    75  
    76  	// Iterate over files.
    77  outer:
    78  	for _, file := range files {
    79  		// Test rules.
    80  		trimmedPath := strings.TrimPrefix(file, uroot)
    81  		for _, r := range rules {
    82  			if r.MatchString(trimmedPath) == r.invert {
    83  				continue outer
    84  			}
    85  		}
    86  
    87  		// Make sure it is not a directory.
    88  		info, err := os.Stat(file)
    89  		if err != nil {
    90  			log.Fatalln("cannot stat", file, err)
    91  		}
    92  		if info.IsDir() {
    93  			continue
    94  		}
    95  
    96  		// Read from the file.
    97  		r, err := os.Open(file)
    98  		if err != nil {
    99  			log.Fatalln("cannot open", file, err)
   100  		}
   101  		defer r.Close()
   102  		contents, err := ioutil.ReadAll(r)
   103  		if err != nil {
   104  			log.Fatalln("cannot read", file, err)
   105  		}
   106  		if !license.Match(contents) {
   107  			p := trimmedPath
   108  			if *absPath {
   109  				p = file
   110  			}
   111  			incorrect = append(incorrect, p)
   112  		}
   113  	}
   114  	if err != nil {
   115  		log.Fatal(err)
   116  	}
   117  
   118  	// Print files with incorrect licenses.
   119  	if len(incorrect) > 0 {
   120  		fmt.Println(strings.Join(incorrect, "\n"))
   121  		os.Exit(1)
   122  	}
   123  }