|
1 import os |
|
2 from django.core.management.base import CommandError, AppCommand, _make_writeable |
|
3 from optparse import make_option |
|
4 |
|
5 class Command(AppCommand): |
|
6 option_list = AppCommand.option_list + ( |
|
7 make_option('--name', '-n', action='store', dest='command_name', default='sample', |
|
8 help='The name to use for the management command'), |
|
9 make_option('--base', '-b', action='store', dest='base_command', default='Base', |
|
10 help='The base class used for implementation of this command. Should be one of Base, App, Label, or NoArgs'), |
|
11 ) |
|
12 |
|
13 help = ("Creates a Django management command directory structure for the given app name" |
|
14 " in the current directory.") |
|
15 args = "[appname]" |
|
16 label = 'application name' |
|
17 |
|
18 requires_model_validation = False |
|
19 # Can't import settings during this command, because they haven't |
|
20 # necessarily been created. |
|
21 can_import_settings = True |
|
22 |
|
23 def handle_app(self, app, **options): |
|
24 directory = os.getcwd() |
|
25 app_name = app.__name__.split('.')[-2] |
|
26 project_dir = os.path.join(directory, app_name) |
|
27 if not os.path.exists(project_dir): |
|
28 try: |
|
29 os.mkdir(project_dir) |
|
30 except OSError, e: |
|
31 raise CommandError(e) |
|
32 |
|
33 copy_template('command_template', project_dir, options.get('command_name'), '%sCommand' % options.get('base_command')) |
|
34 |
|
35 def copy_template(template_name, copy_to, command_name, base_command): |
|
36 """copies the specified template directory to the copy_to location""" |
|
37 import django_extensions |
|
38 import re |
|
39 import shutil |
|
40 |
|
41 template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name) |
|
42 |
|
43 handle_method = "handle(self, *args, **options)" |
|
44 if base_command == 'AppCommand': |
|
45 handle_method = "handle_app(self, app, **options)" |
|
46 elif base_command == 'LabelCommand': |
|
47 handle_method = "handle_label(self, label, **options)" |
|
48 elif base_command == 'NoArgsCommand': |
|
49 handle_method = "handle_noargs(self, **options)" |
|
50 |
|
51 # walks the template structure and copies it |
|
52 for d, subdirs, files in os.walk(template_dir): |
|
53 relative_dir = d[len(template_dir)+1:] |
|
54 if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)): |
|
55 os.mkdir(os.path.join(copy_to, relative_dir)) |
|
56 for i, subdir in enumerate(subdirs): |
|
57 if subdir.startswith('.'): |
|
58 del subdirs[i] |
|
59 for f in files: |
|
60 if f.endswith('.pyc') or f.startswith('.DS_Store'): |
|
61 continue |
|
62 path_old = os.path.join(d, f) |
|
63 path_new = os.path.join(copy_to, relative_dir, f.replace('sample', command_name)) |
|
64 if os.path.exists(path_new): |
|
65 path_new = os.path.join(copy_to, relative_dir, f) |
|
66 if os.path.exists(path_new): |
|
67 continue |
|
68 path_new = path_new.rstrip(".tmpl") |
|
69 fp_old = open(path_old, 'r') |
|
70 fp_new = open(path_new, 'w') |
|
71 fp_new.write(fp_old.read().replace('{{ command_name }}', command_name).replace('{{ base_command }}', base_command).replace('{{ handle_method }}', handle_method)) |
|
72 fp_old.close() |
|
73 fp_new.close() |
|
74 try: |
|
75 shutil.copymode(path_old, path_new) |
|
76 _make_writeable(path_new) |
|
77 except OSError: |
|
78 sys.stderr.write(style.NOTICE("Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.\n" % path_new)) |