Code

Ticket #3528: manage.py

File manage.py, 1.6 KB (added by phil.h.smith@…, 7 years ago)

pseudo code for overriding execute_manager to add custom ./manage.py actions

Line 
1#!/usr/bin/env python
2
3# Import things we need because we duplicated execute_manager
4from django.core.management import stuff...
5
6try:
7    import settings # Assumed to be in the same directory.
8except ImportError:
9    import sys
10    sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
11    sys.exit(1)
12
13# inline because this is dpaste
14def twiddle(app):
15    print 'Twiddle the frotz!'
16
17def execute_manager(settings_mod, argv=None):
18    project_directory = setup_environ(settings_mod)
19    action_mapping = DEFAULT_ACTION_MAPPING.copy()
20
21    # Remove the "startproject" command from the action_mapping, because that's
22    # a django-admin.py command, not a manage.py command.
23    del action_mapping['startproject']
24
25    # Override the startapp handler so that it always uses the
26    # project_directory, not the current working directory (which is default).
27    action_mapping['startapp'] = lambda app_name, directory: startapp(app_name, project_directory)
28    action_mapping['startapp'].__doc__ = startapp.__doc__
29    action_mapping['startapp'].help_doc = startapp.help_doc
30    action_mapping['startapp'].args = startapp.args
31
32    # CUSTOM ACTION MAPPINGS
33    action_mapping['twiddle'] = twiddle
34
35    # Run the django-admin.py command.
36    execute_from_command_line(action_mapping, argv)
37
38if __name__ == "__main__":
39    execute_manager(settings)