'Patch local_settings.py.example with local_settings.diff.
The patch application generates the local_settings.py file (the
local_settings.py.example remains unchanged).
http://github.com/sitkatech/pypatch fails if the
local_settings.py.example file is not 100% identical to the one used to
generate the first diff so we use the patch command instead.'

def patch(self, force=False):

    with DirContext(self.local_settings_dir) as dircontext:
        if os.path.exists(self.local_settings_diff):
            if ((not os.path.exists(self.local_settings_file)) or force):
                local_settings_reject = (self.local_settings_reject_pattern % time.strftime(self.file_time_fmt, time.localtime()))
                patch_cmd = shlex.split(('patch    %s    %s    -o    %s    -r    %s' % (self.local_settings_example, self.local_settings_diff, self.local_settings_file, local_settings_reject)))
                try:
                    subprocess.check_call(patch_cmd)
                except subprocess.CalledProcessError:
                    if os.path.exists(local_settings_reject):
                        sys.exit(('Some    conflict(s)    occurred.    Please    check    "%s"    to    find    unapplied    parts    of    the    diff.\nOnce    conflicts    are    solved,    it    is    safer    to    regenerate    a    newer    diff    with    the    "--gendiff"    option.' % os.path.join(dircontext.curdir, local_settings_reject)))
                    else:
                        sys.exit('An    unhandled    error    occurred.')
                print(('Generation    of    "%s"    successful.' % os.path.join(dircontext.curdir, self.local_settings_file)))
                sys.exit(0)
            else:
                sys.exit(('"%s"    already    exists.' % os.path.join(dircontext.curdir, self.local_settings_file)))
        else:
            sys.exit('No    diff    file    found,    please    generate    one    with    the    "--gendiff"    option.')
