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_disable.py | 144 +++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 gr-utils/src/python/modtool/modtool_disable.py (limited to 'gr-utils/src/python/modtool/modtool_disable.py') diff --git a/gr-utils/src/python/modtool/modtool_disable.py b/gr-utils/src/python/modtool/modtool_disable.py new file mode 100644 index 000000000..67f15ad53 --- /dev/null +++ b/gr-utils/src/python/modtool/modtool_disable.py @@ -0,0 +1,144 @@ +""" Disable blocks module """ + +import os +import re +import sys +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' + aliases = ('dis',) + 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: + 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 disable? (Regex): ') + if len(self._info['pattern']) == 0: + self._info['pattern'] = '.' + self._info['yes'] = options.yes + + def run(self): + """ Go, go, go! """ + def _handle_py_qa(cmake, fname): + """ Do stuff for py qa """ + cmake.comment_out_lines('GR_ADD_TEST.*'+fname) + return True + def _handle_py_mod(cmake, fname): + """ Do stuff for py extra files """ + try: + initfile = open(self._file['pyinit']).read() + except IOError: + print "Could not edit __init__.py, that might be a problem." + return False + pymodname = os.path.splitext(fname)[0] + initfile = re.sub(r'((from|import)\s+\b'+pymodname+r'\b)', r'#\1', initfile) + open(self._file['pyinit'], 'w').write(initfile) + return False + def _handle_cc_qa(cmake, fname): + """ Do stuff for cc qa """ + if self._info['version'] == '37': + cmake.comment_out_lines('\$\{CMAKE_CURRENT_SOURCE_DIR\}/'+fname) + fname_base = os.path.splitext(fname)[0] + ed = CMakeFileEditor(self._file['qalib']) # Abusing the CMakeFileEditor... + ed.comment_out_lines('#include\s+"%s.h"' % fname_base, comment_str='//') + ed.comment_out_lines('%s::suite\(\)' % fname_base, comment_str='//') + ed.write() + elif self._info['version'] == '36': + cmake.comment_out_lines('add_executable.*'+fname) + cmake.comment_out_lines('target_link_libraries.*'+os.path.splitext(fname)[0]) + cmake.comment_out_lines('GR_ADD_TEST.*'+os.path.splitext(fname)[0]) + return True + def _handle_h_swig(cmake, fname): + """ Comment out include files from the SWIG file, + as well as the block magic """ + swigfile = open(self._file['swig']).read() + (swigfile, nsubs) = re.subn('(.include\s+"(%s/)?%s")' % ( + self._info['modname'], fname), + r'//\1', swigfile) + if nsubs > 0: + print "Changing %s..." % self._file['swig'] + if nsubs > 1: # Need to find a single BLOCK_MAGIC + blockname = os.path.splitext(fname[len(self._info['modname'])+1:])[0] + if self._info['version'] == '37': + blockname = os.path.splitext(fname)[0] + (swigfile, nsubs) = re.subn('(GR_SWIG_BLOCK_MAGIC2?.+%s.+;)' % blockname, r'//\1', swigfile) + if nsubs > 1: + print "Hm, changed more then expected while editing %s." % self._file['swig'] + open(self._file['swig'], 'w').write(swigfile) + return False + def _handle_i_swig(cmake, fname): + """ Comment out include files from the SWIG file, + as well as the block magic """ + swigfile = open(self._file['swig']).read() + blockname = os.path.splitext(fname[len(self._info['modname'])+1:])[0] + if self._info['version'] == '37': + blockname = os.path.splitext(fname)[0] + swigfile = re.sub('(%include\s+"'+fname+'")', r'//\1', swigfile) + print "Changing %s..." % self._file['swig'] + 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 + special_treatments = ( + ('python', 'qa.+py$', _handle_py_qa), + ('python', '^(?!qa).+py$', _handle_py_mod), + ('lib', 'qa.+\.cc$', _handle_cc_qa), + ('include/%s' % self._info['modname'], '.+\.h$', _handle_h_swig), + ('include', '.+\.h$', _handle_h_swig), + ('swig', '.+\.i$', _handle_i_swig) + ) + for subdir in self._subdirs: + if self._skip_subdirs[subdir]: continue + if self._info['version'] == '37' and subdir == 'include': + subdir = 'include/%s' % self._info['modname'] + try: + cmake = CMakeFileEditor(os.path.join(subdir, 'CMakeLists.txt')) + except IOError: + continue + print "Traversing %s..." % subdir + filenames = cmake.find_filenames_match(self._info['pattern']) + yes = self._info['yes'] + for fname in filenames: + file_disabled = False + if not yes: + ans = raw_input("Really disable %s? [Y/n/a/q]: " % fname).lower().strip() + if ans == 'a': + yes = True + if ans == 'q': + sys.exit(0) + if ans == 'n': + continue + for special_treatment in special_treatments: + if special_treatment[0] == subdir and re.match(special_treatment[1], fname): + file_disabled = special_treatment[2](cmake, fname) + if not file_disabled: + cmake.disable_file(fname) + cmake.write() + print "Careful: 'gr_modtool disable' does not resolve dependencies." + -- 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_disable.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/modtool_disable.py') diff --git a/gr-utils/src/python/modtool/modtool_disable.py b/gr-utils/src/python/modtool/modtool_disable.py index 67f15ad53..b0fb13245 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' -- 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_disable.py | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_disable.py') diff --git a/gr-utils/src/python/modtool/modtool_disable.py b/gr-utils/src/python/modtool/modtool_disable.py index b0fb13245..36725e557 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), -- cgit