add script to set copyright header for source files

this works for:
- C/C++ files
- files where comment is #

Change include set of simple tests.

Change-Id: I1c08c6c359e2a48957d5d81397c439508ae06c42
Signed-off-by: Artur Harasimiuk <artur.harasimiuk@intel.com>
This commit is contained in:
Artur Harasimiuk
2018-09-13 16:26:01 +02:00
parent 65dc5fb7de
commit 8334d424ba
26 changed files with 365 additions and 66 deletions

View File

@@ -1,22 +1,8 @@
# Copyright (c) 2017 - 2018, Intel Corporation
#
# 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 to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# Copyright (C) 2017-2018 Intel Corporation
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# SPDX-License-Identifier: MIT
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)
include(ExternalProject)

View File

@@ -17,7 +17,7 @@ components:
branch: infra
clean_on_sync: true
dest_dir: infra
revision: e49a489db3191c6a61e2928fed593bb77baa46af
revision: f3158b039683ec9c0ff7dc7fb1178ce88053c5d7
type: git
internal:
branch: master

View File

@@ -1,22 +1,8 @@
# Copyright (c) 2017, Intel Corporation
#
# 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 to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# Copyright (C) 2017-2018 Intel Corporation
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# SPDX-License-Identifier: MIT
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
if(UNIX)
include(GNUInstallDirs)

View File

@@ -1,23 +1,8 @@
/*
* Copyright (c) 2017 - 2018, Intel Corporation
* Copyright (C) 2017-2018 Intel Corporation
*
* 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 to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
* SPDX-License-Identifier: MIT
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "config.h"

View File

@@ -0,0 +1,201 @@
#!/usr/bin/env python
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
import re
import sys
import os.path
from datetime import date
from stat import ST_MODE
# count arguments, need at least 1
if len(sys.argv) < 2:
print "need 1 argument, file\n"
sys.exit(0)
header_cpp = """/*
* Copyright (C) %s Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
"""
header_bash_style = """#
# Copyright (C) %s Intel Corporation
#
# SPDX-License-Identifier: MIT
#
"""
allowed_extensions = [
'cpp', 'h', 'inl', 'hpp', 'm',
'cmake',
'py', 'sh',
'cl',
'exports'
]
allowed_extensions_2 = [
'h.in',
'options.txt'
]
allowed_files = [
'CMakeLists.txt'
]
banned_paths = [
'scripts/tests/copyright/in',
'scripts/tests/copyright/out'
]
def isBanned(path):
path_ok = False
for banned_path in banned_paths:
if dirname.startswith(banned_path):
path_ok = True
break
return path_ok
for path in sys.argv:
# avoid self scan
if os.path.abspath(path) == os.path.abspath(sys.argv[0]):
continue
# check whether we should scan this file
path_ext = path.split('.')
path_ok = False
filename = os.path.basename(path)
dirname = os.path.dirname(path)
while True:
if isBanned(path):
path_ok = False
break
if filename in allowed_files:
path_ok = True
break
if path_ext[-1].lower() in allowed_extensions:
path_ok = True
break
if '.'.join(path_ext[-2:]) in allowed_extensions_2:
path_ok = True
break
break
if not path_ok:
print "Ignoring file: %s" % path
continue
# check that first arg is a existing file
if not os.path.isfile(path):
print "cannot find file %s, skipping" % path
continue
print "Processing file: %s" % path
L = list()
start_year = None
header = header_cpp
header_start = '/*'
header_end = '*/'
comment_char = "\*"
# now read line by line
f = open(path, 'r')
# take care of hashbang
first_line = f.readline()
if not first_line.startswith( '#!' ):
line = first_line
first_line = ''
else:
line = f.readline()
# check whether comment type is '#'
if first_line or line.startswith('#'):
header_start = '#'
header_end = '\n'
header = header_bash_style
comment_char = "#"
curr_comment = list()
# copyright have to be first comment in file
if line.startswith(header_start):
isHeader = True
isHeaderEnd = False
else:
isHeader = False
isHeaderEnd = True
isCopyright = False
while (line):
if isHeader:
if header_end == '\n' and len(line.strip()) == 0:
isHeader = False
isHeaderEnd = True
elif line.strip().endswith(header_end):
isHeader = False
isHeaderEnd = True
elif "Copyright" in line:
tmp = line.split(',')
expr = ("^%s Copyright \([Cc]\) (\d+)( *- *\d+)?" % comment_char)
m = re.match(expr, line.strip())
if not m is None:
start_year = m.groups()[0]
curr_comment = list()
isCopyright = True
if not isCopyright:
curr_comment.append(line)
elif isCopyright and isHeaderEnd:
if len(line.strip()) > 0:
L.append(line)
isHeaderEnd = False
else:
L.append(line)
line = f.readline()
f.close()
year = date.today().year
if start_year is None:
start_year = str(year)
elif int(start_year) < year:
start_year += "-"
start_year += str(year)
# store file mode because we want to preserve this
old_mode = os.stat(path)[ST_MODE]
os.remove(path)
f = open(path, 'w')
if first_line:
f.write(first_line)
f.write(header % start_year)
if len(curr_comment)>0 or len(L)>0:
f.write("\n")
if len(curr_comment)>0:
f.write(''.join(curr_comment))
contents = ''.join(L)
f.write(contents)
f.close()
# chmod to original value
os.chmod(path, old_mode)
del L[:]

14
scripts/lint/set_copyright.sh Executable file
View File

@@ -0,0 +1,14 @@
#!/bin/bash
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
converter=$(dirname $(readlink -f $0))/set_copyright.py
if [ "${1:-STAGED}" = "HEAD" ]; then
git diff-tree --no-commit-id --name-only -r HEAD | xargs -n 1 python $converter
else
git diff --cached --name-only | xargs -n 1 python $converter
fi

18
scripts/run-build.sh Executable file → Normal file
View File

@@ -1,23 +1,9 @@
#!/bin/bash
# Copyright (c) 2018, Intel Corporation
#
# 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 to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# Copyright (C) 2018 Intel Corporation
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
# SPDX-License-Identifier: MIT
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
DOCKERFILE=Dockerfile-${BUILD_OS}-${COMPILER}
IMAGE=neo-${BUILD_OS}-${COMPILER}:ci

View File

@@ -0,0 +1,3 @@
/*
* No copyright at all
*/

View File

@@ -0,0 +1,4 @@
#!/bin/bash
echo 123

View File

@@ -0,0 +1,4 @@
/*
* Copyright (C) 2017 XYZ
*/

View File

@@ -0,0 +1,2 @@
#
# Copyright (C) 2017 XYZ

View File

@@ -0,0 +1,3 @@
//
// This comment shouldn't be removed
//

View File

@@ -0,0 +1,3 @@
#
# This comment shouldn't be removed
#

View File

@@ -0,0 +1,7 @@
/*
* No copyright at all
*/
#include "file.h"
class C;

View File

@@ -0,0 +1,7 @@
#
# No copyright at all
#
echo "file.h"
exit 1

View File

@@ -0,0 +1,5 @@
/*
* Copyright (C) 2012 - 2016 Intel Corporation
*
* No spdx header
*/

View File

@@ -0,0 +1,10 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
/*
* No copyright at all
*/

View File

@@ -0,0 +1,10 @@
#!/bin/bash
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
echo 123

View File

@@ -0,0 +1,6 @@
/*
* Copyright (C) 2017-2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

View File

@@ -0,0 +1,5 @@
#
# Copyright (C) 2017-2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#

View File

@@ -0,0 +1,10 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
//
// This comment shouldn't be removed
//

View File

@@ -0,0 +1,9 @@
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
#
# This comment shouldn't be removed
#

View File

@@ -0,0 +1,14 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
/*
* No copyright at all
*/
#include "file.h"
class C;

View File

@@ -0,0 +1,13 @@
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
#
# No copyright at all
#
echo "file.h"
exit 1

View File

@@ -0,0 +1,6 @@
/*
* Copyright (C) 2012-2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

20
scripts/tests/copyright/test.sh Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/sh
#
# Copyright (C) 2018 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
#
# Simple, file based tests for copyright script
# script return non-zero error code if something went wrong.
# diff output is printed
#
python ../../lint/set_copyright.py in/*
for i in in/*
do
fn=$(basename $i)
diff -du in/$fn out/$fn
done