From a62f90d8cc96b9dea9289ad6e420d1c0b16f6c36 Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Thu, 24 Jan 2013 19:33:03 +0100 Subject: utils: added modtool --- gr-utils/src/python/modtool/modtool_add.py | 296 +++++++++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 gr-utils/src/python/modtool/modtool_add.py (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py new file mode 100644 index 0000000000..c664d7c1a2 --- /dev/null +++ b/gr-utils/src/python/modtool/modtool_add.py @@ -0,0 +1,296 @@ +""" Module to add new blocks """ + +import os +import sys +import re +from optparse import OptionGroup + +from util_functions import append_re_line_sequence, ask_yes_no +from cmakefile_editor import CMakeFileEditor +from modtool_base import ModTool +from templates import Templates +from code_generator import get_template +import Cheetah.Template + +### Add new block module ##################################################### +class ModToolAdd(ModTool): + """ Add block to the out-of-tree module. """ + name = 'add' + aliases = ('insert',) + _block_types = ('sink', 'source', 'sync', 'decimator', 'interpolator', + 'general', 'hier', 'noblock') + def __init__(self): + ModTool.__init__(self) + self._add_cc_qa = False + self._add_py_qa = False + + def setup_parser(self): + parser = ModTool.setup_parser(self) + parser.usage = '%prog add [options]. \n Call %prog without any options to run it interactively.' + ogroup = OptionGroup(parser, "Add module options") + ogroup.add_option("-t", "--block-type", type="choice", + choices=self._block_types, default=None, help="One of %s." % ', '.join(self._block_types)) + ogroup.add_option("--license-file", type="string", default=None, + help="File containing the license header for every source code file.") + ogroup.add_option("--argument-list", type="string", default=None, + help="The argument list for the constructor and make functions.") + ogroup.add_option("--add-python-qa", action="store_true", default=None, + help="If given, Python QA code is automatically added if possible.") + ogroup.add_option("--add-cpp-qa", action="store_true", default=None, + help="If given, C++ QA code is automatically added if possible.") + ogroup.add_option("--skip-cmakefiles", action="store_true", default=False, + help="If given, only source files are written, but CMakeLists.txt files are left unchanged.") + ogroup.add_option("-l", "--lang", type="choice", choices=('cpp', 'c++', 'python'), + default='cpp', help="Language (cpp or python)") + parser.add_option_group(ogroup) + return parser + + def setup(self): + ModTool.setup(self) + options = self.options + self._info['blocktype'] = options.block_type + if self._info['blocktype'] is None: + while self._info['blocktype'] not in self._block_types: + self._info['blocktype'] = raw_input("Enter code type: ") + if self._info['blocktype'] not in self._block_types: + print 'Must be one of ' + str(self._block_types) + self._info['lang'] = options.lang + if self._info['lang'] == 'c++': + self._info['lang'] = 'cpp' + print "Language: %s" % {'cpp': 'C++', 'python': 'Python'}[self._info['lang']] + + if ((self._skip_subdirs['lib'] and self._info['lang'] == 'cpp') + or (self._skip_subdirs['python'] and self._info['lang'] == 'python')): + print "Missing or skipping relevant subdir." + sys.exit(1) + + if self._info['blockname'] is None: + if len(self.args) >= 2: + self._info['blockname'] = self.args[1] + else: + self._info['blockname'] = raw_input("Enter name of block/code (without module name prefix): ") + if not re.match('[a-zA-Z0-9_]+', self._info['blockname']): + print 'Invalid block name.' + sys.exit(2) + print "Block/code identifier: " + self._info['blockname'] + self._info['fullblockname'] = self._info['modname'] + '_' + self._info['blockname'] + self._info['license'] = self.setup_choose_license() + + if options.argument_list is not None: + self._info['arglist'] = options.argument_list + else: + self._info['arglist'] = raw_input('Enter valid argument list, including default arguments: ') + + if not (self._info['blocktype'] in ('noblock') or self._skip_subdirs['python']): + self._add_py_qa = options.add_python_qa + if self._add_py_qa is None: + self._add_py_qa = ask_yes_no('Add Python QA code?', True) + if self._info['lang'] == 'cpp': + self._add_cc_qa = options.add_cpp_qa + if self._add_cc_qa is None: + self._add_cc_qa = ask_yes_no('Add C++ QA code?', not self._add_py_qa) + if self._info['version'] == 'autofoo' and not self.options.skip_cmakefiles: + print "Warning: Autotools modules are not supported. ", + print "Files will be created, but Makefiles will not be edited." + self.options.skip_cmakefiles = True + + + def setup_choose_license(self): + """ Select a license by the following rules, in this order: + 1) The contents of the file given by --license-file + 2) The contents of the file LICENSE or LICENCE in the modules + top directory + 3) The default license. """ + if self.options.license_file is not None \ + and os.path.isfile(self.options.license_file): + return open(self.options.license_file).read() + elif os.path.isfile('LICENSE'): + return open('LICENSE').read() + elif os.path.isfile('LICENCE'): + return open('LICENCE').read() + else: + return Templates['defaultlicense'] + + def _write_tpl(self, tpl, path, fname): + """ Shorthand for writing a substituted template to a file""" + print "Adding file '%s'..." % fname + open(os.path.join(path, fname), 'w').write(get_template(tpl, **self._info)) + + def run(self): + """ Go, go, go. """ + has_swig = ( + self._info['lang'] == 'cpp' + and not self._skip_subdirs['swig'] + ) + has_grc = False + if self._info['lang'] == 'cpp': + print "Traversing lib..." + self._run_lib() + has_grc = has_swig + else: # Python + print "Traversing python..." + self._run_python() + if self._info['blocktype'] != 'noblock': + has_grc = True + if has_swig: + print "Traversing swig..." + self._run_swig() + if self._add_py_qa: + print "Adding Python QA..." + self._run_python_qa() + if has_grc and not self._skip_subdirs['grc']: + print "Traversing grc..." + self._run_grc() + + def _run_lib(self): + """ Do everything that needs doing in the subdir 'lib' and 'include'. + - add .cc and .h files + - include them into CMakeLists.txt + - check if C++ QA code is req'd + - if yes, create qa_*.{cc,h} and add them to CMakeLists.txt + """ + def _add_qa(): + " Add C++ QA files for 3.7 API " + fname_qa_h = 'qa_%s.h' % self._info['blockname'] + fname_qa_cc = 'qa_%s.cc' % self._info['blockname'] + self._write_tpl('qa_cpp', 'lib', fname_qa_cc) + self._write_tpl('qa_h', 'lib', fname_qa_h) + if not self.options.skip_cmakefiles: + try: + append_re_line_sequence(self._file['cmlib'], + '\$\{CMAKE_CURRENT_SOURCE_DIR\}/qa_%s.cc.*\n' % self._info['modname'], + ' ${CMAKE_CURRENT_SOURCE_DIR}/qa_%s.cc' % self._info['blockname']) + append_re_line_sequence(self._file['qalib'], + '#include.*\n', + '#include "%s"' % fname_qa_h) + append_re_line_sequence(self._file['qalib'], + '(addTest.*suite.*\n|new CppUnit.*TestSuite.*\n)', + ' s->addTest(gr::%s::qa_%s::suite());' % (self._info['modname'], + self._info['blockname']) + ) + except IOError: + print "Can't add C++ QA files." + def _add_qa36(): + " Add C++ QA files for pre-3.7 API (not autotools) " + fname_qa_cc = 'qa_%s.cc' % self._info['fullblockname'] + self._write_tpl('qa_cpp36', 'lib', fname_qa_cc) + if not self.options.skip_cmakefiles: + open(self._file['cmlib'], 'a').write( + str( + Cheetah.Template.Template( + Templates['qa_cmakeentry36'], + searchList={'basename': os.path.splitext(fname_qa_cc)[0], + 'filename': fname_qa_cc, + 'modname': self._info['modname'] + } + ) + ) + ) + ed = CMakeFileEditor(self._file['cmlib']) + ed.remove_double_newlines() + ed.write() + fname_cc = None + fname_h = None + if self._info['version'] == '37': + fname_h = self._info['blockname'] + '.h' + fname_cc = self._info['blockname'] + '.cc' + if self._info['blocktype'] in ('source', 'sink', 'sync', 'decimator', + 'interpolator', 'general', 'hier'): + fname_cc = self._info['blockname'] + '_impl.cc' + self._write_tpl('block_impl_h', 'lib', self._info['blockname'] + '_impl.h') + self._write_tpl('block_impl_cpp', 'lib', fname_cc) + self._write_tpl('block_def_h', self._info['includedir'], fname_h) + else: # Pre-3.7 or autotools + fname_h = self._info['fullblockname'] + '.h' + fname_cc = self._info['fullblockname'] + '.cc' + self._write_tpl('block_h36', self._info['includedir'], fname_h) + self._write_tpl('block_cpp36', 'lib', fname_cc) + if not self.options.skip_cmakefiles: + ed = CMakeFileEditor(self._file['cmlib']) + ed.append_value('add_library', fname_cc) + ed.write() + ed = CMakeFileEditor(self._file['cminclude']) + ed.append_value('install', fname_h, 'DESTINATION[^()]+') + ed.write() + if self._add_cc_qa: + if self._info['version'] == '37': + _add_qa() + elif self._info['version'] == '36': + _add_qa36() + elif self._info['version'] == 'autofoo': + print "Warning: C++ QA files not supported for autotools." + + def _run_swig(self): + """ Do everything that needs doing in the subdir 'swig'. + - Edit main *.i file + """ + if self._get_mainswigfile() is None: + print 'Warning: No main swig file found.' + return + print "Editing %s..." % self._file['swig'] + mod_block_sep = '/' + if self._info['version'] == '36': + mod_block_sep = '_' + swig_block_magic_str = get_template('swig_block_magic', **self._info) + include_str = '#include "%s%s%s.h"' % ( + self._info['modname'], + mod_block_sep, + self._info['blockname']) + if re.search('#include', open(self._file['swig'], 'r').read()): + append_re_line_sequence(self._file['swig'], '^#include.*\n', include_str) + else: # I.e., if the swig file is empty + oldfile = open(self._file['swig'], 'r').read() + regexp = re.compile('^%\{\n', re.MULTILINE) + oldfile = regexp.sub('%%{\n%s\n' % include_str, oldfile, count=1) + open(self._file['swig'], 'w').write(oldfile) + open(self._file['swig'], 'a').write(swig_block_magic_str) + + def _run_python_qa(self): + """ Do everything that needs doing in the subdir 'python' to add + QA code. + - add .py files + - include in CMakeLists.txt + """ + fname_py_qa = 'qa_' + self._info['blockname'] + '.py' + self._write_tpl('qa_python', 'python', fname_py_qa) + os.chmod(os.path.join('python', fname_py_qa), 0755) + if self.options.skip_cmakefiles or CMakeFileEditor(self._file['cmpython']).check_for_glob('qa_*.py'): + return + print "Editing python/CMakeLists.txt..." + open(self._file['cmpython'], 'a').write( + 'GR_ADD_TEST(qa_%s ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/%s)\n' % \ + (self._info['blockname'], fname_py_qa)) + + def _run_python(self): + """ Do everything that needs doing in the subdir 'python' to add + a Python block. + - add .py file + - include in CMakeLists.txt + - include in __init__.py + """ + fname_py = self._info['blockname'] + '.py' + self._write_tpl('block_python', 'python', fname_py) + append_re_line_sequence(self._file['pyinit'], + '(^from.*import.*\n|# import any pure.*\n)', + 'from %s import *' % self._info['blockname']) + if self.options.skip_cmakefiles: + return + ed = CMakeFileEditor(self._file['cmpython']) + ed.append_value('GR_PYTHON_INSTALL', fname_py, 'DESTINATION[^()]+') + ed.write() + + def _run_grc(self): + """ Do everything that needs doing in the subdir 'grc' to add + a GRC bindings XML file. + - add .xml file + - include in CMakeLists.txt + """ + fname_grc = self._info['fullblockname'] + '.xml' + self._write_tpl('grc_xml', 'grc', fname_grc) + ed = CMakeFileEditor(self._file['cmgrc'], '\n ') + if self.options.skip_cmakefiles or ed.check_for_glob('*.xml'): + return + print "Editing grc/CMakeLists.txt..." + ed.append_value('install', fname_grc, 'DESTINATION[^()]+') + ed.write() + -- cgit v1.2.3 From c4971ab2cb4bdbb9abc5d5320bd16b45cf263cf8 Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Fri, 25 Jan 2013 17:52:46 +0100 Subject: utils: modtool minor updates --- gr-utils/src/python/modtool/cmakefile_editor.py | 2 +- gr-utils/src/python/modtool/modtool_add.py | 2 +- gr-utils/src/python/modtool/modtool_newmod.py | 2 +- gr-utils/src/python/modtool/templates.py | 6 ++---- 4 files changed, 5 insertions(+), 7 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/modtool/cmakefile_editor.py b/gr-utils/src/python/modtool/cmakefile_editor.py index fe50373bb8..b182757076 100644 --- a/gr-utils/src/python/modtool/cmakefile_editor.py +++ b/gr-utils/src/python/modtool/cmakefile_editor.py @@ -5,7 +5,7 @@ import re ### CMakeFile.txt editor class ############################################### class CMakeFileEditor(object): """A tool for editing CMakeLists.txt files. """ - def __init__(self, filename, separator=' ', indent=' '): + def __init__(self, filename, separator='\n ', indent=' '): self.filename = filename self.cfile = open(filename, 'r').read() self.separator = separator diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py index c664d7c1a2..581f3b0aaf 100644 --- a/gr-utils/src/python/modtool/modtool_add.py +++ b/gr-utils/src/python/modtool/modtool_add.py @@ -272,7 +272,7 @@ class ModToolAdd(ModTool): self._write_tpl('block_python', 'python', fname_py) append_re_line_sequence(self._file['pyinit'], '(^from.*import.*\n|# import any pure.*\n)', - 'from %s import *' % self._info['blockname']) + 'from %s import %s' % (self._info['blockname'], self._info['blockname'])) if self.options.skip_cmakefiles: return ed = CMakeFileEditor(self._file['cmpython']) diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 9b2dc6e30d..0c69cb69e8 100644 --- a/gr-utils/src/python/modtool/modtool_newmod.py +++ b/gr-utils/src/python/modtool/modtool_newmod.py @@ -51,7 +51,7 @@ class ModToolNewModule(ModTool): * Open all files, rename howto and HOWTO to the module name * Rename files and directories that contain the word howto """ - print "Creating out-of-tree module in %s..." % self._dir + print "Creating out-of-tree module in %s..." % self._dir, try: shutil.copytree('/home/braun/.usrlocal/share/gnuradio/modtool/gr-newmod', self._dir) os.chdir(self._dir) diff --git a/gr-utils/src/python/modtool/templates.py b/gr-utils/src/python/modtool/templates.py index e3019bb701..f41049c5ae 100644 --- a/gr-utils/src/python/modtool/templates.py +++ b/gr-utils/src/python/modtool/templates.py @@ -287,8 +287,8 @@ import numpy #set $deciminterp = ', <+interpolation+>' #else if $blocktype == 'decimator' #set $deciminterp = ', <+decimation+>' -#set $deciminterp = '' #else +#set $deciminterp = '' #end if from gnuradio import gr @@ -297,7 +297,7 @@ class ${blockname}(${parenttype}): docstring for block ${blockname} """ def __init__(self#if $arglist == '' then '' else ', '#$arglist): - gr.${parenttype}.__init__(self, + ${parenttype}.__init__(self, #if $blocktype == 'hier' "$blockname", gr.io_signature(${inputsig}), # Input signature @@ -324,8 +324,6 @@ class ${blockname}(${parenttype}): \#self.consume_each(len(input_items[0])) return len(output_items[0]) #stop -#else - def work(self, input_items, output_items): #end if def work(self, input_items, output_items): -- cgit v1.2.3 From 9ef0f125355a4541c691f18d05ad7ca7b6f7125e Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Sun, 27 Jan 2013 16:57:04 +0100 Subject: modtool: added copyleft headers --- gr-utils/src/python/modtool/__init__.py | 2 +- gr-utils/src/python/modtool/cmakefile_editor.py | 21 ++++++++++- gr-utils/src/python/modtool/code_generator.py | 21 ++++++++++- gr-utils/src/python/modtool/grc_xml_generator.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_add.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_base.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_disable.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_help.py | 44 ++++++++++++------------ gr-utils/src/python/modtool/modtool_info.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_makexml.py | 21 ++++++++++- gr-utils/src/python/modtool/modtool_newmod.py | 23 +++++++++++-- gr-utils/src/python/modtool/modtool_rm.py | 21 ++++++++++- gr-utils/src/python/modtool/parser_cc_block.py | 21 ++++++++++- gr-utils/src/python/modtool/templates.py | 22 ++++++++++-- gr-utils/src/python/modtool/util_functions.py | 21 ++++++++++- 15 files changed, 284 insertions(+), 38 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/modtool/__init__.py b/gr-utils/src/python/modtool/__init__.py index a107472540..7935e4b482 100644 --- a/gr-utils/src/python/modtool/__init__.py +++ b/gr-utils/src/python/modtool/__init__.py @@ -1,5 +1,5 @@ # -# Copyright 2012 Free Software Foundation, Inc. +# Copyright 2013 Free Software Foundation, Inc. # # This file is part of GNU Radio # diff --git a/gr-utils/src/python/modtool/cmakefile_editor.py b/gr-utils/src/python/modtool/cmakefile_editor.py index b182757076..ed5b714253 100644 --- a/gr-utils/src/python/modtool/cmakefile_editor.py +++ b/gr-utils/src/python/modtool/cmakefile_editor.py @@ -1,8 +1,27 @@ +# +# 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. +# """ Edit CMakeLists.txt files """ import re -### CMakeFile.txt editor class ############################################### class CMakeFileEditor(object): """A tool for editing CMakeLists.txt files. """ def __init__(self, filename, separator='\n ', indent=' '): diff --git a/gr-utils/src/python/modtool/code_generator.py b/gr-utils/src/python/modtool/code_generator.py index b727f611e5..525b3d1e9a 100644 --- a/gr-utils/src/python/modtool/code_generator.py +++ b/gr-utils/src/python/modtool/code_generator.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. +# """ A code generator (needed by ModToolAdd) """ from templates import Templates @@ -7,7 +27,6 @@ from util_functions import str_to_python_comment from util_functions import strip_default_values from util_functions import strip_arg_types -### Code generator class ##################################################### class GRMTemplate(Cheetah.Template.Template): """ An extended template class """ def __init__(self, src, searchList): diff --git a/gr-utils/src/python/modtool/grc_xml_generator.py b/gr-utils/src/python/modtool/grc_xml_generator.py index 2fa61863f2..7ccd443196 100644 --- a/gr-utils/src/python/modtool/grc_xml_generator.py +++ b/gr-utils/src/python/modtool/grc_xml_generator.py @@ -1,7 +1,26 @@ +# +# 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. +# import xml.etree.ElementTree as ET from util_functions import is_number, xml_indent -### GRC XML Generator ######################################################## try: import lxml.etree LXML_IMPORTED = True diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py index 581f3b0aaf..a6c84bea85 100644 --- a/gr-utils/src/python/modtool/modtool_add.py +++ b/gr-utils/src/python/modtool/modtool_add.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. +# """ Module to add new blocks """ import os @@ -12,7 +32,6 @@ from templates import Templates from code_generator import get_template import Cheetah.Template -### Add new block module ##################################################### class ModToolAdd(ModTool): """ Add block to the out-of-tree module. """ name = 'add' diff --git a/gr-utils/src/python/modtool/modtool_base.py b/gr-utils/src/python/modtool/modtool_base.py index edb0f14eed..d824910e95 100644 --- a/gr-utils/src/python/modtool/modtool_base.py +++ b/gr-utils/src/python/modtool/modtool_base.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. +# """ Base class for the modules """ import os @@ -8,7 +28,6 @@ from optparse import OptionParser, OptionGroup from util_functions import get_modname from templates import Templates -### ModTool base class ####################################################### class ModTool(object): """ Base class for all modtool command classes. """ def __init__(self): diff --git a/gr-utils/src/python/modtool/modtool_disable.py b/gr-utils/src/python/modtool/modtool_disable.py index 67f15ad537..b0fb132451 100644 --- a/gr-utils/src/python/modtool/modtool_disable.py +++ b/gr-utils/src/python/modtool/modtool_disable.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. +# """ Disable blocks module """ import os @@ -8,7 +28,6 @@ from optparse import OptionGroup from modtool_base import ModTool from cmakefile_editor import CMakeFileEditor -### Disable module ########################################################### class ModToolDisable(ModTool): """ Disable block (comments out CMake entries for files) """ name = 'disable' diff --git a/gr-utils/src/python/modtool/modtool_help.py b/gr-utils/src/python/modtool/modtool_help.py index a1dd3c4660..79474a9631 100644 --- a/gr-utils/src/python/modtool/modtool_help.py +++ b/gr-utils/src/python/modtool/modtool_help.py @@ -1,30 +1,30 @@ +# +# 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. +# """ The help module """ -from modtool_base import ModTool -from modtool_info import ModToolInfo -from modtool_add import ModToolAdd -from modtool_rm import ModToolRemove -from modtool_newmod import ModToolNewModule -from modtool_disable import ModToolDisable -from modtool_makexml import ModToolMakeXML -from util_functions import get_command_from_argv +from gnuradio.modtool import * +from util_functions import get_command_from_argv, get_class_dict from templates import Templates -def get_class_dict(): - " Return a dictionary of the available commands in the form command->class " - classdict = {} - for g in globals().values(): - try: - if issubclass(g, ModTool): - classdict[g.name] = g - for a in g.aliases: - classdict[a] = g - except (TypeError, AttributeError): - pass - return classdict - -### Help module ############################################################## def print_class_descriptions(): ''' Go through all ModTool* classes and print their name, alias and description. ''' diff --git a/gr-utils/src/python/modtool/modtool_info.py b/gr-utils/src/python/modtool/modtool_info.py index 80fa278321..e774db9114 100644 --- a/gr-utils/src/python/modtool/modtool_info.py +++ b/gr-utils/src/python/modtool/modtool_info.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. +# """ Returns information about a module """ import os @@ -6,7 +26,6 @@ from optparse import OptionGroup from modtool_base import ModTool from util_functions import get_modname -### Info module ############################################################## class ModToolInfo(ModTool): """ Return information about a given module """ name = 'info' diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index 5a1a24f1bb..acf3e459c0 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' diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 0c69cb69e8..7a5f635dde 100644 --- a/gr-utils/src/python/modtool/modtool_newmod.py +++ b/gr-utils/src/python/modtool/modtool_newmod.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. +# """ Create a whole new out-of-tree module """ import shutil @@ -6,7 +26,6 @@ import re from optparse import OptionGroup from modtool_base import ModTool -### New out-of-tree-mod module ############################################### class ModToolNewModule(ModTool): """ Create a new out-of-tree module """ name = 'newmod' @@ -36,7 +55,6 @@ class ModToolNewModule(ModTool): self._dir = options.directory if self._dir == '.': self._dir = './gr-%s' % self._info['modname'] - print 'Module directory is "%s".' % self._dir try: os.stat(self._dir) except OSError: @@ -56,6 +74,7 @@ class ModToolNewModule(ModTool): shutil.copytree('/home/braun/.usrlocal/share/gnuradio/modtool/gr-newmod', self._dir) os.chdir(self._dir) except OSError: + print 'FAILED' print 'Could not create directory %s. Quitting.' % self._dir exit(2) for root, dirs, files in os.walk('.'): diff --git a/gr-utils/src/python/modtool/modtool_rm.py b/gr-utils/src/python/modtool/modtool_rm.py index 16bfeb34ce..bdbd802f33 100644 --- a/gr-utils/src/python/modtool/modtool_rm.py +++ b/gr-utils/src/python/modtool/modtool_rm.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. +# """ Remove blocks module """ import os @@ -10,7 +30,6 @@ from util_functions import remove_pattern_from_file from modtool_base import ModTool from cmakefile_editor import CMakeFileEditor -### Remove module ########################################################### class ModToolRemove(ModTool): """ Remove block (delete files and remove Makefile entries) """ name = 'remove' diff --git a/gr-utils/src/python/modtool/parser_cc_block.py b/gr-utils/src/python/modtool/parser_cc_block.py index 447fe113dd..d11353cc7a 100644 --- a/gr-utils/src/python/modtool/parser_cc_block.py +++ b/gr-utils/src/python/modtool/parser_cc_block.py @@ -1,8 +1,27 @@ +# +# 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. +# ''' A parser for blocks written in C++ ''' import re import sys -### Parser for CC blocks #################################################### def dummy_translator(the_type, default_v=None): """ Doesn't really translate. """ return the_type diff --git a/gr-utils/src/python/modtool/templates.py b/gr-utils/src/python/modtool/templates.py index f41049c5ae..91d8370b98 100644 --- a/gr-utils/src/python/modtool/templates.py +++ b/gr-utils/src/python/modtool/templates.py @@ -1,10 +1,28 @@ +# +# 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. +# ''' All the templates for skeleton files (needed by ModToolAdd) ''' from datetime import datetime -### Templates ################################################################ Templates = {} -Templates36 = {} # Default licence Templates['defaultlicense'] = ''' diff --git a/gr-utils/src/python/modtool/util_functions.py b/gr-utils/src/python/modtool/util_functions.py index 029ae04bfa..33d8ad3339 100644 --- a/gr-utils/src/python/modtool/util_functions.py +++ b/gr-utils/src/python/modtool/util_functions.py @@ -1,9 +1,28 @@ +# +# 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. +# """ Utility functions for gr_modtool.py """ import re import sys -### Utility functions ######################################################## def get_command_from_argv(possible_cmds): """ Read the requested command from argv. This can't be done with optparse, since the option parser isn't defined before the command is known, and -- cgit v1.2.3 From ede34060de27184eb6f6df6b5fd03ae8a643bf91 Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Mon, 28 Jan 2013 11:48:17 +0100 Subject: modtool: bugfixes --- gr-utils/src/python/modtool/modtool_add.py | 20 ++++++++------------ gr-utils/src/python/modtool/modtool_makexml.py | 2 +- gr-utils/src/python/modtool/modtool_rm.py | 2 +- 3 files changed, 10 insertions(+), 14 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py index a6c84bea85..32cfe04408 100644 --- a/gr-utils/src/python/modtool/modtool_add.py +++ b/gr-utils/src/python/modtool/modtool_add.py @@ -81,7 +81,7 @@ class ModToolAdd(ModTool): if ((self._skip_subdirs['lib'] and self._info['lang'] == 'cpp') or (self._skip_subdirs['python'] and self._info['lang'] == 'python')): print "Missing or skipping relevant subdir." - sys.exit(1) + exit(1) if self._info['blockname'] is None: if len(self.args) >= 2: @@ -90,7 +90,7 @@ class ModToolAdd(ModTool): self._info['blockname'] = raw_input("Enter name of block/code (without module name prefix): ") if not re.match('[a-zA-Z0-9_]+', self._info['blockname']): print 'Invalid block name.' - sys.exit(2) + exit(2) print "Block/code identifier: " + self._info['blockname'] self._info['fullblockname'] = self._info['modname'] + '_' + self._info['blockname'] self._info['license'] = self.setup_choose_license() @@ -143,22 +143,17 @@ class ModToolAdd(ModTool): ) has_grc = False if self._info['lang'] == 'cpp': - print "Traversing lib..." self._run_lib() has_grc = has_swig else: # Python - print "Traversing python..." self._run_python() if self._info['blocktype'] != 'noblock': has_grc = True if has_swig: - print "Traversing swig..." self._run_swig() if self._add_py_qa: - print "Adding Python QA..." self._run_python_qa() if has_grc and not self._skip_subdirs['grc']: - print "Traversing grc..." self._run_grc() def _run_lib(self): @@ -178,7 +173,7 @@ class ModToolAdd(ModTool): try: append_re_line_sequence(self._file['cmlib'], '\$\{CMAKE_CURRENT_SOURCE_DIR\}/qa_%s.cc.*\n' % self._info['modname'], - ' ${CMAKE_CURRENT_SOURCE_DIR}/qa_%s.cc' % self._info['blockname']) + ' ${CMAKE_CURRENT_SOURCE_DIR}/qa_%s.cc' % self._info['blockname']) append_re_line_sequence(self._file['qalib'], '#include.*\n', '#include "%s"' % fname_qa_h) @@ -226,10 +221,11 @@ class ModToolAdd(ModTool): self._write_tpl('block_cpp36', 'lib', fname_cc) if not self.options.skip_cmakefiles: ed = CMakeFileEditor(self._file['cmlib']) - ed.append_value('add_library', fname_cc) + if not ed.append_value('list', fname_cc, to_ignore_start='APPEND %s_sources' % self._info['modname']): + ed.append_value('add_library', fname_cc) ed.write() ed = CMakeFileEditor(self._file['cminclude']) - ed.append_value('install', fname_h, 'DESTINATION[^()]+') + ed.append_value('install', fname_h, to_ignore_end='DESTINATION[^()]+') ed.write() if self._add_cc_qa: if self._info['version'] == '37': @@ -295,7 +291,7 @@ class ModToolAdd(ModTool): if self.options.skip_cmakefiles: return ed = CMakeFileEditor(self._file['cmpython']) - ed.append_value('GR_PYTHON_INSTALL', fname_py, 'DESTINATION[^()]+') + ed.append_value('GR_PYTHON_INSTALL', fname_py, to_ignore_end='DESTINATION[^()]+') ed.write() def _run_grc(self): @@ -310,6 +306,6 @@ class ModToolAdd(ModTool): if self.options.skip_cmakefiles or ed.check_for_glob('*.xml'): return print "Editing grc/CMakeLists.txt..." - ed.append_value('install', fname_grc, 'DESTINATION[^()]+') + ed.append_value('install', fname_grc, to_ignore_end='DESTINATION[^()]+') ed.write() diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index acf3e459c0..104a0fdbde 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): diff --git a/gr-utils/src/python/modtool/modtool_rm.py b/gr-utils/src/python/modtool/modtool_rm.py index bdbd802f33..02ce8ef3f2 100644 --- a/gr-utils/src/python/modtool/modtool_rm.py +++ b/gr-utils/src/python/modtool/modtool_rm.py @@ -83,7 +83,7 @@ class ModToolRemove(ModTool): elif ext == '.cc': ed.remove_value('list', '\$\{CMAKE_CURRENT_SOURCE_DIR\}/%s' % filename, - 'APPEND test_%s_sources' % self._info['modname']) + to_ignore_start='APPEND test_%s_sources' % self._info['modname']) else: filebase = os.path.splitext(filename)[0] ed.delete_entry('add_executable', filebase) -- cgit v1.2.3 From 2d695b3c4c86b5c206f95dcc1d71f97d808d98b8 Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Mon, 28 Jan 2013 15:26:05 +0100 Subject: modtool: cleanup, bugfixes --- gr-utils/src/python/gr_modtool | 20 +----------------- gr-utils/src/python/modtool/__init__.py | 5 +++-- gr-utils/src/python/modtool/cmakefile_editor.py | 2 +- gr-utils/src/python/modtool/modtool_add.py | 1 - gr-utils/src/python/modtool/modtool_base.py | 28 ++++++++++++++++++++----- gr-utils/src/python/modtool/modtool_disable.py | 19 ++--------------- gr-utils/src/python/modtool/modtool_help.py | 4 ++-- gr-utils/src/python/modtool/modtool_info.py | 2 +- gr-utils/src/python/modtool/modtool_makexml.py | 26 +++++++---------------- gr-utils/src/python/modtool/modtool_newmod.py | 2 +- gr-utils/src/python/modtool/modtool_rm.py | 19 ++--------------- gr-utils/src/python/modtool/parser_cc_block.py | 5 +++-- gr-utils/src/python/modtool/templates.py | 6 +++--- gr-utils/src/python/modtool/util_functions.py | 19 ++++------------- 14 files changed, 53 insertions(+), 105 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/gr_modtool b/gr-utils/src/python/gr_modtool index bc41d56f55..8c5c710aff 100755 --- a/gr-utils/src/python/gr_modtool +++ b/gr-utils/src/python/gr_modtool @@ -24,24 +24,9 @@ import sys from gnuradio.modtool import * -def get_class_dict(): - " Return a dictionary of the available commands in the form command->class " - classdict = {} - for g in globals().values(): - try: - if issubclass(g, ModTool): - classdict[g.name] = g - for a in g.aliases: - classdict[a] = g - except (TypeError, AttributeError): - pass - return classdict - - -### Main code ################################################################ def main(): """ Here we go. Parse command, choose class and run. """ - cmd_dict = get_class_dict() + cmd_dict = get_class_dict(globals().values()) command = get_command_from_argv(cmd_dict.keys()) if command is None: print 'Usage:' + templates.Templates['usage'] @@ -51,9 +36,6 @@ def main(): modtool.run() if __name__ == '__main__': - if not ((sys.version_info[0] > 2) or - (sys.version_info[0] == 2 and sys.version_info[1] >= 7)): - print "Using Python < 2.7 is not recommended for gr_modtool." try: main() except KeyboardInterrupt: diff --git a/gr-utils/src/python/modtool/__init__.py b/gr-utils/src/python/modtool/__init__.py index 7935e4b482..a242722ab4 100644 --- a/gr-utils/src/python/modtool/__init__.py +++ b/gr-utils/src/python/modtool/__init__.py @@ -22,13 +22,14 @@ from cmakefile_editor import CMakeFileEditor from code_generator import GRMTemplate from grc_xml_generator import GRCXMLGenerator +from modtool_base import ModTool, get_class_dict from modtool_add import ModToolAdd -from modtool_base import ModTool from modtool_disable import ModToolDisable -from modtool_help import ModToolHelp from modtool_info import ModToolInfo from modtool_makexml import ModToolMakeXML from modtool_newmod import ModToolNewModule from modtool_rm import ModToolRemove +# Leave this at the end +from modtool_help import ModToolHelp from parser_cc_block import ParserCCBlock from util_functions import * diff --git a/gr-utils/src/python/modtool/cmakefile_editor.py b/gr-utils/src/python/modtool/cmakefile_editor.py index 92121dda3b..3d90b8d163 100644 --- a/gr-utils/src/python/modtool/cmakefile_editor.py +++ b/gr-utils/src/python/modtool/cmakefile_editor.py @@ -49,7 +49,7 @@ class CMakeFileEditor(object): """Remove an entry from the current buffer.""" regexp = '%s\s*\([^()]*%s[^()]*\)[^\n]*\n' % (entry, value_pattern) regexp = re.compile(regexp, re.MULTILINE) - (self.cfile, nsubs) = re.sub(regexp, '', self.cfile, count=1) + (self.cfile, nsubs) = re.subn(regexp, '', self.cfile, count=1) return nsubs def write(self): diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py index 32cfe04408..7ca375b6f9 100644 --- a/gr-utils/src/python/modtool/modtool_add.py +++ b/gr-utils/src/python/modtool/modtool_add.py @@ -45,7 +45,6 @@ class ModToolAdd(ModTool): def setup_parser(self): parser = ModTool.setup_parser(self) - parser.usage = '%prog add [options]. \n Call %prog without any options to run it interactively.' ogroup = OptionGroup(parser, "Add module options") ogroup.add_option("-t", "--block-type", type="choice", choices=self._block_types, default=None, help="One of %s." % ', '.join(self._block_types)) diff --git a/gr-utils/src/python/modtool/modtool_base.py b/gr-utils/src/python/modtool/modtool_base.py index d824910e95..3f8f2bc3c7 100644 --- a/gr-utils/src/python/modtool/modtool_base.py +++ b/gr-utils/src/python/modtool/modtool_base.py @@ -28,6 +28,7 @@ from optparse import OptionParser, OptionGroup from util_functions import get_modname from templates import Templates + class ModTool(object): """ Base class for all modtool command classes. """ def __init__(self): @@ -47,15 +48,17 @@ class ModTool(object): def setup_parser(self): """ Init the option parser. If derived classes need to add options, override this and call the parent function. """ - parser = OptionParser(usage=Templates['usage'], add_help_option=False) + parser = OptionParser(add_help_option=False) + parser.usage = '%prog ' + self.name + ' [options] <PATTERN> \n' + \ + ' Call "%prog ' + self.name + '" without any options to run it interactively.' ogroup = OptionGroup(parser, "General options") ogroup.add_option("-h", "--help", action="help", help="Displays this help message.") ogroup.add_option("-d", "--directory", type="string", default=".", - help="Base directory of the module.") + help="Base directory of the module. Defaults to the cwd.") ogroup.add_option("-n", "--module-name", type="string", default=None, - help="Name of the GNU Radio module. If possible, this gets detected from CMakeLists.txt.") + help="Use this to override the current module's name (is normally autodetected).") ogroup.add_option("-N", "--block-name", type="string", default=None, - help="Name of the block, minus the module name prefix.") + help="Name of the block, where applicable.") ogroup.add_option("--skip-lib", action="store_true", default=False, help="Don't do anything in the lib/ subdirectory.") ogroup.add_option("--skip-swig", action="store_true", default=False, @@ -64,6 +67,8 @@ class ModTool(object): help="Don't do anything in the python/ subdirectory.") ogroup.add_option("--skip-grc", action="store_true", default=False, help="Don't do anything in the grc/ subdirectory.") + ogroup.add_option("-y", "--yes", action="store_true", default=False, + help="Answer all questions with 'yes'. This can overwrite and delete your files, so be careful.") parser.add_option_group(ogroup) return parser @@ -74,7 +79,6 @@ class ModTool(object): if not self._check_directory(self._dir): print "No GNU Radio module found in the given directory. Quitting." sys.exit(1) - print "Operating in directory " + self._dir if options.module_name is not None: self._info['modname'] = options.module_name else: @@ -96,6 +100,7 @@ class ModTool(object): self._info['blockname'] = options.block_name self.options = options self._setup_files() + self._info['yes'] = options.yes def _setup_files(self): """ Initialise the self._file[] dictionary """ @@ -156,3 +161,16 @@ class ModTool(object): """ Override this. """ pass +def get_class_dict(the_globals): + " Return a dictionary of the available commands in the form command->class " + classdict = {} + for g in the_globals: + try: + if issubclass(g, ModTool): + classdict[g.name] = g + for a in g.aliases: + classdict[a] = g + except (TypeError, AttributeError): + pass + return classdict + diff --git a/gr-utils/src/python/modtool/modtool_disable.py b/gr-utils/src/python/modtool/modtool_disable.py index b0fb132451..36725e5578 100644 --- a/gr-utils/src/python/modtool/modtool_disable.py +++ b/gr-utils/src/python/modtool/modtool_disable.py @@ -35,24 +35,10 @@ class ModToolDisable(ModTool): def __init__(self): ModTool.__init__(self) - def setup_parser(self): - " Initialise the option parser for 'gr_modtool.py rm' " - parser = ModTool.setup_parser(self) - parser.usage = '%prog disable [options]. \n Call %prog without any options to run it interactively.' - ogroup = OptionGroup(parser, "Disable module options") - ogroup.add_option("-p", "--pattern", type="string", default=None, - help="Filter possible choices for blocks to be disabled.") - ogroup.add_option("-y", "--yes", action="store_true", default=False, - help="Answer all questions with 'yes'.") - 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] @@ -60,7 +46,6 @@ class ModToolDisable(ModTool): self._info['pattern'] = raw_input('Which blocks do you want to disable? (Regex): ') if len(self._info['pattern']) == 0: self._info['pattern'] = '.' - self._info['yes'] = options.yes def run(self): """ Go, go, go! """ @@ -123,7 +108,7 @@ class ModToolDisable(ModTool): swigfile = re.sub('(GR_SWIG_BLOCK_MAGIC2?.+'+blockname+'.+;)', r'//\1', swigfile) open(self._file['swig'], 'w').write(swigfile) return False - # List of special rules: 0: subdir, 1: filename re match, 2: function + # List of special rules: 0: subdir, 1: filename re match, 2: callback special_treatments = ( ('python', 'qa.+py$', _handle_py_qa), ('python', '^(?!qa).+py$', _handle_py_mod), diff --git a/gr-utils/src/python/modtool/modtool_help.py b/gr-utils/src/python/modtool/modtool_help.py index 79474a9631..76d9fd28bd 100644 --- a/gr-utils/src/python/modtool/modtool_help.py +++ b/gr-utils/src/python/modtool/modtool_help.py @@ -21,7 +21,7 @@ """ The help module """ from gnuradio.modtool import * -from util_functions import get_command_from_argv, get_class_dict +from util_functions import get_command_from_argv from templates import Templates @@ -51,7 +51,7 @@ class ModToolHelp(ModTool): pass def run(self): - cmd_dict = get_class_dict() + cmd_dict = get_class_dict(globals().values()) cmds = cmd_dict.keys() cmds.remove(self.name) for a in self.aliases: diff --git a/gr-utils/src/python/modtool/modtool_info.py b/gr-utils/src/python/modtool/modtool_info.py index e774db9114..680bd41b99 100644 --- a/gr-utils/src/python/modtool/modtool_info.py +++ b/gr-utils/src/python/modtool/modtool_info.py @@ -34,7 +34,7 @@ class ModToolInfo(ModTool): ModTool.__init__(self) def setup_parser(self): - " Initialise the option parser for 'gr_modtool.py info' " + " Initialise the option parser for 'gr_modtool info' " parser = ModTool.setup_parser(self) parser.usage = '%prog info [options]. \n Call %prog without any options to run it interactively.' ogroup = OptionGroup(parser, "Info options") diff --git a/gr-utils/src/python/modtool/modtool_makexml.py b/gr-utils/src/python/modtool/modtool_makexml.py index 104a0fdbde..777cc09e1f 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) - diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 5e14493c3c..102d83d8df 100644 --- a/gr-utils/src/python/modtool/modtool_newmod.py +++ b/gr-utils/src/python/modtool/modtool_newmod.py @@ -35,7 +35,7 @@ class ModToolNewModule(ModTool): ModTool.__init__(self) def setup_parser(self): - " Initialise the option parser for 'gr_modtool.py newmod' " + " Initialise the option parser for 'gr_modtool newmod' " parser = ModTool.setup_parser(self) parser.usage = '%prog rm [options]. \n Call %prog without any options to run it interactively.' ogroup = OptionGroup(parser, "New out-of-tree module options") diff --git a/gr-utils/src/python/modtool/modtool_rm.py b/gr-utils/src/python/modtool/modtool_rm.py index 02ce8ef3f2..32dfee4806 100644 --- a/gr-utils/src/python/modtool/modtool_rm.py +++ b/gr-utils/src/python/modtool/modtool_rm.py @@ -37,24 +37,10 @@ class ModToolRemove(ModTool): def __init__(self): ModTool.__init__(self) - def setup_parser(self): - " Initialise the option parser for 'gr_modtool.py rm' " - parser = ModTool.setup_parser(self) - parser.usage = '%prog rm [options]. \n Call %prog without any options to run it interactively.' - ogroup = OptionGroup(parser, "Remove module options") - ogroup.add_option("-p", "--pattern", type="string", default=None, - help="Filter possible choices for blocks to be deleted.") - ogroup.add_option("-y", "--yes", action="store_true", default=False, - help="Answer all questions with 'yes'.") - 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] @@ -62,7 +48,6 @@ class ModToolRemove(ModTool): self._info['pattern'] = raw_input('Which blocks do you want to delete? (Regex): ') if len(self._info['pattern']) == 0: self._info['pattern'] = '.' - self._info['yes'] = options.yes def run(self): """ Go, go, go! """ @@ -108,7 +93,7 @@ class ModToolRemove(ModTool): return regexp # Go, go, go! if not self._skip_subdirs['lib']: - self._run_subdir('lib', ('*.cc', '*.h'), ('add_library',), + self._run_subdir('lib', ('*.cc', '*.h'), ('add_library', 'list'), cmakeedit_func=_remove_cc_test_case) if not self._skip_subdirs['include']: incl_files_deleted = self._run_subdir(self._info['includedir'], ('*.h',), ('install',)) diff --git a/gr-utils/src/python/modtool/parser_cc_block.py b/gr-utils/src/python/modtool/parser_cc_block.py index d11353cc7a..0d1d75f29a 100644 --- a/gr-utils/src/python/modtool/parser_cc_block.py +++ b/gr-utils/src/python/modtool/parser_cc_block.py @@ -121,12 +121,13 @@ class ParserCCBlock(object): if not in_string: if c[i] == ')': if parens_count == 0: - if read_state == 'type': + if read_state == 'type' and len(this_type): raise ValueError( 'Found closing parentheses before finishing last argument (this is how far I got: %s)' % str(param_list) ) - param_list.append((this_type, this_name, this_defv)) + if len(this_type): + param_list.append((this_type, this_name, this_defv)) end_of_list = True break else: diff --git a/gr-utils/src/python/modtool/templates.py b/gr-utils/src/python/modtool/templates.py index 91d8370b98..8777357112 100644 --- a/gr-utils/src/python/modtool/templates.py +++ b/gr-utils/src/python/modtool/templates.py @@ -490,9 +490,9 @@ Templates['grc_xml'] = '''<?xml version="1.0"?> # Usage Templates['usage'] = ''' -gr_modtool.py <command> [options] -- Run <command> with the given options. -gr_modtool.py help -- Show a list of commands. -gr_modtool.py help <command> -- Shows the help for a given command. ''' +gr_modtool <command> [options] -- Run <command> with the given options. +gr_modtool help -- Show a list of commands. +gr_modtool help <command> -- Shows the help for a given command. ''' # SWIG string Templates['swig_block_magic'] = """#if $version == '37' diff --git a/gr-utils/src/python/modtool/util_functions.py b/gr-utils/src/python/modtool/util_functions.py index 33d8ad3339..4ca294ac31 100644 --- a/gr-utils/src/python/modtool/util_functions.py +++ b/gr-utils/src/python/modtool/util_functions.py @@ -18,11 +18,13 @@ # the Free Software Foundation, Inc., 51 Franklin Street, # Boston, MA 02110-1301, USA. # -""" Utility functions for gr_modtool.py """ +""" Utility functions for gr_modtool """ import re import sys +# None of these must depend on other modtool stuff! + def get_command_from_argv(possible_cmds): """ Read the requested command from argv. This can't be done with optparse, since the option parser isn't defined before the command is known, and @@ -38,7 +40,7 @@ def get_command_from_argv(possible_cmds): return None def append_re_line_sequence(filename, linepattern, newline): - """Detects the re 'linepattern' in the file. After its last occurrence, + """ Detects the re 'linepattern' in the file. After its last occurrence, paste 'newline'. If the pattern does not exist, append the new line to the file. Then, write. """ oldfile = open(filename, 'r').read() @@ -99,19 +101,6 @@ def get_modname(): except AttributeError: return None -def get_class_dict(): - " Return a dictionary of the available commands in the form command->class " - classdict = {} - for g in globals().values(): - try: - if issubclass(g, ModTool): - classdict[g.name] = g - for a in g.aliases: - classdict[a] = g - except (TypeError, AttributeError): - pass - return classdict - def is_number(s): " Return True if the string s contains a number. " try: -- cgit v1.2.3 From 2ba9f1a544be17f40d60ba365bb084a342d41e0a Mon Sep 17 00:00:00 2001 From: Martin Braun <martin.braun@kit.edu> Date: Mon, 4 Feb 2013 15:34:41 +0100 Subject: modtool: bugfix, BLOCK_MAGIC should appear before %include --- gr-utils/src/python/modtool/modtool_add.py | 2 +- gr-utils/src/python/modtool/templates.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_add.py') diff --git a/gr-utils/src/python/modtool/modtool_add.py b/gr-utils/src/python/modtool/modtool_add.py index 7ca375b6f9..e1d61cf0fb 100644 --- a/gr-utils/src/python/modtool/modtool_add.py +++ b/gr-utils/src/python/modtool/modtool_add.py @@ -246,6 +246,7 @@ class ModToolAdd(ModTool): if self._info['version'] == '36': mod_block_sep = '_' swig_block_magic_str = get_template('swig_block_magic', **self._info) + open(self._file['swig'], 'a').write(swig_block_magic_str) include_str = '#include "%s%s%s.h"' % ( self._info['modname'], mod_block_sep, @@ -257,7 +258,6 @@ class ModToolAdd(ModTool): regexp = re.compile('^%\{\n', re.MULTILINE) oldfile = regexp.sub('%%{\n%s\n' % include_str, oldfile, count=1) open(self._file['swig'], 'w').write(oldfile) - open(self._file['swig'], 'a').write(swig_block_magic_str) def _run_python_qa(self): """ Do everything that needs doing in the subdir 'python' to add diff --git a/gr-utils/src/python/modtool/templates.py b/gr-utils/src/python/modtool/templates.py index 8777357112..f405bcabbe 100644 --- a/gr-utils/src/python/modtool/templates.py +++ b/gr-utils/src/python/modtool/templates.py @@ -502,8 +502,8 @@ Templates['swig_block_magic'] = """#if $version == '37' #set $mod_block_sep = '_' #set $block_magic_version = '' #end if -%include "${modname}${mod_block_sep}${blockname}.h" GR_SWIG_BLOCK_MAGIC${block_magic_version}($modname, $blockname); +%include "${modname}${mod_block_sep}${blockname}.h" """ ## Old stuff -- cgit v1.2.3