|
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 os |
|
7 |
|
8 class Command(NoArgsCommand): |
|
9 option_list = NoArgsCommand.option_list + ( |
|
10 make_option('--optimize', '-o', '-O', action='store_true', dest='optimize', |
|
11 help='Remove optimized python bytecode files'), |
|
12 make_option('--path', '-p', action='store', dest='path', |
|
13 help='Specify path to recurse into'), |
|
14 ) |
|
15 help = "Removes all python bytecode compiled files from the project." |
|
16 |
|
17 requires_model_validation = False |
|
18 |
|
19 def handle_noargs(self, **options): |
|
20 project_root = options.get("path", None) |
|
21 if not project_root: |
|
22 project_root = get_project_root() |
|
23 exts = options.get("optimize", False) and [".pyc", ".pyo"] or [".pyc"] |
|
24 verbose = int(options.get("verbosity", 1))>1 |
|
25 |
|
26 for root, dirs, files in os.walk(project_root): |
|
27 for file in files: |
|
28 ext = os.path.splitext(file)[1] |
|
29 if ext in exts: |
|
30 full_path = _j(root, file) |
|
31 if verbose: |
|
32 print full_path |
|
33 os.remove(full_path) |
|
34 |
|
35 # Backwards compatibility for Django r9110 |
|
36 if not [opt for opt in Command.option_list if opt.dest=='verbosity']: |
|
37 Command.option_list += ( |
|
38 make_option('--verbosity', '-v', action="store", dest="verbosity", |
|
39 default='1', type='choice', choices=['0', '1', '2'], |
|
40 help="Verbosity level; 0=minimal output, 1=normal output, 2=all output"), |
|
41 ) |