|
1 from django.core.management.base import NoArgsCommand |
|
2 from django_extensions.management.utils import get_project_root |
|
3 from random import choice |
|
4 from optparse import make_option |
|
5 from os.path import join as _j |
|
6 import py_compile |
|
7 import os |
|
8 |
|
9 class Command(NoArgsCommand): |
|
10 option_list = NoArgsCommand.option_list + ( |
|
11 make_option('--path', '-p', action='store', dest='path', |
|
12 help='Specify path to recurse into'), |
|
13 ) |
|
14 help = "Compile python bytecode files for the project." |
|
15 |
|
16 requires_model_validation = False |
|
17 |
|
18 def handle_noargs(self, **options): |
|
19 project_root = options.get("path", None) |
|
20 if not project_root: |
|
21 project_root = get_project_root() |
|
22 verbose = int(options.get("verbosity", 1))>1 |
|
23 |
|
24 for root, dirs, files in os.walk(project_root): |
|
25 for file in files: |
|
26 ext = os.path.splitext(file)[1] |
|
27 if ext==".py": |
|
28 full_path = _j(root, file) |
|
29 if verbose: |
|
30 print "%sc" % full_path |
|
31 py_compile.compile(full_path) |
|
32 |
|
33 # Backwards compatibility for Django r9110 |
|
34 if not [opt for opt in Command.option_list if opt.dest=='verbosity']: |
|
35 Command.option_list += ( |
|
36 make_option('--verbosity', '-v', action="store", dest="verbosity", |
|
37 default='1', type='choice', choices=['0', '1', '2'], |
|
38 help="Verbosity level; 0=minimal output, 1=normal output, 2=all output"), |
|
39 ) |