# Path: fabdeploy/containers.py
# def conf(func):
#     """Decorator to mark function as config source."""
# 
#     func._is_conf = True
#     return func
# 
# Path: fabdeploy/task.py
# class Task(BaseTask):
#     name = None
#     task_kwargs = {}
#     _curr_conf_names = set()
# 
#     def __init__(self, *args, **kwargs):
#         super(Task, self).__init__(*args, **kwargs)
#         if self.name is None:
#             self.name = self._generate_name()
#         self._reset_conf()
# 
#     def before_do(self):
#         pass
# 
#     def after_do(self, result):
#         pass
# 
#     def do(self):
#         raise NotImplementedError()
# 
#     def conf_keys(self):
#         keys = set()
#         keys.update(self.task_kwargs.keys())
#         for name, value in inspect.getmembers(self):
#             if hasattr(value, '_is_conf'):
#                 keys.add(name)
#         return keys
# 
#     def conf_value(self, name, null=object()):
#         if name in self._curr_conf_names:
#             raise MissingVarException
#         self._curr_conf_names.add(name)
#         value = null
# 
#         if name in self.task_kwargs:
#             value = self.task_kwargs[name]
#         else:
#             try:
#                 attr = getattr(self, name)
#                 if hasattr(attr, '_is_conf'):
#                     value = attr()
#             except AttributeError:
#                 pass
# 
#         self._curr_conf_names.remove(name)
#         if value is not null:
#             return value
#         else:
#             raise MissingVarException
# 
#     def _generate_name(self):
#         s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', self.__class__.__name__)
#         return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
# 
#     def _reset_conf(self):
#         self.conf = None
#         self.task_kwargs = {}
# 
#     def _get_module(self):
#         return self.__module__.split('.')[-1]
# 
#     def _namespaces(self, kwargs):
#         module = self._get_module()
#         ns = [
#             '%s__' % module,
#             '%s__' % self.name,
#             '%s__%s__' % (module, self.name),
#         ]
#         if '_namespace' in kwargs:
#             ns.append(kwargs['_namespace'])
#         return ns
# 
#     def _default_conf(self):
#         if not hasattr(env, 'conf'):
#             setup_fabdeploy()
#         assert isinstance(env.conf, BaseConf)
#         conf = env.conf.copy()
#         conf.set_global_conf(env.conf)
#         conf.add_task(self)
#         return conf
# 
#     @contextmanager
#     def tmp_conf(self, conf=None, task_kwargs={}, **fabric_settings):
#         try:
#             if isinstance(conf, dict):
#                 warnings.warn(
#                     'Please pass config dict as explicit task_kwargs kwarg. '
#                     'For example, task.tmp_conf(task_kwargs={"address": ""}).',
#                     DeprecationWarning)
# 
#                 task_kwargs = conf
#                 conf = None
# 
#             if conf is not None:
#                 assert isinstance(conf, BaseConf)
#                 self.conf = conf.copy()
#                 self.task_kwargs = task_kwargs
#                 self.conf.add_task(self)
#             elif self.conf is None:
#                 self.conf = self._default_conf()
#                 self.task_kwargs = task_kwargs
# 
#             self.conf.set_name('%s.%s' % (self._get_module(), self.name))
#             unprefix_conf(self.conf, self._namespaces(self.task_kwargs))
# 
#             with settings(
#                 host_string=self.conf.get('address', ''), **fabric_settings):
#                 yield
#         finally:
#             self._reset_conf()
# 
#     def run(self, **kwargs):
#         with self.tmp_conf(task_kwargs=kwargs):
#             self.before_do()
#             result = self.do()
#             self.after_do(result)
#         return result
# 
# Path: fabdeploy/utils.py
# def upload_config_template(
#     name, to=None, context=None, skip_unexistent=False, **kwargs):
#     config_template = env.conf.config_template_lpath(name)
#     if to is None:
#         to = posixpath.join(env.conf.etc_path, name)
#     if context is None:
#         context = env.conf
# 
#     if config_template is None:
#         if skip_unexistent:
#             return
#         abort('Config template "%s" is not found.' % name)
# 
#     upload_template(
#         config_template, to, context, use_jinja=True, **kwargs)
# 
# Path: fabdeploy/nginx.py
# class PushConfigTask(Task):
#     @conf
#     def config(self):
#         return '/etc/nginx/sites-available/%(instance_name)s'
# 
#     @conf
#     def enabled_config(self):
#         return '/etc/nginx/sites-enabled/%(instance_name)s'
# 
#     def do(self):
#         upload_config_template(
#             self.conf.config_template,
#             self.conf.config,
#             context=self.conf,
#             use_sudo=True)
#         with settings(warn_only=True):
#             sudo('ln --symbolic %(config)s %(enabled_config)s' % self.conf)


from fabric.api import puts, sudo
from .containers import conf
from .task import Task
from .utils import upload_config_template
from .nginx import PushConfigTask as PushNginxConfigTask
import supervisor



__all__ = [
    'push_config',
    'push_nginx_config',
    'reload_with_supervisor',
]


class GunicornMixin(object):
    @conf
    def gunicorn_bind(self):
        return 'unix:/tmp/gunicorn-%(server_name)s.socket'


class PushConfig(GunicornMixin, Task):
    def do(self):
        upload_config_template('gunicorn.conf.py', context=self.conf)

push_config = PushConfig()


class PushNginxConfig(GunicornMixin, PushNginxConfigTask):

# Path: fabdeploy/containers.py
# def conf(func):
#     """Decorator to mark function as config source."""
# 
#     func._is_conf = True
#     return func
# 
# Path: fabdeploy/task.py
# class Task(BaseTask):
#     name = None
#     task_kwargs = {}
#     _curr_conf_names = set()
# 
#     def __init__(self, *args, **kwargs):
#         super(Task, self).__init__(*args, **kwargs)
#         if self.name is None:
#             self.name = self._generate_name()
#         self._reset_conf()
# 
#     def before_do(self):
#         pass
# 
#     def after_do(self, result):
#         pass
# 
#     def do(self):
#         raise NotImplementedError()
# 
#     def conf_keys(self):
#         keys = set()
#         keys.update(self.task_kwargs.keys())
#         for name, value in inspect.getmembers(self):
#             if hasattr(value, '_is_conf'):
#                 keys.add(name)
#         return keys
# 
#     def conf_value(self, name, null=object()):
#         if name in self._curr_conf_names:
#             raise MissingVarException
#         self._curr_conf_names.add(name)
#         value = null
# 
#         if name in self.task_kwargs:
#             value = self.task_kwargs[name]
#         else:
#             try:
#                 attr = getattr(self, name)
#                 if hasattr(attr, '_is_conf'):
#                     value = attr()
#             except AttributeError:
#                 pass
# 
#         self._curr_conf_names.remove(name)
#         if value is not null:
#             return value
#         else:
#             raise MissingVarException
# 
#     def _generate_name(self):
#         s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', self.__class__.__name__)
#         return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
# 
#     def _reset_conf(self):
#         self.conf = None
#         self.task_kwargs = {}
# 
#     def _get_module(self):
#         return self.__module__.split('.')[-1]
# 
#     def _namespaces(self, kwargs):
#         module = self._get_module()
#         ns = [
#             '%s__' % module,
#             '%s__' % self.name,
#             '%s__%s__' % (module, self.name),
#         ]
#         if '_namespace' in kwargs:
#             ns.append(kwargs['_namespace'])
#         return ns
# 
#     def _default_conf(self):
#         if not hasattr(env, 'conf'):
#             setup_fabdeploy()
#         assert isinstance(env.conf, BaseConf)
#         conf = env.conf.copy()
#         conf.set_global_conf(env.conf)
#         conf.add_task(self)
#         return conf
# 
#     @contextmanager
#     def tmp_conf(self, conf=None, task_kwargs={}, **fabric_settings):
#         try:
#             if isinstance(conf, dict):
#                 warnings.warn(
#                     'Please pass config dict as explicit task_kwargs kwarg. '
#                     'For example, task.tmp_conf(task_kwargs={"address": ""}).',
#                     DeprecationWarning)
# 
#                 task_kwargs = conf
#                 conf = None
# 
#             if conf is not None:
#                 assert isinstance(conf, BaseConf)
#                 self.conf = conf.copy()
#                 self.task_kwargs = task_kwargs
#                 self.conf.add_task(self)
#             elif self.conf is None:
#                 self.conf = self._default_conf()
#                 self.task_kwargs = task_kwargs
# 
#             self.conf.set_name('%s.%s' % (self._get_module(), self.name))
#             unprefix_conf(self.conf, self._namespaces(self.task_kwargs))
# 
#             with settings(
#                 host_string=self.conf.get('address', ''), **fabric_settings):
#                 yield
#         finally:
#             self._reset_conf()
# 
#     def run(self, **kwargs):
#         with self.tmp_conf(task_kwargs=kwargs):
#             self.before_do()
#             result = self.do()
#             self.after_do(result)
#         return result
# 
# Path: fabdeploy/utils.py
# def upload_config_template(
#     name, to=None, context=None, skip_unexistent=False, **kwargs):
#     config_template = env.conf.config_template_lpath(name)
#     if to is None:
#         to = posixpath.join(env.conf.etc_path, name)
#     if context is None:
#         context = env.conf
# 
#     if config_template is None:
#         if skip_unexistent:
#             return
#         abort('Config template "%s" is not found.' % name)
# 
#     upload_template(
#         config_template, to, context, use_jinja=True, **kwargs)
# 
# Path: fabdeploy/nginx.py
# class PushConfigTask(Task):
#     @conf
#     def config(self):
#         return '/etc/nginx/sites-available/%(instance_name)s'
# 
#     @conf
#     def enabled_config(self):
#         return '/etc/nginx/sites-enabled/%(instance_name)s'
# 
#     def do(self):
#         upload_config_template(
#             self.conf.config_template,
#             self.conf.config,
#             context=self.conf,
#             use_sudo=True)
#         with settings(warn_only=True):
#             sudo('ln --symbolic %(config)s %(enabled_config)s' % self.conf)


# Path: fabdeploy/gunicorn.py
from fabric.api import puts, sudo
from .containers import conf
from .task import Task
from .utils import upload_config_template
from .nginx import PushConfigTask as PushNginxConfigTask
import supervisor




__all__ = [
    'push_config',
    'push_nginx_config',
    'reload_with_supervisor',
]


class GunicornMixin(object):
    @conf
    def gunicorn_bind(self):
        return 'unix:/tmp/gunicorn-%(server_name)s.socket'


class PushConfig(GunicornMixin, Task):
    def do(self):
        upload_config_template('gunicorn.conf.py', context=self.conf)

push_config = PushConfig()


class PushNginxConfig(GunicornMixin, PushNginxConfigTask):

    @conf