コード例 #1
0
ファイル: widget.py プロジェクト: AbsentMoniker/ECE463Honors
def console():
    """ Defines the behavior of the console web2py execution """
    import optparse
    import textwrap

    usage = "python web2py.py"

    description = """\
    web2py Web Framework startup script.
    ATTENTION: unless a password is specified (-a 'passwd') web2py will
    attempt to run a GUI. In this case command line options are ignored."""

    description = textwrap.dedent(description)

    parser = optparse.OptionParser(
        usage, None, optparse.Option, ProgramVersion)

    parser.description = description

    msg = ('IP address of the server (e.g., 127.0.0.1 or ::1); '
           'Note: This value is ignored when using the \'interfaces\' option.')
    parser.add_option('-i',
                      '--ip',
                      default='127.0.0.1',
                      dest='ip',
                      help=msg)

    parser.add_option('-p',
                      '--port',
                      default='8000',
                      dest='port',
                      type='int',
                      help='port of server (8000)')

    msg = ('password to be used for administration '
           '(use -a "<recycle>" to reuse the last password))')
    parser.add_option('-a',
                      '--password',
                      default='<ask>',
                      dest='password',
                      help=msg)

    parser.add_option('-c',
                      '--ssl_certificate',
                      default='',
                      dest='ssl_certificate',
                      help='file that contains ssl certificate')

    parser.add_option('-k',
                      '--ssl_private_key',
                      default='',
                      dest='ssl_private_key',
                      help='file that contains ssl private key')

    msg = ('Use this file containing the CA certificate to validate X509 '
           'certificates from clients')
    parser.add_option('--ca-cert',
                      action='store',
                      dest='ssl_ca_certificate',
                      default=None,
                      help=msg)

    parser.add_option('-d',
                      '--pid_filename',
                      default='httpserver.pid',
                      dest='pid_filename',
                      help='file to store the pid of the server')

    parser.add_option('-l',
                      '--log_filename',
                      default='httpserver.log',
                      dest='log_filename',
                      help='file to log connections')

    parser.add_option('-n',
                      '--numthreads',
                      default=None,
                      type='int',
                      dest='numthreads',
                      help='number of threads (deprecated)')

    parser.add_option('--minthreads',
                      default=None,
                      type='int',
                      dest='minthreads',
                      help='minimum number of server threads')

    parser.add_option('--maxthreads',
                      default=None,
                      type='int',
                      dest='maxthreads',
                      help='maximum number of server threads')

    parser.add_option('-s',
                      '--server_name',
                      default=socket.gethostname(),
                      dest='server_name',
                      help='server name for the web server')

    msg = 'max number of queued requests when server unavailable'
    parser.add_option('-q',
                      '--request_queue_size',
                      default='5',
                      type='int',
                      dest='request_queue_size',
                      help=msg)

    parser.add_option('-o',
                      '--timeout',
                      default='10',
                      type='int',
                      dest='timeout',
                      help='timeout for individual request (10 seconds)')

    parser.add_option('-z',
                      '--shutdown_timeout',
                      default='5',
                      type='int',
                      dest='shutdown_timeout',
                      help='timeout on shutdown of server (5 seconds)')

    parser.add_option('--socket-timeout',
                      default=5,
                      type='int',
                      dest='socket_timeout',
                      help='timeout for socket (5 second)')

    parser.add_option('-f',
                      '--folder',
                      default=os.getcwd(),
                      dest='folder',
                      help='folder from which to run web2py')

    parser.add_option('-v',
                      '--verbose',
                      action='store_true',
                      dest='verbose',
                      default=False,
                      help='increase --test verbosity')

    parser.add_option('-Q',
                      '--quiet',
                      action='store_true',
                      dest='quiet',
                      default=False,
                      help='disable all output')

    msg = ('set debug output level (0-100, 0 means all, 100 means none; '
           'default is 30)')
    parser.add_option('-D',
                      '--debug',
                      dest='debuglevel',
                      default=30,
                      type='int',
                      help=msg)

    msg = ('run web2py in interactive shell or IPython (if installed) with '
           'specified appname (if app does not exist it will be created). '
           'APPNAME like a/c/f (c,f optional)')
    parser.add_option('-S',
                      '--shell',
                      dest='shell',
                      metavar='APPNAME',
                      help=msg)

    msg = ('run web2py in interactive shell or bpython (if installed) with '
           'specified appname (if app does not exist it will be created).\n'
           'Use combined with --shell')
    parser.add_option('-B',
                      '--bpython',
                      action='store_true',
                      default=False,
                      dest='bpython',
                      help=msg)

    msg = 'only use plain python shell; should be used with --shell option'
    parser.add_option('-P',
                      '--plain',
                      action='store_true',
                      default=False,
                      dest='plain',
                      help=msg)

    msg = ('auto import model files; default is False; should be used '
           'with --shell option')
    parser.add_option('-M',
                      '--import_models',
                      action='store_true',
                      default=False,
                      dest='import_models',
                      help=msg)

    msg = ('run PYTHON_FILE in web2py environment; '
           'should be used with --shell option')
    parser.add_option('-R',
                      '--run',
                      dest='run',
                      metavar='PYTHON_FILE',
                      default='',
                      help=msg)

    msg = ('run scheduled tasks for the specified apps: expects a list of '
           'app names as -K app1,app2,app3 '
           'or a list of app:groups as -K app1:group1:group2,app2:group1 '
           'to override specific group_names. (only strings, no spaces '
           'allowed. Requires a scheduler defined in the models')
    parser.add_option('-K',
                      '--scheduler',
                      dest='scheduler',
                      default=None,
                      help=msg)

    msg = 'run schedulers alongside webserver, needs -K app1 and -a too'
    parser.add_option('-X',
                      '--with-scheduler',
                      action='store_true',
                      default=False,
                      dest='with_scheduler',
                      help=msg)

    msg = ('run doctests in web2py environment; '
           'TEST_PATH like a/c/f (c,f optional)')
    parser.add_option('-T',
                      '--test',
                      dest='test',
                      metavar='TEST_PATH',
                      default=None,
                      help=msg)

    parser.add_option('-W',
                      '--winservice',
                      dest='winservice',
                      default='',
                      help='-W install|start|stop as Windows service')

    msg = 'trigger a cron run manually; usually invoked from a system crontab'
    parser.add_option('-C',
                      '--cron',
                      action='store_true',
                      dest='extcron',
                      default=False,
                      help=msg)

    msg = 'triggers the use of softcron'
    parser.add_option('--softcron',
                      action='store_true',
                      dest='softcron',
                      default=False,
                      help=msg)

    parser.add_option('-Y',
                      '--run-cron',
                      action='store_true',
                      dest='runcron',
                      default=False,
                      help='start the background cron process')

    parser.add_option('-J',
                      '--cronjob',
                      action='store_true',
                      dest='cronjob',
                      default=False,
                      help='identify cron-initiated command')

    parser.add_option('-L',
                      '--config',
                      dest='config',
                      default='',
                      help='config file')

    parser.add_option('-F',
                      '--profiler',
                      dest='profiler_dir',
                      default=None,
                      help='profiler dir')

    parser.add_option('-t',
                      '--taskbar',
                      action='store_true',
                      dest='taskbar',
                      default=False,
                      help='use web2py gui and run in taskbar (system tray)')

    parser.add_option('',
                      '--nogui',
                      action='store_true',
                      default=False,
                      dest='nogui',
                      help='text-only, no GUI')

    msg = ('should be followed by a list of arguments to be passed to script, '
           'to be used with -S, -A must be the last option')
    parser.add_option('-A',
                      '--args',
                      action='store',
                      dest='args',
                      default=None,
                      help=msg)

    parser.add_option('--no-banner',
                      action='store_true',
                      default=False,
                      dest='nobanner',
                      help='Do not print header banner')

    msg = ('listen on multiple addresses: '
           '"ip1:port1:key1:cert1:ca_cert1;ip2:port2:key2:cert2:ca_cert2;..." '
           '(:key:cert:ca_cert optional; no spaces; IPv6 addresses must be in '
           'square [] brackets)')
    parser.add_option('--interfaces',
                      action='store',
                      dest='interfaces',
                      default=None,
                      help=msg)

    msg = 'runs web2py tests'
    parser.add_option('--run_system_tests',
                      action='store_true',
                      dest='run_system_tests',
                      default=False,
                      help=msg)

    msg = ('adds coverage reporting (needs --run_system_tests), '
           'python 2.7 and the coverage module installed. '
           'You can alter the default path setting the environmental '
           'var "COVERAGE_PROCESS_START". '
           'By default it takes gluon/tests/coverage.ini')
    parser.add_option('--with_coverage',
                      action='store_true',
                      dest='with_coverage',
                      default=False,
                      help=msg)

    if '-A' in sys.argv:
        k = sys.argv.index('-A')
    elif '--args' in sys.argv:
        k = sys.argv.index('--args')
    else:
        k = len(sys.argv)
    sys.argv, other_args = sys.argv[:k], sys.argv[k + 1:]
    (options, args) = parser.parse_args()
    options.args = [options.run] + other_args
    global_settings.cmd_options = options
    global_settings.cmd_args = args

    try:
        options.ips = list(set( # no duplicates
            [addrinfo[4][0] for addrinfo in getipaddrinfo(socket.getfqdn())
             if not is_loopback_ip_address(addrinfo=addrinfo)]))
    except socket.gaierror:
        options.ips = []

    if options.run_system_tests:
        run_system_tests(options)

    if options.quiet:
        capture = cStringIO.StringIO()
        sys.stdout = capture
        logger.setLevel(logging.CRITICAL + 1)
    else:
        logger.setLevel(options.debuglevel)

    if options.config[-3:] == '.py':
        options.config = options.config[:-3]

    if options.cronjob:
        global_settings.cronjob = True  # tell the world
        options.plain = True    # cronjobs use a plain shell
        options.nobanner = True
        options.nogui = True

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

    #  accept --interfaces in the form
    #  "ip1:port1:key1:cert1:ca_cert1;[ip2]:port2;ip3:port3:key3:cert3"
    #  (no spaces; optional key:cert indicate SSL)
    if isinstance(options.interfaces, str):
        interfaces = options.interfaces.split(';')
        options.interfaces = []
        for interface in interfaces:
            if interface.startswith('['):  # IPv6
                ip, if_remainder = interface.split(']', 1)
                ip = ip[1:]
                if_remainder = if_remainder[1:].split(':')
                if_remainder[0] = int(if_remainder[0])  # numeric port
                options.interfaces.append(tuple([ip] + if_remainder))
            else:  # IPv4
                interface = interface.split(':')
                interface[1] = int(interface[1])  # numeric port
                options.interfaces.append(tuple(interface))

    #  accepts --scheduler in the form
    #  "app:group1,group2,app2:group1"
    scheduler = []
    options.scheduler_groups = None
    if isinstance(options.scheduler, str):
        if ':' in options.scheduler:
            for opt in options.scheduler.split(','):
                scheduler.append(opt.split(':'))
            options.scheduler = ','.join([app[0] for app in scheduler])
            options.scheduler_groups = scheduler

    if options.numthreads is not None and options.minthreads is None:
        options.minthreads = options.numthreads  # legacy

    create_welcome_w2p()

    if not options.cronjob:
        # If we have the applications package or if we should upgrade
        if not os.path.exists('applications/__init__.py'):
            write_file('applications/__init__.py', '')

    return options, args
コード例 #2
0
ファイル: widget.py プロジェクト: bmiklautz/web2py
def start():
    """ Starts server and other services """

    # get command line arguments
    options = console(version=ProgramVersion)

    if options.gae:
        # write app.yaml, gaehandler.py, and exit
        if not os.path.exists('app.yaml'):
            name = options.gae
            # for backward compatibility
            if name == 'configure':
                if PY2: input = raw_input
                name = input("Your GAE app name: ")
            content = open(os.path.join('examples', 'app.example.yaml'),
                           'rb').read()
            open('app.yaml', 'wb').write(content.replace("yourappname", name))
        else:
            print("app.yaml alreday exists in the web2py folder")
        if not os.path.exists('gaehandler.py'):
            content = open(os.path.join('handlers', 'gaehandler.py'),
                           'rb').read()
            open('gaehandler.py', 'wb').write(content)
        else:
            print("gaehandler.py alreday exists in the web2py folder")
        return

    logger = logging.getLogger("web2py")
    logger.setLevel(options.log_level)

    # on new installation build the scaffolding app
    create_welcome_w2p()

    if options.run_system_tests:
        # run system test and exit
        run_system_tests(options)

    if options.quiet:
        # to prevent writes on stdout set a null stream
        class NullFile(object):
            def write(self, x):
                pass

        sys.stdout = NullFile()
        # but still has to mute existing loggers, to do that iterate
        # over all existing loggers (root logger included) and remove
        # all attached logging.StreamHandler instances currently
        # streaming on sys.stdout or sys.stderr
        loggers = [logging.getLogger()]
        loggers.extend(logging.Logger.manager.loggerDict.values())
        for l in loggers:
            if isinstance(l, logging.PlaceHolder): continue
            for h in l.handlers[:]:
                if isinstance(h, logging.StreamHandler) and \
                    h.stream in (sys.stdout, sys.stderr):
                    l.removeHandler(h)
        # NOTE: stderr.write() is still working

    if not options.no_banner:
        # banner
        print(ProgramName)
        print(ProgramAuthor)
        print(ProgramVersion)
        from pydal.drivers import DRIVERS
        print('Database drivers available: %s' % ', '.join(DRIVERS))

    if options.run_doctests:
        # run doctests and exit
        test(options.run_doctests, verbose=options.verbose)
        return

    if options.shell:
        # run interactive shell and exit
        sys.argv = [options.run or ''] + options.args
        run(options.shell,
            plain=options.plain,
            bpython=options.bpython,
            import_models=options.import_models,
            startfile=options.run,
            cron_job=options.cron_job)
        return

    if options.cron_run:
        # run cron (extcron) and exit
        logger.debug('Starting extcron...')
        global_settings.web2py_crontype = 'external'
        extcron = newcron.extcron(options.folder, apps=options.crontabs)
        extcron.start()
        extcron.join()
        return

    if not options.with_scheduler and options.schedulers:
        # run schedulers and exit
        try:
            start_schedulers(options)
        except KeyboardInterrupt:
            pass
        return

    if options.with_cron:
        if options.soft_cron:
            print(
                'Using cron software emulation (but this is not very efficient)'
            )
            global_settings.web2py_crontype = 'soft'
        else:
            # start hardcron thread
            logger.debug('Starting hardcron...')
            global_settings.web2py_crontype = 'hard'
            newcron.hardcron(options.folder, apps=options.crontabs).start()

    # if no password provided and have Tk library start GUI (when not
    # explicitly disabled), we also need a GUI to put in taskbar (system tray)
    # when requested
    root = None

    if (not options.no_gui and options.password == '<ask>') or options.taskbar:
        try:
            if PY2:
                import Tkinter as tkinter
            else:
                import tkinter
            root = tkinter.Tk()
        except (ImportError, OSError):
            logger.warn(
                'GUI not available because Tk library is not installed')
            options.no_gui = True
        except:
            logger.exception('cannot get Tk root window, GUI disabled')
            options.no_gui = True

    if root:
        # run GUI and exit
        root.focus_force()

        # Mac OS X - make the GUI window rise to the top
        if os.path.exists("/usr/bin/osascript"):
            applescript = """
tell application "System Events"
    set proc to first process whose unix id is %d
    set frontmost of proc to true
end tell
""" % (os.getpid())
            os.system("/usr/bin/osascript -e '%s'" % applescript)

        # web2pyDialog takes care of schedulers
        master = web2pyDialog(root, options)
        signal.signal(signal.SIGTERM, lambda a, b: master.quit())

        try:
            root.mainloop()
        except:
            master.quit()

        sys.exit()

    spt = None

    if options.with_scheduler and options.schedulers:
        # start schedulers in a separate thread
        spt = threading.Thread(target=start_schedulers, args=(options, ))
        spt.start()

    # start server

    if options.password == '<ask>':
        options.password = getpass.getpass('choose a password:'******'no password, disable admin interface')

    # Use first interface IP and port if interfaces specified, since the
    # interfaces option overrides the IP (and related) options.
    if not options.interfaces:
        ip = options.ip
        port = options.port
    else:
        first_if = options.interfaces[0]
        ip = first_if[0]
        port = first_if[1]

    if options.server_key and options.server_cert:
        proto = 'https'
    else:
        proto = 'http'

    url = get_url(ip, proto=proto, port=port)

    if not options.no_banner:
        message = '\nplease visit:\n\t%s\n'
        if sys.platform.startswith('win'):
            message += 'use "taskkill /f /pid %i" to shutdown the web2py server\n\n'
        else:
            message += 'use "kill -SIGTERM %i" to shutdown the web2py server\n\n'
        print(message % (url, os.getpid()))

    # enhance linecache.getline (used by debugger) to look at the source file
    # if the line was not found (under py2exe & when file was modified)
    import linecache
    py2exe_getline = linecache.getline

    def getline(filename, lineno, *args, **kwargs):
        line = py2exe_getline(filename, lineno, *args, **kwargs)
        if not line:
            try:
                with open(filename, "rb") as f:
                    for i, line in enumerate(f):
                        line = line.decode('utf-8')
                        if lineno == i + 1:
                            break
                    else:
                        line = ''
            except (IOError, OSError):
                line = ''
        return line

    linecache.getline = getline

    server = main.HttpServer(ip=ip,
                             port=port,
                             password=options.password,
                             pid_filename=options.pid_filename,
                             log_filename=options.log_filename,
                             profiler_dir=options.profiler_dir,
                             ssl_certificate=options.server_cert,
                             ssl_private_key=options.server_key,
                             ssl_ca_certificate=options.ca_cert,
                             min_threads=options.min_threads,
                             max_threads=options.max_threads,
                             server_name=options.server_name,
                             request_queue_size=options.request_queue_size,
                             timeout=options.timeout,
                             socket_timeout=options.socket_timeout,
                             shutdown_timeout=options.shutdown_timeout,
                             path=options.folder,
                             interfaces=options.interfaces)

    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
        if spt is not None:
            try:
                spt.join()
            except:
                logger.exception('error terminating schedulers')
                pass
    logging.shutdown()
コード例 #3
0
ファイル: widget.py プロジェクト: wpr101/web2py
def console():
    """ Defines the behavior of the console web2py execution """
    import optparse
    import textwrap

    usage = "python web2py.py"

    description = """\
    web2py Web Framework startup script.
    ATTENTION: unless a password is specified (-a 'passwd') web2py will
    attempt to run a GUI. In this case command line options are ignored."""

    description = textwrap.dedent(description)

    parser = optparse.OptionParser(usage, None, optparse.Option,
                                   ProgramVersion)

    parser.description = description

    msg = ('IP address of the server (e.g., 127.0.0.1 or ::1); '
           'Note: This value is ignored when using the \'interfaces\' option.')
    parser.add_option('-i', '--ip', default='127.0.0.1', dest='ip', help=msg)

    parser.add_option('-p',
                      '--port',
                      default='8000',
                      dest='port',
                      type='int',
                      help='port of server (8000)')

    parser.add_option(
        '-G',
        '--GAE',
        default=None,
        dest='gae',
        help="'-G configure' will create app.yaml and gaehandler.py")

    msg = ('password to be used for administration '
           '(use -a "<recycle>" to reuse the last password))')
    parser.add_option('-a',
                      '--password',
                      default='<ask>',
                      dest='password',
                      help=msg)

    parser.add_option('-c',
                      '--ssl_certificate',
                      default='',
                      dest='ssl_certificate',
                      help='file that contains ssl certificate')

    parser.add_option('-k',
                      '--ssl_private_key',
                      default='',
                      dest='ssl_private_key',
                      help='file that contains ssl private key')

    msg = ('Use this file containing the CA certificate to validate X509 '
           'certificates from clients')
    parser.add_option('--ca-cert',
                      action='store',
                      dest='ssl_ca_certificate',
                      default=None,
                      help=msg)

    parser.add_option('-d',
                      '--pid_filename',
                      default='httpserver.pid',
                      dest='pid_filename',
                      help='file to store the pid of the server')

    parser.add_option('-l',
                      '--log_filename',
                      default='httpserver.log',
                      dest='log_filename',
                      help='file to log connections')

    parser.add_option('-n',
                      '--numthreads',
                      default=None,
                      type='int',
                      dest='numthreads',
                      help='number of threads (deprecated)')

    parser.add_option('--minthreads',
                      default=None,
                      type='int',
                      dest='minthreads',
                      help='minimum number of server threads')

    parser.add_option('--maxthreads',
                      default=None,
                      type='int',
                      dest='maxthreads',
                      help='maximum number of server threads')

    parser.add_option('-s',
                      '--server_name',
                      default=socket.gethostname(),
                      dest='server_name',
                      help='server name for the web server')

    msg = 'max number of queued requests when server unavailable'
    parser.add_option('-q',
                      '--request_queue_size',
                      default='5',
                      type='int',
                      dest='request_queue_size',
                      help=msg)

    parser.add_option('-o',
                      '--timeout',
                      default='10',
                      type='int',
                      dest='timeout',
                      help='timeout for individual request (10 seconds)')

    parser.add_option('-z',
                      '--shutdown_timeout',
                      default='5',
                      type='int',
                      dest='shutdown_timeout',
                      help='timeout on shutdown of server (5 seconds)')

    parser.add_option('--socket-timeout',
                      default=5,
                      type='int',
                      dest='socket_timeout',
                      help='timeout for socket (5 second)')

    parser.add_option('-f',
                      '--folder',
                      default=os.getcwd(),
                      dest='folder',
                      help='folder from which to run web2py')

    parser.add_option('-v',
                      '--verbose',
                      action='store_true',
                      dest='verbose',
                      default=False,
                      help='increase --test verbosity')

    parser.add_option('-Q',
                      '--quiet',
                      action='store_true',
                      dest='quiet',
                      default=False,
                      help='disable all output')

    parser.add_option('-e',
                      '--errors_to_console',
                      action='store_true',
                      dest='print_errors',
                      default=False,
                      help='log all errors to console')

    msg = ('set debug output level (0-100, 0 means all, 100 means none; '
           'default is 30)')
    parser.add_option('-D',
                      '--debug',
                      dest='debuglevel',
                      default=30,
                      type='int',
                      help=msg)

    msg = ('run web2py in interactive shell or IPython (if installed) with '
           'specified appname (if app does not exist it will be created). '
           'APPNAME like a/c/f (c,f optional)')
    parser.add_option('-S',
                      '--shell',
                      dest='shell',
                      metavar='APPNAME',
                      help=msg)

    msg = ('run web2py in interactive shell or bpython (if installed) with '
           'specified appname (if app does not exist it will be created).\n'
           'Use combined with --shell')
    parser.add_option('-B',
                      '--bpython',
                      action='store_true',
                      default=False,
                      dest='bpython',
                      help=msg)

    msg = 'only use plain python shell; should be used with --shell option'
    parser.add_option('-P',
                      '--plain',
                      action='store_true',
                      default=False,
                      dest='plain',
                      help=msg)

    msg = ('auto import model files; default is False; should be used '
           'with --shell option')
    parser.add_option('-M',
                      '--import_models',
                      action='store_true',
                      default=False,
                      dest='import_models',
                      help=msg)

    msg = ('run PYTHON_FILE in web2py environment; '
           'should be used with --shell option')
    parser.add_option('-R',
                      '--run',
                      dest='run',
                      metavar='PYTHON_FILE',
                      default='',
                      help=msg)

    msg = ('run scheduled tasks for the specified apps: expects a list of '
           'app names as -K app1,app2,app3 '
           'or a list of app:groups as -K app1:group1:group2,app2:group1 '
           'to override specific group_names. (only strings, no spaces '
           'allowed. Requires a scheduler defined in the models')
    parser.add_option('-K',
                      '--scheduler',
                      dest='scheduler',
                      default=None,
                      help=msg)

    msg = 'run schedulers alongside webserver, needs -K app1 and -a too'
    parser.add_option('-X',
                      '--with-scheduler',
                      action='store_true',
                      default=False,
                      dest='with_scheduler',
                      help=msg)

    msg = ('run doctests in web2py environment; '
           'TEST_PATH like a/c/f (c,f optional)')
    parser.add_option('-T',
                      '--test',
                      dest='test',
                      metavar='TEST_PATH',
                      default=None,
                      help=msg)

    msg = 'trigger a cron run manually; usually invoked from a system crontab'
    parser.add_option('-C',
                      '--cron',
                      action='store_true',
                      dest='extcron',
                      default=False,
                      help=msg)

    msg = 'triggers the use of softcron'
    parser.add_option('--softcron',
                      action='store_true',
                      dest='softcron',
                      default=False,
                      help=msg)

    parser.add_option('-Y',
                      '--run-cron',
                      action='store_true',
                      dest='runcron',
                      default=False,
                      help='start the background cron process')

    parser.add_option('-J',
                      '--cronjob',
                      action='store_true',
                      dest='cronjob',
                      default=False,
                      help='identify cron-initiated command')

    parser.add_option('-L',
                      '--config',
                      dest='config',
                      default='',
                      help='config file')

    parser.add_option('-F',
                      '--profiler',
                      dest='profiler_dir',
                      default=None,
                      help='profiler dir')

    parser.add_option('-t',
                      '--taskbar',
                      action='store_true',
                      dest='taskbar',
                      default=False,
                      help='use web2py gui and run in taskbar (system tray)')

    parser.add_option('',
                      '--nogui',
                      action='store_true',
                      default=False,
                      dest='nogui',
                      help='text-only, no GUI')

    msg = ('should be followed by a list of arguments to be passed to script, '
           'to be used with -S, -A must be the last option')
    parser.add_option('-A',
                      '--args',
                      action='store',
                      dest='args',
                      default=None,
                      help=msg)

    parser.add_option('--no-banner',
                      action='store_true',
                      default=False,
                      dest='nobanner',
                      help='Do not print header banner')

    msg = ('listen on multiple addresses: '
           '"ip1:port1:key1:cert1:ca_cert1;ip2:port2:key2:cert2:ca_cert2;..." '
           '(:key:cert:ca_cert optional; no spaces; IPv6 addresses must be in '
           'square [] brackets)')
    parser.add_option('--interfaces',
                      action='store',
                      dest='interfaces',
                      default=None,
                      help=msg)

    msg = 'runs web2py tests'
    parser.add_option('--run_system_tests',
                      action='store_true',
                      dest='run_system_tests',
                      default=False,
                      help=msg)

    msg = ('adds coverage reporting (needs --run_system_tests), '
           'python 2.7 and the coverage module installed. '
           'You can alter the default path setting the environmental '
           'var "COVERAGE_PROCESS_START". '
           'By default it takes gluon/tests/coverage.ini')
    parser.add_option('--with_coverage',
                      action='store_true',
                      dest='with_coverage',
                      default=False,
                      help=msg)

    if '-A' in sys.argv:
        k = sys.argv.index('-A')
    elif '--args' in sys.argv:
        k = sys.argv.index('--args')
    else:
        k = len(sys.argv)
    sys.argv, other_args = sys.argv[:k], sys.argv[k + 1:]
    (options, args) = parser.parse_args()
    options.args = [options.run] + other_args

    copy_options = copy.deepcopy(options)
    copy_options.password = '******'
    global_settings.cmd_options = copy_options
    global_settings.cmd_args = args

    if options.gae:
        if not os.path.exists('app.yaml'):
            name = raw_input("Your GAE app name: ")
            content = open(os.path.join('examples', 'app.example.yaml'),
                           'rb').read()
            open('app.yaml', 'wb').write(content.replace("yourappname", name))
        else:
            print("app.yaml alreday exists in the web2py folder")
        if not os.path.exists('gaehandler.py'):
            content = open(os.path.join('handlers', 'gaehandler.py'),
                           'rb').read()
            open('gaehandler.py', 'wb').write(content)
        else:
            print("gaehandler.py alreday exists in the web2py folder")
        sys.exit(0)

    try:
        options.ips = list(
            set(  # no duplicates
                [
                    addrinfo[4][0]
                    for addrinfo in getipaddrinfo(socket.getfqdn())
                    if not is_loopback_ip_address(addrinfo=addrinfo)
                ]))
    except socket.gaierror:
        options.ips = []

    if options.run_system_tests:
        run_system_tests(options)

    if options.quiet:
        capture = StringIO()
        sys.stdout = capture
        logger.setLevel(logging.CRITICAL + 1)
    else:
        logger.setLevel(options.debuglevel)

    if options.config[-3:] == '.py':
        options.config = options.config[:-3]

    if options.cronjob:
        global_settings.cronjob = True  # tell the world
        options.plain = True  # cronjobs use a plain shell
        options.nobanner = True
        options.nogui = True

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

    #  accept --interfaces in the form
    #  "ip1:port1:key1:cert1:ca_cert1;[ip2]:port2;ip3:port3:key3:cert3"
    #  (no spaces; optional key:cert indicate SSL)
    if isinstance(options.interfaces, str):
        interfaces = options.interfaces.split(';')
        options.interfaces = []
        for interface in interfaces:
            if interface.startswith('['):  # IPv6
                ip, if_remainder = interface.split(']', 1)
                ip = ip[1:]
                if_remainder = if_remainder[1:].split(':')
                if_remainder[0] = int(if_remainder[0])  # numeric port
                options.interfaces.append(tuple([ip] + if_remainder))
            else:  # IPv4
                interface = interface.split(':')
                interface[1] = int(interface[1])  # numeric port
                options.interfaces.append(tuple(interface))

    #  accepts --scheduler in the form
    #  "app:group1,group2,app2:group1"
    scheduler = []
    options.scheduler_groups = None
    if isinstance(options.scheduler, str):
        if ':' in options.scheduler:
            for opt in options.scheduler.split(','):
                scheduler.append(opt.split(':'))
            options.scheduler = ','.join([app[0] for app in scheduler])
            options.scheduler_groups = scheduler

    if options.numthreads is not None and options.minthreads is None:
        options.minthreads = options.numthreads  # legacy

    create_welcome_w2p()

    if not options.cronjob:
        # If we have the applications package or if we should upgrade
        if not os.path.exists('applications/__init__.py'):
            write_file('applications/__init__.py', '')

    return options, args
コード例 #4
0
ファイル: widget.py プロジェクト: web2py/web2py
def start():
    """ Starts server and other services """

    # get command line arguments
    options = console(version=ProgramVersion)

    if options.gae:
        # write app.yaml, gaehandler.py, and exit
        if not os.path.exists('app.yaml'):
            name = options.gae
            # for backward compatibility
            if name == 'configure':
                if PY2: input = raw_input
                name = input("Your GAE app name: ")
            content = open(os.path.join('examples', 'app.example.yaml'), 'rb').read()
            open('app.yaml', 'wb').write(content.replace("yourappname", name))
        else:
            print("app.yaml alreday exists in the web2py folder")
        if not os.path.exists('gaehandler.py'):
            content = open(os.path.join('handlers', 'gaehandler.py'), 'rb').read()
            open('gaehandler.py', 'wb').write(content)
        else:
            print("gaehandler.py alreday exists in the web2py folder")
        return

    logger = logging.getLogger("web2py")
    logger.setLevel(options.log_level)

    # on new installation build the scaffolding app
    create_welcome_w2p()

    if options.run_system_tests:
        # run system test and exit
        run_system_tests(options)

    if options.quiet:
        # to prevent writes on stdout set a null stream
        class NullFile(object):
            def write(self, x):
                pass
        sys.stdout = NullFile()
        # but still has to mute existing loggers, to do that iterate
        # over all existing loggers (root logger included) and remove
        # all attached logging.StreamHandler instances currently
        # streaming on sys.stdout or sys.stderr
        loggers = [logging.getLogger()]
        loggers.extend(logging.Logger.manager.loggerDict.values())
        for l in loggers:
            if isinstance(l, logging.PlaceHolder): continue
            for h in l.handlers[:]:
                if isinstance(h, logging.StreamHandler) and \
                    h.stream in (sys.stdout, sys.stderr):
                    l.removeHandler(h)
        # NOTE: stderr.write() is still working

    if not options.no_banner:
        # banner
        print(ProgramName)
        print(ProgramAuthor)
        print(ProgramVersion)
        from pydal.drivers import DRIVERS
        print('Database drivers available: %s' % ', '.join(DRIVERS))

    if options.run_doctests:
        # run doctests and exit
        test(options.run_doctests, verbose=options.verbose)
        return

    if options.shell:
        # run interactive shell and exit
        sys.argv = [options.run or ''] + options.args
        run(options.shell, plain=options.plain, bpython=options.bpython,
            import_models=options.import_models, startfile=options.run,
            cron_job=options.cron_job)
        return

    if options.cron_run:
        # run cron (extcron) and exit
        logger.debug('Starting extcron...')
        global_settings.web2py_crontype = 'external'
        extcron = newcron.extcron(options.folder, apps=options.crontabs)
        extcron.start()
        extcron.join()
        return

    if not options.with_scheduler and options.schedulers:
        # run schedulers and exit
        try:
            start_schedulers(options)
        except KeyboardInterrupt:
            pass
        return

    if options.with_cron:
        if options.soft_cron:
            print('Using cron software emulation (but this is not very efficient)')
            global_settings.web2py_crontype = 'soft'
        else:
            # start hardcron thread
            logger.debug('Starting hardcron...')
            global_settings.web2py_crontype = 'hard'
            newcron.hardcron(options.folder, apps=options.crontabs).start()

    # if no password provided and have Tk library start GUI (when not
    # explicitly disabled), we also need a GUI to put in taskbar (system tray)
    # when requested
    root = None

    if (not options.no_gui and options.password == '<ask>') or options.taskbar:
        try:
            if PY2:
                import Tkinter as tkinter
            else:
                import tkinter
            root = tkinter.Tk()
        except (ImportError, OSError):
            logger.warn(
                'GUI not available because Tk library is not installed')
            options.no_gui = True
        except:
            logger.exception('cannot get Tk root window, GUI disabled')
            options.no_gui = True

    if root:
        # run GUI and exit
        root.focus_force()

        # Mac OS X - make the GUI window rise to the top
        if os.path.exists("/usr/bin/osascript"):
            applescript = """
tell application "System Events"
    set proc to first process whose unix id is %d
    set frontmost of proc to true
end tell
""" % (os.getpid())
            os.system("/usr/bin/osascript -e '%s'" % applescript)

        # web2pyDialog takes care of schedulers
        master = web2pyDialog(root, options)
        signal.signal(signal.SIGTERM, lambda a, b: master.quit())

        try:
            root.mainloop()
        except:
            master.quit()

        sys.exit()

    spt = None

    if options.with_scheduler and options.schedulers:
        # start schedulers in a separate thread
        spt = threading.Thread(target=start_schedulers, args=(options,))
        spt.start()

    # start server

    if options.password == '<ask>':
        options.password = getpass.getpass('choose a password:'******'no password, disable admin interface')

    # Use first interface IP and port if interfaces specified, since the
    # interfaces option overrides the IP (and related) options.
    if not options.interfaces:
        ip = options.ip
        port = options.port
    else:
        first_if = options.interfaces[0]
        ip = first_if[0]
        port = first_if[1]

    if options.server_key and options.server_cert:
        proto = 'https'
    else:
        proto = 'http'

    url = get_url(ip, proto=proto, port=port)

    if not options.no_banner:
        message = '\nplease visit:\n\t%s\n'
        if sys.platform.startswith('win'):
            message += 'use "taskkill /f /pid %i" to shutdown the web2py server\n\n'
        else:
            message += 'use "kill -SIGTERM %i" to shutdown the web2py server\n\n'
        print(message % (url, os.getpid()))

    # enhance linecache.getline (used by debugger) to look at the source file
    # if the line was not found (under py2exe & when file was modified)
    import linecache
    py2exe_getline = linecache.getline

    def getline(filename, lineno, *args, **kwargs):
        line = py2exe_getline(filename, lineno, *args, **kwargs)
        if not line:
            try:
                with open(filename, "rb") as f:
                    for i, line in enumerate(f):
                        line = line.decode('utf-8')
                        if lineno == i + 1:
                            break
                    else:
                        line = ''
            except (IOError, OSError):
                line = ''
        return line
    linecache.getline = getline

    server = main.HttpServer(ip=ip,
                             port=port,
                             password=options.password,
                             pid_filename=options.pid_filename,
                             log_filename=options.log_filename,
                             profiler_dir=options.profiler_dir,
                             ssl_certificate=options.server_cert,
                             ssl_private_key=options.server_key,
                             ssl_ca_certificate=options.ca_cert,
                             min_threads=options.min_threads,
                             max_threads=options.max_threads,
                             server_name=options.server_name,
                             request_queue_size=options.request_queue_size,
                             timeout=options.timeout,
                             socket_timeout=options.socket_timeout,
                             shutdown_timeout=options.shutdown_timeout,
                             path=options.folder,
                             interfaces=options.interfaces)

    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
        if spt is not None:
            try:
                spt.join()
            except:
                logger.exception('error terminating schedulers')
                pass
    logging.shutdown()
コード例 #5
0
ファイル: widget.py プロジェクト: zcomx/zcomix.com
def console():
    """ Defines the behavior of the console web2py execution """
    import optparse

    parser = optparse.OptionParser(
        usage='python %prog [options]',
        version=ProgramVersion,
        description='web2py Web Framework startup script.',
        epilog='''NOTE: unless a password is specified (-a 'passwd')
web2py will attempt to run a GUI to ask for it
(if not disabled with --nogui).''')

    parser.add_option('-i', '--ip',
                      default='127.0.0.1',
                      help=\
        'IP address of the server (e.g., 127.0.0.1 or ::1); ' \
        'Note: This value is ignored when using the --interfaces option')

    parser.add_option('-p', '--port',
                      default='8000',
                      type='int',
                      help='port of server (%default)')

    parser.add_option('-G', '--GAE', dest='gae',
                      default=None,
                      metavar='APP_NAME', help=\
      "will create app.yaml and gaehandler.py")

    parser.add_option('-a', '--password',
                      default='<ask>',
                      help=\
        'password to be used for administration ' \
        '(use -a "<recycle>" to reuse the last password))')

    parser.add_option('-c', '--ssl_certificate',
                      default='',
                      help='file that contains ssl certificate')

    parser.add_option('-k', '--ssl_private_key',
                      default='',
                      help='file that contains ssl private key')

    parser.add_option('--ca-cert', dest='ssl_ca_certificate',
                      default=None,
                      help=\
        'use this file containing the CA certificate to validate X509 ' \
        'certificates from clients')

    parser.add_option('-d', '--pid_filename',
                      default='httpserver.pid',
                      help='file to store the pid of the server')

    parser.add_option('-l', '--log_filename',
                      default='httpserver.log',
                      help='name for the server log file')

    parser.add_option('-n', '--numthreads',
                      default=None,
                      type='int',
                      help='number of threads (deprecated)')

    parser.add_option('--minthreads',
                      default=None,
                      type='int',
                      help='minimum number of server threads')

    parser.add_option('--maxthreads',
                      default=None,
                      type='int',
                      help='maximum number of server threads')

    parser.add_option('-s', '--server_name',
                      default=socket.gethostname(),
                      help='web server name (%default)')

    parser.add_option('-q', '--request_queue_size',
                      default='5',
                      type='int',
                      help=\
        'max number of queued requests when server unavailable')

    parser.add_option('-o', '--timeout',
                      default='10',
                      type='int',
                      help='timeout for individual request (%default seconds)')

    parser.add_option('-z', '--shutdown_timeout',
                      default='5',
                      type='int',
                      help='timeout on shutdown of server (%default seconds)')

    parser.add_option('--socket-timeout', dest='socket_timeout', # not needed
                      default=5,
                      type='int',
                      help='timeout for socket (%default seconds)')

    parser.add_option('-f', '--folder',
                      default=os.getcwd(),
                      help='folder from which to run web2py')

    parser.add_option('-v', '--verbose',
                      default=False,
                      action='store_true',
                      help='increase --test and --run_system_tests verbosity')

    parser.add_option('-Q', '--quiet',
                      default=False,
                      action='store_true',
                      help='disable all output')

    parser.add_option('-e', '--errors_to_console', dest='print_errors',
                      default=False,
                      action='store_true',
                      help='log all errors to console')

    parser.add_option('-D', '--debug', dest='debuglevel',
                      default=30,
                      type='int',
                      help=\
        'set debug output level (0-100, 0 means all, 100 means none; ' \
        'default is %default)')

    parser.add_option('-S', '--shell',
                      default=None,
                      metavar='APPNAME', help=\
        'run web2py in interactive shell or IPython (if installed) with ' \
        'specified appname (if app does not exist it will be created). ' \
        'APPNAME like a/c/f?x=y (c,f and vars x,y optional)')

    parser.add_option('-B', '--bpython',
                      default=False,
                      action='store_true',
                      help=\
        'run web2py in interactive shell or bpython (if installed) with ' \
        'specified appname (if app does not exist it will be created). ' \
        'Use combined with --shell')

    parser.add_option('-P', '--plain',
                      default=False,
                      action='store_true',
                      help=\
        'only use plain python shell; should be used with --shell option')

    parser.add_option('-M', '--import_models',
                      default=False,
                      action='store_true',
                      help=\
        'auto import model files; default is %default; should be used ' \
        'with --shell option')

    parser.add_option('-R', '--run',
                      default='', # NOTE: used for sys.argv[0] if --shell
                      metavar='PYTHON_FILE', help=\
        'run PYTHON_FILE in web2py environment; ' \
        'should be used with --shell option')

    parser.add_option('-K', '--scheduler',
                      default=None,
                      help=\
        'run scheduled tasks for the specified apps: expects a list of ' \
        'app names as -K app1,app2,app3 ' \
        'or a list of app:groups as -K app1:group1:group2,app2:group1 ' \
        'to override specific group_names. (only strings, no spaces ' \
        'allowed. Requires a scheduler defined in the models')

    parser.add_option('-X', '--with-scheduler', dest='with_scheduler', # not needed
                      default=False,
                      action='store_true',
                      help=\
        'run schedulers alongside webserver, needs -K app1 and -a too')

    parser.add_option('-T', '--test',
                      default=None,
                      metavar='TEST_PATH', help=\
        'run doctests in web2py environment; ' \
        'TEST_PATH like a/c/f (c,f optional)')

    parser.add_option('-C', '--cron', dest='extcron',
                      default=False,
                      action='store_true',
                      help=\
        'trigger a cron run manually; usually invoked from a system crontab')

    parser.add_option('--softcron',
                      default=False,
                      action='store_true',
                      help='triggers the use of softcron')

    parser.add_option('-Y', '--run-cron', dest='runcron',
                      default=False,
                      action='store_true',
                      help='start the background cron process')

    parser.add_option('-J', '--cronjob',
                      default=False,
                      action='store_true',
                      help='identify cron-initiated command')

    parser.add_option('-L', '--config',
                      default='',
                      help='config file')

    parser.add_option('-F', '--profiler', dest='profiler_dir',
                      default=None,
                      help='profiler dir')

    parser.add_option('-t', '--taskbar',
                      default=False,
                      action='store_true',
                      help='use web2py GUI and run in taskbar (system tray)')

    parser.add_option('--nogui',
                      default=False,
                      action='store_true',
                      help='do not run GUI')

    parser.add_option('-A', '--args',
                      default=None,
                      help=\
        'should be followed by a list of arguments to be passed to script, ' \
        'to be used with -S, -A must be the last option')

    parser.add_option('--no-banner', dest='nobanner',
                      default=False,
                      action='store_true',
                      help='do not print header banner')

    parser.add_option('--interfaces',
                      default=None,
                      help=\
        'listen on multiple addresses: ' \
        '"ip1:port1:key1:cert1:ca_cert1;ip2:port2:key2:cert2:ca_cert2;..." ' \
        '(:key:cert:ca_cert optional; no spaces; IPv6 addresses must be in ' \
        'square [] brackets)')

    parser.add_option('--run_system_tests',
                      default=False,
                      action='store_true',
                      help='run web2py tests')

    parser.add_option('--with_coverage',
                      default=False,
                      action='store_true',
                      help=\
        'adds coverage reporting (needs --run_system_tests), ' \
        'python 2.7 and the coverage module installed. ' \
        'You can alter the default path setting the environment ' \
        'variable "COVERAGE_PROCESS_START" ' \
        '(by default it takes gluon/tests/coverage.ini)')

    if '-A' in sys.argv:
        k = sys.argv.index('-A')
    elif '--args' in sys.argv:
        k = sys.argv.index('--args')
    else:
        k = len(sys.argv)
    sys.argv, other_args = sys.argv[:k], sys.argv[k + 1:]
    (options, args) = parser.parse_args()
    options.args = other_args

    if options.config.endswith('.py'):
        options.config = options.config[:-3]

    # TODO: process --config here; now is done in start function, too late

    copy_options = copy.deepcopy(options)
    copy_options.password = '******'
    global_settings.cmd_options = copy_options
    global_settings.cmd_args = args

    if options.gae:
        if not os.path.exists('app.yaml'):
            name = options.gae
            # for backward compatibility
            if name == 'configure':
                name = input("Your GAE app name: ")
            content = open(os.path.join('examples', 'app.example.yaml'), 'rb').read()
            open('app.yaml', 'wb').write(content.replace("yourappname", name))
        else:
            print("app.yaml alreday exists in the web2py folder")
        if not os.path.exists('gaehandler.py'):
            content = open(os.path.join('handlers', 'gaehandler.py'), 'rb').read()
            open('gaehandler.py', 'wb').write(content)
        else:
            print("gaehandler.py alreday exists in the web2py folder")
        sys.exit(0)

    try:
        options.ips = list(set(  # no duplicates
            [addrinfo[4][0] for addrinfo in getipaddrinfo(socket.getfqdn())
             if not is_loopback_ip_address(addrinfo=addrinfo)]))
    except socket.gaierror:
        options.ips = []

    # FIXME: this should be done after create_welcome_w2p
    if options.run_system_tests:
        # run system test and exit
        run_system_tests(options)

    if options.quiet:
        capture = StringIO()
        sys.stdout = capture
        logger.setLevel(logging.CRITICAL + 1)
    else:
        logger.setLevel(options.debuglevel)

    if options.cronjob:
        global_settings.cronjob = True  # tell the world
        options.plain = True    # cronjobs use a plain shell
        options.nobanner = True
        options.nogui = True

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

    #  accept --interfaces in the form
    #  "ip1:port1:key1:cert1:ca_cert1;[ip2]:port2;ip3:port3:key3:cert3"
    #  (no spaces; optional key:cert indicate SSL)
    if isinstance(options.interfaces, str):
        interfaces = options.interfaces.split(';')
        options.interfaces = []
        for interface in interfaces:
            if interface.startswith('['):
                # IPv6
                ip, if_remainder = interface.split(']', 1)
                ip = ip[1:]
                interface = if_remainder[1:].split(':')
                interface.insert(0, ip)
            else:
                # IPv4
                interface = interface.split(':')
            interface[1] = int(interface[1])  # numeric port
            options.interfaces.append(tuple(interface))

    #  accepts --scheduler in the form
    #  "app:group1:group2,app2:group1"
    scheduler = []
    options.scheduler_groups = None
    if isinstance(options.scheduler, str):
        if ':' in options.scheduler:
            for opt in options.scheduler.split(','):
                scheduler.append(opt.split(':'))
            options.scheduler = ','.join([app[0] for app in scheduler])
            options.scheduler_groups = scheduler

    if options.numthreads is not None and options.minthreads is None:
        options.minthreads = options.numthreads  # legacy

    create_welcome_w2p()

    # FIXME: do we still really need this?
    if not options.cronjob:
        # If we have the applications package or if we should upgrade
        if not os.path.exists('applications/__init__.py'):
            write_file('applications/__init__.py', '')

    return options, args
コード例 #6
0
ファイル: widget.py プロジェクト: ngorham/project_bacon
def console():
    """ Defines the behavior of the console web2py execution """
    import optparse
    import textwrap

    usage = "python web2py.py"

    description = """\
    web2py Web Framework startup script.
    ATTENTION: unless a password is specified (-a 'passwd') web2py will
    attempt to run a GUI. In this case command line options are ignored."""

    description = textwrap.dedent(description)

    parser = optparse.OptionParser(usage, None, optparse.Option, ProgramVersion)

    parser.description = description

    msg = (
        "IP address of the server (e.g., 127.0.0.1 or ::1); "
        "Note: This value is ignored when using the 'interfaces' option."
    )
    parser.add_option("-i", "--ip", default="127.0.0.1", dest="ip", help=msg)

    parser.add_option("-p", "--port", default="8000", dest="port", type="int", help="port of server (8000)")

    parser.add_option(
        "-G", "--GAE", default=None, dest="gae", help="'-G configure' will create app.yaml and gaehandler.py"
    )

    msg = "password to be used for administration " '(use -a "<recycle>" to reuse the last password))'
    parser.add_option("-a", "--password", default="<ask>", dest="password", help=msg)

    parser.add_option(
        "-c", "--ssl_certificate", default="", dest="ssl_certificate", help="file that contains ssl certificate"
    )

    parser.add_option(
        "-k", "--ssl_private_key", default="", dest="ssl_private_key", help="file that contains ssl private key"
    )

    msg = "Use this file containing the CA certificate to validate X509 " "certificates from clients"
    parser.add_option("--ca-cert", action="store", dest="ssl_ca_certificate", default=None, help=msg)

    parser.add_option(
        "-d",
        "--pid_filename",
        default="httpserver.pid",
        dest="pid_filename",
        help="file to store the pid of the server",
    )

    parser.add_option(
        "-l", "--log_filename", default="httpserver.log", dest="log_filename", help="file to log connections"
    )

    parser.add_option(
        "-n", "--numthreads", default=None, type="int", dest="numthreads", help="number of threads (deprecated)"
    )

    parser.add_option(
        "--minthreads", default=None, type="int", dest="minthreads", help="minimum number of server threads"
    )

    parser.add_option(
        "--maxthreads", default=None, type="int", dest="maxthreads", help="maximum number of server threads"
    )

    parser.add_option(
        "-s", "--server_name", default=socket.gethostname(), dest="server_name", help="server name for the web server"
    )

    msg = "max number of queued requests when server unavailable"
    parser.add_option("-q", "--request_queue_size", default="5", type="int", dest="request_queue_size", help=msg)

    parser.add_option(
        "-o", "--timeout", default="10", type="int", dest="timeout", help="timeout for individual request (10 seconds)"
    )

    parser.add_option(
        "-z",
        "--shutdown_timeout",
        default="5",
        type="int",
        dest="shutdown_timeout",
        help="timeout on shutdown of server (5 seconds)",
    )

    parser.add_option(
        "--socket-timeout", default=5, type="int", dest="socket_timeout", help="timeout for socket (5 second)"
    )

    parser.add_option("-f", "--folder", default=os.getcwd(), dest="folder", help="folder from which to run web2py")

    parser.add_option(
        "-v", "--verbose", action="store_true", dest="verbose", default=False, help="increase --test verbosity"
    )

    parser.add_option("-Q", "--quiet", action="store_true", dest="quiet", default=False, help="disable all output")

    parser.add_option(
        "-e",
        "--errors_to_console",
        action="store_true",
        dest="print_errors",
        default=False,
        help="log all errors to console",
    )

    msg = "set debug output level (0-100, 0 means all, 100 means none; " "default is 30)"
    parser.add_option("-D", "--debug", dest="debuglevel", default=30, type="int", help=msg)

    msg = (
        "run web2py in interactive shell or IPython (if installed) with "
        "specified appname (if app does not exist it will be created). "
        "APPNAME like a/c/f (c,f optional)"
    )
    parser.add_option("-S", "--shell", dest="shell", metavar="APPNAME", help=msg)

    msg = (
        "run web2py in interactive shell or bpython (if installed) with "
        "specified appname (if app does not exist it will be created).\n"
        "Use combined with --shell"
    )
    parser.add_option("-B", "--bpython", action="store_true", default=False, dest="bpython", help=msg)

    msg = "only use plain python shell; should be used with --shell option"
    parser.add_option("-P", "--plain", action="store_true", default=False, dest="plain", help=msg)

    msg = "auto import model files; default is False; should be used " "with --shell option"
    parser.add_option("-M", "--import_models", action="store_true", default=False, dest="import_models", help=msg)

    msg = "run PYTHON_FILE in web2py environment; " "should be used with --shell option"
    parser.add_option("-R", "--run", dest="run", metavar="PYTHON_FILE", default="", help=msg)

    msg = (
        "run scheduled tasks for the specified apps: expects a list of "
        "app names as -K app1,app2,app3 "
        "or a list of app:groups as -K app1:group1:group2,app2:group1 "
        "to override specific group_names. (only strings, no spaces "
        "allowed. Requires a scheduler defined in the models"
    )
    parser.add_option("-K", "--scheduler", dest="scheduler", default=None, help=msg)

    msg = "run schedulers alongside webserver, needs -K app1 and -a too"
    parser.add_option("-X", "--with-scheduler", action="store_true", default=False, dest="with_scheduler", help=msg)

    msg = "run doctests in web2py environment; " "TEST_PATH like a/c/f (c,f optional)"
    parser.add_option("-T", "--test", dest="test", metavar="TEST_PATH", default=None, help=msg)

    msg = "trigger a cron run manually; usually invoked from a system crontab"
    parser.add_option("-C", "--cron", action="store_true", dest="extcron", default=False, help=msg)

    msg = "triggers the use of softcron"
    parser.add_option("--softcron", action="store_true", dest="softcron", default=False, help=msg)

    parser.add_option(
        "-Y", "--run-cron", action="store_true", dest="runcron", default=False, help="start the background cron process"
    )

    parser.add_option(
        "-J", "--cronjob", action="store_true", dest="cronjob", default=False, help="identify cron-initiated command"
    )

    parser.add_option("-L", "--config", dest="config", default="", help="config file")

    parser.add_option("-F", "--profiler", dest="profiler_dir", default=None, help="profiler dir")

    parser.add_option(
        "-t",
        "--taskbar",
        action="store_true",
        dest="taskbar",
        default=False,
        help="use web2py gui and run in taskbar (system tray)",
    )

    parser.add_option("", "--nogui", action="store_true", default=False, dest="nogui", help="text-only, no GUI")

    msg = (
        "should be followed by a list of arguments to be passed to script, "
        "to be used with -S, -A must be the last option"
    )
    parser.add_option("-A", "--args", action="store", dest="args", default=None, help=msg)

    parser.add_option(
        "--no-banner", action="store_true", default=False, dest="nobanner", help="Do not print header banner"
    )

    msg = (
        "listen on multiple addresses: "
        '"ip1:port1:key1:cert1:ca_cert1;ip2:port2:key2:cert2:ca_cert2;..." '
        "(:key:cert:ca_cert optional; no spaces; IPv6 addresses must be in "
        "square [] brackets)"
    )
    parser.add_option("--interfaces", action="store", dest="interfaces", default=None, help=msg)

    msg = "runs web2py tests"
    parser.add_option("--run_system_tests", action="store_true", dest="run_system_tests", default=False, help=msg)

    msg = (
        "adds coverage reporting (needs --run_system_tests), "
        "python 2.7 and the coverage module installed. "
        "You can alter the default path setting the environmental "
        'var "COVERAGE_PROCESS_START". '
        "By default it takes gluon/tests/coverage.ini"
    )
    parser.add_option("--with_coverage", action="store_true", dest="with_coverage", default=False, help=msg)

    if "-A" in sys.argv:
        k = sys.argv.index("-A")
    elif "--args" in sys.argv:
        k = sys.argv.index("--args")
    else:
        k = len(sys.argv)
    sys.argv, other_args = sys.argv[:k], sys.argv[k + 1 :]
    (options, args) = parser.parse_args()
    options.args = [options.run] + other_args
    global_settings.cmd_options = options
    global_settings.cmd_args = args

    if options.gae:
        if not os.path.exists("app.yaml"):
            name = raw_input("Your GAE app name: ")
            content = open(os.path.join("examples", "app.example.yaml"), "rb").read()
            open("app.yaml", "wb").write(content.replace("yourappname", name))
        else:
            print "app.yaml alreday exists in the web2py folder"
        if not os.path.exists("gaehandler.py"):
            content = open(os.path.join("handlers", "gaehandler.py"), "rb").read()
            open("gaehandler.py", "wb").write(content)
        else:
            print "gaehandler.py alreday exists in the web2py folder"
        sys.exit(0)

    try:
        options.ips = list(
            set(  # no duplicates
                [
                    addrinfo[4][0]
                    for addrinfo in getipaddrinfo(socket.getfqdn())
                    if not is_loopback_ip_address(addrinfo=addrinfo)
                ]
            )
        )
    except socket.gaierror:
        options.ips = []

    if options.run_system_tests:
        run_system_tests(options)

    if options.quiet:
        capture = cStringIO.StringIO()
        sys.stdout = capture
        logger.setLevel(logging.CRITICAL + 1)
    else:
        logger.setLevel(options.debuglevel)

    if options.config[-3:] == ".py":
        options.config = options.config[:-3]

    if options.cronjob:
        global_settings.cronjob = True  # tell the world
        options.plain = True  # cronjobs use a plain shell
        options.nobanner = True
        options.nogui = True

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

    #  accept --interfaces in the form
    #  "ip1:port1:key1:cert1:ca_cert1;[ip2]:port2;ip3:port3:key3:cert3"
    #  (no spaces; optional key:cert indicate SSL)
    if isinstance(options.interfaces, str):
        interfaces = options.interfaces.split(";")
        options.interfaces = []
        for interface in interfaces:
            if interface.startswith("["):  # IPv6
                ip, if_remainder = interface.split("]", 1)
                ip = ip[1:]
                if_remainder = if_remainder[1:].split(":")
                if_remainder[0] = int(if_remainder[0])  # numeric port
                options.interfaces.append(tuple([ip] + if_remainder))
            else:  # IPv4
                interface = interface.split(":")
                interface[1] = int(interface[1])  # numeric port
                options.interfaces.append(tuple(interface))

    #  accepts --scheduler in the form
    #  "app:group1,group2,app2:group1"
    scheduler = []
    options.scheduler_groups = None
    if isinstance(options.scheduler, str):
        if ":" in options.scheduler:
            for opt in options.scheduler.split(","):
                scheduler.append(opt.split(":"))
            options.scheduler = ",".join([app[0] for app in scheduler])
            options.scheduler_groups = scheduler

    if options.numthreads is not None and options.minthreads is None:
        options.minthreads = options.numthreads  # legacy

    create_welcome_w2p()

    if not options.cronjob:
        # If we have the applications package or if we should upgrade
        if not os.path.exists("applications/__init__.py"):
            write_file("applications/__init__.py", "")

    return options, args
コード例 #7
0
def start(cron=True):
    """ Starts server and other services """

    # get command line arguments
    (options, args) = console()

    if options.gae:
        # write app.yaml, gaehandler.py, and exit
        if not os.path.exists('app.yaml'):
            name = options.gae
            # for backward compatibility
            if name == 'configure':
                if PY2: input = raw_input
                name = input("Your GAE app name: ")
            content = open(os.path.join('examples', 'app.example.yaml'),
                           'rb').read()
            open('app.yaml', 'wb').write(content.replace("yourappname", name))
        else:
            print("app.yaml alreday exists in the web2py folder")
        if not os.path.exists('gaehandler.py'):
            content = open(os.path.join('handlers', 'gaehandler.py'),
                           'rb').read()
            open('gaehandler.py', 'wb').write(content)
        else:
            print("gaehandler.py alreday exists in the web2py folder")
        return

    create_welcome_w2p()

    if options.run_system_tests:
        # run system test and exit
        run_system_tests(options)

    if options.quiet:
        capture = StringIO()
        sys.stdout = capture
        logger.setLevel(logging.CRITICAL + 1)
    else:
        logger.setLevel(options.debuglevel)

    if not options.nobanner:
        # banner
        print(ProgramName)
        print(ProgramAuthor)
        print(ProgramVersion)
        from pydal.drivers import DRIVERS
        print('Database drivers available: %s' % ', '.join(DRIVERS))

    if options.test:
        # run doctests and exit
        test(options.test, verbose=options.verbose)
        return

    if options.shell:
        # run interactive shell and exit
        if options.folder:
            os.chdir(options.folder)
        sys.argv = [options.run] + options.args
        run(options.shell,
            plain=options.plain,
            bpython=options.bpython,
            import_models=options.import_models,
            startfile=options.run,
            cronjob=options.cronjob)
        return

    if options.extcron:
        # run cron (extcron) and exit
        logger.debug('Starting extcron...')
        global_settings.web2py_crontype = 'external'
        if options.scheduler:
            # run cron for applications listed with --scheduler (-K)
            apps = [
                app.strip() for app in options.scheduler.split(',')
                if check_existent_app(options, app.strip())
            ]
        else:
            apps = None
        extcron = newcron.extcron(options.folder, apps=apps)
        extcron.start()
        extcron.join()
        return

    if options.scheduler and not options.with_scheduler:
        # run schedulers and exit
        try:
            start_schedulers(options)
        except KeyboardInterrupt:
            pass
        return

    if cron and options.runcron:
        if options.softcron:
            print('Using softcron (but this is not very efficient)')
            global_settings.web2py_crontype = 'soft'
        else:
            # start hardcron thread
            logger.debug('Starting hardcron...')
            global_settings.web2py_crontype = 'hard'
            newcron.hardcron(options.folder).start()

    # if no password provided and have Tk library start GUI (when not
    # explicitly disabled), we also need a GUI to put in taskbar (system tray)
    # when requested

    # FIXME: this check should be done first
    if options.taskbar and os.name != 'nt':
        die('taskbar not supported on this platform')

    root = None

    if (not options.nogui and options.password == '<ask>') or options.taskbar:
        try:
            if PY2:
                import Tkinter as tkinter
            else:
                import tkinter
            root = tkinter.Tk()
        except (ImportError, OSError):
            logger.warn(
                'GUI not available because Tk library is not installed')
            options.nogui = True
        except:
            logger.exception('cannot get Tk root window, GUI disabled')
            options.nogui = True

    if root:
        # run GUI and exit
        root.focus_force()

        # Mac OS X - make the GUI window rise to the top
        if os.path.exists("/usr/bin/osascript"):
            applescript = """
tell application "System Events"
    set proc to first process whose unix id is %d
    set frontmost of proc to true
end tell
""" % (os.getpid())
            os.system("/usr/bin/osascript -e '%s'" % applescript)

        # web2pyDialog takes care of schedulers
        master = web2pyDialog(root, options)
        signal.signal(signal.SIGTERM, lambda a, b: master.quit())

        try:
            root.mainloop()
        except:
            master.quit()

        sys.exit()

    if options.password == '<ask>':
        options.password = getpass.getpass('choose a password:'******'no password, disable admin interface')

    spt = None

    if options.scheduler and options.with_scheduler:
        # start schedulers in a separate thread
        spt = threading.Thread(target=start_schedulers, args=(options, ))
        spt.start()

    # start server

    # Use first interface IP and port if interfaces specified, since the
    # interfaces option overrides the IP (and related) options.
    if not options.interfaces:
        ip = options.ip
        port = options.port
    else:
        first_if = options.interfaces[0]
        ip = first_if[0]
        port = first_if[1]

    if options.ssl_certificate or options.ssl_private_key:
        proto = 'https'
    else:
        proto = 'http'

    url = get_url(ip, proto=proto, port=port)

    if not options.nobanner:
        message = '\nplease visit:\n\t%s\n'
        if sys.platform.startswith('win'):
            message += 'use "taskkill /f /pid %i" to shutdown the web2py server\n\n'
        else:
            message += 'use "kill -SIGTERM %i" to shutdown the web2py server\n\n'
        print(message % (url, os.getpid()))

    # enhance linecache.getline (used by debugger) to look at the source file
    # if the line was not found (under py2exe & when file was modified)
    import linecache
    py2exe_getline = linecache.getline

    def getline(filename, lineno, *args, **kwargs):
        line = py2exe_getline(filename, lineno, *args, **kwargs)
        if not line:
            try:
                with open(filename, "rb") as f:
                    for i, line in enumerate(f):
                        line = line.decode('utf-8')
                        if lineno == i + 1:
                            break
                    else:
                        line = ''
            except (IOError, OSError):
                line = ''
        return line

    linecache.getline = getline

    server = main.HttpServer(ip=ip,
                             port=port,
                             password=options.password,
                             pid_filename=options.pid_filename,
                             log_filename=options.log_filename,
                             profiler_dir=options.profiler_dir,
                             ssl_certificate=options.ssl_certificate,
                             ssl_private_key=options.ssl_private_key,
                             ssl_ca_certificate=options.ssl_ca_certificate,
                             min_threads=options.minthreads,
                             max_threads=options.maxthreads,
                             server_name=options.server_name,
                             request_queue_size=options.request_queue_size,
                             timeout=options.timeout,
                             socket_timeout=options.socket_timeout,
                             shutdown_timeout=options.shutdown_timeout,
                             path=options.folder,
                             interfaces=options.interfaces)

    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()
        if spt is not None:
            try:
                spt.join()
            except:
                logger.exception('error terminating schedulers')
                pass
    logging.shutdown()