Skip to content

Commit

Permalink
Add correct license header to attribute to temporal and update licens…
Browse files Browse the repository at this point in the history
…e generation tool (cadence-workflow#3674)
  • Loading branch information
andrewjdawson2016 authored Oct 22, 2020
1 parent 51355ed commit daba348
Show file tree
Hide file tree
Showing 115 changed files with 331 additions and 294 deletions.
22 changes: 0 additions & 22 deletions .gen/proto/gogo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions .gen/proto/message.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
The MIT License (MIT)

Copyright (c) 2017-2020 Uber Technologies Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,16 @@ cadence-canary: $(ALL_SRC)
@echo "compiling cadence-canary with OS: $(GOOS), ARCH: $(GOARCH)"
go build -o cadence-canary cmd/canary/main.go

go-generate-format: go-generate fmt

go-generate:
GO111MODULE=off go get -u github.com/myitcv/gobin
GOOS= GOARCH= gobin -mod=readonly github.com/golang/mock/mockgen
GOOS= GOARCH= gobin -mod=readonly github.com/dmarkham/enumer
@echo "running go generate ./..."
@go generate ./...
@echo "running go run cmd/tools/copyright/licensegen.go"
@go run cmd/tools/copyright/licensegen.go

lint:
@echo "running linter"
Expand Down
2 changes: 1 addition & 1 deletion client/clientBean.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

package client

Expand Down
5 changes: 2 additions & 3 deletions client/clientBean_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 104 additions & 15 deletions cmd/tools/copyright/licensegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package main

import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
Expand All @@ -40,17 +41,26 @@ type (

// command line config params
config struct {
rootDir string
verifyOnly bool
rootDir string
verifyOnly bool
temporalAddMode bool
temporalModifyMode bool
filePaths string
}
)

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

// unique prefix that identifies a license header
const licenseHeaderPrefixOld = "// Copyright (c)"
const licenseHeaderPrefixOld = "Copyright (c)"
const licenseHeaderPrefix = "// The MIT License (MIT)"
const cadenceCopyright = "// Copyright (c) 2017-2020 Uber Technologies Inc."
const cadenceModificationHeader = "// Modifications Copyright (c) 2020 Uber Technologies Inc."
const temporalCopyright = "// Copyright (c) 2020 Temporal Technologies, Inc."
const temporalPartialCopyright = "// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc."

const firstLinesToCheck = 10

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

if err := verifyCfg(cfg); err != nil {
fmt.Println(err)
os.Exit(-1)
}

task := newAddLicenseHeaderTask(&cfg)
if err := task.run(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}

func verifyCfg(cfg config) error {
if cfg.verifyOnly {
if cfg.temporalModifyMode || cfg.temporalAddMode {
return errors.New("invalid config, can only specify one of temporalAddMode, temporalModifyMode or verifyOnly")
}
}
if cfg.temporalAddMode && cfg.temporalModifyMode {
return errors.New("invalid config, can only specify temporalAddMode or temporalModifyMode")
}
if (cfg.temporalModifyMode || cfg.temporalAddMode) && len(cfg.filePaths) == 0 {
return errors.New("invalid config, when running in temporalAddMode or temporalModifyMode must provide filePaths")
}
return nil
}

func newAddLicenseHeaderTask(cfg *config) *addLicenseHeaderTask {
return &addLicenseHeaderTask{
config: cfg,
Expand All @@ -89,12 +122,28 @@ func (task *addLicenseHeaderTask) run() error {
if err != nil {
return fmt.Errorf("error reading license file, errr=%v", err.Error())
}

task.license, err = commentOutLines(string(data))
if err != nil {
return fmt.Errorf("copyright header failed to comment out lines, err=%v", err.Error())
}

if task.config.temporalAddMode {
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", cadenceModificationHeader, temporalCopyright, task.license)
} else if task.config.temporalModifyMode {
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", cadenceCopyright, temporalPartialCopyright, task.license)
}
if task.config.temporalModifyMode || task.config.temporalAddMode {
filePaths, fileInfos, err := getFilePaths(task.config.filePaths)
if err != nil {
return err
}
for i := 0; i < len(filePaths); i++ {
if err := task.handleFile(filePaths[i], fileInfos[i], nil); err != nil {
return err
}
}
return nil
}
task.license = fmt.Sprintf("%v\n\n%v\n\n%v", licenseHeaderPrefix, cadenceCopyright, task.license)
err = filepath.Walk(task.config.rootDir, task.handleFile)
if err != nil {
return fmt.Errorf("copyright header check failed, err=%v", err.Error())
Expand All @@ -120,7 +169,7 @@ func (task *addLicenseHeaderTask) handleFile(path string, fileInfo os.FileInfo,
return nil
}

if !strings.HasSuffix(fileInfo.Name(), ".go") {
if !strings.HasSuffix(fileInfo.Name(), ".go") && !strings.HasSuffix(fileInfo.Name(), ".proto") {
return nil
}

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

scanner := bufio.NewScanner(f)
readLineSucc := scanner.Scan()
if !readLineSucc {
return fmt.Errorf("fail to read first line of file %v", path)
ok, err := hasCopyright(f)
if err != nil {
return err
}
firstLine := strings.TrimSpace(scanner.Text())
if err := scanner.Err(); err != nil {

if err := f.Close(); err != nil {
return err
}
f.Close()

if strings.Contains(firstLine, licenseHeaderPrefixOld) || strings.Contains(firstLine, licenseHeaderPrefix) {
return nil // file already has the copyright header
if ok {
if task.config.temporalModifyMode || task.config.temporalAddMode {
return fmt.Errorf("when running in temporalModifyMode or temporalAddMode please first remove existing license header: %v", path)
}
return nil
}

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

func hasCopyright(f *os.File) (bool, error) {
scanner := bufio.NewScanner(f)
lineSuccess := scanner.Scan()
if !lineSuccess {
return false, fmt.Errorf("fail to read first line of file %v", f.Name())
}
i := 0
for i < firstLinesToCheck && lineSuccess {
i++
line := strings.TrimSpace(scanner.Text())
if err := scanner.Err(); err != nil {
return false, err
}
if lineHasCopyright(line) {
return true, nil
}
lineSuccess = scanner.Scan()
}
return false, nil
}

func isFileAutogenerated(path string) bool {
return strings.HasPrefix(path, ".gen")
}
Expand All @@ -189,3 +260,21 @@ func commentOutLines(str string) (string, error) {
}
return strings.Join(lines, ""), nil
}

func lineHasCopyright(line string) bool {
return strings.Contains(line, licenseHeaderPrefixOld) ||
strings.Contains(line, licenseHeaderPrefix)
}

func getFilePaths(filePaths string) ([]string, []os.FileInfo, error) {
paths := strings.Split(filePaths, ",")
var fileInfos []os.FileInfo
for _, p := range paths {
fileInfo, err := os.Stat(p)
if err != nil {
return nil, nil, err
}
fileInfos = append(fileInfos, fileInfo)
}
return paths, fileInfos, nil
}
2 changes: 1 addition & 1 deletion common/archiver/filestore/queryParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

package filestore

Expand Down
5 changes: 2 additions & 3 deletions common/archiver/filestore/queryParser_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/archiver/gcloud/queryParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

package gcloud

Expand Down
5 changes: 2 additions & 3 deletions common/archiver/gcloud/queryParser_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/archiver/historyIterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

package archiver

Expand Down
5 changes: 2 additions & 3 deletions common/archiver/historyIterator_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/archiver/s3store/queryParser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

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

package s3store

Expand Down
5 changes: 2 additions & 3 deletions common/archiver/s3store/queryParser_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit daba348

Please sign in to comment.