From 652348a3bed7ab393bf9edceef3dabf1a1400c66 Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 3 Dec 2020 13:07:09 -0800 Subject: [PATCH] fix generator test and linter (#323) * fix generator test * generator cleanup --- .github/workflows/tests.yml | 2 +- .pre-commit-config.yaml | 3 +- can/tests/test_generator.sh | 13 --------- generator/generator.py | 58 ++++++++++++++++++++----------------- generator/test_generator.py | 16 ++++++++++ 5 files changed, 50 insertions(+), 42 deletions(-) delete mode 100755 can/tests/test_generator.sh create mode 100755 generator/test_generator.py diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7a64fa2..5fcf6b4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,7 +21,7 @@ jobs: docker run opendbc bash -c "cd opendbc && git init && git add -A && pre-commit run --all" - name: Generator test run: | - docker run opendbc bash -c "cd opendbc/can/tests/; PYTHONPATH=/ ./test_generator.sh" + docker run opendbc bash -c "cd opendbc/generator && ./test_generator.py" - name: Unit tests run: | docker run opendbc bash -c "python -m unittest discover opendbc" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2f13d51..cccdfd7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,7 +16,8 @@ repos: - id: flake8 exclude: 'site_scons/' args: - - --ignore=E111,E114,E121,E124,E302,E501,E741 + - --select=F,E112,E113,E304,E501,E502,E701,E702,E703,E71,E72,E731,W191,W6 + - --max-line-length=240 - --statistics - repo: local hooks: diff --git a/can/tests/test_generator.sh b/can/tests/test_generator.sh deleted file mode 100755 index c4df547..0000000 --- a/can/tests/test_generator.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash -e - -cd ../../generator/ - -# run generator -./generator.py - -if [ -n "$(git status --untracked-files=no --porcelain)" ]; then - echo "Unexpected changes after running generator.py"; - exit 1 -else - echo "Success"; -fi diff --git a/generator/generator.py b/generator/generator.py index ca7daac..ceed4a0 100755 --- a/generator/generator.py +++ b/generator/generator.py @@ -3,47 +3,51 @@ import os import re cur_path = os.path.dirname(os.path.realpath(__file__)) -generator_path = os.path.join(cur_path, '../') +opendbc_root = os.path.join(cur_path, '../') include_pattern = re.compile(r'CM_ "IMPORT (.*?)"') -def read_dbc(dir_name, filename): - with open(os.path.join(dir_name, filename)) as file_in: - return file_in.read() +def read_dbc(src_dir, filename): + with open(os.path.join(src_dir, filename)) as file_in: + return file_in.read() -def create_dbc(dir_name, filename): - dbc_file_in = read_dbc(dir_name, filename) +def create_dbc(src_dir, filename, output_path): + dbc_file_in = read_dbc(src_dir, filename) - includes = include_pattern.findall(dbc_file_in) + includes = include_pattern.findall(dbc_file_in) - output_filename = filename.replace('.dbc', '_generated.dbc') - output_file_location = os.path.join(generator_path, output_filename) + output_filename = filename.replace('.dbc', '_generated.dbc') + output_file_location = os.path.join(output_path, output_filename) - with open(output_file_location, 'w') as dbc_file_out: - dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n') + with open(output_file_location, 'w') as dbc_file_out: + dbc_file_out.write('CM_ "AUTOGENERATED FILE, DO NOT EDIT"\n') - for include_filename in reversed(includes): - include_file_header = '\n\nCM_ "Imported file %s starts here"\n' % include_filename - dbc_file_out.write(include_file_header) + for include_filename in reversed(includes): + include_file_header = '\n\nCM_ "Imported file %s starts here"\n' % include_filename + dbc_file_out.write(include_file_header) - include_file = read_dbc(dir_name, include_filename) - dbc_file_out.write(include_file) + include_file = read_dbc(src_dir, include_filename) + dbc_file_out.write(include_file) - dbc_file_out.write('\nCM_ "%s starts here"\n' % filename) + dbc_file_out.write('\nCM_ "%s starts here"\n' % filename) - core_dbc = include_pattern.sub('', dbc_file_in) - dbc_file_out.write(core_dbc) + core_dbc = include_pattern.sub('', dbc_file_in) + dbc_file_out.write(core_dbc) -for dir_name, _, filenames in os.walk(cur_path): - if dir_name == cur_path: +def create_all(output_path): + for src_dir, _, filenames in os.walk(cur_path): + if src_dir == cur_path: + continue + + #print(src_dir) + for filename in filenames: + if filename.startswith('_') or not filename.endswith('.dbc'): continue - print(dir_name) - for filename in filenames: - if filename.startswith('_'): - continue + #print(filename) + create_dbc(src_dir, filename, output_path) - print(filename) - create_dbc(dir_name, filename) +if __name__ == "__main__": + create_all(opendbc_root) diff --git a/generator/test_generator.py b/generator/test_generator.py new file mode 100755 index 0000000..fb95344 --- /dev/null +++ b/generator/test_generator.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +import os +import filecmp +import tempfile +from opendbc.generator.generator import create_all, opendbc_root + + +def test_generator(): + with tempfile.TemporaryDirectory() as d: + create_all(d) + + ignore = [f for f in os.listdir(opendbc_root) if not f.endswith('_generated.dbc')] + comp = filecmp.dircmp(opendbc_root, d, ignore=ignore) + assert len(comp.diff_files) == 0, f"Different files: {comp.diff_files}" + +test_generator()