# Path: makesite/settings.py
# BASEDIR = op.abspath(op.dirname(__file__))
# TPL_DIR = op.join(BASEDIR, 'templates')
# MOD_DIR = op.join(BASEDIR, 'modules')
# TPLNAME = '.makesite'
# CFGNAME = 'makesite.ini'
# BASECONFIG = op.join(BASEDIR, CFGNAME)
# HOMECONFIG = op.join(getenv('HOME', '~'), CFGNAME)
# MAKESITE_HOME = environ.get('MAKESITE_HOME')
# USER = environ.get('USER', 'roor')
# SRC_CLONE = (
#     ('svn', 'svn checkout %(src)s %(source_dir)s'),
#     ('git', 'git clone %(src)s %(source_dir)s -b %(branch)s'),
#     ('hg', 'hg clone %(src)s %(source_dir)s'),
# )
# class MakesiteParser(ConfigParser):
#     def __init__(self, *args, **kwargs):
#     def defaults(self):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value, section='Main'):
#     def __getattr__(self, name):
#     def as_dict(self, section='Main', **kwargs):
#     def read(self, filenames, extending=False, map_sections=None):
# 
# Path: makesite/core.py
# def walklevel(dirpath, level=1):
#     dirpath = dirpath.rstrip(op.sep)
#     assert op.isdir(dirpath)
#     start = dirpath.count(op.sep)
#     for root, dirs, files in walk(dirpath):
#         yield root, dirs, files
#         if start + level <= root.count(op.sep):
#             del dirs[:]
# 
# def call(cmd, shell=True, **kwargs):
#     " Run shell command. "
# 
#     LOGGER.debug("Cmd: %s" % cmd)
#     check_call(cmd, shell=shell, stdout=LOGFILE_HANDLER.stream, **kwargs)
# 
# def print_header(msg, sep='='):
#     " More strong message "
# 
#     LOGGER.info("\n%s\n%s" % (msg, ''.join(sep for _ in msg)))
# 
# def is_exe(path):
#     return op.exists(path) and access(path, X_OK)
# 
# def gen_template_files(path):
#     " Generate relative template pathes. "
# 
#     path = path.rstrip(op.sep)
#     for root, _, files in walk(path):
#         for f in filter(lambda x: not x in (TPLNAME, CFGNAME), files):
#             yield op.relpath(op.join(root, f), path)
# 
# LOGGER = logging.getLogger('Makesite')


from os import path as op, listdir, makedirs, remove
from shutil import copy2
from tempfile import mkdtemp
from tempita import Template
from makesite import settings
from makesite.core import walklevel, call, print_header, is_exe, gen_template_files, LOGGER
        if not template.startswith(self.source_dir):
            for f in gen_template_files(template):
                call('sudo rm -f %s' % op.join(self.deploy_dir, f))
        self.templates = filter(lambda x: x != template_name, self.templates)
        self['template'] = ','.join(self.templates)
        self.write()

    def write(self, deploy_dir=None):
        deploy_dir = deploy_dir or self.deploy_dir
        with open(op.join(deploy_dir, settings.CFGNAME), 'w') as f:
            super(Site, self).write(f)

    def clean(self):
        print_header('Delete %s' % self.get_name(), sep="-")
        call('sudo rm -rf %s' % self.deploy_dir)

    @property
    def safe_branch(self):
        return self['safe_branch'] or self.branch.replace('/', '-').replace(' ', '-')

    def _gen_scripts(self, prefix, service_dir=None, template_name=None):
        service_dir = service_dir or self.service_dir or op.join(
            self.deploy_dir, 'service')
        files = sorted(listdir(service_dir))
        for template in self.templates:
            for f in files:
                if template_name and not f.startswith("%s_" % template_name):
                    continue

                path = op.join(service_dir, f)

# Path: makesite/settings.py
# BASEDIR = op.abspath(op.dirname(__file__))
# TPL_DIR = op.join(BASEDIR, 'templates')
# MOD_DIR = op.join(BASEDIR, 'modules')
# TPLNAME = '.makesite'
# CFGNAME = 'makesite.ini'
# BASECONFIG = op.join(BASEDIR, CFGNAME)
# HOMECONFIG = op.join(getenv('HOME', '~'), CFGNAME)
# MAKESITE_HOME = environ.get('MAKESITE_HOME')
# USER = environ.get('USER', 'roor')
# SRC_CLONE = (
#     ('svn', 'svn checkout %(src)s %(source_dir)s'),
#     ('git', 'git clone %(src)s %(source_dir)s -b %(branch)s'),
#     ('hg', 'hg clone %(src)s %(source_dir)s'),
# )
# class MakesiteParser(ConfigParser):
#     def __init__(self, *args, **kwargs):
#     def defaults(self):
#     def __getitem__(self, name):
#     def __setitem__(self, name, value, section='Main'):
#     def __getattr__(self, name):
#     def as_dict(self, section='Main', **kwargs):
#     def read(self, filenames, extending=False, map_sections=None):
# 
# Path: makesite/core.py
# def walklevel(dirpath, level=1):
#     dirpath = dirpath.rstrip(op.sep)
#     assert op.isdir(dirpath)
#     start = dirpath.count(op.sep)
#     for root, dirs, files in walk(dirpath):
#         yield root, dirs, files
#         if start + level <= root.count(op.sep):
#             del dirs[:]
# 
# def call(cmd, shell=True, **kwargs):
#     " Run shell command. "
# 
#     LOGGER.debug("Cmd: %s" % cmd)
#     check_call(cmd, shell=shell, stdout=LOGFILE_HANDLER.stream, **kwargs)
# 
# def print_header(msg, sep='='):
#     " More strong message "
# 
#     LOGGER.info("\n%s\n%s" % (msg, ''.join(sep for _ in msg)))
# 
# def is_exe(path):
#     return op.exists(path) and access(path, X_OK)
# 
# def gen_template_files(path):
#     " Generate relative template pathes. "
# 
#     path = path.rstrip(op.sep)
#     for root, _, files in walk(path):
#         for f in filter(lambda x: not x in (TPLNAME, CFGNAME), files):
#             yield op.relpath(op.join(root, f), path)
# 
# LOGGER = logging.getLogger('Makesite')


# Path: makesite/site.py
from os import path as op, listdir, makedirs, remove
from shutil import copy2
from tempfile import mkdtemp
from tempita import Template
from makesite import settings
from makesite.core import walklevel, call, print_header, is_exe, gen_template_files, LOGGER

        if not template.startswith(self.source_dir):
            for f in gen_template_files(template):
                call('sudo rm -f %s' % op.join(self.deploy_dir, f))
        self.templates = filter(lambda x: x != template_name, self.templates)
        self['template'] = ','.join(self.templates)
        self.write()

    def write(self, deploy_dir=None):
        deploy_dir = deploy_dir or self.deploy_dir
        with open(op.join(deploy_dir, settings.CFGNAME), 'w') as f:
            super(Site, self).write(f)

    def clean(self):
        print_header('Delete %s' % self.get_name(), sep="-")
        call('sudo rm -rf %s' % self.deploy_dir)

    @property
    def safe_branch(self):
        return self['safe_branch'] or self.branch.replace('/', '-').replace(' ', '-')

    def _gen_scripts(self, prefix, service_dir=None, template_name=None):
        service_dir = service_dir or self.service_dir or op.join(
            self.deploy_dir, 'service')
        files = sorted(listdir(service_dir))
        for template in self.templates:
            for f in files:
                if template_name and not f.startswith("%s_" % template_name):
                    continue

                path = op.join(service_dir, f)

                if f.startswith(template) and (not prefix or prefix in f) and is_exe(path):