From a62f90d8cc96b9dea9289ad6e420d1c0b16f6c36 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 24 Jan 2013 19:33:03 +0100 Subject: utils: added modtool --- gr-utils/src/python/modtool/modtool_makexml.py | 151 +++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 gr-utils/src/python/modtool/modtool_makexml.py (limited to 'gr-utils/src/python/modtool/modtool_makexml.py') diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py new file mode 100644 index 000000000..5a1a24f1b --- /dev/null +++ b/gr-utils/src/python/modtool/modtool_makexml.py @@ -0,0 +1,151 @@ +""" Automatically create XML bindings for GRC from block code """ + +import sys +import os +import re +import glob +from optparse import OptionGroup + +from modtool_base import ModTool +from parser_cc_block import ParserCCBlock +from grc_xml_generator import GRCXMLGenerator +from cmakefile_editor import CMakeFileEditor + +### Remove module ########################################################### +class ModToolMakeXML(ModTool): + """ Make XML file for GRC block bindings """ + name = 'makexml' + aliases = ('mx',) + def __init__(self): + ModTool.__init__(self) + + def setup_parser(self): + " Initialise the option parser for 'gr_modtool.py makexml' " + parser = ModTool.setup_parser(self) + parser.usage = '%prog makexml [options]. \n Call %prog without any options to run it interactively.' + ogroup = OptionGroup(parser, "Make XML module options") + ogroup.add_option("-p", "--pattern", type="string", default=None, + help="Filter possible choices for blocks to be parsed.") + ogroup.add_option("-y", "--yes", action="store_true", default=False, + help="Answer all questions with 'yes'. This can overwrite existing files!") + parser.add_option_group(ogroup) + return parser + + def setup(self): + ModTool.setup(self) + options = self.options + if options.pattern is not None: + self._info['pattern'] = options.pattern + elif options.block_name is not None: + self._info['pattern'] = options.block_name + elif len(self.args) >= 2: + self._info['pattern'] = self.args[1] + else: + self._info['pattern'] = raw_input('Which blocks do you want to parse? (Regex): ') + if len(self._info['pattern']) == 0: + self._info['pattern'] = '.' + self._info['yes'] = options.yes + + def run(self): + """ Go, go, go! """ + print "Warning: This is an experimental feature. Don't expect any magic." + # 1) Go through lib/ + if not self._skip_subdirs['lib']: + if self._info['version'] == '37': + files = self._search_files('lib', '*_impl.cc') + else: + files = self._search_files('lib', '*.cc') + for f in files: + if os.path.basename(f)[0:2] == 'qa': + continue + (params, iosig, blockname) = self._parse_cc_h(f) + self._make_grc_xml_from_block_data(params, iosig, blockname) + # 2) Go through python/ + + + def _search_files(self, path, path_glob): + """ Search for files matching pattern in the given path. """ + files = glob.glob("%s/%s"% (path, path_glob)) + files_filt = [] + print "Searching for matching files in %s/:" % path + for f in files: + if re.search(self._info['pattern'], os.path.basename(f)) is not None: + files_filt.append(f) + if len(files_filt) == 0: + print "None found." + return files_filt + + def _make_grc_xml_from_block_data(self, params, iosig, blockname): + """ Take the return values from the parser and call the XML + generator. Also, check the makefile if the .xml file is in there. + If necessary, add. """ + fname_xml = '%s_%s.xml' % (self._info['modname'], blockname) + # Some adaptions for the GRC + for inout in ('in', 'out'): + if iosig[inout]['max_ports'] == '-1': + iosig[inout]['max_ports'] = '$num_%sputs' % inout + params.append({'key': 'num_%sputs' % inout, + 'type': 'int', + 'name': 'Num %sputs' % inout, + 'default': '2', + 'in_constructor': False}) + if os.path.isfile(os.path.join('grc', fname_xml)): + # TODO add an option to keep + print "Warning: Overwriting existing GRC file." + grc_generator = GRCXMLGenerator( + modname=self._info['modname'], + blockname=blockname, + params=params, + iosig=iosig + ) + grc_generator.save(os.path.join('grc', fname_xml)) + if not self._skip_subdirs['grc']: + ed = CMakeFileEditor(self._file['cmgrc']) + if re.search(fname_xml, ed.cfile) is None and not ed.check_for_glob('*.xml'): + print "Adding GRC bindings to grc/CMakeLists.txt..." + ed.append_value('install', fname_xml, 'DESTINATION[^()]+') + ed.write() + + def _parse_cc_h(self, fname_cc): + """ Go through a .cc and .h-file defining a block and return info """ + def _type_translate(p_type, default_v=None): + """ Translates a type from C++ to GRC """ + translate_dict = {'float': 'float', + 'double': 'real', + 'int': 'int', + 'gr_complex': 'complex', + 'char': 'byte', + 'unsigned char': 'byte', + 'std::string': 'string', + 'std::vector': 'int_vector', + 'std::vector': 'real_vector', + 'std::vector': 'complex_vector', + } + if p_type in ('int',) and default_v[:2].lower() == '0x': + return 'hex' + try: + return translate_dict[p_type] + except KeyError: + return 'raw' + def _get_blockdata(fname_cc): + """ Return the block name and the header file name from the .cc file name """ + blockname = os.path.splitext(os.path.basename(fname_cc.replace('_impl.', '.')))[0] + fname_h = (blockname + '.h').replace('_impl.', '.') + blockname = blockname.replace(self._info['modname']+'_', '', 1) + return (blockname, fname_h) + # Go, go, go + print "Making GRC bindings for %s..." % fname_cc + (blockname, fname_h) = _get_blockdata(fname_cc) + try: + parser = ParserCCBlock(fname_cc, + os.path.join(self._info['includedir'], fname_h), + blockname, + self._info['version'], + _type_translate + ) + except IOError: + print "Can't open some of the files necessary to parse %s." % fname_cc + sys.exit(1) + return (parser.read_params(), parser.read_io_signature(), blockname) + + -- cgit From 9ef0f125355a4541c691f18d05ad7ca7b6f7125e Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Sun, 27 Jan 2013 16:57:04 +0100 Subject: modtool: added copyleft headers --- gr-utils/src/python/modtool/modtool_makexml.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/modtool_makexml.py') diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index 5a1a24f1b..acf3e459c 100644 --- a/gr-utils/src/python/modtool/modtool_makexml.py +++ b/gr-utils/src/python/modtool/modtool_makexml.py @@ -1,3 +1,23 @@ +# +# Copyright 2013 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# """ Automatically create XML bindings for GRC from block code """ import sys @@ -11,7 +31,6 @@ from parser_cc_block import ParserCCBlock from grc_xml_generator import GRCXMLGenerator from cmakefile_editor import CMakeFileEditor -### Remove module ########################################################### class ModToolMakeXML(ModTool): """ Make XML file for GRC block bindings """ name = 'makexml' -- cgit From ede34060de27184eb6f6df6b5fd03ae8a643bf91 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 28 Jan 2013 11:48:17 +0100 Subject: modtool: bugfixes --- gr-utils/src/python/modtool/modtool_makexml.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/modtool_makexml.py') diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index acf3e459c..104a0fdbd 100644 --- a/gr-utils/src/python/modtool/modtool_makexml.py +++ b/gr-utils/src/python/modtool/modtool_makexml.py @@ -122,7 +122,7 @@ class ModToolMakeXML(ModTool): ed = CMakeFileEditor(self._file['cmgrc']) if re.search(fname_xml, ed.cfile) is None and not ed.check_for_glob('*.xml'): print "Adding GRC bindings to grc/CMakeLists.txt..." - ed.append_value('install', fname_xml, 'DESTINATION[^()]+') + ed.append_value('install', fname_xml, to_ignore_end='DESTINATION[^()]+') ed.write() def _parse_cc_h(self, fname_cc): -- cgit From 2d695b3c4c86b5c206f95dcc1d71f97d808d98b8 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 28 Jan 2013 15:26:05 +0100 Subject: modtool: cleanup, bugfixes --- gr-utils/src/python/modtool/modtool_makexml.py | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_makexml.py') diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index 104a0fdbd..777cc09e1 100644 --- a/gr-utils/src/python/modtool/modtool_makexml.py +++ b/gr-utils/src/python/modtool/modtool_makexml.py @@ -30,6 +30,7 @@ from modtool_base import ModTool from parser_cc_block import ParserCCBlock from grc_xml_generator import GRCXMLGenerator from cmakefile_editor import CMakeFileEditor +from util_functions import ask_yes_no class ModToolMakeXML(ModTool): """ Make XML file for GRC block bindings """ @@ -38,24 +39,10 @@ class ModToolMakeXML(ModTool): def __init__(self): ModTool.__init__(self) - def setup_parser(self): - " Initialise the option parser for 'gr_modtool.py makexml' " - parser = ModTool.setup_parser(self) - parser.usage = '%prog makexml [options]. \n Call %prog without any options to run it interactively.' - ogroup = OptionGroup(parser, "Make XML module options") - ogroup.add_option("-p", "--pattern", type="string", default=None, - help="Filter possible choices for blocks to be parsed.") - ogroup.add_option("-y", "--yes", action="store_true", default=False, - help="Answer all questions with 'yes'. This can overwrite existing files!") - parser.add_option_group(ogroup) - return parser - def setup(self): ModTool.setup(self) options = self.options - if options.pattern is not None: - self._info['pattern'] = options.pattern - elif options.block_name is not None: + if options.block_name is not None: self._info['pattern'] = options.block_name elif len(self.args) >= 2: self._info['pattern'] = self.args[1] @@ -63,7 +50,6 @@ class ModToolMakeXML(ModTool): self._info['pattern'] = raw_input('Which blocks do you want to parse? (Regex): ') if len(self._info['pattern']) == 0: self._info['pattern'] = '.' - self._info['yes'] = options.yes def run(self): """ Go, go, go! """ @@ -109,8 +95,11 @@ class ModToolMakeXML(ModTool): 'default': '2', 'in_constructor': False}) if os.path.isfile(os.path.join('grc', fname_xml)): - # TODO add an option to keep - print "Warning: Overwriting existing GRC file." + if not self._info['yes']: + if not ask_yes_no('Overwrite existing GRC file?', False): + return + else: + print "Warning: Overwriting existing GRC file." grc_generator = GRCXMLGenerator( modname=self._info['modname'], blockname=blockname, @@ -167,4 +156,3 @@ class ModToolMakeXML(ModTool): sys.exit(1) return (parser.read_params(), parser.read_io_signature(), blockname) - -- cgit