django-envconf / management /

import sys
import os
import envconf
from import CommandError

def _make_writeable(filename):
    Make sure that the file is writeable. Useful if our source is

    import stat
    if sys.platform.startswith('java'):
        # On Jython there is no os.access()
    if not os.access(filename, os.W_OK):
        st = os.stat(filename)
        new_permissions = stat.S_IMODE(st.st_mode) | stat.S_IWUSR
        os.chmod(filename, new_permissions)

def copy_helper(style, enviornemt_or_project, name, directory, other_name = ''):
    import re
    import shutil
    other = {'project': 'environment', 'environment': 'project'}[enviornemt_or_project]
    if not'^[_a-zA-Z]\w*$', name): # If it's not a valid directory name.
        # Provide a smart error message, depending on the error.
        if not'^[_a-zA-Z]', name):
            message = 'make sure the name begins with a letter or underscore'
            message = 'use only numbers, letters and underscores'
        raise CommandError("%r is not a valid %s name. Please %s." % (name, enviornemt_or_project, message))
    top_dir = os.path.join(directory, name)
    except OSError, e:
        raise CommandError(e)

    # Determine where the app or project templates are. Use
    # envconf.__path__[0] because we don't know into which directory
    # envconf has been installed.
    template_dir = os.path.join(envconf.__path__[0], 'conf', '%s_template' % enviornemt_or_project)

    for d, subdirs, files in os.walk(template_dir):
        relative_dir = d[len(template_dir) + 1:].replace('%s_name' % enviornemt_or_project, name)
        if relative_dir:
            os.mkdir(os.path.join(top_dir, relative_dir))
        for subdir in subdirs[:]:
            if subdir.startswith('.'):
        for f in files:
            if not f.endswith('.py'):
                # Ignore .pyc, .pyo, .py.class etc, as they cause various
                # breakages.
            path_old = os.path.join(d, f)
            path_new = os.path.join(top_dir, relative_dir, f.replace('%s_name' % enviornemt_or_project, name))
            fp_old = open(path_old, 'r')
            fp_new = open(path_new, 'w')
            fp_new.write('{{ %s_name }}' % enviornemt_or_project, name).replace('{{ %s_name }}' % other, other_name))
                shutil.copymode(path_old, 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))