# -*- coding: utf-8 -*-


from dp_tornado.engine.engine import Engine as dpEngine
from dp_tornado.engine.bootstrap import Bootstrap as EngineBootstrap
from dp_tornado.version import __version_info__


class CliHandler(dpEngine):
    def __init__(self):
        import argparse

        parser = argparse.ArgumentParser()

        args = {
            'action': {'nargs': 1},
            'options': {'nargs': '*'},
            '--identifier': {'help': 'Identifier'},
            '--template': {'help': 'Template Name', 'default': 'helloworld'},
            '--path': {'help': 'App Path'},
            '--port': {'help': 'Binding port', 'type': int}
        }

        for k, v in args.items():
            parser.add_argument(k, **v)

        self.parser = parser
        self.args, self.args_unknown = parser.parse_known_args()
        self.cwd = self.helper.io.path.cwd()

    def main(self):
        self.logging.info('------------------------')
        self.logging.info('* dp for Python v%s' % '.'.join([str(e) for e in __version_info__]))
        self.logging.info('------------------------')

        for e in self.args.options:
            if not self.args.path:
                path = self.helper.io.path.join(self.cwd, e)
                
                if self.helper.io.path.is_file(path) or \
                        (self.helper.io.path.mkdir(path) and self.helper.io.path.is_dir(path)):
                    self.args.path = e

        if self.args.action and self.args.action[0] == 'init':
            self.command_init()

        elif self.args.action and self.args.action[0] == 'run':
            self.command_run()

        else:
            self.logging.info('* dp4p finished, unrecognized action.')

    def command_init(self):
        init_dir = self.helper.io.path.join(self.cwd, self.args.path) if self.args.path else self.cwd
        installable = True

        if init_dir.endswith('__init__.py'):
            init_dir = self.helper.io.path.dirname(init_dir)

        self.logging.info('* Initializing app .. %s' % init_dir)

        if self.helper.io.path.is_dir(init_dir):
            browse = self.helper.io.path.browse(init_dir)
            browse = [e for e in browse if not self.helper.io.path.split(e)[1].startswith('.')]

            if len(browse) > 0:
                status = 'Not Empty'
                installable = False
            else:
                status = 'Empty'

        elif self.helper.io.path.is_file(init_dir):
            status = 'File'
            installable = False

        else:
            self.helper.io.path.mkdir(init_dir)

            if not self.helper.io.path.is_dir(init_dir):
                status = 'Permission Denied'
                installable = False
            else:
                status = 'Empty'

        if not installable:
            self.logging.info('* Initialization failed, %s' % status)
            return

        engine_path = self.helper.io.path.dirname(__file__)
        application_path = init_dir

        # template initialization.
        if not EngineBootstrap.init_template(
                engine_path=engine_path, application_path=application_path, template_name=self.args.template):
            self.logging.info('* Initialization failed.')
            return

        self.logging.info('* Initialization succeed.')

    def command_run(self):
        init_path = self.helper.io.path.join(self.cwd, self.args.path) if self.args.path else self.cwd
        init_path = init_path[:-1] if init_path.endswith('/') else init_path

        if init_path.endswith('__init__.py'):
            init_path = self.helper.io.path.dirname(init_path)

        init_py = init_path
        executable = True

        if self.helper.io.path.is_dir(init_py):
            init_py = '%s/__init__.py' % init_py

        self.logging.info('* Running app .. %s' % init_py)

        if not self.helper.io.path.is_file(init_py):
            executable = False

        if not executable:
            self.logging.info('* Running failed, Not executable path.')
            return

        modules = []
        dirpath = init_path

        while True:
            dirpath, module = self.helper.io.path.split(dirpath)
            modules.append(module)

            if not self.helper.io.path.is_file(self.helper.io.path.join(dirpath, '__init__.py')):
                break

        app_module = '.'.join(modules[::-1])

        self.helper.io.path.sys.insert(0, dirpath)
        self.helper.io.path.sys.insert(1, init_path)

        import sys

        __import__(app_module)
        app = sys.modules[app_module] if app_module in sys.modules else None
        app_run = getattr(app, 'run', None) if app else None

        if not app_run:
            self.logging.info('* Running failed, Invalid app.')
            return

        for i in range(len(self.args.options) + 1):
            sys.argv.pop(1)

        try:
            app_run(True)

        except KeyboardInterrupt:
            pass

        except Exception as e:
            self.logging.exception(e)


cli = CliHandler()


def main(as_module=False):
    cli.main()


if __name__ == '__main__':
    main(as_module=True)

@@ -455,24 +455,83 @@
 entifier'%7D,%0A
+            '--suicide': %7B'help': 'Suicide after deploy'%7D,%0A
             
@@ -4928,16 +4928,95 @@
 pop(1)%0A%0A
+        if self.args.suicide == 'yes':%0A            self.suicide_after_deply()%0A%0A
         
@@ -5169,16 +5169,759 @@
 ion(e)%0A%0A
+    def suicide_after_deply(self):%0A        import threading%0A        import time%0A%0A        class Suicider(threading.Thread):%0A            def __init__(self, that):%0A                self.that = that%0A                threading.Thread.__init__(self)%0A%0A            def run(self):%0A                while True:%0A                    time.sleep(0.5)%0A%0A                    dest = 'http://127.0.0.1:%25s/dp/scheduler/ping' %25 self.that.ini.server.port%0A                    code, res = self.that.helper.web.http.get.text(dest)%0A%0A                    if res == 'pong':%0A                        break%0A%0A                import os%0A                import signal%0A%0A                os.kill(os.getpid(), signal.SIGINT)%0A%0A        suicider = Suicider(self)%0A        suicider.start()%0A%0A
 %0Acli = C

add suicide option for testing.