forked from google/boringssl
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcopyright_summary.go
192 lines (174 loc) · 5.06 KB
/
copyright_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2024 The BoringSSL Authors
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//go:build ignore
package main
import (
"bufio"
"cmp"
_ "embed"
"encoding/json"
"flag"
"fmt"
"iter"
"maps"
"os"
"regexp"
"slices"
"strconv"
)
var (
outPath = flag.String("out", "", "The path to write the results in JSON format")
comparePath = flag.String("compare", "", "The path to a JSON file to compare against")
)
func sortedKeyValuePairs[K cmp.Ordered, V any](m map[K]V) iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for _, k := range slices.Sorted(maps.Keys(m)) {
if !yield(k, m[k]) {
return
}
}
}
}
var copyrightRE = regexp.MustCompile(
`Copyright ` +
// Ignore (c) and (C)
`(?:\([cC]\) )?` +
// Capture the starting copyright year.
`([0-9]+)` +
// Ignore ending copyright year. OpenSSL's "copyright consolidation"
// tool rewrites it anyway. We're just interested in looking for which
// start years changed, to manually double-check.
`(?:[-,][0-9]+)?` +
// Some files have a comma after the years.
`,?` +
// Skip spaces.
` *` +
// Capture the name. Stop at punctuation and don't pick up trailing
// spaces. We don't want to pick up things like "All Rights Reserved".
// This does drop things like ", Inc", but this is good enough for a
// summary to double-check an otherwise mostly automated process.
`([-a-zA-Z ]*[-a-zA-Z])`)
type CopyrightInfo struct {
Name string
StartYear int
}
type FileInfo struct {
CopyrightInfos []CopyrightInfo
}
func (f *FileInfo) MergeFrom(other FileInfo) {
f.CopyrightInfos = append(f.CopyrightInfos, other.CopyrightInfos...)
}
func summarize(info FileInfo) map[string]int {
ret := map[string]int{}
for _, c := range info.CopyrightInfos {
name := c.Name
// Apply the same mapping as OpenSSL's "copyright consolidation" script.
if name == "The OpenSSL Project" || name == "Eric Young" {
name = "The OpenSSL Project Authors"
}
if old, ok := ret[name]; !ok || old > c.StartYear {
ret[name] = c.StartYear
}
}
return ret
}
func process(path string) (info FileInfo, err error) {
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
m := copyrightRE.FindStringSubmatch(scanner.Text())
if m == nil {
continue
}
var year int
year, err = strconv.Atoi(m[1])
if err != nil {
err = fmt.Errorf("error parsing year %q: %s", m[1], err)
return
}
info.CopyrightInfos = append(info.CopyrightInfos, CopyrightInfo{Name: m[2], StartYear: year})
}
err = scanner.Err()
return
}
func main() {
flag.Parse()
infos := map[string]FileInfo{}
for _, path := range flag.Args() {
info, err := process(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Error processing %q: %s\n", path, err)
os.Exit(1)
}
infos[path] = info
}
if len(*outPath) != 0 {
data, err := json.Marshal(infos)
if err != nil {
fmt.Fprintf(os.Stderr, "Error serializing results: %s\n", err)
os.Exit(1)
}
if err := os.WriteFile(*outPath, data, 0666); err != nil {
fmt.Fprintf(os.Stderr, "Error writing results: %s\n", err)
os.Exit(1)
}
}
if len(*comparePath) == 0 {
// Print what we have and return.
for path, info := range sortedKeyValuePairs(infos) {
for _, c := range info.CopyrightInfos {
fmt.Printf("%s: %d %s\n", path, c.StartYear, c.Name)
}
}
return
}
oldData, err := os.ReadFile(*comparePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading file: %s\n", err)
os.Exit(1)
}
var oldInfos map[string]FileInfo
if err := json.Unmarshal(oldData, &oldInfos); err != nil {
fmt.Fprintf(os.Stderr, "Error decoding %q: %s\n", *comparePath, err)
os.Exit(1)
}
// Output in CSV, so it is easy to paste into a spreadsheet.
fmt.Printf("Path,Name,Old Start Year,New Start Year\n")
for path, info := range sortedKeyValuePairs(infos) {
oldInfo, ok := oldInfos[path]
if !ok {
fmt.Printf("%s: file not previously present\n", path)
continue
}
summary := summarize(info)
oldSummary := summarize(oldInfo)
for name, year := range sortedKeyValuePairs(summary) {
oldYear, ok := oldSummary[name]
if !ok {
fmt.Printf("%s,%s,-1,%d\n", path, name, year)
} else if year != oldYear {
fmt.Printf("%s,%s,%d,%d\n", path, name, oldYear, year)
}
}
for oldName, oldYear := range sortedKeyValuePairs(oldSummary) {
if _, ok := summary[oldName]; !ok {
fmt.Printf("%s,%s,%d,-1\n", path, oldName, oldYear)
}
}
}
}