Exemplo n.º 1
0
 def serve(self):
     """ A "fake" serve function, for the benefit of autoreload. """
     # Note that, becuse of the way the autorestarter works, all the code
     # which executes up to here is run twice (except, that is, for the
     # code in this block)
     if "RUN_MAIN" not in environ:
         log.info("Starting standalone server on port %s" % (self._port, ))
     if config.as_bool('no_reload'):
         self._serve()
     else:
         autoreload.main(self._serve, self._modification_callback)
Exemplo n.º 2
0
 def serve(self):
     """ A "fake" serve function, for the benefit of autoreload. """
     # Note that, becuse of the way the autorestarter works, all the code
     # which executes up to here is run twice (except, that is, for the
     # code in this block)
     if "RUN_MAIN" not in environ:
         log.info("Starting standalone server on port %s" %(self._port, ))
     if config.as_bool('no_reload'):
         self._serve()
     else:
         autoreload.main(self._serve, self._modification_callback)
Exemplo n.º 3
0
    def handle_connection(self, conn):
        self._output = conn.makefile("w")

        input = conn.makefile("r")
        self.env = self.read_env(input)
        # CONTENT_LENGTH is set by the SCGI server.
        # See RFC 3875, section 4.1.2
        body_size = int(self.env.get('CONTENT_LENGTH', 0))
        request_text = input.read(body_size)
        self.register_instance(self.dispatcher(self.env['PATH_INFO']))
        self.handle_request(request_text)

        try:
            self._output.close()
            input.close()
            conn.close()
        except IOError, err:
            log.info("IOError while closing connection ignored: %s" % err)
Exemplo n.º 4
0
    def handle_connection(self, conn):
        self._output = conn.makefile("w")

        input = conn.makefile("r")
        self.env = self.read_env(input)
        # CONTENT_LENGTH is set by the SCGI server.
        # See RFC 3875, section 4.1.2
        body_size = int(self.env.get('CONTENT_LENGTH', 0))
        request_text = input.read(body_size)
        self.register_instance(self.dispatcher(self.env['PATH_INFO']))
        self.handle_request(request_text)

        try:
            self._output.close()
            input.close()
            conn.close()
        except IOError, err:
            log.info("IOError while closing connection ignored: %s" % err)
Exemplo n.º 5
0
def test_create_env():
    dir = tempdir()

    env0 = EnvironmentManager()
    env0.create(dir, {'web_wrapper': 'cgi', 'log_type': 'file'})

    ### Test that the correct values are loaded
    env1 = EnvironmentManager()
    env1.load(dir, {})
    assert_equal(config['web_wrapper'], 'cgi')
    
    ### Test that things get logged to files properly
    log.info('testing log')
    log_data = open(env1.path('pyols.log')).read()
    assert 'testing log' in log_data

    ### Test that an error is raised on loading a bad version
    for env_class in (EnvWithSmallVersion, EnvWithBigVersion):
        env = env_class()
        assert_raises(PyolsEnvironmentError, env.load, dir, {})
Exemplo n.º 6
0
Arquivo: env.py Projeto: nsi-iff/pyOLS
class EnvironmentManager:
    # Increase this version each time something that will break backwards
    # compatibility changes to cause a warning at startup.
    version = 0

    def path(self, *p):
        """ Return a path reletive to the environment path.
            >>> e = EnvironmentManager()
            >>> e._path = "/tmp"
            >>> e.path()
            '/tmp'
            >>> e.path('version')
            '/tmp/version'
            >>> """
        return path.join(self._path, *p)

    def assert_env_version(self):
        """ Assert that the environment is the correct version. """
        ver = file(self.path('version')).read().split()[-1]
        ver = int(ver)
        if ver > self.version:
            raise PyolsEnvironmentError("The environment was created with a "
                                        "newer version of PyOLS.")
        elif ver < self.version:
            # XXX: Right now there is no code to actually upgrade an
            #      environment which is out of date.  Please write it :)
            raise PyolsEnvironmentError("The environment needs upgrading.")

    def load(self, p, c):
        """ Load the environment from path 'p' with config options 'c'.
            An exception will be raised if there is something wrong with it
            (version is too old, it doesn't exist, etc). """
        if not path.exists(p):
            raise PyolsNotFound("The environment path '%s' does not exist. "
                                "Create it with '-c'?" % (p))
        self._path = p

        self.assert_env_version()

        config.load(self.path('config.ini'))
        config.update(c)

        log.reconfigure(config, self.path('pyols.log'))

        # Note: The DB path is hard-coded here for two reasons:
        #       0) I cannot think of any good reason to change it
        #       1) It would involve more code to get the environment
        #          path into the config parser.
        db.connect('sqlite:///' + self.path('pyOLS.sqlite3'),
                   debug=config['log_level'] == 'debug')

    def create(self, path, c):
        """ Create an environment at 'path' with config options 'c'.
            The 'path' will be created, and an error will be raised
            if it already exists.
            `load(path)` is implied by calling `create(path)`. """
        self._path = path

        def mkfile(path, data):
            f = open(self.path(path), "w")
            f.write(data)
            f.close()

        try:
            mkdir(self.path())
        except OSError, e:
            raise PyolsValidationError("Cannot create a new environment:\n"\
                                       + str(e))
        mkfile('version', 'PyOLS environment version: %s' % self.version)
        mkfile(
            'README', 'A PyOLS environment.\n'
            'See http://nsi.cefetcampos.br/softwares/pyols/')
        mkfile('config.ini', config.default_config())

        gv_path = find_graphviz()
        if gv_path:
            config['graphviz_path'] = gv_path
        else:
            log.warning("Could not find graphviz binaries. Before PyOLS will "
                        "be able to generate graphs, the 'graphviz_path' "
                        "in pyols.ini will need to be set.")

        self.load(path, c)

        config.write()
        db.create_tables()

        log.info("Environment created at %r" % (path))
Exemplo n.º 7
0
 def _modification_callback(self, file):
     log.info("%s was modified.  Restarting." % (file, ))
Exemplo n.º 8
0
 def _modification_callback(self, file):
     log.info("%s was modified.  Restarting." %(file, ))