Updated check_style_guide to test for include guards and copyright

notices
 - Fix include guards
 - Fix copyright header indentation
 - Update copyright years
This commit is contained in:
Alexander Bock
2017-02-09 23:55:39 -05:00
parent ea485fd418
commit 23ea86949f
38 changed files with 360 additions and 369 deletions

View File

@@ -26,8 +26,10 @@ guards for correctness. At the moment this includes:
* Correctness (file has a #ifndef. #define, and #endif lines)
* Equality (using the same name for the #ifdef and #define)
* Styling (no empty line between #ifndef and #define lines, empty lines before and
after #ifndef #define block)
after #ifndef #define block, files end with an empty line, and copyright header
is correctly indented)
* Correct usage of the name in the final comment of the file
* Correct year of copyright notice
* Naming convention (OpenSpace include guards start with OPENSPACE, Ghoul with GHOUL,
module includes have the module name in it)
* Checking for duplicates between all files
@@ -37,10 +39,13 @@ be passed, otherwise the first and only argument has to point to the base direct
Thus, the default value of the first argument is '.'
"""
import fnmatch
import glob
import re
import sys
current_year = '2017'
def get_ifndef_symbol(lines):
index = [i for i,s in enumerate(lines) if '#ifndef ' in s]
@@ -96,6 +101,13 @@ def check_styling(lines):
if lines[define_line + 1].strip() != '':
return 'Following line is not empty'
if not lines[-1][-1] in ['\n', '\r']:
return 'Last line must end with a newline'
for l in lines[2:23]:
if l[0] != ' ':
return 'Copyright header must be indented'
return ''
def check_comment(lines):
@@ -109,6 +121,32 @@ def check_comment(lines):
else:
return ''
def check_copyright(lines):
index = [i for i,s in enumerate(lines[0:23]) if 'Copyright' in s]
if len(index) == 0:
return 'No copyright header found'
beginning_string = ' * Copyright (c) 2012-'
# * Copyright (c) 2014-
year = lines[index[0]][len(beginning_string) : len(beginning_string) + 4]
if year != current_year:
return 'Out of date copyright notice ' + year + ' || ' + current_year
else:
return ''
def check_naming_convention(lines, component):
ifndef_symbol, _ = get_ifndef_symbol(lines)
component_part = ifndef_symbol[2:2 + len(component)]
if component_part != component.upper():
return '#ifndef naming convention broken: ' + ifndef_symbol + ' // ' + component.upper()
else:
return ''
def check_duplicates(lines, previousSymbols):
ifndef_symbol, _ = get_ifndef_symbol(lines)
@@ -117,68 +155,63 @@ def check_duplicates(lines, previousSymbols):
else:
return True, ifndef_symbol
basePath = './'
if len(sys.argv) > 1:
basePath = sys.argv[1] + '/'
positivePathList = [
'include/**/*.h',
'apps/**/*.h',
'modules/**/*.h',
'ext/ghoul/include/**/*.h'
]
negativePathList = [
'modules/**/ext/**/*.h',
'apps/**/ext/**/*.h',
]
# Collect all the files that we might apply this script to
files = []
for p in positivePathList:
f = glob.glob(basePath + p, recursive=True)
files = files + f
# Collect all files that we want to remove from the full list
# These are mostly the external directories from modules
negativeFiles = []
for p in negativePathList:
f = glob.glob(basePath + p, recursive=True)
negativeFiles = negativeFiles + f
# Actually remove the negative files from the positive ones
files = [f for f in files if f not in negativeFiles]
previousSymbols = {}
for file in files:
success = True
def check_file(file, component):
with open(file, 'r+') as f:
lines = f.readlines()
correctness = check_correctness(lines)
if correctness:
print(file, '\t', 'Correctness check failed', '\t', correctness)
continue
return
equality = check_equality(lines)
if equality:
print(file, '\t', 'Equality check failed', '\t', equality)
continue
return
styling = check_styling(lines)
if styling:
print(file, '\t', 'Styling check failed', '\t', styling)
continue
return
comment = check_comment(lines)
if comment:
print(file, '\t', 'Comment check failed', '\t', comment)
continue
return
copyright = check_copyright(lines)
if copyright:
print(file, '\t', 'Copyright check failed', '\t', copyright)
return
naming = check_naming_convention(lines, component)
if naming:
print(file, '\t', 'Naming convention broken', '\t', naming)
return
duplicates, symbol = check_duplicates(lines, previousSymbols)
if not duplicates:
print(file, '\t', 'Duplicate include guard', symbol, 'first in', previousSymbols[symbol])
continue
return
else:
previousSymbols[symbol] = file
previousSymbols[symbol] = file
def check_files(positiveList, negativeList, component):
files = glob.glob(positiveList, recursive=True)
negativeFiles = glob.glob(negativeList, recursive=True)
files = [f for f in files if f not in negativeFiles]
for file in files:
check_file(file, component)
basePath = './'
if len(sys.argv) > 1:
basePath = sys.argv[1] + '/'
check_files(basePath + 'include/**/*.h', '', 'openspace_core')
check_files(basePath + 'apps/**/*.h', basePath + 'apps/**/ext/**/*.h', 'openspace_app')
check_files(basePath + 'modules/**/*.h', basePath + 'modules/**/ext/**/*.h', 'openspace_module')
check_files(basePath + 'ext/ghoul/include/**/*.h', '', 'ghoul')