Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update license header and add check for CI #294

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
version: "1.0"
- run: cargo fmt --all -- --check
- run: cargo sort --workspace --check
- run: npx @kt3k/license-checker

test:
runs-on: [self-hosted, "${{ matrix.os }}", "${{ matrix.arch }}"]
Expand Down
21 changes: 21 additions & 0 deletions .licenserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"**/*.{cpp,h,rs}": [
"// Copyright 2022 RISC Zero, Inc.",
"//",
"// Licensed under the Apache License, Version 2.0 (the \"License\");",
"// you may not use this file except in compliance with the License.",
"// You may obtain a copy of the License at",
"//",
"// http://www.apache.org/licenses/LICENSE-2.0",
"//",
"// Unless required by applicable law or agreed to in writing, software",
"// distributed under the License is distributed on an \"AS IS\" BASIS,",
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.",
"// See the License for the specific language governing permissions and",
"// limitations under the License."
],
"ignore": [
"target/",
"tmp/"
]
}
13 changes: 13 additions & 0 deletions LICENSE-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2022 RISC Zero, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
193 changes: 193 additions & 0 deletions license-header.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#!/usr/bin/env python

# Tool license-header
#
# Copyright (C) 2022 Wang Qi (wqking)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import glob
import argparse
import traceback
import difflib

class Application :
def __init__(self) :
self._action = None
self._sourcePatterns = []
self._excludePatterns = []
self._licenseFile = None
self._before = ''
self._after = ''
self._lineBefore = ''
self._lineAfter = ''
self._licenseLines = []

def run(self) :
try :
self._parseCommandLine(sys.argv[1:])
self._doRun()
except Exception as e:
traceback.print_exc()

def _unescapeText(self, text) :
if text is None :
return text;
text = text.replace("\\n", "\n")
return text

def _doRun(self) :
self._before = self._unescapeText(self._before)
self._after = self._unescapeText(self._after)

with open(self._licenseFile, 'r') as f :
licenseLines = f.readlines()
lastEmptyLineIndex = len(licenseLines)
for i in range(len(licenseLines) - 1, -1, -1) :
line = licenseLines[i]
line = line.strip()
if line != '' :
break
lastEmptyLineIndex = i
if lastEmptyLineIndex < len(licenseLines) :
licenseLines = licenseLines[0 : lastEmptyLineIndex]
self._licenseLines = []
if self._before is not None :
self._licenseLines.append(self._before + "\n")
for line in licenseLines :
if self._lineBefore is not None :
line = self._lineBefore + line
if self._lineAfter is not None :
line = line + self._lineAfter
self._licenseLines.append(line)
if self._after is not None :
self._licenseLines.append(self._after + "\n")

for sourcePattern in self._sourcePatterns :
self._doOnSourcePattern(sourcePattern)

def _doOnSourcePattern(self, sourcePattern) :
fileList = glob.glob(sourcePattern, recursive = True)
for file in fileList :
self._doOnFile(file)

def _doOnFile(self, file) :
if not self._canProcessFile(file) :
return
with open(file, 'r') as f :
sourceLine = f.readlines()
tempLineList = self._doRemoveLicense(sourceLine)
lineList = []
for line in tempLineList :
if len(lineList) == 0 :
if line.strip() == '' :
continue
lineList.append(line)
if self._action == 'update' :
lineList = self._licenseLines + lineList
if not self._isSameList(sourceLine, lineList) :
with open(file, 'w') as f :
f.writelines(lineList)

def _canProcessFile(self, file) :
for exclude in self._excludePatterns :
if exclude in file :
return False
return True

def _isSameList(self, a, b) :
if len(a) != len(b) :
return False
for i in range(len(a)) :
if a[i] != b[i] :
return False
return True

def _doRemoveLicense(self, lineList) :
matchedLineCount = self._doGetMatchedLicenseLineCount(lineList)
if matchedLineCount > 0 :
resultList = lineList[matchedLineCount : ]
else :
resultList = lineList + []
return resultList

def _doGetMatchedLicenseLineCount(self, lineList) :
if len(lineList) < len(self._licenseLines) :
return 0;
maxLineCount = len(self._licenseLines) + 10
for i in range(maxLineCount - 1, 0, -1) :
if i >= len(lineList) :
continue
if self._doLineMatchLicense(lineList[i]) :
return i + 1
return 0

def _doLineMatchLicense(self, a) :
if a.strip() == '' :
return False
for line in self._licenseLines :
if self._doLineMatch(a, line) :
return True
return False

def _doLineMatch(self, a, b) :
a = self._doNormalizeLineForMatch(a)
b = self._doNormalizeLineForMatch(b)
if len(a) < 10 :
return False
ratio = difflib.SequenceMatcher(None, a, b).ratio()
return ratio >= 0.6

def _doNormalizeLineForMatch(self, line) :
line = line.replace(" ", "")
line = line.replace("\t", "")
return line

def _parseCommandLine(self, commandLineArguments) :
parser = argparse.ArgumentParser(add_help = False)
parser.add_argument('--help', action = 'store_true', help = 'Show help message')
parser.add_argument('-h', action = 'store_true', dest = 'help', help = 'Show help message')
parser.add_argument('action', help = "The action, can be update or remove", default = 'update', choices = [ 'update', 'remove' ])
parser.add_argument('--source', action = 'append', required = True, help = "The source file patterns, can have path and wildcard")
parser.add_argument('--license', required = True, help = "The license file location")
parser.add_argument('--before', required = False, help = "The text added before the license block", default = None)
parser.add_argument('--after', required = False, help = "The text added after the license block", default = None)
parser.add_argument('--line-before', required = False, help = "The text added before each line", default = None)
parser.add_argument('--line-after', required = False, help = "The text added after each line", default = None)
parser.add_argument('--exclude', action = 'append', required = False, help = "The patterns to exclude, can not have wildcard")

options = parser.parse_args(commandLineArguments)
options = vars(options)
#print(options)

if options['help'] :
self._showUsage(parser)
return False

self._action = options['action']
self._sourcePatterns = options['source']
self._licenseFile = options['license']
self._before = options['before']
self._after = options['after']
self._lineBefore = options['line_before']
self._lineAfter = options['line_after']
self._excludePatterns = options['exclude']
if self._excludePatterns is None :
self._excludePatterns = []

def _showUsage(self, parser) :
parser.print_help()


Application().run()
5 changes: 5 additions & 0 deletions license-header.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

python license-header.py update --source 'risc0/**/*.cpp' --license LICENSE-header --line-before '//' --after ""
python license-header.py update --source 'risc0/**/*.h' --license LICENSE-header --line-before '//' --after ""
python license-header.py update --source 'risc0/**/*.rs' --license LICENSE-header --line-before '//' --after ""
2 changes: 1 addition & 1 deletion risc0/build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion risc0/circuit/rv32im/benches/eval_check.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
14 changes: 14 additions & 0 deletions risc0/circuit/rv32im/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{
env,
path::{Path, PathBuf},
Expand Down
2 changes: 1 addition & 1 deletion risc0/circuit/rv32im/cxx/ffi.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion risc0/circuit/rv32im/cxx/ffi.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion risc0/circuit/rv32im/cxx/fp.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion risc0/circuit/rv32im/cxx/fp4.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Risc0, Inc.
// Copyright 2022 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down