Skip to content

Commit

Permalink
Add rust file support to verify_headers.py and check crates/ (#11850)
Browse files Browse the repository at this point in the history
This commit updates the verify_headers.py file to run the validation on
the rust files in crates/ too. After noticing we were missing a file
header in one of the rust source files this has become more necessary as
it's easy to potentially overlook the standard required file header for
Qiskit source files.
  • Loading branch information
mtreinish committed Feb 21, 2024
1 parent 6a8bdb7 commit e448134
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
12 changes: 12 additions & 0 deletions crates/accelerate/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// This code is part of Qiskit.
//
// (C) Copyright IBM 2024
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

use pyo3::prelude::*;

use faer::prelude::*;
Expand Down
39 changes: 32 additions & 7 deletions tools/verify_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

# pylint: disable=too-many-return-statements

"""Utility script to verify qiskit copyright file headers"""

import argparse
Expand All @@ -21,6 +23,8 @@

# regex for character encoding from PEP 263
pep263 = re.compile(r"^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)")
line_start = re.compile(r"^(\/\/|#) This code is part of Qiskit.$")
copyright_line = re.compile(r"^(\/\/|#) \(C\) Copyright IBM 20")


def discover_files(code_paths):
Expand All @@ -37,6 +41,7 @@ def discover_files(code_paths):
subfile.endswith(".py")
or subfile.endswith(".pyx")
or subfile.endswith(".pxd")
or subfile.endswith(".rs")
):
out_paths.append(os.path.join(dir_path, subfile))
return out_paths
Expand All @@ -55,6 +60,18 @@ def validate_header(file_path):
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
header_rs = """// This code is part of Qiskit.
//
"""
apache_text_rs = """//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE.txt file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.
"""
count = 0
with open(file_path, encoding="utf8") as fd:
Expand All @@ -66,15 +83,23 @@ def validate_header(file_path):
return file_path, False, "Header not found in first 5 lines"
if count <= 2 and pep263.match(line):
return file_path, False, "Unnecessary encoding specification (PEP 263, 3120)"
if line == "# This code is part of Qiskit.\n":
if line_start.search(line):
start = index
break
if "".join(lines[start : start + 2]) != header:
return (file_path, False, "Header up to copyright line does not match: %s" % header)
if not lines[start + 2].startswith("# (C) Copyright IBM 20"):
return (file_path, False, "Header copyright line not found")
if "".join(lines[start + 3 : start + 11]) != apache_text:
return (file_path, False, "Header apache text string doesn't match:\n %s" % apache_text)
if file_path.endswith(".rs"):
if "".join(lines[start : start + 2]) != header_rs:
return (file_path, False, "Header up to copyright line does not match: %s" % header)
if not copyright_line.search(lines[start + 2]):
return (file_path, False, "Header copyright line not found")
if "".join(lines[start + 3 : start + 11]) != apache_text_rs:
return (file_path, False, "Header apache text string doesn't match:\n %s" % apache_text)
else:
if "".join(lines[start : start + 2]) != header:
return (file_path, False, "Header up to copyright line does not match: %s" % header)
if not copyright_line.search(lines[start + 2]):
return (file_path, False, "Header copyright line not found")
if "".join(lines[start + 3 : start + 11]) != apache_text:
return (file_path, False, "Header apache text string doesn't match:\n %s" % apache_text)
return (file_path, True, None)


Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ commands =
# This line is commented out until #6649 merges. We can't run this currently
# via tox because tox doesn't support globbing
# pylint -rn --disable='invalid-name,missing-module-docstring,redefined-outer-name' examples/python/*.py
python {toxinidir}/tools/verify_headers.py qiskit test tools examples
python {toxinidir}/tools/verify_headers.py qiskit test tools examples crates
python {toxinidir}/tools/find_optional_imports.py
python {toxinidir}/tools/find_stray_release_notes.py
reno -q lint
Expand All @@ -53,7 +53,7 @@ commands =
-git fetch -q https://github.com/Qiskit/qiskit.git :lint_incr_latest
python {toxinidir}/tools/pylint_incr.py -rn -j4 -sn --paths :/qiskit/*.py :/test/*.py :/tools/*.py
python {toxinidir}/tools/pylint_incr.py -rn -j4 -sn --disable='invalid-name,missing-module-docstring,redefined-outer-name' --paths :(glob,top)examples/python/*.py
python {toxinidir}/tools/verify_headers.py qiskit test tools examples
python {toxinidir}/tools/verify_headers.py qiskit test tools examples crates
python {toxinidir}/tools/find_optional_imports.py
python {toxinidir}/tools/find_stray_release_notes.py
reno -q lint
Expand Down

0 comments on commit e448134

Please sign in to comment.