def setUp(self):
        # make an in-memory sqlite database to use during testing
        self.connection = Connection_wrapper(
            sqlite.connect(":memory:",
                           detect_types=sqlite.PARSE_DECLTYPES,
                           check_same_thread=False))
        self.cache = Stub_cache()
        cursor = self.connection.cursor()
        cursor.execute(Stub_object.sql_create_table())

        self.fake_files = {
        }  # map of fake filename (full path) to fake file contents
        self.database = Database(self.connection, self.cache)
        self.upgrader = Schema_upgrader(self.database,
                                        glob=self.glob,
                                        read_file=self.read_file)
Пример #2
0
def main(options):
    change_to_main_dir()

    cherrypy.config.update(Common.settings)
    if options.development:
        from config import Development
        settings = Development.settings
    elif options.desktop:
        from config import Desktop
        settings = Desktop.settings
    else:
        from config import Production
        settings = Production.settings

    cherrypy.config.update(settings)

    # Don't launch web browser if -w flag is set
    if options.no_webbrowser:
        launch_browser = False
    else:
        launch_browser = cherrypy.config[u"luminotes.launch_browser"]

    socket.setdefaulttimeout(INITIAL_SOCKET_TIMEOUT_SECONDS)
    port_filename = cherrypy.config[u"luminotes.port_file"]
    socket_port = cherrypy.config[u"server.socket_port"]
    existing_socket_port = port_filename and os.path.exists(
        port_filename) and file(port_filename).read() or socket_port
    server_url = u"http://localhost:%s/" % existing_socket_port
    server_present = True

    # if requested, attempt to shutdown an existing server and exit
    if options.kill:
        try:
            urllib.urlopen("%sshutdown" % server_url)
        except urllib.URLError:
            pass
        sys.exit(0)

    # check to see if the server is already running
    try:
        urllib.urlopen("%sping" % server_url)
    except urllib.URLError:
        server_present = False

    if server_present is True:
        print "Luminotes server is already running. aborting"

        if launch_browser is True:
            webbrowser.open_new(server_url)

        sys.exit(0)

    server_url = u"http://127.0.0.1:%s/" % socket_port

    # remove the existing log files, if any
    try:
        log_access_file = cherrypy.config[u"server.log_access_file"]
        if log_access_file:
            os.remove(log_access_file)
    except OSError:
        pass

    try:
        log_file = cherrypy.config[u"server.log_file"]
        if log_file:
            os.remove(log_file)
    except OSError:
        pass

    socket.setdefaulttimeout(SOCKET_TIMEOUT_SECONDS)

    database = Database(
        host=cherrypy.config[u"luminotes.db_host"],
        ssl_mode=cherrypy.config[u"luminotes.db_ssl_mode"],
    )

    # if necessary, upgrade the database schema to match this current version of the code
    schema_upgrader = Schema_upgrader(database)
    schema_upgrader.upgrade_schema(to_version=VERSION)

    cherrypy.lowercase_api = True
    root = Root(database, cherrypy.config)
    cherrypy.tree.mount(root, '/', config=settings)

    cherrypy.engine.start_with_callback(
        callback, (log_access_file, log_file, server_url, port_filename,
                   socket_port, launch_browser))
    cherrypy.engine.block()