web/lib/django_extensions/management/commands/create_app.py
changeset 3 526ebd3988b0
equal deleted inserted replaced
1:ebaad720f88b 3:526ebd3988b0
       
     1 import os
       
     2 import re
       
     3 import django_extensions
       
     4 from django.core.management.base import CommandError, LabelCommand, _make_writeable
       
     5 from optparse import make_option
       
     6 
       
     7 class Command(LabelCommand):
       
     8     option_list = LabelCommand.option_list + (
       
     9         make_option('--template', '-t', action='store', dest='app_template', 
       
    10             help='The path to the app template'),
       
    11         make_option('--parent_path', '-p', action='store', dest='parent_path', 
       
    12             help='The parent path of the app to be created'),
       
    13     )
       
    14     
       
    15     help = ("Creates a Django application directory structure based on the specified template directory.")
       
    16     args = "[appname]"
       
    17     label = 'application name'
       
    18     
       
    19     requires_model_validation = False
       
    20     can_import_settings = True
       
    21     
       
    22     def handle_label(self, label, **options):
       
    23         project_dir = os.getcwd()
       
    24         project_name = os.path.split(project_dir)[-1]
       
    25         app_name =label
       
    26         app_template = options.get('app_template') or os.path.join(django_extensions.__path__[0], 'conf', 'app_template')
       
    27         app_dir = os.path.join(options.get('parent_path') or project_dir, app_name)
       
    28                 
       
    29         if not os.path.exists(app_template):
       
    30             raise CommandError("The template path, %r, does not exist." % app_template)
       
    31         
       
    32         if not re.search(r'^\w+$', label):
       
    33             raise CommandError("%r is not a valid application name. Please use only numbers, letters and underscores." % label)
       
    34         try:
       
    35             os.makedirs(app_dir)
       
    36         except OSError, e:
       
    37             raise CommandError(e)
       
    38         
       
    39         copy_template(app_template, app_dir, project_name, app_name)
       
    40         
       
    41 def copy_template(app_template, copy_to, project_name, app_name):
       
    42     """copies the specified template directory to the copy_to location"""
       
    43     import shutil
       
    44     
       
    45     # walks the template structure and copies it
       
    46     for d, subdirs, files in os.walk(app_template):
       
    47         relative_dir = d[len(app_template)+1:]
       
    48         if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)):
       
    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') or f.startswith('.DS_Store'):
       
    55                 continue
       
    56             path_old = os.path.join(d, f)
       
    57             path_new = os.path.join(copy_to, relative_dir, f.replace('app_name', app_name))
       
    58             if os.path.exists(path_new):
       
    59                 path_new = os.path.join(copy_to, relative_dir, f)
       
    60                 if os.path.exists(path_new):
       
    61                     continue
       
    62             path_new = path_new.rstrip(".tmpl")
       
    63             fp_old = open(path_old, 'r')
       
    64             fp_new = open(path_new, 'w')
       
    65             fp_new.write(fp_old.read().replace('{{ app_name }}', app_name).replace('{{ project_name }}', project_name))
       
    66             fp_old.close()
       
    67             fp_new.close()
       
    68             try:
       
    69                 shutil.copymode(path_old, path_new)
       
    70                 _make_writeable(path_new)
       
    71             except OSError:
       
    72                 sys.stderr.write(style.NOTICE("Notice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.\n" % path_new))