Skip to content

Commit

Permalink
Add a --script-args option to the runscript management command, which…
Browse files Browse the repository at this point in the history
… allows

arbitrary positional string arguments to be passed to the scripts being run.
The same arguments are passed to all scripts. This should be backwards-compatible
with existing, no-argument scripts.
  • Loading branch information
Dan Fairs authored and justinabrahms committed Nov 24, 2010
1 parent e366b76 commit a2c7f99
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions django_extensions/management/commands/runscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
except NameError:
from sets import Set as set # Python 2.3 fallback

def vararg_callback(option, opt_str, opt_value, parser):
parser.rargs.insert(0, opt_value)
value = []
for arg in parser.rargs:
# stop on --foo like options
if arg[:2] == "--" and len(arg) > 2:
break
# stop on -a like options
if arg[:1] == "-":
break
value.append(arg)

del parser.rargs[:len(value)]
setattr(parser.values, option.dest, value)


class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--fixtures', action='store_true', dest='infixtures', default=False,
Expand All @@ -19,6 +35,9 @@ class Command(BaseCommand):
help='Run silently, do not show errors and tracebacks'),
make_option('--no-traceback', action='store_true', dest='no_traceback', default=False,
help='Do not show tracebacks'),
make_option('--script-args', action='callback', callback=vararg_callback, type='string',
help='Space-separated argument list to be passed to the scripts. Note that the '
'same arguments will be passed to all named scripts.'),
)
help = 'Runs a script in django context.'
args = "script [script ...]"
Expand Down Expand Up @@ -57,10 +76,9 @@ def handle(self, *scripts, **options):
print ERROR("Script name required.")
return

def run_script(mod):
# TODO: add arguments to run
def run_script(mod, *script_args):
try:
mod.run()
mod.run(*script_args)
except Exception, e:
if silent:
return
Expand Down Expand Up @@ -114,7 +132,11 @@ def find_modules_for_script(script):
modules.append(mod)

return modules


if options.get('script_args'):
script_args = options['script_args']
else:
script_args = []
for script in scripts:
modules = find_modules_for_script(script)
if not modules:
Expand All @@ -123,7 +145,8 @@ def find_modules_for_script(script):
for mod in modules:
if verbosity>1:
print NOTICE2("Running script '%s' ..." % mod.__name__)
run_script(mod)
run_script(mod, *script_args)


# Backwards compatibility for Django r9110
if not [opt for opt in Command.option_list if opt.dest=='verbosity']:
Expand Down

0 comments on commit a2c7f99

Please sign in to comment.