mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-18 05:42:35 +00:00
Merge pull request #6964 from davidhorstmann-arm/code-style-improvements
Improvements to code style script
This commit is contained in:
commit
c567b74e6e
@ -19,8 +19,6 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
# Line length options
|
|
||||||
|
|
||||||
# Wrap lines at 100 characters
|
# Wrap lines at 100 characters
|
||||||
code_width = 100
|
code_width = 100
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Check or fix the code style by running Uncrustify.
|
"""Check or fix the code style by running Uncrustify.
|
||||||
|
|
||||||
Note: The code style enforced by this script is not yet introduced to
|
This script must be run from the root of a Git work tree containing Mbed TLS.
|
||||||
Mbed TLS. At present this script will only be used to prepare for a future
|
|
||||||
change of code style.
|
|
||||||
"""
|
"""
|
||||||
# Copyright The Mbed TLS Contributors
|
# Copyright The Mbed TLS Contributors
|
||||||
# SPDX-License-Identifier: Apache-2.0
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
@ -20,7 +18,6 @@ change of code style.
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
import argparse
|
import argparse
|
||||||
import io
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -31,12 +28,10 @@ UNCRUSTIFY_SUPPORTED_VERSION = "0.75.1"
|
|||||||
CONFIG_FILE = ".uncrustify.cfg"
|
CONFIG_FILE = ".uncrustify.cfg"
|
||||||
UNCRUSTIFY_EXE = "uncrustify"
|
UNCRUSTIFY_EXE = "uncrustify"
|
||||||
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
|
UNCRUSTIFY_ARGS = ["-c", CONFIG_FILE]
|
||||||
STDOUT_UTF8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
|
|
||||||
STDERR_UTF8 = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
|
|
||||||
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
|
CHECK_GENERATED_FILES = "tests/scripts/check-generated-files.sh"
|
||||||
|
|
||||||
def print_err(*args):
|
def print_err(*args):
|
||||||
print("Error: ", *args, file=STDERR_UTF8)
|
print("Error: ", *args, file=sys.stderr)
|
||||||
|
|
||||||
# Match FILENAME(s) in "check SCRIPT (FILENAME...)"
|
# Match FILENAME(s) in "check SCRIPT (FILENAME...)"
|
||||||
CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)",
|
CHECK_CALL_RE = re.compile(r"\n\s*check\s+[^\s#$&*?;|]+([^\n#$&*?;|]+)",
|
||||||
@ -69,8 +64,8 @@ def get_src_files() -> List[str]:
|
|||||||
"tests/suites/*.function",
|
"tests/suites/*.function",
|
||||||
"scripts/data_files/*.fmt"]
|
"scripts/data_files/*.fmt"]
|
||||||
|
|
||||||
result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE, \
|
result = subprocess.run(git_ls_files_cmd, stdout=subprocess.PIPE,
|
||||||
stderr=STDERR_UTF8, check=False)
|
check=False)
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print_err("git ls-files returned: " + str(result.returncode))
|
print_err("git ls-files returned: " + str(result.returncode))
|
||||||
@ -90,8 +85,9 @@ def get_uncrustify_version() -> str:
|
|||||||
"""
|
"""
|
||||||
Get the version string from Uncrustify
|
Get the version string from Uncrustify
|
||||||
"""
|
"""
|
||||||
result = subprocess.run([UNCRUSTIFY_EXE, "--version"], \
|
result = subprocess.run([UNCRUSTIFY_EXE, "--version"],
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False)
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
||||||
|
check=False)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8"))
|
print_err("Could not get Uncrustify version:", str(result.stderr, "utf-8"))
|
||||||
return ""
|
return ""
|
||||||
@ -106,26 +102,25 @@ def check_style_is_correct(src_file_list: List[str]) -> bool:
|
|||||||
style_correct = True
|
style_correct = True
|
||||||
for src_file in src_file_list:
|
for src_file in src_file_list:
|
||||||
uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file]
|
uncrustify_cmd = [UNCRUSTIFY_EXE] + UNCRUSTIFY_ARGS + [src_file]
|
||||||
result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE, \
|
result = subprocess.run(uncrustify_cmd, stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE, check=False)
|
stderr=subprocess.PIPE, check=False)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print_err("Uncrustify returned " + str(result.returncode) + \
|
print_err("Uncrustify returned " + str(result.returncode) +
|
||||||
" correcting file " + src_file)
|
" correcting file " + src_file)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Uncrustify makes changes to the code and places the result in a new
|
# Uncrustify makes changes to the code and places the result in a new
|
||||||
# file with the extension ".uncrustify". To get the changes (if any)
|
# file with the extension ".uncrustify". To get the changes (if any)
|
||||||
# simply diff the 2 files.
|
# simply diff the 2 files.
|
||||||
diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"]
|
diff_cmd = ["diff", "-u", src_file, src_file + ".uncrustify"]
|
||||||
result = subprocess.run(diff_cmd, stdout=subprocess.PIPE, \
|
cp = subprocess.run(diff_cmd, check=False)
|
||||||
stderr=STDERR_UTF8, check=False)
|
|
||||||
if len(result.stdout) > 0:
|
if cp.returncode == 1:
|
||||||
print(src_file + " - Incorrect code style.", file=STDOUT_UTF8)
|
print(src_file + " changed - code style is incorrect.")
|
||||||
print("File changed - diff:", file=STDOUT_UTF8)
|
|
||||||
print(str(result.stdout, "utf-8"), file=STDOUT_UTF8)
|
|
||||||
style_correct = False
|
style_correct = False
|
||||||
else:
|
elif cp.returncode != 0:
|
||||||
print(src_file + " - OK.", file=STDOUT_UTF8)
|
raise subprocess.CalledProcessError(cp.returncode, cp.args,
|
||||||
|
cp.stdout, cp.stderr)
|
||||||
|
|
||||||
# Tidy up artifact
|
# Tidy up artifact
|
||||||
os.remove(src_file + ".uncrustify")
|
os.remove(src_file + ".uncrustify")
|
||||||
@ -139,12 +134,11 @@ def fix_style_single_pass(src_file_list: List[str]) -> bool:
|
|||||||
code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"]
|
code_change_args = UNCRUSTIFY_ARGS + ["--no-backup"]
|
||||||
for src_file in src_file_list:
|
for src_file in src_file_list:
|
||||||
uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file]
|
uncrustify_cmd = [UNCRUSTIFY_EXE] + code_change_args + [src_file]
|
||||||
result = subprocess.run(uncrustify_cmd, check=False, \
|
result = subprocess.run(uncrustify_cmd, check=False)
|
||||||
stdout=STDOUT_UTF8, stderr=STDERR_UTF8)
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
print_err("Uncrustify with file returned: " + \
|
print_err("Uncrustify with file returned: " +
|
||||||
str(result.returncode) + " correcting file " + \
|
str(result.returncode) + " correcting file " +
|
||||||
src_file)
|
src_file)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -160,7 +154,7 @@ def fix_style(src_file_list: List[str]) -> int:
|
|||||||
# Guard against future changes that cause the codebase to require
|
# Guard against future changes that cause the codebase to require
|
||||||
# more passes.
|
# more passes.
|
||||||
if not check_style_is_correct(src_file_list):
|
if not check_style_is_correct(src_file_list):
|
||||||
print("Code style still incorrect after second run of Uncrustify.")
|
print_err("Code style still incorrect after second run of Uncrustify.")
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
@ -172,9 +166,9 @@ def main() -> int:
|
|||||||
uncrustify_version = get_uncrustify_version().strip()
|
uncrustify_version = get_uncrustify_version().strip()
|
||||||
if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version:
|
if UNCRUSTIFY_SUPPORTED_VERSION not in uncrustify_version:
|
||||||
print("Warning: Using unsupported Uncrustify version '" +
|
print("Warning: Using unsupported Uncrustify version '" +
|
||||||
uncrustify_version + "'", file=STDOUT_UTF8)
|
uncrustify_version + "'")
|
||||||
print("Note: The only supported version is " +
|
print("Note: The only supported version is " +
|
||||||
UNCRUSTIFY_SUPPORTED_VERSION, file=STDOUT_UTF8)
|
UNCRUSTIFY_SUPPORTED_VERSION)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument('-f', '--fix', action='store_true',
|
parser.add_argument('-f', '--fix', action='store_true',
|
||||||
@ -203,6 +197,7 @@ def main() -> int:
|
|||||||
else:
|
else:
|
||||||
# Check mode
|
# Check mode
|
||||||
if check_style_is_correct(src_files):
|
if check_style_is_correct(src_files):
|
||||||
|
print("Checked {} files, style ok.".format(len(src_files)))
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
return 1
|
return 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user