web/lib/django_extensions/management/commands/create_jobs.py
changeset 3 526ebd3988b0
equal deleted inserted replaced
1:ebaad720f88b 3:526ebd3988b0
       
     1 import os
       
     2 import sys
       
     3 from django.core.management.base import CommandError, AppCommand, _make_writeable
       
     4 
       
     5 class Command(AppCommand):
       
     6     help = ("Creates a Django jobs command directory structure for the given app name in the current directory.")
       
     7     args = "[appname]"
       
     8     label = 'application name'
       
     9 
       
    10     requires_model_validation = False
       
    11     # Can't import settings during this command, because they haven't
       
    12     # necessarily been created.
       
    13     can_import_settings = True
       
    14 
       
    15     def handle_app(self, app, **options):
       
    16         app_dir = os.path.dirname(app.__file__)
       
    17         copy_template('jobs_template', app_dir)
       
    18 
       
    19 def copy_template(template_name, copy_to):
       
    20     """copies the specified template directory to the copy_to location"""
       
    21     import django_extensions
       
    22     import re
       
    23     import shutil
       
    24     
       
    25     template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name)
       
    26 
       
    27     # walks the template structure and copies it
       
    28     for d, subdirs, files in os.walk(template_dir):
       
    29         relative_dir = d[len(template_dir)+1:]
       
    30         if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)):
       
    31             os.mkdir(os.path.join(copy_to, relative_dir))
       
    32         for i, subdir in enumerate(subdirs):
       
    33             if subdir.startswith('.'):
       
    34                 del subdirs[i]
       
    35         for f in files:
       
    36             if f.endswith('.pyc') or f.startswith('.DS_Store'):
       
    37                 continue
       
    38             path_old = os.path.join(d, f)
       
    39             path_new = os.path.join(copy_to, relative_dir, f)
       
    40             if os.path.exists(path_new):
       
    41                 path_new = os.path.join(copy_to, relative_dir, f)
       
    42                 if os.path.exists(path_new):
       
    43                     continue
       
    44             path_new = path_new.rstrip(".tmpl")
       
    45             fp_old = open(path_old, 'r')
       
    46             fp_new = open(path_new, 'w')
       
    47             fp_new.write(fp_old.read())
       
    48             fp_old.close()
       
    49             fp_new.close()
       
    50             try:
       
    51                 shutil.copymode(path_old, path_new)
       
    52                 _make_writeable(path_new)
       
    53             except OSError:
       
    54                 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))