compile_pyc.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. import fnmatch
  3. import os
  4. import py_compile
  5. from os.path import join as _j
  6. from django.conf import settings
  7. from django.core.management.base import BaseCommand, CommandError
  8. from django_extensions.management.utils import signalcommand
  9. class Command(BaseCommand):
  10. help = "Compile python bytecode files for the project."
  11. requires_system_checks = False
  12. def add_arguments(self, parser):
  13. parser.add_argument('--path', '-p', action='store', dest='path',
  14. help='Specify path to recurse into')
  15. @signalcommand
  16. def handle(self, *args, **options):
  17. project_root = options["path"]
  18. if not project_root:
  19. project_root = getattr(settings, 'BASE_DIR', None)
  20. verbosity = options["verbosity"]
  21. if not project_root:
  22. raise CommandError("No --path specified and settings.py does not contain BASE_DIR")
  23. for root, dirs, filenames in os.walk(project_root):
  24. for filename in fnmatch.filter(filenames, '*.py'):
  25. full_path = _j(root, filename)
  26. if verbosity > 1:
  27. self.stdout.write("Compiling %s...\n" % full_path)
  28. py_compile.compile(full_path)