Skip to content

Commit daba348

Browse files
Add correct license header to attribute to temporal and update license generation tool (cadence-workflow#3674)
1 parent 51355ed commit daba348

File tree

115 files changed

+331
-294
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+331
-294
lines changed

.gen/proto/gogo.pb.go

-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.gen/proto/message.pb.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
The MIT License (MIT)
2-
3-
Copyright (c) 2017-2020 Uber Technologies Inc.
4-
51
Permission is hereby granted, free of charge, to any person obtaining a copy
62
of this software and associated documentation files (the "Software"), to deal
73
in the Software without restriction, including without limitation the rights

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,16 @@ cadence-canary: $(ALL_SRC)
145145
@echo "compiling cadence-canary with OS: $(GOOS), ARCH: $(GOARCH)"
146146
go build -o cadence-canary cmd/canary/main.go
147147

148+
go-generate-format: go-generate fmt
149+
148150
go-generate:
149151
GO111MODULE=off go get -u github.com/myitcv/gobin
150152
GOOS= GOARCH= gobin -mod=readonly github.com/golang/mock/mockgen
151153
GOOS= GOARCH= gobin -mod=readonly github.com/dmarkham/enumer
152154
@echo "running go generate ./..."
153155
@go generate ./...
156+
@echo "running go run cmd/tools/copyright/licensegen.go"
157+
@go run cmd/tools/copyright/licensegen.go
154158

155159
lint:
156160
@echo "running linter"

client/clientBean.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:generate mockgen -copyright_file ../LICENSE -package $GOPACKAGE -source $GOFILE -destination clientBean_mock.go -self_package github.com/uber/cadence/client
21+
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination clientBean_mock.go -self_package github.com/uber/cadence/client
2222

2323
package client
2424

client/clientBean_mock.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/tools/copyright/licensegen.go

+104-15
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package main
2222

2323
import (
2424
"bufio"
25+
"errors"
2526
"flag"
2627
"fmt"
2728
"io/ioutil"
@@ -40,17 +41,26 @@ type (
4041

4142
// command line config params
4243
config struct {
43-
rootDir string
44-
verifyOnly bool
44+
rootDir string
45+
verifyOnly bool
46+
temporalAddMode bool
47+
temporalModifyMode bool
48+
filePaths string
4549
}
4650
)
4751

4852
// licenseFileName is the name of the license file
4953
const licenseFileName = "LICENSE"
5054

5155
// unique prefix that identifies a license header
52-
const licenseHeaderPrefixOld = "// Copyright (c)"
56+
const licenseHeaderPrefixOld = "Copyright (c)"
5357
const licenseHeaderPrefix = "// The MIT License (MIT)"
58+
const cadenceCopyright = "// Copyright (c) 2017-2020 Uber Technologies Inc."
59+
const cadenceModificationHeader = "// Modifications Copyright (c) 2020 Uber Technologies Inc."
60+
const temporalCopyright = "// Copyright (c) 2020 Temporal Technologies, Inc."
61+
const temporalPartialCopyright = "// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc."
62+
63+
const firstLinesToCheck = 10
5464

5565
var (
5666
// directories to be excluded
@@ -69,15 +79,38 @@ func main() {
6979
flag.StringVar(&cfg.rootDir, "rootDir", ".", "project root directory")
7080
flag.BoolVar(&cfg.verifyOnly, "verifyOnly", false,
7181
"don't automatically add headers, just verify all files")
82+
flag.BoolVar(&cfg.temporalAddMode, "temporalAddMode", false, "add copyright for new file copied from temporal")
83+
flag.BoolVar(&cfg.temporalModifyMode, "temporalModifyMode", false, "add copyright for existing file which has parts copied from temporal")
84+
flag.StringVar(&cfg.filePaths, "filePaths", "", "comma separated list of files to run temporal license on")
7285
flag.Parse()
7386

87+
if err := verifyCfg(cfg); err != nil {
88+
fmt.Println(err)
89+
os.Exit(-1)
90+
}
91+
7492
task := newAddLicenseHeaderTask(&cfg)
7593
if err := task.run(); err != nil {
7694
fmt.Println(err)
7795
os.Exit(-1)
7896
}
7997
}
8098

99+
func verifyCfg(cfg config) error {
100+
if cfg.verifyOnly {
101+
if cfg.temporalModifyMode || cfg.temporalAddMode {
102+
return errors.New("invalid config, can only specify one of temporalAddMode, temporalModifyMode or verifyOnly")
103+
}
104+
}
105+
if cfg.temporalAddMode && cfg.temporalModifyMode {
106+
return errors.New("invalid config, can only specify temporalAddMode or temporalModifyMode")
107+
}
108+
if (cfg.temporalModifyMode || cfg.temporalAddMode) && len(cfg.filePaths) == 0 {
109+
return errors.New("invalid config, when running in temporalAddMode or temporalModifyMode must provide filePaths")
110+
}
111+
return nil
112+
}
113+
81114
func newAddLicenseHeaderTask(cfg *config) *addLicenseHeaderTask {
82115
return &addLicenseHeaderTask{
83116
config: cfg,
@@ -89,12 +122,28 @@ func (task *addLicenseHeaderTask) run() error {
89122
if err != nil {
90123
return fmt.Errorf("error reading license file, errr=%v", err.Error())
91124
}
92-
93125
task.license, err = commentOutLines(string(data))
94126
if err != nil {
95127
return fmt.Errorf("copyright header failed to comment out lines, err=%v", err.Error())
96128
}
97-
129+
if task.config.temporalAddMode {
130+
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", cadenceModificationHeader, temporalCopyright, task.license)
131+
} else if task.config.temporalModifyMode {
132+
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", cadenceCopyright, temporalPartialCopyright, task.license)
133+
}
134+
if task.config.temporalModifyMode || task.config.temporalAddMode {
135+
filePaths, fileInfos, err := getFilePaths(task.config.filePaths)
136+
if err != nil {
137+
return err
138+
}
139+
for i := 0; i < len(filePaths); i++ {
140+
if err := task.handleFile(filePaths[i], fileInfos[i], nil); err != nil {
141+
return err
142+
}
143+
}
144+
return nil
145+
}
146+
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", licenseHeaderPrefix, cadenceCopyright, task.license)
98147
err = filepath.Walk(task.config.rootDir, task.handleFile)
99148
if err != nil {
100149
return fmt.Errorf("copyright header check failed, err=%v", err.Error())
@@ -120,7 +169,7 @@ func (task *addLicenseHeaderTask) handleFile(path string, fileInfo os.FileInfo,
120169
return nil
121170
}
122171

123-
if !strings.HasSuffix(fileInfo.Name(), ".go") {
172+
if !strings.HasSuffix(fileInfo.Name(), ".go") && !strings.HasSuffix(fileInfo.Name(), ".proto") {
124173
return nil
125174
}
126175

@@ -131,19 +180,20 @@ func (task *addLicenseHeaderTask) handleFile(path string, fileInfo os.FileInfo,
131180
return err
132181
}
133182

134-
scanner := bufio.NewScanner(f)
135-
readLineSucc := scanner.Scan()
136-
if !readLineSucc {
137-
return fmt.Errorf("fail to read first line of file %v", path)
183+
ok, err := hasCopyright(f)
184+
if err != nil {
185+
return err
138186
}
139-
firstLine := strings.TrimSpace(scanner.Text())
140-
if err := scanner.Err(); err != nil {
187+
188+
if err := f.Close(); err != nil {
141189
return err
142190
}
143-
f.Close()
144191

145-
if strings.Contains(firstLine, licenseHeaderPrefixOld) || strings.Contains(firstLine, licenseHeaderPrefix) {
146-
return nil // file already has the copyright header
192+
if ok {
193+
if task.config.temporalModifyMode || task.config.temporalAddMode {
194+
return fmt.Errorf("when running in temporalModifyMode or temporalAddMode please first remove existing license header: %v", path)
195+
}
196+
return nil
147197
}
148198

149199
// at this point, src file is missing the header
@@ -163,6 +213,27 @@ func (task *addLicenseHeaderTask) handleFile(path string, fileInfo os.FileInfo,
163213
return ioutil.WriteFile(path, []byte(task.license+string(data)), defaultFilePerms)
164214
}
165215

216+
func hasCopyright(f *os.File) (bool, error) {
217+
scanner := bufio.NewScanner(f)
218+
lineSuccess := scanner.Scan()
219+
if !lineSuccess {
220+
return false, fmt.Errorf("fail to read first line of file %v", f.Name())
221+
}
222+
i := 0
223+
for i < firstLinesToCheck && lineSuccess {
224+
i++
225+
line := strings.TrimSpace(scanner.Text())
226+
if err := scanner.Err(); err != nil {
227+
return false, err
228+
}
229+
if lineHasCopyright(line) {
230+
return true, nil
231+
}
232+
lineSuccess = scanner.Scan()
233+
}
234+
return false, nil
235+
}
236+
166237
func isFileAutogenerated(path string) bool {
167238
return strings.HasPrefix(path, ".gen")
168239
}
@@ -189,3 +260,21 @@ func commentOutLines(str string) (string, error) {
189260
}
190261
return strings.Join(lines, ""), nil
191262
}
263+
264+
func lineHasCopyright(line string) bool {
265+
return strings.Contains(line, licenseHeaderPrefixOld) ||
266+
strings.Contains(line, licenseHeaderPrefix)
267+
}
268+
269+
func getFilePaths(filePaths string) ([]string, []os.FileInfo, error) {
270+
paths := strings.Split(filePaths, ",")
271+
var fileInfos []os.FileInfo
272+
for _, p := range paths {
273+
fileInfo, err := os.Stat(p)
274+
if err != nil {
275+
return nil, nil, err
276+
}
277+
fileInfos = append(fileInfos, fileInfo)
278+
}
279+
return paths, fileInfos, nil
280+
}

common/archiver/filestore/queryParser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
21+
//go:generate mockgen -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
2222

2323
package filestore
2424

common/archiver/filestore/queryParser_mock.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/archiver/gcloud/queryParser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
21+
//go:generate mockgen -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
2222

2323
package gcloud
2424

common/archiver/gcloud/queryParser_mock.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/archiver/historyIterator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination historyIterator_mock.go -self_package github.com/uber/cadence/common/archiver
21+
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination historyIterator_mock.go -self_package github.com/uber/cadence/common/archiver
2222

2323
package archiver
2424

common/archiver/historyIterator_mock.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/archiver/s3store/queryParser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
//go:generate mockgen -copyright_file ../../../LICENSE -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
21+
//go:generate mockgen -package $GOPACKAGE -source queryParser.go -destination queryParser_mock.go -mock_names Interface=MockQueryParser
2222

2323
package s3store
2424

common/archiver/s3store/queryParser_mock.go

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)