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

Reformat the codebase and enable black check in CI #2750

Merged
merged 15 commits into from
Jun 14, 2023
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d7c5ae98686622fe0b5298566d35df828be8f841
9 changes: 5 additions & 4 deletions .github/workflows/nestbuildmatrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,17 @@ jobs:
run: |
pylint --jobs=$(nproc) pynest/ testsuite/pytests/*.py testsuite/regressiontests/*.py

black_IGNORED:
black:
runs-on: "ubuntu-20.04"
steps:
- name: "Checkout repository content"
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: "Run black..."
continue-on-error: true
uses: psf/[email protected]
with:
jupyter: true

flake8:
runs-on: "ubuntu-20.04"
Expand Down Expand Up @@ -402,7 +403,7 @@ jobs:
build_linux:
if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
runs-on: ${{ matrix.os }}
needs: [clang-format, mypy, copyright_headers, unused_names, forbidden_types, pylint, black_IGNORED, flake8]
needs: [clang-format, mypy, copyright_headers, unused_names, forbidden_types, pylint, black, flake8]
env:
CXX_FLAGS: "-pedantic -Wextra -Woverloaded-virtual -Wno-unknown-pragmas"
NEST_VPATH: "build"
Expand Down Expand Up @@ -598,7 +599,7 @@ jobs:
build_macos:
if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
runs-on: ${{ matrix.os }}
needs: [clang-format, mypy, copyright_headers, unused_names, forbidden_types, pylint, black_IGNORED, flake8]
needs: [clang-format, mypy, copyright_headers, unused_names, forbidden_types, pylint, black, flake8]
env:
CXX_FLAGS: "-pedantic -Wextra -Woverloaded-virtual -Wno-unknown-pragmas"
NEST_VPATH: "build"
Expand Down
62 changes: 29 additions & 33 deletions build_support/check_copyright_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,48 +55,46 @@ def eprint(*args, **kwargs):

try:
heuristic_folders = "nest nestkernel build_support models .git"
if 'NEST_SOURCE' not in os.environ:
if "NEST_SOURCE" not in os.environ:
if all([name in os.listdir() for name in heuristic_folders.split()]):
os.environ['NEST_SOURCE'] = '.'
os.environ["NEST_SOURCE"] = "."
else:
print("Script does not seem to be called from the NEST repository root.")
source_dir = os.environ['NEST_SOURCE']
source_dir = os.environ["NEST_SOURCE"]
except KeyError:
print("Please make NEST_SOURCE environment variable to point to " +
"the source tree you want to check!")
print("Please make NEST_SOURCE environment variable to point to the source tree you want to check!")
sys.exit(EXIT_NO_SOURCE)

exclude_dirs = [
'libltdl',
'.git',
'CMakeFiles',
'result', # ignore files in $NEST_RESULT of CI builds
'thirdparty',
"libltdl",
".git",
"CMakeFiles",
"result", # ignore files in $NEST_RESULT of CI builds
"thirdparty",
]

# match all file names against these regular expressions. if a match
# is found the file is excluded from the check
exclude_file_patterns = [r'\.#.*', '#.*', '.*~', '.*.bak']
exclude_file_patterns = [r"\.#.*", "#.*", ".*~", ".*.bak"]
exclude_file_regex = [re.compile(pattern) for pattern in exclude_file_patterns]

exclude_files = [
'doc/copyright_header.cpp',
'doc/copyright_header.py',
'nest/static_modules.h',
'pynest/pynestkernel.cpp',
'get-pip.py'
"doc/copyright_header.cpp",
"doc/copyright_header.py",
"nest/static_modules.h",
"pynest/pynestkernel.cpp",
"get-pip.py",
]

templates = {
('cc', 'cpp', 'h', 'sli'): 'cpp',
('py', 'pyx', 'pxd'): 'py',
("cc", "cpp", "h", "sli"): "cpp",
("py", "pyx", "pxd"): "py",
}

template_contents = {}

for extensions, template_ext in templates.items():
template_name = "{0}/doc/copyright_header.{1}".format(source_dir,
template_ext)
template_name = "{0}/doc/copyright_header.{1}".format(source_dir, template_ext)
with open(template_name) as template_file:
template = template_file.readlines()
for ext in extensions:
Expand All @@ -105,7 +103,6 @@ def eprint(*args, **kwargs):
total_files = 0
total_errors = 0
for dirpath, _, fnames in os.walk(source_dir):

if any([exclude_dir in dirpath for exclude_dir in exclude_dirs]):
continue

Expand All @@ -119,34 +116,33 @@ def eprint(*args, **kwargs):

tested_file = os.path.join(dirpath, fname)

if any([exclude_file in tested_file
for exclude_file in exclude_files]):
if any([exclude_file in tested_file for exclude_file in exclude_files]):
continue

with open(tested_file, encoding='utf-8') as source_file:
with open(tested_file, encoding="utf-8") as source_file:
total_files += 1
for template_line in template_contents[extension]:
try:
line_src = source_file.readline()
except UnicodeDecodeError as err:
print("Unable to decode bytes in '{0}': {1}".format(
tested_file, err))
print("Unable to decode bytes in '{0}': {1}".format(tested_file, err))
total_errors += 1
break
if (extension == 'py' and
line_src.strip() == '#!/usr/bin/env python3'):
if extension == "py" and line_src.strip() == "#!/usr/bin/env python3":
line_src = source_file.readline()
line_exp = template_line.replace('{{file_name}}', fname)
line_exp = template_line.replace("{{file_name}}", fname)
if line_src != line_exp:
fname = os.path.relpath(tested_file)
eprint("[COPY] {0}: expected '{1}', found '{2}'.".format(
fname, line_exp.rstrip('\n'), line_src.rstrip('\n')))
eprint(
"[COPY] {0}: expected '{1}', found '{2}'.".format(
fname, line_exp.rstrip("\n"), line_src.rstrip("\n")
)
)
print("... {}\\n".format(fname))
total_errors += 1
break

print("{0} out of {1} files have an erroneous copyright header.".format(
total_errors, total_files))
print("{0} out of {1} files have an erroneous copyright header.".format(total_errors, total_files))

if total_errors > 0:
sys.exit(EXIT_BAD_HEADER)
Expand Down
16 changes: 7 additions & 9 deletions build_support/check_unused_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,14 @@ def eprint(*args, **kwargs):

try:
heuristic_folders = "nest nestkernel build_support models .git"
if 'NEST_SOURCE' not in os.environ:
if "NEST_SOURCE" not in os.environ:
if all([name in os.listdir() for name in heuristic_folders.split()]):
os.environ['NEST_SOURCE'] = '.'
os.environ["NEST_SOURCE"] = "."
else:
print("Script does not seem to be called from the NEST repository root.")
source_dir = os.environ['NEST_SOURCE']
source_dir = os.environ["NEST_SOURCE"]
except KeyError:
eprint("Please make NEST_SOURCE environment variable to point to " +
"the source tree you want to check!")
eprint("Please make NEST_SOURCE environment variable to point to the source tree you want to check!")
sys.exit(EXIT_NO_SOURCE)

# Base names of files that contain const Name definitions
Expand Down Expand Up @@ -100,9 +99,8 @@ def get_names(fname, pattern):

for h, s in zip(names_header, names_source):
if h != s:
eprint("[NAME] {}: inconsistent declaration: {} != {}".format(
names_file, h, s))
print("... {}\\n".format(names_file))
eprint(f"[NAME] {names_file}: inconsistent declaration: {h} != {s}")
print(f"... {names_file}\\n")
sys.exit(EXIT_NAME_H_CPP_MISMATCH)
else:
names_defined.add(h)
Expand All @@ -128,7 +126,7 @@ def get_names(fname, pattern):
if len(names_unused) != 0:
msg = "unused Name definition(s): " + ", ".join(names_unused)
eprint("[NAME] " + msg)
print("... {}\\n".format(msg))
print(f"... {msg}\\n")
sys.exit(EXIT_UNUSED_NAME)


Expand Down
Loading