Exemplo n.º 1
0
def validate_options():
    if not options.command:
        options.print_help()
        raise Error('missing required command option')
    if not options.hosts and not options.hosts_file:
        options.print_help()
        raise Error('missing required option hosts or hosts-file')
Exemplo n.º 2
0
    def start(self):

        def kill_server(sig, frame):

            LOG.warning( 'Catch SIG: %d' % sig )

            tornado.ioloop.IOLoop.instance().stop()


        # ignore Broken Pipe signal
        signal.signal(signal.SIGPIPE, signal.SIG_IGN);
                            
        # catch kill signal
        signal.signal(signal.SIGINT, kill_server)
        signal.signal(signal.SIGQUIT, kill_server)
        signal.signal(signal.SIGTERM, kill_server)
        signal.signal(signal.SIGHUP, kill_server)

        for log_name in self.log_list:
            mownfish.util.log.setup(log_name)

        LOG.info('START TORNADO WEB SERVER ...')

        for key, value in sorted(options.items(), key=lambda d:d[0]):
            if key not in ('help', 'log_file_prefix', 'log_to_stderr') \
                    and value.value() is None:
                sys.stderr.write('must specify %s\n' % key)
                options.print_help()
                sys.exit(0)
            LOG.info('Options: (%s, %s)', key, value.value())

        try:
            sockets = tornado.netutil.bind_sockets(options.port,
                    address=options.bind_ip,
                    backlog=128)

            if not options.multiports:
                task_id = tornado.process.fork_processes(options.num_process)
            
            http_server =  \
                tornado.httpserver.HTTPServer(xheaders=True,
                                            request_callback=self.application)
            http_server.add_sockets(sockets)

            self.prepare()

            tornado.ioloop.IOLoop.instance().start()

            http_server.stop()
            tornado.ioloop.IOLoop.instance().stop()

            LOG.info('STOP TORNADO WEB SERVER ...')
        except socket.error as e:
            LOG.warning('Socket Error: %s' % str(e))
        except KeyboardInterrupt as e:
            LOG.warning('Gently Quit')
        except Exception as e:
            LOG.error('UnCaught Exception: %s' % e, exc_info=True)
Exemplo n.º 3
0
    def start(self):
        def kill_server(sig, frame):

            LOG.warning('Catch SIG: %d' % sig)

            tornado.ioloop.IOLoop.instance().stop()

        # ignore Broken Pipe signal
        signal.signal(signal.SIGPIPE, signal.SIG_IGN)

        # catch kill signal
        signal.signal(signal.SIGINT, kill_server)
        signal.signal(signal.SIGQUIT, kill_server)
        signal.signal(signal.SIGTERM, kill_server)
        signal.signal(signal.SIGHUP, kill_server)

        for log_name in self.log_list:
            mownfish.util.log.setup(log_name)

        LOG.info('START TORNADO WEB SERVER ...')

        for key, value in sorted(options.items(), key=lambda d: d[0]):
            if key not in ('help', 'log_file_prefix', 'log_to_stderr') \
                    and value.value() is None:
                sys.stderr.write('must specify %s\n' % key)
                options.print_help()
                sys.exit(0)
            LOG.info('Options: (%s, %s)', key, value.value())

        try:
            sockets = tornado.netutil.bind_sockets(options.port,
                                                   address=options.bind_ip,
                                                   backlog=128)

            if not options.multiports:
                task_id = tornado.process.fork_processes(options.num_process)

            http_server =  \
                tornado.httpserver.HTTPServer(xheaders=True,
                                            request_callback=self.application)
            http_server.add_sockets(sockets)

            self.prepare()

            tornado.ioloop.IOLoop.instance().start()

            http_server.stop()
            tornado.ioloop.IOLoop.instance().stop()

            LOG.info('STOP TORNADO WEB SERVER ...')
        except socket.error as e:
            LOG.warning('Socket Error: %s' % str(e))
        except KeyboardInterrupt as e:
            LOG.warning('Gently Quit')
        except Exception as e:
            LOG.error('UnCaught Exception: %s' % e, exc_info=True)
Exemplo n.º 4
0
def init_options():
    # maybe some options will be use before load config file
    options.parse_command_line()
    options.parse_config_file(options.cfg_file)
    if not options.log_root_path or not options.port:
        options.print_help()
        _usage()
    options.log_root_path = _check_dir_tail(options.log_root_path)
    options.log_path = '%s/%d' % (options.log_root_path, options.port)
    _mkdir(options.log_path)
Exemplo n.º 5
0
def init_options():
    # maybe some options will be use before load config file
    options.parse_command_line()
    options.cfg_file = os.path.abspath(options.cfg_file)
    options.parse_config_file(options.cfg_file)
    if not options.log_path or not options.port:
        options.print_help()
        _usage()

    options.log_path = os.path.abspath(options.log_path)

    if not os.path.exists(options.log_path):
        os.makedirs(options.log_path)
Exemplo n.º 6
0
def main():
    define("port", default=2080, help="run on the givent port", type=int)
    define("debug", default=False, help="run in debug mode", type=bool)
    define("config", default="", help="load the givent config file")
    tornado.options.parse_command_line()
    try:
        tornado.options.parse_config_file(options.config)
    except IOError:
        options.print_help()
        return
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 7
0
def init_options():
    # maybe some options will be use before load config file
    options.parse_command_line()
    options.cfg_file = os.path.abspath(options.cfg_file)
    options.parse_config_file(options.cfg_file)
    if not options.log_path or not options.port:
        options.print_help()
        _usage()
    
    options.log_path = os.path.abspath(options.log_path)

    if not os.path.exists(options.log_path):
        os.makedirs(options.log_path)
Exemplo n.º 8
0
def main():
    define("port", default=2080, help="run on the givent port", type=int)
    define("debug", default=False, help="run in debug mode", type=bool)
    define("config", default="", help="load the givent config file")
    tornado.options.parse_command_line()
    try:
        tornado.options.parse_config_file(options.config)
    except IOError:
        options.print_help()
        return
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 9
0
def main():
    # 配置main.py的命令
    define("port", default=None, help="Run server on a specific port, mast input",
           type=int)

    # start from cmd
    options.parse_command_line()
    try:
        if options.port == None:
            options.print_help()
            return
    except:
        print 'Usage: python main.py --port=8000'
        return

    # 信号监听
    def sig_handler(sig, frame):
        logging.warning('Caught signal: %s', sig)
        tornado.ioloop.IOLoop.instance().add_callback(shutdown)

    # 关闭服务器
    def shutdown():
        logging.info('Stopping http server')
        http_server.stop()

        logging.info('server will shutdown in 2 seconds ...')
        io_loop = tornado.ioloop.IOLoop.instance()

        deadline = time.time() + 2

        def stop_loop():
            now = time.time()
            if now < deadline and (io_loop._callbacks or io_loop._timeouts):
                io_loop.add_timeout(now + 1, stop_loop)
            else:
                io_loop.stop()
                logging.info('Shutdown')

        stop_loop()

    global http_server
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)

    # 按Ctrl+C退出程序
    signal.signal(signal.SIGTERM, sig_handler)
    signal.signal(signal.SIGINT, sig_handler)

    # start server
    tornado.ioloop.IOLoop.instance().start()
    logging.info('Exit Master')
Exemplo n.º 10
0
def define_options():
    define("port", default=8888, help="run on the given port", type=int)
    define("log_file", help="(required) path to JSONL log file", type=str)
    define("logs_per_request",
           default=2,
           help="log lines returned per request",
           type=int)
    tornado.options.parse_command_line()
    if not options.log_file:
        options.print_help()
        logging.error(
            "Miss required parameter: {0}, for example {0}=test_file.log".
            format("--log-file"))
        sys.exit(1)
Exemplo n.º 11
0
def main():
    options.parse_command_line()
    if not options.app:
        options.print_help()
        sys.exit()

    app_name = options.app
    src_path = os.path.realpath(
                os.path.join(twork.__path__[0],
                    '..',
                    'scaffold',
                    ))
    dst_path = os.path.expanduser(options.prefix)

    #make_clean(src_path)

    shutil.copytree(os.path.join(src_path, ORI_PROJECT),
            os.path.join(dst_path, app_name))

    rename_dirs = []
    for root, dirs, files in os.walk(os.path.join(dst_path, app_name)):
        for ori_name in dirs:
            if ORI_PROJECT in ori_name:
                dst_name = ori_name.replace(ORI_PROJECT, app_name)
                rename_dirs.append((os.path.join(root, ori_name),
                        os.path.join(root, dst_name)))

        for name in files:
            if '.pyc' in name:
                os.remove(os.path.realpath(os.path.join(root, name)))
                continue

            if re.match(r'.+([py]|[md]|[sh]|[Makefile]|[cfg]|[gitignore])$|' + app_name,
                    name):
                with open(os.path.join(root,name),"r+") as f:
                    d = f.read()
                    d = d.replace(ORI_PROJECT, app_name)
                    d = d.replace(ORI_PROJECT.upper(), app_name.upper())
                    f.truncate(0)
                    f.seek(0,0)
                    f.write(d)

    for dirs in reversed(rename_dirs):
        ori_name, dst_name = dirs
        os.rename(ori_name, dst_name)

    make_README(dst_path)
    make_git_repo(dst_path)
Exemplo n.º 12
0
def main():
    options.parse_command_line()
    if not options.app:
        options.print_help()
        sys.exit()

    app_name = options.app
    src_path = os.path.realpath(
        os.path.join(
            twork.__path__[0],
            '..',
            'scaffold',
        ))
    dst_path = os.path.expanduser(options.prefix)

    #make_clean(src_path)

    shutil.copytree(os.path.join(src_path, ORI_PROJECT),
                    os.path.join(dst_path, app_name))

    rename_dirs = []
    for root, dirs, files in os.walk(os.path.join(dst_path, app_name)):
        for ori_name in dirs:
            if ORI_PROJECT in ori_name:
                dst_name = ori_name.replace(ORI_PROJECT, app_name)
                rename_dirs.append(
                    (os.path.join(root,
                                  ori_name), os.path.join(root, dst_name)))

        for name in files:
            if '.pyc' in name:
                os.remove(os.path.realpath(os.path.join(root, name)))

            if re.match(r'.+[py|md|sh|Makefile]$|' + app_name, name):
                with open(os.path.join(root, name), "r+") as f:
                    d = f.read()
                    d = d.replace(ORI_PROJECT, app_name)
                    d = d.replace(ORI_PROJECT.upper(), app_name.upper())
                    f.truncate(0)
                    f.seek(0, 0)
                    f.write(d)

    for dirs in reversed(rename_dirs):
        ori_name, dst_name = dirs
        os.rename(ori_name, dst_name)

    make_README(dst_path)
    make_git_repo(dst_path)
Exemplo n.º 13
0
def main():
    options.parse_command_line()
    if not options.project:
        options.print_help()
        sys.exit()

    project_name = options.project
    src_path = os.path.normpath(
        os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir))
    dst_path = os.path.expanduser(options.prefix)

    make_clean(src_path)

    shutil.copytree(os.path.join(src_path, ORI_PROJECT),
                    os.path.join(dst_path, project_name, project_name))
    for file_name in ROOT_FILES:
        shutil.copyfile(os.path.join(src_path, file_name),
                        os.path.join(dst_path, project_name, file_name))

    for root, dirs, files in os.walk(os.path.join(dst_path, project_name)):
        for name in files:
            if name.find(ORI_PROJECT) != -1:
                old_name = name
                name = name.replace(ORI_PROJECT, project_name)
                os.rename(os.path.join(root, old_name),
                          os.path.join(root, name))

            if re.match(r'.+[py|md]$|' + project_name, name):
                with open(os.path.join(root, name), "r+") as f:
                    d = f.read()
                    d = d.replace(ORI_PROJECT, project_name)
                    d = d.replace(ORI_PROJECT.upper(), project_name.upper())
                    d = d.replace('based on tornado',
                                  'based on %s' % ORI_PROJECT)
                    d = d.replace('bufferx/%s' % project_name,
                                  'bufferx/%s' % ORI_PROJECT)
                    f.truncate(0)
                    f.seek(0, 0)
                    f.write(d)

    make_git_repo(dst_path, project_name)
Exemplo n.º 14
0
def main():
    options.parse_command_line()
    if not options.project:
        options.print_help()
        sys.exit()

    project_name = options.project
    src_path = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
        os.pardir,os.pardir))
    dst_path = os.path.expanduser(options.prefix)

    make_clean(src_path)

    shutil.copytree(os.path.join(src_path,ORI_PROJECT),
            os.path.join(dst_path,project_name,project_name))
    for file_name in ROOT_FILES:
        shutil.copyfile(os.path.join(src_path,file_name),
                os.path.join(dst_path,project_name,file_name))

    for root, dirs, files in os.walk(os.path.join(dst_path, project_name)):
        for name in files:
            if name.find(ORI_PROJECT) != -1:
                old_name = name
                name = name.replace(ORI_PROJECT, project_name)
                os.rename(os.path.join(root, old_name), 
                        os.path.join(root, name))

            if re.match(r'.+[py|md]$|'+project_name, name):
                with open(os.path.join(root,name),"r+") as f:
                    d = f.read()
                    d = d.replace(ORI_PROJECT, project_name)
                    d = d.replace(ORI_PROJECT.upper(), project_name.upper())
                    d = d.replace('based on tornado',
                            'based on %s' % ORI_PROJECT)
                    d = d.replace('bufferx/%s' % project_name,
                            'bufferx/%s' % ORI_PROJECT)
                    f.truncate(0)
                    f.seek(0,0)
                    f.write(d)

    make_git_repo(dst_path, project_name)
Exemplo n.º 15
0
def start_service():
    global Log
    options.parse_command_line()

    Log = logging.getLogger()
    if options.debug:
        Log.setLevel(logging.DEBUG)

    required_options = ('port', 'fe_path', 'db_host', 'db_port', 'db_name', 'db_user', 'db_pw')
    for opt in required_options:
        if getattr(options, opt) is None:
            Log.error('Missing {} parameter'.format(opt))
            options.print_help()
            return 2

    Log.info('Starting Pinterest clone app')
    for opt, val in options.as_dict().iteritems():
        Log.info('  {}={}'.format(opt, val))

    # enable clean shutdown via kill
    signal.signal(signal.SIGTERM, stopService)

    ioloop.IOLoop.instance().run_sync(init_db)

    service = PinterestAPI(options.fe_path, Log)
    if options.certfile and options.keyfile:
        http_server = httpserver.HTTPServer(service, ssl_options={'keyfile': options.keyfile,
                                                                  'certfile': options.certfile})
    else:
        http_server = httpserver.HTTPServer(service, xheaders=True)
    http_server.listen(options.port)

    try:
        ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        pass

    stopService(signal.SIGINT, None)
Exemplo n.º 16
0
define("port", help="port to listen", default=9527)
define("debug", default=False, help="enable debug?")
tornado.options.parse_command_line()
settings = {
    #'template_path' : os.path.join(os.path.dirname(__file__), 'templates'),
    "debug": options.debug,
    "gzip": True,
}

application = tornado.web.Application(
    [
        (r"/", MainHandler),
        (r"/static/(.*)", FileHandler),
        (r"/dynamic/(.*)", DynamicHandler),
        (r"/code/(\d+).*", CodeHandler),
        (r"/size/([\d|k|m]+).*", SizeHandler),
        (r"/slow/(\d+)-?(\d+)?.*", SlowHandler),
        (r"/redirect/(.*)", RedirectHandler),
        (r"/*", MainHandler),
    ],
    **settings
)

if __name__ == "__main__":
    if not options.port:
        options.print_help()
        sys.exit()
    application.listen(options.port, address=options.ip)

    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 17
0
def parse_cli_options():
    options.parse_command_line()
    if options.camera_ip is None:
        sys.stderr.write("ERROR: camera_ip is required\n\n")
        options.print_help()
        sys.exit(1)
Exemplo n.º 18
0
            self.write('<meta http-equiv="refresh" content="0; url=%s" />' %
                       url)
        else:
            self.write('wrong argument')


define("ip", help="ip to bind", default="0.0.0.0")
define("port", help="port to listen", default=9527)
define("debug", default=False, help="enable debug?")
tornado.options.parse_command_line()
SETTINGS = {'debug': options.debug, 'node_id': get_host_hash(), 'gzip': True}

APP = tornado.web.Application([
    (r'/', MainHandler),
    (r'/static/(.*)', FileHandler),
    (r'/dynamic/(.*)', DynamicHandler),
    (r'/code/(\d+).*', CodeHandler),
    (r'/size/([\d|k|m]+).*', SizeHandler),
    (r'/slow/(\d+)-?(\d+)?.*', SlowHandler),
    (r'/redirect/(.*)', RedirectHandler),
    (r'/*', MainHandler),
], **SETTINGS)

if __name__ == "__main__":
    if not options.port:
        options.print_help()
        sys.exit()
    APP.listen(options.port, address=options.ip)

    tornado.ioloop.IOLoop.instance().start()
Exemplo n.º 19
0
def print_help(output=sys.stdout):
    """Prints all the command line options to stdout - replacment for tornano's print_help. """
    options.print_help(output)
    if cmd is not None:
        print >> output, cmd.get_usage()