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_newmod.py | 88 +++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 gr-utils/src/python/modtool/modtool_newmod.py (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py new file mode 100644 index 000000000..730b72e39 --- /dev/null +++ b/gr-utils/src/python/modtool/modtool_newmod.py @@ -0,0 +1,88 @@ +""" Create a whole new out-of-tree module """ + +import os +import re +import sys +import base64 +import tarfile +from optparse import OptionGroup + +from modtool_base import ModTool +from newmod_tarfile import NEWMOD_TARFILE + +### New out-of-tree-mod module ############################################### +class ModToolNewModule(ModTool): + """ Create a new out-of-tree module """ + name = 'newmod' + aliases = ('nm', 'create') + def __init__(self): + ModTool.__init__(self) + + def setup_parser(self): + " Initialise the option parser for 'gr_modtool.py 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") + parser.add_option_group(ogroup) + return parser + + def setup(self): + (options, self.args) = self.parser.parse_args() + self._info['modname'] = options.module_name + if self._info['modname'] is None: + if len(self.args) >= 2: + self._info['modname'] = self.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.' + sys.exit(2) + 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: + pass # This is what should happen + else: + print 'The given directory exists.' + sys.exit(2) + + def run(self): + """ + * Unpack the tar.bz2 to the new locations + * Remove the bz2 + * Open all files, rename howto and HOWTO to the module name + * Rename files and directories that contain the word howto + """ + print "Creating directory..." + try: + os.mkdir(self._dir) + os.chdir(self._dir) + except OSError: + print 'Could not create directory %s. Quitting.' % self._dir + sys.exit(2) + print "Copying howto example..." + open('tmp.tar.bz2', 'wb').write(base64.b64decode(NEWMOD_TARFILE)) + print "Unpacking..." + tar = tarfile.open('tmp.tar.bz2', mode='r:bz2') + tar.extractall() + tar.close() + os.unlink('tmp.tar.bz2') + print "Replacing occurences of 'howto' to '%s'..." % self._info['modname'], + for root, dirs, files in os.walk('.'): + for filename in files: + f = os.path.join(root, filename) + s = open(f, 'r').read() + s = s.replace('howto', self._info['modname']) + s = s.replace('HOWTO', self._info['modname'].upper()) + open(f, 'w').write(s) + if filename.find('howto') != -1: + os.rename(f, os.path.join(root, filename.replace('howto', self._info['modname']))) + if os.path.basename(root) == 'howto': + os.rename(root, os.path.join(os.path.dirname(root), self._info['modname'])) + print "Done." + print "Use 'gr_modtool add' to add a new block to this currently empty module." + + -- cgit From f24128ea9036f68e61ff9350ce86ec619f9ae286 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 25 Jan 2013 15:43:02 +0100 Subject: utils: modtool now installs an example dir instead of the bz2 --- gr-utils/src/python/modtool/modtool_newmod.py | 28 +++++++-------------------- 1 file changed, 7 insertions(+), 21 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 730b72e39..9b2dc6e30 100644 --- a/gr-utils/src/python/modtool/modtool_newmod.py +++ b/gr-utils/src/python/modtool/modtool_newmod.py @@ -1,14 +1,10 @@ """ Create a whole new out-of-tree module """ +import shutil import os import re -import sys -import base64 -import tarfile from optparse import OptionGroup - from modtool_base import ModTool -from newmod_tarfile import NEWMOD_TARFILE ### New out-of-tree-mod module ############################################### class ModToolNewModule(ModTool): @@ -36,7 +32,7 @@ class ModToolNewModule(ModTool): 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.' - sys.exit(2) + exit(2) self._dir = options.directory if self._dir == '.': self._dir = './gr-%s' % self._info['modname'] @@ -47,30 +43,21 @@ class ModToolNewModule(ModTool): pass # This is what should happen else: print 'The given directory exists.' - sys.exit(2) + exit(2) def run(self): """ - * Unpack the tar.bz2 to the new locations - * Remove the bz2 + * Copy the example dir recursively * Open all files, rename howto and HOWTO to the module name * Rename files and directories that contain the word howto """ - print "Creating directory..." + print "Creating out-of-tree module in %s..." % self._dir try: - os.mkdir(self._dir) + shutil.copytree('/home/braun/.usrlocal/share/gnuradio/modtool/gr-newmod', self._dir) os.chdir(self._dir) except OSError: print 'Could not create directory %s. Quitting.' % self._dir - sys.exit(2) - print "Copying howto example..." - open('tmp.tar.bz2', 'wb').write(base64.b64decode(NEWMOD_TARFILE)) - print "Unpacking..." - tar = tarfile.open('tmp.tar.bz2', mode='r:bz2') - tar.extractall() - tar.close() - os.unlink('tmp.tar.bz2') - print "Replacing occurences of 'howto' to '%s'..." % self._info['modname'], + exit(2) for root, dirs, files in os.walk('.'): for filename in files: f = os.path.join(root, filename) @@ -85,4 +72,3 @@ class ModToolNewModule(ModTool): print "Done." print "Use 'gr_modtool add' to add a new block to this currently empty module." - -- cgit From c4971ab2cb4bdbb9abc5d5320bd16b45cf263cf8 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 25 Jan 2013 17:52:46 +0100 Subject: utils: modtool minor updates --- gr-utils/src/python/modtool/modtool_newmod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 9b2dc6e30..0c69cb69e 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) -- 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_newmod.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 0c69cb69e..7a5f635dd 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('.'): -- cgit From 03f101f509818a22d9488b4e5d08b9f0cd2b3b26 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Mon, 28 Jan 2013 11:48:10 +0100 Subject: modtool: newmod uses gr.prefs or --srcdir --- gr-utils/src/python/modtool/modtool_newmod.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 7a5f635dd..5e14493c3 100644 --- a/gr-utils/src/python/modtool/modtool_newmod.py +++ b/gr-utils/src/python/modtool/modtool_newmod.py @@ -24,6 +24,7 @@ import shutil import os import re from optparse import OptionGroup +from gnuradio import gr from modtool_base import ModTool class ModToolNewModule(ModTool): @@ -38,6 +39,8 @@ class ModToolNewModule(ModTool): 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") + ogroup.add_option("--srcdir", type="string", + help="Source directory for the module template.") parser.add_option_group(ogroup) return parser @@ -62,6 +65,12 @@ class ModToolNewModule(ModTool): else: print 'The given directory exists.' exit(2) + 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) def run(self): """ @@ -71,10 +80,10 @@ class ModToolNewModule(ModTool): """ print "Creating out-of-tree module in %s..." % self._dir, try: - shutil.copytree('/home/braun/.usrlocal/share/gnuradio/modtool/gr-newmod', self._dir) + shutil.copytree(self._srcdir, self._dir) os.chdir(self._dir) except OSError: - print 'FAILED' + print 'Failed.' print 'Could not create directory %s. Quitting.' % self._dir exit(2) for root, dirs, files in os.walk('.'): -- 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_newmod.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gr-utils/src/python/modtool/modtool_newmod.py') diff --git a/gr-utils/src/python/modtool/modtool_newmod.py b/gr-utils/src/python/modtool/modtool_newmod.py index 5e14493c3..102d83d8d 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") -- cgit