summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2013-06-26 17:15:19 +0200
committerGuido Günther <agx@sigxcpu.org>2013-06-26 18:18:32 +0200
commita5aff11ba22a5b2f2c56d3325f431d51681e6c9b (patch)
tree3417d73fb0eddf7aa64591ed0b0e2b4873ef4673
parent375014db7fa3981b1b0e071013fb365a3840b32d (diff)
downloadgit-buildpackage-a5aff11ba22a5b2f2c56d3325f431d51681e6c9b.tar.gz
git-buildpackage-a5aff11ba22a5b2f2c56d3325f431d51681e6c9b.tar.bz2
git-buildpackage-a5aff11ba22a5b2f2c56d3325f431d51681e6c9b.zip
Consistently call gbp the supercommand
and robustify against invalid modules names.
-rw-r--r--gbp/config.py4
-rw-r--r--[-rwxr-xr-x]gbp/scripts/supercommand.py (renamed from gbp/scripts/command.py)26
-rw-r--r--setup.py2
-rw-r--r--tests/16_test_supercommand.py (renamed from tests/16_test_wrapper.py)21
4 files changed, 39 insertions, 14 deletions
diff --git a/gbp/config.py b/gbp/config.py
index f61ebc55..fdf16477 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -313,8 +313,8 @@ class GbpOptionParser(OptionParser):
if not (self.command.startswith('gbp-') or
self.command.startswith('git-')):
- # Invoked as gbp <subcommand> syntax, so parse the old sections
- # of {gbp.git}-<subcommand> for backward compatibility:
+ # Invoked as gbp <command> syntax, so parse the old sections
+ # of {gbp.git}-<command> for backward compatibility:
for prefix in ['gbp', 'git']:
oldcmd = '%s-%s' % (prefix, self.command)
if parser.has_section(oldcmd):
diff --git a/gbp/scripts/command.py b/gbp/scripts/supercommand.py
index a3cb6571..4f2721f3 100755..100644
--- a/gbp/scripts/command.py
+++ b/gbp/scripts/supercommand.py
@@ -15,11 +15,20 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-"""Wrapper for all gbp commands"""
+"""Supercommand for all gbp commands"""
+import re
import sys
+# Command is this module and common/ is shared code
+# so we don't allow these to be imported:
+invalid_modules = [ 'common', 'supercommand' ]
+
def sanitize(cmd):
+ """
+ '-' is not allowed in module names
+ so turn it into an underscore.
+ """
return cmd.replace('-', '_')
def usage():
@@ -35,15 +44,18 @@ The most commonly used commands are:
import-dscs - import multiple Debian source packages
"""
-def import_command(cmd):
- if '.' in cmd:
- raise ImportError('Illegal module name')
+def import_command(modulename):
+ """
+ Import the module that implements the given command
+ """
+ if (not re.match(r'[a-z][a-z0-9_]', modulename) or
+ modulename in invalid_modules):
+ raise ImportError('Illegal module name %s' % modulename)
- m = __import__('gbp.scripts.%s' % cmd, fromlist='main', level=0)
- return m
+ return __import__('gbp.scripts.%s' % modulename, fromlist='main', level=0)
-def gbp_command(argv=None):
+def supercommand(argv=None):
argv = argv or sys.argv
if len(argv) < 2:
diff --git a/setup.py b/setup.py
index fe7109a1..f74715f0 100644
--- a/setup.py
+++ b/setup.py
@@ -60,6 +60,6 @@ setup(name = "gbp",
setup_requires=['nose>=0.11.1', 'coverage>=2.85', 'nosexcover>=1.0.7'] if \
os.getenv('WITHOUT_NOSETESTS') is None else [],
entry_points = {
- 'console_scripts': [ 'gbp = gbp.scripts.command:gbp_command' ],
+ 'console_scripts': [ 'gbp = gbp.scripts.supercommand:supercommand' ],
},
)
diff --git a/tests/16_test_wrapper.py b/tests/16_test_supercommand.py
index 35f50bc8..5a9f0374 100644
--- a/tests/16_test_wrapper.py
+++ b/tests/16_test_supercommand.py
@@ -16,14 +16,27 @@
"""Test L{gbp} command wrapper"""
import unittest
-import gbp.scripts.command
+import gbp.scripts.supercommand
-class TestWrapper(unittest.TestCase):
+class TestSuperCommand(unittest.TestCase):
+
+ def test_import(self):
+ self.assertRaises(ImportError,
+ gbp.scripts.supercommand.import_command,
+ 'not.allowed')
+ self.assertRaises(ImportError,
+ gbp.scripts.supercommand.import_command,
+ 'not/allowed')
+ self.assertRaises(ImportError,
+ gbp.scripts.supercommand.import_command,
+ '0notallowed')
def test_invalid_command(self):
"""Test if we can import a valid command"""
- self.assertEqual(gbp.scripts.command.gbp_command(['argv0', 'asdf']), 2)
+ self.assertEqual(gbp.scripts.supercommand.supercommand(
+ ['argv0', 'asdf']), 2)
def test_missing_arg(self):
- self.assertEqual(gbp.scripts.command.gbp_command(['argv0']), 1)
+ self.assertEqual(gbp.scripts.supercommand.supercommand(
+ ['argv0']), 1)