mirror of
https://pagure.io/fedora-qa/os-autoinst-distri-fedora.git
synced 2025-10-17 17:18:49 +00:00
Add a simple perl lint script and fix all the errors it found
This adds a script that checks for common whitespace issues (per our house style) and can (if run with --write) correct some of them. It adds a run of the script (without --write) to the tox config, and includes fixes for all the issues the script discovered in the existing tests and libs. Signed-off-by: Adam Williamson <awilliam@redhat.com>
This commit is contained in:
parent
ad5eb54715
commit
f0b9bd3e94
@ -467,5 +467,4 @@ sub report_bug_text {
|
|||||||
|
|
||||||
# Quit anaconda
|
# Quit anaconda
|
||||||
type_string "4\n";
|
type_string "4\n";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
110
perllint.py
Executable file
110
perllint.py
Executable file
@ -0,0 +1,110 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# Copyright Red Hat
|
||||||
|
#
|
||||||
|
# This file is part of os-autoinst-distri-fedora.
|
||||||
|
#
|
||||||
|
# os-autoinst-distri-fedora is free software; you can redistribute it
|
||||||
|
# and/or modify it under the terms of the GNU General Public License
|
||||||
|
# as published by the Free Software Foundation, either version 2 of
|
||||||
|
# the License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# Author: Adam Williamson <awilliam@redhat.com>
|
||||||
|
|
||||||
|
"""This is a simple linter for perl files in the repository. It fails
|
||||||
|
if any line contains a tab, ends with whitespace characters (which
|
||||||
|
includes lines containing *only* whitespace characters), or starts
|
||||||
|
with a number of spaces that is not divisible by four. If run with
|
||||||
|
--write, it will strip trailing whitespace (including turning lines
|
||||||
|
that contain only whitespace into blank lines). It does not currently
|
||||||
|
attempt to replace tabs or correct leading whitespace.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import glob
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def run(write):
|
||||||
|
"""
|
||||||
|
Main run function.
|
||||||
|
"""
|
||||||
|
exitcode = 0
|
||||||
|
scriptloc = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
filenames = glob.glob(f"{scriptloc}/**/*.pm", recursive=True)
|
||||||
|
for filename in filenames:
|
||||||
|
if check_file(filename, write):
|
||||||
|
exitcode = 1
|
||||||
|
if exitcode == 0:
|
||||||
|
print("No errors found!")
|
||||||
|
sys.exit(exitcode)
|
||||||
|
|
||||||
|
|
||||||
|
def check_file(filename, write):
|
||||||
|
"""
|
||||||
|
Do the checks. Returns false if there is no failure, true if there
|
||||||
|
is a failure.
|
||||||
|
"""
|
||||||
|
failed = False
|
||||||
|
with open(filename, "r", encoding="UTF-8") as readfh:
|
||||||
|
lines = readfh.readlines()
|
||||||
|
newlines = []
|
||||||
|
for (num, line) in enumerate(lines, 1):
|
||||||
|
# tab check
|
||||||
|
if "\t" in line:
|
||||||
|
print(f"Line {num} of file {filename} contains a tab!")
|
||||||
|
failed = True
|
||||||
|
|
||||||
|
# trailing whitespace check and replace
|
||||||
|
newline = line.rstrip("\n").rstrip(" \t") + "\n"
|
||||||
|
if newline != line:
|
||||||
|
print(f"Line {num} of file {filename} ends with a space or tab!")
|
||||||
|
failed = True
|
||||||
|
if write:
|
||||||
|
# if we're writing changes, feed the *changed* line
|
||||||
|
# to the next checks
|
||||||
|
line = newline
|
||||||
|
|
||||||
|
# correct number of starting spaces check
|
||||||
|
spaces = 0
|
||||||
|
for char in line:
|
||||||
|
if char != " ":
|
||||||
|
break
|
||||||
|
spaces += 1
|
||||||
|
if spaces > 0 and spaces % 4 != 0:
|
||||||
|
print(
|
||||||
|
f"Line {num} of file {filename} starts with an invalid number of spaces!"
|
||||||
|
)
|
||||||
|
failed = True
|
||||||
|
|
||||||
|
# write the line back to newlines
|
||||||
|
if write:
|
||||||
|
newlines.append(line)
|
||||||
|
|
||||||
|
if write and newlines != lines:
|
||||||
|
with open(filename, "w", encoding="UTF-8") as writefh:
|
||||||
|
writefh.writelines(newlines)
|
||||||
|
|
||||||
|
return failed
|
||||||
|
|
||||||
|
|
||||||
|
# Parse args and call run().
|
||||||
|
if len(sys.argv) > 2:
|
||||||
|
sys.exit("Too many arguments!")
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
if sys.argv[1] == "--write":
|
||||||
|
run(write=True)
|
||||||
|
else:
|
||||||
|
sys.exit("Invalid argument! Only valid argument is --write")
|
||||||
|
|
||||||
|
# no args
|
||||||
|
run(write=False)
|
1
tox.ini
1
tox.ini
@ -14,6 +14,7 @@ deps =
|
|||||||
commands=
|
commands=
|
||||||
python ./fifloader.py --clean templates.fif.json templates-updates.fif.json
|
python ./fifloader.py --clean templates.fif.json templates-updates.fif.json
|
||||||
python ./check-needles.py
|
python ./check-needles.py
|
||||||
|
python ./perllint.py
|
||||||
py.test unittests/
|
py.test unittests/
|
||||||
py.test --cov-report term-missing --cov-report xml --cov fifloader unittests/
|
py.test --cov-report term-missing --cov-report xml --cov fifloader unittests/
|
||||||
diff-cover coverage.xml --fail-under=90 --compare-branch=origin/main
|
diff-cover coverage.xml --fail-under=90 --compare-branch=origin/main
|
||||||
|
Loading…
Reference in New Issue
Block a user