| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "go/format" |
| 7 | "os" |
| 8 | "strings" |
| 9 | ) |
| 10 | |
| 11 | type ExceptionData struct { |
| 12 | Version string `json:"licenseListVersion"` |
| 13 | Exceptions []Exception `json:"exceptions"` |
| 14 | } |
| 15 | |
| 16 | type Exception struct { |
| 17 | Reference string `json:"reference"` |
| 18 | IsDeprecated bool `json:"isDeprecatedLicenseId"` |
| 19 | DetailsURL string `json:"detailsUrl"` |
| 20 | ReferenceNumber int `json:"referenceNumber"` |
| 21 | Name string `json:"name"` |
| 22 | LicenseID string `json:"licenseExceptionId"` |
| 23 | SeeAlso []string `json:"seeAlso"` |
| 24 | IsOsiApproved bool `json:"isOsiApproved"` |
| 25 | } |
| 26 | |
| 27 | // extractExceptionLicenseIDs read official exception licenses file copied from spdx/license-list-data |
| 28 | // and write file exception_license_ids.json containing just the non-deprecated exception license IDs. |
| 29 | // NOTE: For now, this function ignores the deprecated exception licenses. |
| 30 | func extractExceptionLicenseIDs() error { |
| 31 | // open file |
| 32 | file, err := os.Open("exceptions.json") |
| 33 | if err != nil { |
| 34 | fmt.Println(err) |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // read in all licenses marshalled into a slice of exception structs |
| 39 | var exceptionData ExceptionData |
| 40 | err = json.NewDecoder(file).Decode(&exceptionData) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | |
| 45 | // create slice of exception license IDs that are not deprecated |
| 46 | var exceptionLicenseIDs []string |
| 47 | for _, e := range exceptionData.Exceptions { |
| 48 | if !e.IsDeprecated { |
| 49 | exceptionLicenseIDs = append(exceptionLicenseIDs, e.LicenseID) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // generate the GetExceptions() function in get_exceptions.go |
| 54 | getExceptionsContents := []byte(`package spdxlicenses |
| 55 | |
| 56 | // Code generated by go-spdx cmd/exceptions.go. DO NOT EDIT. |
| 57 | // Source: https://github.com/spdx/license-list-data specifies official SPDX license list. |
| 58 | |
| 59 | import "strings" |
| 60 | |
| 61 | // IsException does a case-insensitive lookup for the exception id in the exceptions map. |
| 62 | // It returns true and the case-sensitive ID if found, otherwise false and the original id. |
| 63 | func IsException(id string) (bool, string) { |
| 64 | foundID, ok := exceptionsMap[strings.ToUpper(id)] |
| 65 | if ok { |
| 66 | return true, foundID |
| 67 | } |
| 68 | return false, id |
| 69 | } |
| 70 | |
| 71 | // GetExceptionsMap returns a map of exception license IDs keyed by uppercase ID. |
| 72 | func GetExceptionsMap() map[string]string { |
| 73 | copied := make(map[string]string, len(exceptionsMap)) |
| 74 | for k, v := range exceptionsMap { |
| 75 | copied[k] = v |
| 76 | } |
| 77 | return copied |
| 78 | } |
| 79 | |
| 80 | // GetExceptions returns a slice of exception license IDs. |
| 81 | func GetExceptions() []string { |
| 82 | return []string{ |
| 83 | `) |
| 84 | for _, id := range exceptionLicenseIDs { |
| 85 | getExceptionsContents = append(getExceptionsContents, ` "`+id+`", |
| 86 | `...) |
| 87 | } |
| 88 | getExceptionsContents = append(getExceptionsContents, ` } |
| 89 | } |
| 90 | |
| 91 | `...) |
| 92 | |
| 93 | getExceptionsContents = append(getExceptionsContents, `var exceptionsMap = map[string]string{ |
| 94 | `...) |
| 95 | for _, id := range exceptionLicenseIDs { |
| 96 | getExceptionsContents = append(getExceptionsContents, ` "`+strings.ToUpper(id)+`": "`+id+`", |
| 97 | `...) |
| 98 | } |
| 99 | getExceptionsContents = append(getExceptionsContents, `} |
| 100 | `...) |
| 101 | |
| 102 | getExceptionsContents, err = format.Source(getExceptionsContents) |
| 103 | if err != nil { |
| 104 | return fmt.Errorf("format generated get_exceptions.go: %w", err) |
| 105 | } |
| 106 | |
| 107 | err = os.WriteFile("../spdxexp/spdxlicenses/get_exceptions.go", getExceptionsContents, 0600) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | fmt.Println("Writing `../spdxexp/spdxlicenses/get_exceptions.go`... COMPLETE") |
| 112 | |
| 113 | return nil |
| 114 | } |