github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/utils/3pdeps/main.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"bytes"
    19  	"crypto/sha512"
    20  	"encoding/hex"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"log"
    26  	"os"
    27  	"path"
    28  	"sort"
    29  )
    30  
    31  type Module struct {
    32  	Path    string  `json:",omitempty"`
    33  	Replace *Module `json:",omitempty"`
    34  	Main    bool    `json:",omitempty"`
    35  	Dir     string  `json:",omitempty"`
    36  }
    37  
    38  type Package struct {
    39  	Root     string  `json:",omitempty"`
    40  	Module   *Module `json:",omitempty"`
    41  	Standard bool    `json:",omitempty"`
    42  }
    43  
    44  func main() {
    45  	var verifyFilename *string
    46  	if len(os.Args) > 1 {
    47  		if os.Args[1] == "-verify" {
    48  			if len(os.Args) != 3 {
    49  				fmt.Printf("Usage: 3pdeps [-verify EXISTING_LICENSES_FILENAME]\n")
    50  				os.Exit(1)
    51  			}
    52  			verifyFilename = &os.Args[2]
    53  		} else {
    54  			fmt.Printf("Usage: 3pdeps [-verify EXISTING_LICENSES_FILENAME]\n")
    55  			os.Exit(1)
    56  		}
    57  	}
    58  
    59  	d := json.NewDecoder(os.Stdin)
    60  	var root string
    61  	var mods []string
    62  	var modPkgs map[string][]Package = make(map[string][]Package)
    63  	for {
    64  		var dep Package
    65  		if err := d.Decode(&dep); err == io.EOF {
    66  			break
    67  		} else if err != nil {
    68  			log.Fatalf("Error reading `go list` stdin input: %v\n", err)
    69  		}
    70  		if dep.Standard {
    71  			root = dep.Root
    72  		} else if dep.Module != nil {
    73  			if dep.Module.Main {
    74  			} else if dep.Module.Replace != nil {
    75  				mods = append(mods, dep.Module.Replace.Path)
    76  				modPkgs[dep.Module.Replace.Path] = append(modPkgs[dep.Module.Replace.Path], dep)
    77  			} else {
    78  				mods = append(mods, dep.Module.Path)
    79  				modPkgs[dep.Module.Path] = append(modPkgs[dep.Module.Path], dep)
    80  			}
    81  		} else {
    82  			log.Fatalf("Unexpected dependency read from stdin; not gosdk, not main module, not a module.\n")
    83  		}
    84  	}
    85  
    86  	var out io.Writer = os.Stdout
    87  	if verifyFilename != nil {
    88  		out = &bytes.Buffer{}
    89  	}
    90  
    91  	PrintDoltLicense(out)
    92  	PrintGoLicense(out, root)
    93  
    94  	sort.Strings(mods)
    95  	var l string
    96  	for _, k := range mods {
    97  		if k != l {
    98  			m := modPkgs[k][0].Module
    99  			dir := m.Dir
   100  			if m.Replace != nil {
   101  				dir = m.Replace.Dir
   102  			}
   103  			PrintPkgLicense(out, k, dir)
   104  		}
   105  		l = k
   106  	}
   107  
   108  	if verifyFilename != nil {
   109  		verifyFile, err := os.Open(*verifyFilename)
   110  		if err != nil {
   111  			log.Fatalf("Error opening -verify file %s: %v\n", *verifyFilename, err)
   112  		}
   113  		verifyContents, err := ioutil.ReadAll(verifyFile)
   114  		if err != nil {
   115  			log.Fatalf("Error reading -verify file %s: %v\n", *verifyFilename, err)
   116  		}
   117  		if !bytes.Equal(out.(*bytes.Buffer).Bytes(), verifyContents) {
   118  			fmt.Printf("Difference found between current output and %s\n", *verifyFilename)
   119  			fmt.Printf("Please run ./Godeps/update.sh and check in the results.\n")
   120  			os.Exit(1)
   121  		}
   122  	}
   123  
   124  }
   125  
   126  func PrintDoltLicense(out io.Writer) {
   127  	fmt.Fprintf(out, "================================================================================\n")
   128  	fmt.Fprintf(out, "= Dolt licensed under: =\n\n")
   129  	PrintLicense(out, "../LICENSE")
   130  	fmt.Fprintf(out, "================================================================================\n")
   131  }
   132  
   133  func PrintGoLicense(out io.Writer, root string) {
   134  	filepath := FindLicenseFile(root, []string{"LICENSE", "../LICENSE"})
   135  	fmt.Fprintf(out, "\n================================================================================\n")
   136  	fmt.Fprintf(out, "= Go standard library licensed under: =\n\n")
   137  	PrintLicense(out, filepath)
   138  	fmt.Fprintf(out, "================================================================================\n")
   139  }
   140  
   141  var StandardCandidates = []string{
   142  	"LICENSE",
   143  	"LICENSE.txt",
   144  	"LICENSE.md",
   145  	"COPYING",
   146  	"LICENSE-MIT",
   147  }
   148  
   149  func PrintPkgLicense(out io.Writer, pkg string, dir string) {
   150  	filepath := FindLicenseFile(dir, StandardCandidates)
   151  	fmt.Fprintf(out, "\n================================================================================\n")
   152  	fmt.Fprintf(out, "= %v licensed under: =\n\n", pkg)
   153  	PrintLicense(out, filepath)
   154  	fmt.Fprintf(out, "================================================================================\n")
   155  }
   156  
   157  func FindLicenseFile(dir string, candidates []string) string {
   158  	for _, c := range candidates {
   159  		if _, err := os.Stat(dir + "/" + c); err == nil {
   160  			return dir + "/" + c
   161  		}
   162  	}
   163  	log.Fatalf("Required license not found in directory %s", dir)
   164  	// Unreachable
   165  	return ""
   166  }
   167  
   168  func PrintLicense(out io.Writer, filepath string) {
   169  	f, err := os.Open(filepath)
   170  	if err != nil {
   171  		log.Fatalf("Error opening license file [%s] for copying: %v\n", filepath, err)
   172  	}
   173  	contents, err := ioutil.ReadAll(f)
   174  	if err != nil {
   175  		log.Fatalf("Error reading license file [%s] for copying: %v\n", filepath, err)
   176  	}
   177  	_, err = out.Write(contents)
   178  	if err != nil {
   179  		log.Fatalf("Error writing license file contents to out: %v", err)
   180  	}
   181  	base := path.Base(filepath)
   182  	sum := sha512.Sum512_224(contents)
   183  	fmt.Fprintf(out, "\n= %v %v =\n", base, hex.EncodeToString(sum[:]))
   184  }