2018-05-12 01:32:00 +02:00
|
|
|
#!/usr/bin/env python3
|
2019-04-26 21:21:50 +02:00
|
|
|
# See LICENSE for licensing information.
|
|
|
|
|
#
|
2024-01-03 23:32:44 +01:00
|
|
|
# Copyright (c) 2016-2024 Regents of the University of California and The Board
|
2019-06-14 17:43:41 +02:00
|
|
|
# of Regents for the Oklahoma Agricultural and Mechanical College
|
|
|
|
|
# (acting for and on behalf of Oklahoma State University)
|
|
|
|
|
# All rights reserved.
|
2019-04-26 21:21:50 +02:00
|
|
|
#
|
2022-11-27 22:01:20 +01:00
|
|
|
import sys, os, re
|
2016-11-08 18:57:35 +01:00
|
|
|
import unittest
|
2019-05-31 19:51:42 +02:00
|
|
|
from testutils import *
|
2022-07-13 19:57:56 +02:00
|
|
|
|
2022-11-27 22:01:20 +01:00
|
|
|
import openram
|
|
|
|
|
from openram import debug
|
|
|
|
|
|
2016-11-08 18:57:35 +01:00
|
|
|
|
2018-01-30 01:59:29 +01:00
|
|
|
class code_format_test(openram_test):
|
2016-11-08 18:57:35 +01:00
|
|
|
"Run a test to check for tabs instead of spaces in the all source files."
|
|
|
|
|
|
|
|
|
|
def runTest(self):
|
2017-04-24 20:55:11 +02:00
|
|
|
source_code_dir = os.environ["OPENRAM_HOME"]
|
2016-11-08 18:57:35 +01:00
|
|
|
source_codes = setup_files(source_code_dir)
|
|
|
|
|
errors = 0
|
|
|
|
|
|
2018-08-07 18:40:45 +02:00
|
|
|
# Check for tabs or carriage returns
|
2016-11-08 18:57:35 +01:00
|
|
|
for code in source_codes:
|
|
|
|
|
if re.search("gdsMill", code):
|
|
|
|
|
continue
|
|
|
|
|
errors += check_file_format_tab(code)
|
2020-11-03 15:29:17 +01:00
|
|
|
errors += check_file_format_carriage(code)
|
2022-07-22 17:22:40 +02:00
|
|
|
errors += check_file_format_whitespace(code)
|
2016-11-08 18:57:35 +01:00
|
|
|
|
2022-11-30 23:50:43 +01:00
|
|
|
# Check for "print"
|
2016-11-08 18:57:35 +01:00
|
|
|
for code in source_codes:
|
|
|
|
|
if re.search("gdsMill", code):
|
|
|
|
|
continue
|
|
|
|
|
if re.search("debug.py$", code):
|
|
|
|
|
continue
|
2022-11-06 23:05:08 +01:00
|
|
|
if re.search("sram_compiler.py$", code):
|
2016-11-08 18:57:35 +01:00
|
|
|
continue
|
2019-01-18 19:16:55 +01:00
|
|
|
if re.search("testutils.py$", code):
|
2018-10-15 22:23:31 +02:00
|
|
|
continue
|
2018-02-26 17:54:35 +01:00
|
|
|
if re.search("gen_stimulus.py$", code):
|
|
|
|
|
continue
|
2016-11-08 18:57:35 +01:00
|
|
|
errors += check_print_output(code)
|
|
|
|
|
|
2022-11-30 23:50:43 +01:00
|
|
|
# Check for copyright
|
|
|
|
|
for code in source_codes:
|
|
|
|
|
if re.search("gdsMill", code):
|
|
|
|
|
continue
|
|
|
|
|
errors += check_copyright(code)
|
2018-08-07 18:40:45 +02:00
|
|
|
|
2016-11-08 18:57:35 +01:00
|
|
|
# fails if there are any tabs in any files
|
|
|
|
|
self.assertEqual(errors, 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_files(path):
|
|
|
|
|
files = []
|
|
|
|
|
for (dir, _, current_files) in os.walk(path):
|
|
|
|
|
for f in current_files:
|
2022-07-22 17:11:14 +02:00
|
|
|
files.append(os.path.join(dir, f))
|
2016-11-08 18:57:35 +01:00
|
|
|
nametest = re.compile("\.py$", re.IGNORECASE)
|
2018-10-15 22:23:31 +02:00
|
|
|
select_files = list(filter(nametest.search, files))
|
2016-11-08 18:57:35 +01:00
|
|
|
return select_files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_file_format_tab(file_name):
|
2018-08-07 18:40:45 +02:00
|
|
|
"""
|
|
|
|
|
Check if any files contain tabs and return the number of tabs.
|
|
|
|
|
"""
|
2016-11-08 18:57:35 +01:00
|
|
|
f = open(file_name, "r+b")
|
|
|
|
|
key_positions = []
|
|
|
|
|
for num, line in enumerate(f, 1):
|
2018-05-12 01:32:00 +02:00
|
|
|
if b'\t' in line:
|
2016-11-08 18:57:35 +01:00
|
|
|
key_positions.append(num)
|
|
|
|
|
if len(key_positions) > 0:
|
2018-08-07 18:40:45 +02:00
|
|
|
if len(key_positions)>10:
|
|
|
|
|
line_numbers = key_positions[:10] + [" ..."]
|
|
|
|
|
else:
|
2018-08-15 12:33:33 +02:00
|
|
|
line_numbers = key_positions
|
2016-11-08 18:57:35 +01:00
|
|
|
debug.info(0, '\nFound ' + str(len(key_positions)) + ' tabs in ' +
|
2018-08-15 12:33:33 +02:00
|
|
|
str(file_name) + ' (lines ' + ",".join(str(x) for x in line_numbers) + ')')
|
2018-08-07 18:40:45 +02:00
|
|
|
f.close()
|
|
|
|
|
return len(key_positions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_file_format_carriage(file_name):
|
|
|
|
|
"""
|
2020-11-03 15:29:17 +01:00
|
|
|
Check if file contains carriage returns at the end of lines
|
2018-08-07 18:40:45 +02:00
|
|
|
and return the number of carriage return lines.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
f = open(file_name, 'r+b')
|
|
|
|
|
key_positions = []
|
|
|
|
|
for num, line in enumerate(f.readlines()):
|
|
|
|
|
if b'\r\n' in line:
|
|
|
|
|
key_positions.append(num)
|
|
|
|
|
if len(key_positions) > 0:
|
|
|
|
|
if len(key_positions)>10:
|
|
|
|
|
line_numbers = key_positions[:10] + [" ..."]
|
|
|
|
|
else:
|
2022-07-22 17:15:27 +02:00
|
|
|
line_numbers = key_positions
|
2018-08-07 18:40:45 +02:00
|
|
|
debug.info(0, '\nFound ' + str(len(key_positions)) + ' carriage returns in ' +
|
|
|
|
|
str(file_name) + ' (lines ' + ",".join(str(x) for x in line_numbers) + ')')
|
2018-05-12 01:32:00 +02:00
|
|
|
f.close()
|
2016-11-08 18:57:35 +01:00
|
|
|
return len(key_positions)
|
|
|
|
|
|
|
|
|
|
|
2022-07-22 17:22:40 +02:00
|
|
|
def check_file_format_whitespace(file_name):
|
|
|
|
|
"""
|
|
|
|
|
Check if file contains a line with whitespace at the end
|
|
|
|
|
and return the number of these lines.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
f = open(file_name, "r")
|
|
|
|
|
key_positions = []
|
|
|
|
|
for num, line in enumerate(f.readlines()):
|
|
|
|
|
if re.match(r".*[ \t]$", line):
|
|
|
|
|
key_positions.append(num)
|
|
|
|
|
if len(key_positions) > 0:
|
|
|
|
|
if len(key_positions) > 10:
|
|
|
|
|
line_numbers = key_positions[:10] + [" ..."]
|
|
|
|
|
else:
|
|
|
|
|
line_numbers = key_positions
|
|
|
|
|
debug.info(0, "\nFound " + str(len(key_positions)) + " ending whitespace in " +
|
|
|
|
|
str(file_name) + " (lines " + ",".join(str(x) for x in line_numbers) + ")")
|
|
|
|
|
f.close()
|
|
|
|
|
return len(key_positions)
|
|
|
|
|
|
|
|
|
|
|
2016-11-08 18:57:35 +01:00
|
|
|
def check_print_output(file_name):
|
|
|
|
|
"""Check if any files (except debug.py) call the _print_ function. We should
|
|
|
|
|
use the debug output with verbosity instead!"""
|
2022-07-26 21:20:15 +02:00
|
|
|
|
|
|
|
|
skip_files = ["printGDS.py", "uniquifyGDS.py", "processGDS.py", "model_data_util.py"]
|
|
|
|
|
base_file_name = os.path.basename(file_name)
|
|
|
|
|
if base_file_name in skip_files:
|
|
|
|
|
return(0)
|
2016-11-08 18:57:35 +01:00
|
|
|
file = open(file_name, "r+b")
|
2018-10-15 22:23:31 +02:00
|
|
|
line = file.read().decode('utf-8')
|
2016-11-08 18:57:35 +01:00
|
|
|
# skip comments with a hash
|
|
|
|
|
line = re.sub(r'#.*', '', line)
|
|
|
|
|
# skip doc string comments
|
|
|
|
|
line=re.sub(r'\"\"\"[^\"]*\"\"\"', '', line, flags=re.S|re.M)
|
2018-10-15 22:23:31 +02:00
|
|
|
count = len(re.findall("[^p]+print\(", line))
|
2016-11-08 18:57:35 +01:00
|
|
|
if count > 0:
|
|
|
|
|
debug.info(0, "\nFound " + str(count) +
|
|
|
|
|
" _print_ calls " + str(file_name))
|
|
|
|
|
|
2018-10-15 22:23:31 +02:00
|
|
|
file.close()
|
2016-11-08 18:57:35 +01:00
|
|
|
return(count)
|
|
|
|
|
|
|
|
|
|
|
2022-11-30 23:50:43 +01:00
|
|
|
def check_copyright(file_name):
|
|
|
|
|
""" Check if any file doesn't contain the copyright at the top. """
|
|
|
|
|
|
|
|
|
|
from datetime import date
|
|
|
|
|
year = date.today().year
|
|
|
|
|
old_copyright = ("# See LICENSE for licensing information.\n"
|
|
|
|
|
"#\n"
|
|
|
|
|
"# Copyright (c) 2016-{} Regents of the University of California and The Board\n"
|
|
|
|
|
"# of Regents for the Oklahoma Agricultural and Mechanical College\n"
|
|
|
|
|
"# (acting for and on behalf of Oklahoma State University)\n"
|
|
|
|
|
"# All rights reserved.\n"
|
|
|
|
|
"#\n").format(year)
|
|
|
|
|
new_copyright = ("# See LICENSE for licensing information.\n"
|
|
|
|
|
"#\n"
|
|
|
|
|
"# Copyright (c) 2016-{} Regents of the University of California, Santa Cruz\n"
|
|
|
|
|
"# All rights reserved.\n"
|
|
|
|
|
"#\n").format(year)
|
|
|
|
|
skip_files = []
|
|
|
|
|
base_file_name = os.path.basename(file_name)
|
|
|
|
|
if base_file_name in skip_files:
|
|
|
|
|
return 0
|
|
|
|
|
file = open(file_name, "r")
|
|
|
|
|
line = file.read()
|
|
|
|
|
file.close()
|
|
|
|
|
# Skip possible shebang at the top
|
|
|
|
|
line = re.sub(r'#!.*\n', '', line)
|
|
|
|
|
# Check if copyright is missing
|
|
|
|
|
if not line.startswith(old_copyright) and not line.startswith(new_copyright):
|
|
|
|
|
debug.info(0, "\nFound missing/wrong copyright in " + file_name)
|
|
|
|
|
return 1
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
2018-11-03 00:34:26 +01:00
|
|
|
# run the test from the command line
|
2016-11-08 18:57:35 +01:00
|
|
|
if __name__ == "__main__":
|
2022-11-27 22:01:20 +01:00
|
|
|
(OPTS, args) = openram.parse_args()
|
2016-11-08 18:57:35 +01:00
|
|
|
del sys.argv[1:]
|
|
|
|
|
header(__file__, OPTS.tech_name)
|
2019-05-31 19:51:42 +02:00
|
|
|
unittest.main(testRunner=debugTestRunner())
|