1// Copyright 2017 Google 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// The license_serializer program normalizes and serializes the known 16// licenseclassifier licenses into a compressed archive. The hash values for 17// the licenses are calculated and added to the archive. These can then be used 18// to determine where in unknown text is a good offset to run through the 19// Levenshtein Distance algorithm. 20package main 21 22import ( 23 "flag" 24 "fmt" 25 "log" 26 "os" 27 "path/filepath" 28 "strings" 29 30 "github.com/google/licenseclassifier" 31 "github.com/google/licenseclassifier/serializer" 32) 33 34var ( 35 forbiddenOnly = flag.Bool("forbidden", false, "serialize only forbidden licenses") 36 outputDir = flag.String("output", "", "output directory") 37) 38 39func init() { 40 flag.Usage = func() { 41 fmt.Fprintf(os.Stderr, `Usage: %s [OPTIONS] 42 43Calculate the hash values for files and serialize them into a database. 44See go/license-classifier 45 46Options: 47`, filepath.Base(os.Args[0])) 48 flag.PrintDefaults() 49 } 50} 51 52func main() { 53 flag.Parse() 54 55 archiveName := licenseclassifier.LicenseArchive 56 if *forbiddenOnly { 57 archiveName = licenseclassifier.ForbiddenLicenseArchive 58 } 59 60 fn := filepath.Join(*outputDir, archiveName) 61 out, err := os.Create(fn) 62 if err != nil { 63 log.Fatalf("error: cannot create file %q: %v", fn, err) 64 } 65 defer out.Close() 66 67 lics, err := licenseclassifier.ReadLicenseDir() 68 if err != nil { 69 log.Fatalf("error: cannot read licenses directory: %v", err) 70 } 71 72 var licenses []string 73 for _, lic := range lics { 74 if !*forbiddenOnly || licenseclassifier.LicenseType(strings.TrimSuffix(lic.Name(), ".txt")) == "FORBIDDEN" { 75 licenses = append(licenses, lic.Name()) 76 } 77 } 78 79 if err := serializer.ArchiveLicenses(licenses, out); err != nil { 80 log.Fatalf("error: cannot create database: %v", err) 81 } 82} 83