Eliminate pylint warnings from generate_pins.py (#1731)
It also contains small refactoring steps: * Added a main() function - instead of just writing all the code into the "if name" block - to avoids creating global variables that could affect other functions. * Added a `write_pins_to_files` function - which writes the generated pins into the output JS and C++ files - to avoid too many local variables. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
This commit is contained in:
@@ -22,22 +22,38 @@ It's expecting to be run from the targets/mbedos5 directory.
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
from pycparser import parse_file, c_ast, c_generator
|
import argparse
|
||||||
from pycparserext.ext_c_parser import GnuCParser
|
|
||||||
|
|
||||||
from simpleeval import SimpleEval, DEFAULT_OPERATORS
|
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
|
|
||||||
import argparse
|
|
||||||
import json
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from simpleeval import SimpleEval, DEFAULT_OPERATORS
|
||||||
|
from pycparserext.ext_c_parser import GnuCParser
|
||||||
|
from pycparser import parse_file, c_ast, c_generator
|
||||||
|
|
||||||
# import mbed tools
|
# import mbed tools
|
||||||
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mbed-os'))
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'mbed-os'))
|
||||||
from tools.targets import Target
|
from tools.targets import Target
|
||||||
|
|
||||||
|
LICENSE = '''/* Copyright JS Foundation and other contributors, http://js.foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the \"License\");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an \"AS IS\" BASIS
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
* This file is generated by generate_pins.py. Please do not modify.
|
||||||
|
*/
|
||||||
|
'''
|
||||||
|
|
||||||
|
|
||||||
def find_file(root_dir, directories, name):
|
def find_file(root_dir, directories, name):
|
||||||
"""
|
"""
|
||||||
@@ -53,41 +69,49 @@ def find_file(root_dir, directories, name):
|
|||||||
|
|
||||||
for root, dirs, files in os.walk(root_dir, topdown=True):
|
for root, dirs, files in os.walk(root_dir, topdown=True):
|
||||||
# modify dirs in place
|
# modify dirs in place
|
||||||
dirs[:] = filter(lambda x: x in directories or not x.startswith('TARGET_'), dirs)
|
dirs[:] = [directory for directory in dirs if directory in directories or not directory.startswith('TARGET_')]
|
||||||
|
|
||||||
if name in files:
|
if name in files:
|
||||||
return os.path.join(root, name)
|
return os.path.join(root, name)
|
||||||
|
|
||||||
|
|
||||||
def enumerate_includes(root_dir, directories):
|
def enumerate_includes(root_dir, directories):
|
||||||
"""
|
"""
|
||||||
Walk through the directory tree, starting at root_dir, and enumerate all
|
Walk through the directory tree, starting at root_dir, and enumerate all
|
||||||
valid include directories.
|
valid include directories.
|
||||||
"""
|
"""
|
||||||
for root, dirs, files in os.walk(root_dir, topdown=True):
|
for root, dirs, _ in os.walk(root_dir, topdown=True):
|
||||||
# modify dirs in place
|
# modify dirs in place
|
||||||
dirs[:] = filter(lambda x: x in directory_labels
|
dirs[:] = [dir_label for dir_label in dirs
|
||||||
or ( not x.startswith('TARGET_')
|
if dir_label in directories
|
||||||
and not x.startswith('TOOLCHAIN_')), dirs)
|
or (not dir_label.startswith('TARGET_')
|
||||||
|
and not dir_label.startswith('TOOLCHAIN_'))]
|
||||||
yield root
|
yield root
|
||||||
|
|
||||||
|
|
||||||
class TypeDeclVisitor(c_ast.NodeVisitor):
|
class TypeDeclVisitor(c_ast.NodeVisitor):
|
||||||
def __init__(self, filter_names=[]):
|
"""
|
||||||
self.names = filter_names
|
A TypeDecl visitor class that walks the ast and calls a visitor function for every node found.
|
||||||
|
"""
|
||||||
|
def __init__(self, filter_names=None):
|
||||||
|
self.names = filter_names or []
|
||||||
|
|
||||||
def visit(self, node):
|
def visit(self, node):
|
||||||
value = None
|
value = None
|
||||||
|
|
||||||
if node.__class__.__name__ == "TypeDecl":
|
if node.__class__.__name__ == "TypeDecl":
|
||||||
value = self.visit_TypeDecl(node)
|
value = self.visit_typedecl(node)
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
for name, c in node.children():
|
for _, child_node in node.children():
|
||||||
value = value or self.visit(c)
|
value = value or self.visit(child_node)
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def visit_TypeDecl(self, node):
|
def visit_typedecl(self, node):
|
||||||
|
"""
|
||||||
|
Visit a node.
|
||||||
|
"""
|
||||||
if node.declname in self.names:
|
if node.declname in self.names:
|
||||||
c_gen = c_generator.CGenerator()
|
c_gen = c_generator.CGenerator()
|
||||||
pins = {}
|
pins = {}
|
||||||
@@ -96,7 +120,7 @@ class TypeDeclVisitor(c_ast.NodeVisitor):
|
|||||||
operators[ast.BitOr] = lambda a, b: a | b
|
operators[ast.BitOr] = lambda a, b: a | b
|
||||||
operators[ast.LShift] = lambda a, b: a << b
|
operators[ast.LShift] = lambda a, b: a << b
|
||||||
operators[ast.RShift] = lambda a, b: a << b
|
operators[ast.RShift] = lambda a, b: a << b
|
||||||
evaluator = SimpleEval(DEFAULT_OPERATORS )
|
evaluator = SimpleEval(DEFAULT_OPERATORS)
|
||||||
|
|
||||||
for pin in node.type.values.enumerators:
|
for pin in node.type.values.enumerators:
|
||||||
expr = c_gen.visit(pin.value)
|
expr = c_gen.visit(pin.value)
|
||||||
@@ -111,6 +135,7 @@ class TypeDeclVisitor(c_ast.NodeVisitor):
|
|||||||
|
|
||||||
return pins
|
return pins
|
||||||
|
|
||||||
|
|
||||||
def enumerate_pins(c_source_file, include_dirs, definitions):
|
def enumerate_pins(c_source_file, include_dirs, definitions):
|
||||||
"""
|
"""
|
||||||
Enumerate pins specified in PinNames.h, by looking for a PinName enum
|
Enumerate pins specified in PinNames.h, by looking for a PinName enum
|
||||||
@@ -122,18 +147,56 @@ def enumerate_pins(c_source_file, include_dirs, definitions):
|
|||||||
gcc_args += ['-I' + directory for directory in include_dirs]
|
gcc_args += ['-I' + directory for directory in include_dirs]
|
||||||
|
|
||||||
gcc_args += ['-D' + definition for definition in definitions]
|
gcc_args += ['-D' + definition for definition in definitions]
|
||||||
ast = parse_file(c_source_file,
|
parsed_ast = parse_file(c_source_file,
|
||||||
use_cpp=True,
|
use_cpp=True,
|
||||||
cpp_path='arm-none-eabi-gcc',
|
cpp_path='arm-none-eabi-gcc',
|
||||||
cpp_args=gcc_args,
|
cpp_args=gcc_args,
|
||||||
parser=GnuCParser())
|
parser=GnuCParser())
|
||||||
|
|
||||||
# now, walk the AST
|
# now, walk the AST
|
||||||
v = TypeDeclVisitor(['PinName'])
|
visitor = TypeDeclVisitor(['PinName'])
|
||||||
return v.visit(ast)
|
return visitor.visit(parsed_ast)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
def write_pins_to_files(pins, out_js_file, out_cpp_file):
|
||||||
|
"""
|
||||||
|
Write the generated pins for a specified mbed board into the output JS and C++ files.
|
||||||
|
"""
|
||||||
|
out_js = '\r\n'.join(['var %s = %s;' % pin for pin in pins])
|
||||||
|
out_js_file.write(out_js)
|
||||||
|
|
||||||
|
count = '''
|
||||||
|
unsigned int jsmbed_js_magic_string_count = {};
|
||||||
|
'''.format(len(pins))
|
||||||
|
|
||||||
|
lengths = ',\n '.join(str(len(pin[0])) for pin in pins)
|
||||||
|
lenghts_source = '''
|
||||||
|
unsigned int jsmbed_js_magic_string_lengths[] = {
|
||||||
|
%s
|
||||||
|
};
|
||||||
|
''' % lengths
|
||||||
|
|
||||||
|
magic_values = ',\n '.join(str(pin[1]) for pin in pins)
|
||||||
|
magic_source = '''
|
||||||
|
unsigned int jsmbed_js_magic_string_values[] = {
|
||||||
|
%s
|
||||||
|
};
|
||||||
|
''' % magic_values
|
||||||
|
|
||||||
|
magic_strings = ',\n '.join('"' + pin[0] + '"' for pin in pins)
|
||||||
|
magic_string_source = '''
|
||||||
|
const char * jsmbed_js_magic_strings[] = {
|
||||||
|
%s
|
||||||
|
};
|
||||||
|
''' % magic_strings
|
||||||
|
|
||||||
|
out_cpp_file.write(LICENSE + count + lenghts_source + magic_source + magic_string_source)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
"""
|
||||||
|
Perform the main function of this program
|
||||||
|
"""
|
||||||
if not os.path.exists('./mbed-os'):
|
if not os.path.exists('./mbed-os'):
|
||||||
print("Fatal: mbed-os directory does not exist.")
|
print("Fatal: mbed-os directory does not exist.")
|
||||||
print("Try running 'make getlibs'")
|
print("Try running 'make getlibs'")
|
||||||
@@ -174,53 +237,11 @@ if __name__ == "__main__":
|
|||||||
pins = enumerate_pins(pins_file, ['./tools'] + list(includes), defines)
|
pins = enumerate_pins(pins_file, ['./tools'] + list(includes), defines)
|
||||||
|
|
||||||
# first sort alphabetically, then by length.
|
# first sort alphabetically, then by length.
|
||||||
pins = [ (x, pins[x]) for x in pins] # turn dict into tuples, which can be sorted
|
pins = [(x, pins[x]) for x in pins] # turn dict into tuples, which can be sorted
|
||||||
pins = sorted(pins, key = lambda x: (len(x[0]), x[0].lower()))
|
pins = sorted(pins, key=lambda x: (len(x[0]), x[0].lower()))
|
||||||
|
|
||||||
out_file = '\r\n'.join(['var %s = %s;' % pin for pin in pins])
|
write_pins_to_files(pins, args.o, args.c)
|
||||||
args.o.write(out_file)
|
|
||||||
|
|
||||||
LICENSE = '''/* Copyright JS Foundation and other contributors, http://js.foundation
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the \"License\");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an \"AS IS\" BASIS
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*
|
|
||||||
* This file is generated by generate_pins.py. Please do not modify.
|
|
||||||
*/
|
|
||||||
'''
|
|
||||||
|
|
||||||
COUNT = '''
|
if __name__ == "__main__":
|
||||||
unsigned int jsmbed_js_magic_string_count = {};
|
main()
|
||||||
'''.format(len(pins))
|
|
||||||
|
|
||||||
LENGTHS = ',\n '.join(str(len(pin[0])) for pin in pins)
|
|
||||||
LENGTHS_SOURCE = '''
|
|
||||||
unsigned int jsmbed_js_magic_string_lengths[] = {
|
|
||||||
%s
|
|
||||||
};
|
|
||||||
''' % LENGTHS
|
|
||||||
|
|
||||||
MAGIC_VALUES = ',\n '.join(str(pin[1]) for pin in pins)
|
|
||||||
MAGIC_SOURCE = '''
|
|
||||||
unsigned int jsmbed_js_magic_string_values[] = {
|
|
||||||
%s
|
|
||||||
};
|
|
||||||
''' % MAGIC_VALUES
|
|
||||||
|
|
||||||
MAGIC_STRINGS = ',\n '.join('"' + pin[0] + '"' for pin in pins)
|
|
||||||
MAGIC_STRING_SOURCE = '''
|
|
||||||
const char * jsmbed_js_magic_strings[] = {
|
|
||||||
%s
|
|
||||||
};
|
|
||||||
''' % MAGIC_STRINGS
|
|
||||||
|
|
||||||
args.c.write(LICENSE + COUNT + LENGTHS_SOURCE + MAGIC_SOURCE + MAGIC_STRING_SOURCE)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user