diff options
author | Sebastian Koslowski <koslowski@kit.edu> | 2013-11-24 20:23:39 +0100 |
---|---|---|
committer | Martin Braun <martin.braun@kit.edu> | 2013-11-27 22:33:04 +0100 |
commit | fa5480e8bccec39278e9c5f81c09d1e702fd0fd7 (patch) | |
tree | 15d398b9828da5586944157857189de78f2a126f | |
parent | 77b36be049ed864beaf450413e4b960d909d6f8a (diff) |
modtool: no more exit() in modtool package
Conflicts:
gr-utils/python/modtool/modtool_newmod.py
-rw-r--r-- | gr-utils/python/modtool/__init__.py | 3 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_add.py | 48 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_base.py | 20 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_disable.py | 13 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_help.py | 10 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_info.py | 38 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_makexml.py | 21 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_newmod.py | 22 | ||||
-rw-r--r-- | gr-utils/python/modtool/modtool_rm.py | 17 | ||||
-rw-r--r-- | gr-utils/python/modtool/util_functions.py | 9 | ||||
-rwxr-xr-x | gr-utils/python/utils/gr_modtool | 14 |
11 files changed, 105 insertions, 110 deletions
diff --git a/gr-utils/python/modtool/__init__.py b/gr-utils/python/modtool/__init__.py index a242722ab4..a6a022a202 100644 --- a/gr-utils/python/modtool/__init__.py +++ b/gr-utils/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_base import ModTool, ModToolException, get_class_dict from modtool_add import ModToolAdd from modtool_disable import ModToolDisable from modtool_info import ModToolInfo from modtool_makexml import ModToolMakeXML from modtool_newmod import ModToolNewModule from modtool_rm import ModToolRemove +from templates import Templates # Leave this at the end from modtool_help import ModToolHelp from parser_cc_block import ParserCCBlock diff --git a/gr-utils/python/modtool/modtool_add.py b/gr-utils/python/modtool/modtool_add.py index 88b9068cda..1be20f0485 100644 --- a/gr-utils/python/modtool/modtool_add.py +++ b/gr-utils/python/modtool/modtool_add.py @@ -21,27 +21,30 @@ """ 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 modtool_base import ModTool, ModToolException from templates import Templates from code_generator import get_template import Cheetah.Template + class ModToolAdd(ModTool): """ Add block to the out-of-tree module. """ name = 'add' aliases = ('insert',) _block_types = ('sink', 'source', 'sync', 'decimator', 'interpolator', 'general', 'tagged_stream', 'hier', 'noblock') + def __init__(self): ModTool.__init__(self) self._add_cc_qa = False self._add_py_qa = False + self._skip_cmakefiles = False + self._license_file = None def setup_parser(self): parser = ModTool.setup_parser(self) @@ -63,9 +66,9 @@ class ModToolAdd(ModTool): parser.add_option_group(ogroup) return parser - def setup(self): - ModTool.setup(self) - options = self.options + def setup(self, options, args): + ModTool.setup(self, options, args) + self._info['blocktype'] = options.block_type if self._info['blocktype'] is None: while self._info['blocktype'] not in self._block_types: @@ -79,17 +82,15 @@ 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." - exit(1) + raise ModToolException('Missing or skipping relevant subdir.') if self._info['blockname'] is None: - if len(self.args) >= 2: - self._info['blockname'] = self.args[1] + if len(args) >= 2: + self._info['blockname'] = 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.' - exit(2) + raise ModToolException('Invalid block name.') print "Block/code identifier: " + self._info['blockname'] self._info['fullblockname'] = self._info['modname'] + '_' + self._info['blockname'] self._info['license'] = self.setup_choose_license() @@ -107,11 +108,12 @@ class ModToolAdd(ModTool): 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: + self._skip_cmakefiles = options.skip_cmakefiles + if self._info['version'] == 'autofoo' and not self._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 - + self._skip_cmakefiles = True + self._license_file = options.license_file def setup_choose_license(self): """ Select a license by the following rules, in this order: @@ -119,9 +121,9 @@ class ModToolAdd(ModTool): 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() + if self._license_file is not None \ + and os.path.isfile(self._license_file): + return open(self._license_file).read() elif os.path.isfile('LICENSE'): return open('LICENSE').read() elif os.path.isfile('LICENCE'): @@ -170,7 +172,7 @@ class ModToolAdd(ModTool): 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: + if not self._skip_cmakefiles: try: append_re_line_sequence(self._file['cmlib'], '\$\{CMAKE_CURRENT_SOURCE_DIR\}/qa_%s.cc.*\n' % self._info['modname'], @@ -189,7 +191,7 @@ class ModToolAdd(ModTool): " 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: + if not self._skip_cmakefiles: open(self._file['cmlib'], 'a').write( str( Cheetah.Template.Template( @@ -221,7 +223,7 @@ class ModToolAdd(ModTool): 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: + if not self._skip_cmakefiles: ed = CMakeFileEditor(self._file['cmlib']) cmake_list_var = '[a-z]*_?' + self._info['modname'] + '_sources' if not ed.append_value('list', fname_cc, to_ignore_start='APPEND ' + cmake_list_var): @@ -272,7 +274,7 @@ class ModToolAdd(ModTool): fname_py_qa = 'qa_' + self._info['blockname'] + '.py' self._write_tpl('qa_python', self._info['pydir'], fname_py_qa) os.chmod(os.path.join(self._info['pydir'], fname_py_qa), 0755) - if self.options.skip_cmakefiles or CMakeFileEditor(self._file['cmpython']).check_for_glob('qa_*.py'): + if self._skip_cmakefiles or CMakeFileEditor(self._file['cmpython']).check_for_glob('qa_*.py'): return print "Editing %s/CMakeLists.txt..." % self._info['pydir'] open(self._file['cmpython'], 'a').write( @@ -291,7 +293,7 @@ class ModToolAdd(ModTool): append_re_line_sequence(self._file['pyinit'], '(^from.*import.*\n|# import any pure.*\n)', 'from %s import %s' % (self._info['blockname'], self._info['blockname'])) - if self.options.skip_cmakefiles: + if self._skip_cmakefiles: return ed = CMakeFileEditor(self._file['cmpython']) ed.append_value('GR_PYTHON_INSTALL', fname_py, to_ignore_end='DESTINATION[^()]+') @@ -306,7 +308,7 @@ class ModToolAdd(ModTool): 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'): + if self._skip_cmakefiles or ed.check_for_glob('*.xml'): return print "Editing grc/CMakeLists.txt..." ed.append_value('install', fname_grc, to_ignore_end='DESTINATION[^()]+') diff --git a/gr-utils/python/modtool/modtool_base.py b/gr-utils/python/modtool/modtool_base.py index 86cbd8f420..768bce0f77 100644 --- a/gr-utils/python/modtool/modtool_base.py +++ b/gr-utils/python/modtool/modtool_base.py @@ -22,11 +22,14 @@ import os import re -import sys from optparse import OptionParser, OptionGroup from util_functions import get_modname -from templates import Templates + + +class ModToolException(BaseException): + """ Standard exception for modtool classes. """ + pass class ModTool(object): @@ -41,8 +44,6 @@ class ModTool(object): self._has_subdirs[subdir] = False self._skip_subdirs[subdir] = False self.parser = self.setup_parser() - self.args = None - self.options = None self._dir = None def setup_parser(self): @@ -72,20 +73,17 @@ class ModTool(object): parser.add_option_group(ogroup) return parser - def setup(self): + def setup(self, options, args): """ Initialise all internal variables, such as the module name etc. """ - (options, self.args) = self.parser.parse_args() self._dir = options.directory if not self._check_directory(self._dir): - print "No GNU Radio module found in the given directory. Quitting." - sys.exit(1) + raise ModToolException('No GNU Radio module found in the given directory.') if options.module_name is not None: self._info['modname'] = options.module_name else: self._info['modname'] = get_modname() if self._info['modname'] is None: - print "No GNU Radio module found in the given directory. Quitting." - sys.exit(1) + raise ModToolException('No GNU Radio module found in the given directory.') print "GNU Radio module name identified: " + self._info['modname'] if self._info['version'] == '36' and ( os.path.isdir(os.path.join('include', self._info['modname'])) or @@ -101,7 +99,6 @@ class ModTool(object): if options.skip_grc or not self._has_subdirs['grc']: self._skip_subdirs['grc'] = True self._info['blockname'] = options.block_name - self.options = options self._setup_files() self._info['yes'] = options.yes @@ -171,6 +168,7 @@ 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 = {} diff --git a/gr-utils/python/modtool/modtool_disable.py b/gr-utils/python/modtool/modtool_disable.py index 36725e5578..0538b47142 100644 --- a/gr-utils/python/modtool/modtool_disable.py +++ b/gr-utils/python/modtool/modtool_disable.py @@ -23,25 +23,26 @@ import os import re import sys -from optparse import OptionGroup from modtool_base import ModTool from cmakefile_editor import CMakeFileEditor + class ModToolDisable(ModTool): """ Disable block (comments out CMake entries for files) """ name = 'disable' aliases = ('dis',) + def __init__(self): ModTool.__init__(self) - def setup(self): - ModTool.setup(self) - options = self.options + def setup(self, options, args): + ModTool.setup(self, options, args) + if options.block_name is not None: self._info['pattern'] = options.block_name - elif len(self.args) >= 2: - self._info['pattern'] = self.args[1] + elif len(args) >= 2: + self._info['pattern'] = args[1] else: self._info['pattern'] = raw_input('Which blocks do you want to disable? (Regex): ') if len(self._info['pattern']) == 0: diff --git a/gr-utils/python/modtool/modtool_help.py b/gr-utils/python/modtool/modtool_help.py index 76d9fd28bd..b894e272e8 100644 --- a/gr-utils/python/modtool/modtool_help.py +++ b/gr-utils/python/modtool/modtool_help.py @@ -26,8 +26,8 @@ from templates import Templates def print_class_descriptions(): - ''' Go through all ModTool* classes and print their name, - alias and description. ''' + """ Go through all ModTool* classes and print their name, + alias and description. """ desclist = [] for gvar in globals().values(): try: @@ -40,14 +40,16 @@ def print_class_descriptions(): for description in desclist: print '%-8s %-12s %s' % description + class ModToolHelp(ModTool): - ''' Show some help. ''' + """ Show some help. """ name = 'help' aliases = ('h', '?') + def __init__(self): ModTool.__init__(self) - def setup(self): + def setup(self, options, args): pass def run(self): diff --git a/gr-utils/python/modtool/modtool_info.py b/gr-utils/python/modtool/modtool_info.py index 3b392a3102..4c7480d698 100644 --- a/gr-utils/python/modtool/modtool_info.py +++ b/gr-utils/python/modtool/modtool_info.py @@ -23,18 +23,23 @@ import os from optparse import OptionGroup -from modtool_base import ModTool +from modtool_base import ModTool, ModToolException from util_functions import get_modname + class ModToolInfo(ModTool): """ Return information about a given module """ name = 'info' aliases = ('getinfo', 'inf') + def __init__(self): ModTool.__init__(self) + self._directory = None + self._python_readable = False + self._suggested_dirs = None def setup_parser(self): - " Initialise the option parser for 'gr_modtool 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") @@ -45,28 +50,22 @@ class ModToolInfo(ModTool): parser.add_option_group(ogroup) return parser - def setup(self): + def setup(self, options, args): # Won't call parent's setup(), because that's too chatty - (self.options, self.args) = self.parser.parse_args() + self._directory = options.directory + self._python_readable = options.python_readable + self._suggested_dirs = options.suggested_dirs def run(self): """ Go, go, go! """ - mod_info = {} - mod_info['base_dir'] = self._get_base_dir(self.options.directory) + mod_info = dict() + mod_info['base_dir'] = self._get_base_dir(self._directory) if mod_info['base_dir'] is None: - if self.options.python_readable: - print '{}' - else: - print "No module found." - exit(1) + raise ModToolException('{}' if self._python_readable else "No module found.") os.chdir(mod_info['base_dir']) mod_info['modname'] = get_modname() if mod_info['modname'] is None: - if self.options.python_readable: - print '{}' - else: - print "No module found." - exit(1) + raise ModToolException('{}' if self._python_readable else "No module found.") if self._info['version'] == '36' and ( os.path.isdir(os.path.join('include', mod_info['modname'])) or os.path.isdir(os.path.join('include', 'gnuradio', mod_info['modname'])) @@ -85,7 +84,7 @@ class ModToolInfo(ModTool): if build_dir is not None: mod_info['build_dir'] = build_dir mod_info['incdirs'] += self._get_include_dirs(mod_info) - if self.options.python_readable: + if self._python_readable: print str(mod_info) else: self._pretty_print(mod_info) @@ -134,8 +133,8 @@ class ModToolInfo(ModTool): inc_dirs += line.replace('GNURADIO_RUNTIME_INCLUDE_DIRS:%s=' % path_or_internal, '').strip().split(';') except IOError: pass - if len(inc_dirs) == 0 and self.options.suggested_dirs is not None: - inc_dirs = [os.path.normpath(path) for path in self.options.suggested_dirs.split(':') if os.path.isdir(path)] + if len(inc_dirs) == 0 and self._suggested_dirs is not None: + inc_dirs = [os.path.normpath(path) for path in self._suggested_dirs.split(':') if os.path.isdir(path)] return inc_dirs def _pretty_print(self, mod_info): @@ -154,4 +153,3 @@ class ModToolInfo(ModTool): }[mod_info['version']] else: print '%19s: %s' % (index_names[key], mod_info[key]) - diff --git a/gr-utils/python/modtool/modtool_makexml.py b/gr-utils/python/modtool/modtool_makexml.py index 777cc09e1f..4b67d1b062 100644 --- a/gr-utils/python/modtool/modtool_makexml.py +++ b/gr-utils/python/modtool/modtool_makexml.py @@ -20,32 +20,32 @@ # """ 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 modtool_base import ModTool, ModToolException 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 """ name = 'makexml' aliases = ('mx',) + def __init__(self): ModTool.__init__(self) - def setup(self): - ModTool.setup(self) - options = self.options + def setup(self, options, args): + ModTool.setup(self, options, args) + if options.block_name is not None: self._info['pattern'] = options.block_name - elif len(self.args) >= 2: - self._info['pattern'] = self.args[1] + elif len(args) >= 2: + self._info['pattern'] = args[1] else: self._info['pattern'] = raw_input('Which blocks do you want to parse? (Regex): ') if len(self._info['pattern']) == 0: @@ -67,7 +67,6 @@ class ModToolMakeXML(ModTool): 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)) @@ -152,7 +151,7 @@ class ModToolMakeXML(ModTool): _type_translate ) except IOError: - print "Can't open some of the files necessary to parse %s." % fname_cc - sys.exit(1) + raise ModToolException("Can't open some of the files necessary to parse {}.".format(fname_cc)) + return (parser.read_params(), parser.read_io_signature(), blockname) diff --git a/gr-utils/python/modtool/modtool_newmod.py b/gr-utils/python/modtool/modtool_newmod.py index 6e55c5ea59..59818b1e62 100644 --- a/gr-utils/python/modtool/modtool_newmod.py +++ b/gr-utils/python/modtool/modtool_newmod.py @@ -25,7 +25,7 @@ import os import re from optparse import OptionGroup from gnuradio import gr -from modtool_base import ModTool +from modtool_base import ModTool, ModToolException class ModToolNewModule(ModTool): """ Create a new out-of-tree module """ @@ -44,17 +44,15 @@ class ModToolNewModule(ModTool): parser.add_option_group(ogroup) return parser - def setup(self): - (options, self.args) = self.parser.parse_args() + def setup(self, options, args): self._info['modname'] = options.module_name if self._info['modname'] is None: - if len(self.args) >= 2: - self._info['modname'] = self.args[1] + if len(args) >= 2: + self._info['modname'] = args[1] else: self._info['modname'] = raw_input('Name of the new module: ') if not re.match('[a-zA-Z0-9_]+$', self._info['modname']): - print 'Invalid module name.' - exit(2) + raise ModToolException('Invalid module name.') self._dir = options.directory if self._dir == '.': self._dir = './gr-%s' % self._info['modname'] @@ -63,14 +61,12 @@ class ModToolNewModule(ModTool): except OSError: pass # This is what should happen else: - print 'The given directory exists.' - exit(2) + raise ModToolException('The given directory exists.') if options.srcdir is None: options.srcdir = '/usr/local/share/gnuradio/modtool/gr-newmod' self._srcdir = gr.prefs().get_string('modtool', 'newmod_path', options.srcdir) if not os.path.isdir(self._srcdir): - print 'Error: Could not find gr-newmod source dir.' - exit(2) + raise ModToolException('Could not find gr-newmod source dir.') def run(self): """ @@ -83,9 +79,7 @@ class ModToolNewModule(ModTool): shutil.copytree(self._srcdir, self._dir) os.chdir(self._dir) except OSError: - print 'Failed.' - print 'Could not create directory %s. Quitting.' % self._dir - exit(2) + raise ModToolException('Could not create directory %s.' % self._dir) for root, dirs, files in os.walk('.'): for filename in files: f = os.path.join(root, filename) diff --git a/gr-utils/python/modtool/modtool_rm.py b/gr-utils/python/modtool/modtool_rm.py index 32dfee4806..4b69be180a 100644 --- a/gr-utils/python/modtool/modtool_rm.py +++ b/gr-utils/python/modtool/modtool_rm.py @@ -24,26 +24,27 @@ import os import re import sys import glob -from optparse import OptionGroup from util_functions import remove_pattern_from_file from modtool_base import ModTool from cmakefile_editor import CMakeFileEditor + class ModToolRemove(ModTool): """ Remove block (delete files and remove Makefile entries) """ name = 'remove' aliases = ('rm', 'del') + def __init__(self): ModTool.__init__(self) - def setup(self): - ModTool.setup(self) - options = self.options + def setup(self, options, args): + ModTool.setup(self, options, args) + if options.block_name is not None: self._info['pattern'] = options.block_name - elif len(self.args) >= 2: - self._info['pattern'] = self.args[1] + elif len(args) >= 2: + self._info['pattern'] = args[1] else: self._info['pattern'] = raw_input('Which blocks do you want to delete? (Regex): ') if len(self._info['pattern']) == 0: @@ -111,7 +112,6 @@ class ModToolRemove(ModTool): if not self._skip_subdirs['grc']: self._run_subdir('grc', ('*.xml',), ('install',)) - def _run_subdir(self, path, globs, makefile_vars, cmakeedit_func=None): """ Delete all files that match a certain pattern in path. path - The directory in which this will take place @@ -132,7 +132,7 @@ class ModToolRemove(ModTool): if len(files_filt) == 0: print "None found." return [] - # 2. Delete files, Makefile entries and other occurences + # 2. Delete files, Makefile entries and other occurrences files_deleted = [] ed = CMakeFileEditor('%s/CMakeLists.txt' % path) yes = self._info['yes'] @@ -156,4 +156,3 @@ class ModToolRemove(ModTool): cmakeedit_func(b, ed) ed.write() return files_deleted - diff --git a/gr-utils/python/modtool/util_functions.py b/gr-utils/python/modtool/util_functions.py index 71a7a7f535..ea7af0cf7c 100644 --- a/gr-utils/python/modtool/util_functions.py +++ b/gr-utils/python/modtool/util_functions.py @@ -29,13 +29,8 @@ 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 optparse throws an error.""" - command = None for arg in sys.argv: - if arg[0] == "-": - continue - else: - command = arg - if command in possible_cmds: + if arg[0] != "-" and arg in possible_cmds: return arg return None @@ -111,7 +106,7 @@ def get_modname(): return None def is_number(s): - " Return True if the string s contains a number. " + """ Return True if the string s contains a number. """ try: float(s) return True diff --git a/gr-utils/python/utils/gr_modtool b/gr-utils/python/utils/gr_modtool index 8c5c710aff..e714cf48e5 100755 --- a/gr-utils/python/utils/gr_modtool +++ b/gr-utils/python/utils/gr_modtool @@ -21,19 +21,25 @@ # """ A tool for editing GNU Radio out-of-tree modules. """ -import sys from gnuradio.modtool import * + def main(): """ Here we go. Parse command, choose class and run. """ cmd_dict = get_class_dict(globals().values()) command = get_command_from_argv(cmd_dict.keys()) if command is None: - print 'Usage:' + templates.Templates['usage'] + print 'Usage:' + Templates['usage'] exit(2) modtool = cmd_dict[command]() - modtool.setup() - modtool.run() + try: + (options, args) = modtool.parser.parse_args() + modtool.setup(options, args) + modtool.run() + + except ModToolException as err: + print >> sys.stderr, err + exit(1) if __name__ == '__main__': try: |