diff --git a/.devcontainer/linux/Dockerfile b/.devcontainer/linux/Dockerfile index 3301d47d..1517f67a 100644 --- a/.devcontainer/linux/Dockerfile +++ b/.devcontainer/linux/Dockerfile @@ -33,6 +33,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ sudo \ jq \ yamllint \ + python3-autopep8 \ && curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \ echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \ apt-get update && apt-get install -y --no-install-recommends \ diff --git a/.github/workflows/formatting-checks.yml b/.github/workflows/formatting-checks.yml index dd38741d..8863a5ca 100644 --- a/.github/workflows/formatting-checks.yml +++ b/.github/workflows/formatting-checks.yml @@ -39,6 +39,9 @@ jobs: - name: Run YAML Format Checks run: sh scripts/yml_linter.sh + - name: Run Python Format Checks + run: sh scripts/check_py_formatting.sh + - name: License Banner Checks(.c files) run: sh scripts/check_license_banner_c.sh diff --git a/scripts/check_license_banner_conf_files.sh b/scripts/check_license_banner_conf_files.sh index fae8e4c9..81a342de 100644 --- a/scripts/check_license_banner_conf_files.sh +++ b/scripts/check_license_banner_conf_files.sh @@ -4,7 +4,7 @@ # # SPDX-License-Identifier: Apache-2.0 -# Checks all C/C++ files for license banner +# Checks license banner in all files that use '#' comment syntax set -e @@ -17,13 +17,13 @@ check_license_banner() { local file_header local start_line=1 - # Check first line and skip if it's a shebang line(only for .sh scripts) + # Skip shebang line if present (e.g. #!/bin/sh or #!/usr/bin/python3) local first_line first_line=$(head -n 1 "$file") - if [ "${first_line#\#\!/bin}" != "$first_line" ]; then + if [ "${first_line#\#\!/}"x != "${first_line}x" ]; then start_line=2 fi - + file_header=$(tail -n +$start_line "$file" | head -n 4) # Expected license banner @@ -44,14 +44,11 @@ echo "Checking license banners." ERROR_FOUND=0 for file in $(find . -type f \( -name 'CMakeLists.txt' -o -name '*.yaml' \ -o -name '*.yml' -o -name '*.awk' -o -name '*.conf' -o -name 'Kconfig*' \ - -o -name '.gitignore' -o -name '.gitmodules' -o -name '*.sh' -o -name '*.cmake' \) \ - ! -name 'utlist.h' \ + -o -name '.gitignore' -o -name '.gitmodules' -o -name '*.sh' -o -name '*.cmake' -o -name '*.py' \) \ ! -path './tests/Unity/*' \ ! -path './build/*' \ ! -path './wasm-micro-runtime/*' \ ! -path './ocre-sdk/*' \ - ! -path './src/shell/sha256/*' \ - ! -path './src/runtime/wamr-wasip1/ocre_api/utils/*' \ | sort); do if ! check_license_banner "$file"; then diff --git a/scripts/check_py_formatting.sh b/scripts/check_py_formatting.sh new file mode 100644 index 00000000..add15c69 --- /dev/null +++ b/scripts/check_py_formatting.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 + +# Python formatter and linter +# Checks all .py files agains autopep8 rules +# Usage: check_py_formatting.sh [-f] +# -f Fix files in place(formatting) + +set -e + +ARGUMENT="--diff --exit-code" + +if [ $# -eq 1 ]; then + if [ "$1" = "-f" ]; then + echo "Fixing files in place if necessary" + ARGUMENT="--in-place" + else + echo "Invalid option: $1" + exit 1 + fi +fi + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" +cd "$ROOT_DIR" + +echo "Checking Python formatting." + +find . -type f -name '*.py' \ + ! -path './tests/Unity/*' \ + ! -path './wasm-micro-runtime/*' \ + ! -path './ocre-sdk/*' \ + -print0 | xargs -0 autopep8 ${ARGUMENT} diff --git a/scripts/yml_linter_rules.yml b/scripts/yml_linter_rules.yml index fbd2432f..0753defb 100644 --- a/scripts/yml_linter_rules.yml +++ b/scripts/yml_linter_rules.yml @@ -6,9 +6,7 @@ extends: default rules: - line-length: - max: 150 - level: warning + line-length: disable indentation: spaces: 2 document-start: disable @@ -18,3 +16,4 @@ rules: empty-lines: max: 1 truthy: {check-keys: false} + comments-indentation: disable diff --git a/tests_hw/groups/demo/testcase.py b/tests_hw/groups/demo/testcase.py index 260b4c8b..76825892 100755 --- a/tests_hw/groups/demo/testcase.py +++ b/tests_hw/groups/demo/testcase.py @@ -1,10 +1,14 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import sys sys.path.append("../..") -import pexpect -import testlib +import pexpect # noqa: E402 +import testlib # noqa: E402 """ This testcase is to be used following the flashing of the default demo sample to a board. @@ -24,6 +28,7 @@ "Demo completed successfully", ] + def main(): serial_conn, pex = testlib.setup('/dev/ttyACM0') serial_conn.send_break() @@ -43,6 +48,6 @@ def main(): testlib.format_runtime_output(runtime_output, "Entire") testlib.full_exit(serial_conn, 0) - + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/groups/flashValidation/flash_validation_hello_world.py b/tests_hw/groups/flashValidation/flash_validation_hello_world.py index 251403ab..11904208 100755 --- a/tests_hw/groups/flashValidation/flash_validation_hello_world.py +++ b/tests_hw/groups/flashValidation/flash_validation_hello_world.py @@ -1,4 +1,8 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import serial import time @@ -11,6 +15,7 @@ the string Hello World! appears in the output of that break command. """ + def main(): conn = serial.Serial('/dev/ttyACM0', 115200, timeout=10) @@ -43,4 +48,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/groups/mini/testcase.py b/tests_hw/groups/mini/testcase.py index 9a57bc01..2de891d6 100755 --- a/tests_hw/groups/mini/testcase.py +++ b/tests_hw/groups/mini/testcase.py @@ -1,10 +1,15 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import sys sys.path.append("../..") -import pexpect -import testlib +import pexpect # noqa: E402 +import testlib # noqa: E402 + """ This testcase is to be used following the flashing of the default mini sample to a board. @@ -17,6 +22,7 @@ "powered by Ocre", ] + def main(): serial_conn, pex = testlib.setup('/dev/ttyACM0') serial_conn.send_break() @@ -36,6 +42,6 @@ def main(): testlib.format_runtime_output(runtime_output, "Entire") testlib.full_exit(serial_conn, 0) - + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/groups/supervisor-helloWorld/clean.py b/tests_hw/groups/supervisor-helloWorld/clean.py index a751d939..cab24cd0 100755 --- a/tests_hw/groups/supervisor-helloWorld/clean.py +++ b/tests_hw/groups/supervisor-helloWorld/clean.py @@ -1,10 +1,14 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import sys sys.path.append("../..") -import pexpect -import testlib +import testlib # noqa: E402 +import pexpect # noqa: E402 def main(): @@ -21,5 +25,6 @@ def main(): testlib.format_runtime_output(runtime_output, "Cleanup") testlib.full_exit(serial_conn, 0) + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/groups/supervisor-helloWorld/setup.py b/tests_hw/groups/supervisor-helloWorld/setup.py index e8421105..b44f20bc 100755 --- a/tests_hw/groups/supervisor-helloWorld/setup.py +++ b/tests_hw/groups/supervisor-helloWorld/setup.py @@ -1,10 +1,15 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import sys sys.path.append("../..") -import testlib -import pexpect +import testlib # noqa: E402 +import pexpect # noqa: E402 + def main(): serial_conn, pex = testlib.setup('/dev/ttyACM0') @@ -13,7 +18,7 @@ def main(): pex.write(b'ocre create -n hello-world -k ocre:api hello-world.wasm\n') expect_index = pex.expect(["ocre:~$", pexpect.TIMEOUT], 30) runtime_output = bytes(pex.before).decode(errors='ignore') - + if (expect_index == 1): print("Container failed to create container in given timeout") testlib.format_runtime_output(runtime_output, "Failed") @@ -23,9 +28,9 @@ def main(): print("Failed to create container") testlib.format_runtime_output(runtime_output, "Failed") testlib.full_exit(serial_conn, 1) - + testlib.full_exit(serial_conn, 0) - + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/groups/supervisor-helloWorld/testcase.py b/tests_hw/groups/supervisor-helloWorld/testcase.py index 4b6bd759..55fd7ae1 100755 --- a/tests_hw/groups/supervisor-helloWorld/testcase.py +++ b/tests_hw/groups/supervisor-helloWorld/testcase.py @@ -1,10 +1,14 @@ #!/usr/bin/env python3 +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 import sys sys.path.append("../..") -import pexpect -import testlib +import pexpect # noqa: E402 +import testlib # noqa: E402 """ This testcase is to be used following the flashing of the supervisor sample to a board with the hello-world container put up. @@ -15,6 +19,7 @@ line = "powered by Ocre" + def main(): serial_conn, pex = testlib.setup('/dev/ttyACM0') @@ -36,6 +41,7 @@ def main(): testlib.format_runtime_output(runtime_output, "Entire") testlib.full_exit(serial_conn, 0) - + + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/tests_hw/testlib.py b/tests_hw/testlib.py index a138c633..97a5ccc9 100644 --- a/tests_hw/testlib.py +++ b/tests_hw/testlib.py @@ -1,10 +1,16 @@ +# @copyright Copyright (c) contributors to Project Ocre, +# which has been established as Project Ocre a Series of LF Projects, LLC +# +# SPDX-License-Identifier: Apache-2.0 + import serial import sys import pexpect import pexpect.fdpexpect from typing import Tuple -shell_prompt = "ocre:~$" +shell_prompt = "ocre:~$" + def setup(device_filepath: str) -> Tuple[serial.Serial, pexpect.fdpexpect.fdspawn]: conn = serial.Serial(device_filepath, 115200, timeout=10) @@ -29,4 +35,4 @@ def format_runtime_output(runtime_output: str, section: str) -> None: {runtime_output} ==========--------------------------========== """) - return \ No newline at end of file + return