My favorites | Sign in
Project Home
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from django.core.management.base import BaseCommand
from django.core.management.color import no_style
from optparse import make_option
import sys
import os

try:
set
except NameError:
from sets import Set as set # Python 2.3 fallback

class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--fixtures', action='store_true', dest='infixtures', default=False,
help='Only look in app.fixtures subdir'),
make_option('--noscripts', action='store_true', dest='noscripts', default=False,
help='Look in app.scripts subdir'),
)
help = 'Runs a script in django context.'
args = "script [script ...]"

def handle(self, *scripts, **options):
from django.db.models import get_apps

subdirs = []

if not options.get('noscripts'):
subdirs.append('scripts')
if options.get('infixtures'):
subdirs.append('fixtures')
verbosity = int(options.get('verbosity', 1))
show_traceback = options.get('traceback', False)

if len(subdirs) < 1:
print "No subdirs to run left."
return

if len(scripts) < 1:
print "Script name required."
return

def run_script(name):
if verbosity > 1:
print "check for %s" % name
try:
t = __import__(name, [], [], [" "])

if verbosity > 0:
print "Found script %s ..." %name
if hasattr(t, "run"):
if verbosity > 1:
print "found run() in %s. executing..." % name
# TODO: add arguments to run
try:
t.run()
except Exception, e:
if verbosity > 0:
print "Exception while running run() in %s" %name
if show_traceback:
raise
else:
if verbosity > 1:
print "no run() function found."

except ImportError:
pass


for app in get_apps():
app_name = app.__name__.split(".")[:-1] # + ['fixtures']

for subdir in subdirs:
for script in scripts:
run_script(".".join(app_name + [subdir, script]))

# try app.DIR.script import
for script in scripts:
sa = script.split(".")
for subdir in subdirs:
nn = ".".join(sa[:-1] + [subdir, sa[-1]])
run_script(nn)

# try direct import
if script.find(".") != -1:
run_script(script)



# Backwards compatibility for Django r9110
if not [opt for opt in Command.option_list if opt.dest=='verbosity']:
Command.option_list += (
make_option('--verbosity', '-v', action="store", dest="verbosity",
default='1', type='choice', choices=['0', '1', '2'],
help="Verbosity level; 0=minimal output, 1=normal output, 2=all output"),
)

Change log

r149 by v.oostveen on Oct 22, 2008   Diff
missed verbosity patch for runscript
thanks clintecker! fixes ticket #61
Go to: 
Project members, sign in to write a code review

Older revisions

r94 by v.oostveen on Aug 9, 2008   Diff
name change, from django-command-
extensions to django-extensions
r93 by brosner on Aug 8, 2008   Diff
Chnaged the default verbosity level to
0 and adjusted the help to show the no
output option. Quiet please!
r74 by v.oostveen on Jun 24, 2008   Diff
added dumpscript (from willhardy) and
runscript from (poelzi)
All revisions of this file

File info

Size: 3129 bytes, 95 lines
Powered by Google Project Hosting