|
| 1 | +import os |
| 2 | +import shutil |
| 3 | + |
| 4 | +from django.conf import settings |
| 5 | +from django.core.management.base import BaseCommand |
| 6 | + |
| 7 | + |
| 8 | +class Command(BaseCommand): |
| 9 | + |
| 10 | + def add_arguments(self, parser): |
| 11 | + parser.add_argument('command', choices=[ |
| 12 | + 'all', |
| 13 | + 'dist', |
| 14 | + 'media', |
| 15 | + 'npm', |
| 16 | + 'python', |
| 17 | + 'static', |
| 18 | + ]) |
| 19 | + |
| 20 | + def handle(self, *args, **options): |
| 21 | + if options['command'] in ['all', 'dist']: |
| 22 | + self.clean_dir('dist') |
| 23 | + self.clean_dir('daiquiri.egg-info') |
| 24 | + if options['command'] in ['all', 'media']: |
| 25 | + self.clean_dir(settings.MEDIA_ROOT) |
| 26 | + if options['command'] in ['all', 'npm']: |
| 27 | + self.clean_dir('node_modules') |
| 28 | + if options['command'] in ['all', 'static']: |
| 29 | + self.clean_static() |
| 30 | + if options['command'] in ['all', 'python']: |
| 31 | + self.clean_python() |
| 32 | + |
| 33 | + def clean_python(self): |
| 34 | + for root, dirs, files in os.walk('.'): |
| 35 | + for dir_name in dirs: |
| 36 | + if dir_name == '__pycache__': |
| 37 | + self.clean_dir(os.path.join(root, dir_name), quiet=True) |
| 38 | + |
| 39 | + def clean_static(self): |
| 40 | + self.clean_dir(settings.STATIC_ROOT) |
| 41 | + |
| 42 | + for path in [ |
| 43 | + 'daiquiri/auth/static/', |
| 44 | + 'daiquiri/contact/static/', |
| 45 | + 'daiquiri/core/static/', |
| 46 | + 'daiquiri/metadata/static/', |
| 47 | + 'daiquiri/query/static/', |
| 48 | + 'daiquiri/serve/static/', |
| 49 | + ]: |
| 50 | + self.clean_dir(path) |
| 51 | + |
| 52 | + def clean_dir(self, path, quiet=False): |
| 53 | + if path and os.path.exists(path): |
| 54 | + shutil.rmtree(path) |
| 55 | + if not quiet: |
| 56 | + print(f'Directory "{path}" has been removed!') |
0 commit comments