def copy_template(app_template, copy_to, project_name, app_name): """copies the specified template directory to the copy_to location""" import shutil # walks the template structure and copies it for d, subdirs, files in os.walk(app_template): relative_dir = d[len(app_template)+1:] if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)): os.mkdir(os.path.join(copy_to, relative_dir)) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc') or f.startswith('.DS_Store'): continue path_old = os.path.join(d, f) path_new = os.path.join(copy_to, relative_dir, f.replace('app_name', app_name)) if os.path.exists(path_new): path_new = os.path.join(copy_to, relative_dir, f) if os.path.exists(path_new): continue path_new = path_new.rstrip(".tmpl") fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ app_name }}', app_name).replace('{{ project_name }}', project_name)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
def copy_dir(source, dest, name, remove_if_exists, style, link=False): """ Copy directory recursively from a template directory """ if remove_if_exists: remove_dst_if_exist(dest) os.makedirs(dest) for d, subdirs, files in os.walk(source): relative_dir = d[len(source) + 1:].replace('project_name', name) new_relative_dir = os.path.join(dest, relative_dir) if not os.path.exists(new_relative_dir): os.makedirs(new_relative_dir) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc'): continue path_old = os.path.join(d, f) path_new = os.path.join(dest, relative_dir, f.replace('project_name', name)) fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ project_name }}', name)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
def copy_dir(source, dest, name, remove_if_exists, style, link=False): """ Copy directory recursively from a template directory """ if remove_if_exists: remove_dst_if_exist(dest) os.makedirs(dest) for d, subdirs, files in os.walk(source): relative_dir = d[len(source) + 1:].replace('project_name', name) new_relative_dir = os.path.join(dest, relative_dir) if not os.path.exists(new_relative_dir): os.makedirs(new_relative_dir) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc'): continue path_old = os.path.join(d, f) path_new = os.path.join(dest, relative_dir, f.replace('project_name', name)) fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ project_name }}', name)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
def copy_template( template_name, copy_to, command_name, base_command, ): """copies the specified template directory to the copy_to location""" import django_extensions import re import shutil template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name) handle_method = 'handle(self, *args, **options)' if base_command == 'AppCommand': handle_method = 'handle_app(self, app, **options)' elif base_command == 'LabelCommand': handle_method = 'handle_label(self, label, **options)' elif base_command == 'NoArgsCommand': handle_method = 'handle_noargs(self, **options)' # walks the template structure and copies it for (d, subdirs, files) in os.walk(template_dir): relative_dir = d[len(template_dir) + 1:] if relative_dir and not os.path.exists(os.path.join(copy_to, relative_dir)): os.mkdir(os.path.join(copy_to, relative_dir)) for (i, subdir) in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc') or f.startswith('.DS_Store'): continue path_old = os.path.join(d, f) path_new = os.path.join(copy_to, relative_dir, f.replace('sample', command_name)) if os.path.exists(path_new): path_new = os.path.join(copy_to, relative_dir, f) if os.path.exists(path_new): continue path_new = path_new.rstrip('.tmpl') fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ command_name }}', command_name).replace('{{ base_command }}', base_command).replace('{{ handle_method }}', handle_method)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
def make_writeable(target): """ Changes permissions for the target using internal Django function. """ for dir, subdirs, files in os.walk(target): file_names = [os.path.join(dir, fn) for fn in files] subdir_names = [os.path.join(dir, subdir) for subdir in subdirs] entries = file_names + subdir_names for entry in entries: _make_writeable(entry)
def copy_template(template_name, copy_to, command_name, base_command): """copies the specified template directory to the copy_to location""" import django_extensions import re import shutil template_dir = os.path.join(django_extensions.__path__[0], 'conf', template_name) handle_method = "handle(self, *args, **options)" if base_command == 'AppCommand': handle_method = "handle_app(self, app, **options)" elif base_command == 'LabelCommand': handle_method = "handle_label(self, label, **options)" elif base_command == 'NoArgsCommand': handle_method = "handle_noargs(self, **options)" # walks the template structure and copies it for d, subdirs, files in os.walk(template_dir): relative_dir = d[len(template_dir) + 1:] if relative_dir and not os.path.exists( os.path.join(copy_to, relative_dir)): os.mkdir(os.path.join(copy_to, relative_dir)) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] for f in files: if f.endswith('.pyc') or f.startswith('.DS_Store'): continue path_old = os.path.join(d, f) path_new = os.path.join(copy_to, relative_dir, f.replace('sample', command_name)) if os.path.exists(path_new): path_new = os.path.join(copy_to, relative_dir, f) if os.path.exists(path_new): continue path_new = path_new.rstrip(".tmpl") fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ command_name }}', command_name).replace( '{{ base_command }}', base_command).replace( '{{ handle_method }}', handle_method)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
def copy_template(app_template, copy_to, project_name, app_name): """copies the specified template directory to the copy_to location""" import shutil app_template = os.path.normpath(app_template) # walks the template structure and copies it for d, subdirs, files in os.walk(app_template): relative_dir = d[len(app_template) + 1:] d_new = os.path.join(copy_to, relative_dir).replace('app_name', app_name) if relative_dir and not os.path.exists(d_new): os.mkdir(d_new) for i, subdir in enumerate(subdirs): if subdir.startswith('.'): del subdirs[i] replacements = {'app_name': app_name, 'project_name': project_name} replacements.update(REPLACEMENTS) for f in files: if f.endswith('.pyc') or f.startswith('.DS_Store'): continue path_old = os.path.join(d, f) path_new = os.path.join(d_new, f.replace('app_name', app_name)) if os.path.exists(path_new): path_new = os.path.join(d_new, f) if os.path.exists(path_new): continue if path_new.endswith('.tmpl'): path_new = path_new[:-5] fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(Template(fp_old.read()).render(Context(replacements))) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
# Determine where the app or project templates are. Use # django.__path__[0] because we don't know into which directory # django has been installed. # template_dir = os.path.join(django.__path__[0], 'conf', '%s_template' % app_or_project) for d, subdirs, files in os.walk(template_dir): relative_dir = d[len(template_dir)+1:].replace('%s_name' % app_or_project, name) if relative_dir: os.mkdir(os.path.join(top_dir, relative_dir)) for subdir in subdirs[:]: if subdir.startswith('.'): subdirs.remove(subdir) for f in files: if not f.endswith('.py'): # Ignore .pyc, .pyo, .py.class etc, as they cause various # breakages. continue path_old = os.path.join(d, f) path_new = os.path.join(top_dir, relative_dir, f.replace('%s_name' % app_or_project, name)) fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') fp_new.write(fp_old.read().replace('{{ %s_name }}' % app_or_project, name).replace('{{ %s_name }}' % other, other_name)) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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))
# Ignore .pyc, .pyo, .py.class etc, as they cause various # breakages. continue path_old = os.path.join(d, f) path_new = os.path.join(top_dir, relative_dir, f.replace('project_name', name)) fp_old = open(path_old, 'r') fp_new = open(path_new, 'w') if noadmin: fp_new.write(fp_old.read().replace('{{ project_name }}', name)) else: fp_new.write(fp_old.read().replace('{{ project_name }}', name).replace('## ','')) fp_old.close() fp_new.close() try: shutil.copymode(path_old, path_new) _make_writeable(path_new) except OSError: 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)) def start_distribution(project_name, template_dir, dist, noadmin): """ Custom startproject command to override django default """ directory = os.getcwd() # Check that the project_name cannot be imported. try: import_module(project_name) except ImportError: