fix generator test and linter (#323)

* fix generator test

* generator cleanup
master
Adeeb Shihadeh 2020-12-03 13:07:09 -08:00 committed by GitHub
parent fe85e93c0e
commit 652348a3be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 42 deletions

View File

@ -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"

View File

@ -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:

View File

@ -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

View File

@ -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)

View File

@ -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()