create_command.py 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import sys
  4. import shutil
  5. from django.core.management.base import AppCommand
  6. from django.core.management.color import color_style
  7. from django_extensions.management.utils import _make_writeable, signalcommand
  8. class Command(AppCommand):
  9. help = "Creates a Django management command directory structure for the given app name in the app's directory."
  10. requires_system_checks = False
  11. # Can't import settings during this command, because they haven't
  12. # necessarily been created.
  13. can_import_settings = True
  14. def add_arguments(self, parser):
  15. super().add_arguments(parser)
  16. parser.add_argument(
  17. '--name', '-n', action='store', dest='command_name',
  18. default='sample',
  19. help='The name to use for the management command'
  20. )
  21. parser.add_argument(
  22. '--base', '-b', action='store', dest='base_command',
  23. default='Base', help='The base class used for implementation of '
  24. 'this command. Should be one of Base, App, Label, or NoArgs'
  25. )
  26. parser.add_argument(
  27. '--dry-run', action='store_true', default=False,
  28. help='Do not actually create any files'
  29. )
  30. @signalcommand
  31. def handle_app_config(self, args, **options):
  32. app = args
  33. copy_template('command_template', app.path, **options)
  34. def copy_template(template_name, copy_to, **options):
  35. """Copy the specified template directory to the copy_to location"""
  36. import django_extensions
  37. style = color_style()
  38. ERROR = getattr(style, 'ERROR', lambda x: x)
  39. SUCCESS = getattr(style, 'SUCCESS', lambda x: x)
  40. command_name, base_command = options['command_name'], '%sCommand' % options['base_command']
  41. dry_run = options['dry_run']
  42. verbosity = options["verbosity"]
  43. template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name)
  44. # walk the template structure and copies it
  45. for d, subdirs, files in os.walk(template_dir):
  46. relative_dir = d[len(template_dir) + 1:]
  47. if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)):
  48. if not dry_run:
  49. os.mkdir(os.path.join(copy_to, relative_dir))
  50. for i, subdir in enumerate(subdirs):
  51. if subdir.startswith('.'):
  52. del subdirs[i]
  53. for f in files:
  54. if f.endswith(('.pyc', '.pyo')) or f.startswith(('.DS_Store', '__pycache__')):
  55. continue
  56. path_old = os.path.join(d, f)
  57. path_new = os.path.join(copy_to, relative_dir, f.replace('sample', command_name)).rstrip(".tmpl")
  58. if os.path.exists(path_new):
  59. path_new = os.path.join(copy_to, relative_dir, f).rstrip(".tmpl")
  60. if os.path.exists(path_new):
  61. if verbosity > 1:
  62. print(ERROR("%s already exists" % path_new))
  63. continue
  64. if verbosity > 1:
  65. print(SUCCESS("%s" % path_new))
  66. with open(path_old, 'r') as fp_orig:
  67. data = fp_orig.read()
  68. data = data.replace('{{ command_name }}', command_name)
  69. data = data.replace('{{ base_command }}', base_command)
  70. if not dry_run:
  71. with open(path_new, 'w') as fp_new:
  72. fp_new.write(data)
  73. if not dry_run:
  74. try:
  75. shutil.copymode(path_old, path_new)
  76. _make_writeable(path_new)
  77. except OSError:
  78. sys.stderr.write("Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.\n" % path_new)