Ejemplo n.º 1
0
def includeme(app: App):
    app.include("discovery")

    # TODO: spawn, when using
    # TODO: spawn, when dry_run=False only
    def _register():
        from discovery import get_discovery

        port = None

        if app.registry.dry_run:
            logger.info("dry run, %s skipped", NAME)
            get_discovery().register("gofmtrpc",
                                     url=f"http://127.0.0.1:{port}")
            return

        from egoist.internal.netutil import find_free_port
        from util import spawn_with_connection

        port = find_free_port()
        get_discovery().register("gofmtrpc", url=f"http://127.0.0.1:{port}")

        argv = ["gofmtrpc", "-addr", f":{port}"]
        p, _ = spawn_with_connection(argv, sentinel_option="-sentinel")

        import atexit

        def _shutdown():
            logger.info("terminate gofmtrpc")
            with p:
                p.terminate()

        atexit.register(_shutdown)

    app.action(NAME, _register)
Ejemplo n.º 2
0
def includeme(app: App):
    app.include("discovery")

    # TODO: only spawn, when using
    def _register():
        import shutil
        from egoist.internal.netutil import find_free_port
        from discovery import get_discovery
        import util

        sentinel = util.create_sentinel_file()
        port = find_free_port()

        get_discovery().register("gofmtrpc", url=f"http://127.0.0.1:{port}")

        assert shutil.which("gofmtrpc")
        argv = [
            "gofmtrpc",
            "-addr",
            f":{port}",
            "-sentinel",
            sentinel,
        ]
        p = util.ConnectedProcess().spawn(argv, sentinel=sentinel)
        import atexit

        def _shutdown():
            logger.info("terminate gofmtrpc")
            with p:
                p.terminate()

        atexit.register(_shutdown)

    app.action(NAME, _register)
Ejemplo n.º 3
0
def includeme(app: App) -> None:
    app.include(".tracker")

    actual: FSFactory = create_fs
    app.register_factory(NAME, actual)

    for_dry_run: FSFactory = create_dummy_fs
    app.register_dryurn_factory(NAME, for_dry_run)
Ejemplo n.º 4
0
def setup_server(app: App) -> None:
    from egoist.ext.serverprocess.lazyparams import find_free_port, create_sentinel_file

    app.include("egoist.ext.serverprocess")  # for add_server_process
    app.add_server_process(
        "uvicorn server:app --port {port}",
        params=dict(port=find_free_port),
        name="api-server",
        env={"SENTINEL": create_sentinel_file},
    )
Ejemplo n.º 5
0
def includeme(app: App) -> None:
    app.include("directives.define_server_process")

    from rpcutil import find_free_port, create_sentinel_file

    app.define_server_process(
        "gofmtrpc -addr :{port} -sentinel {sentinel}",
        params=dict(port=find_free_port, sentinel=create_sentinel_file),
        name="gofmtrpc",
    )
Ejemplo n.º 6
0
def includeme(app: App) -> None:
    from egoist.experimental.serverprocess.lazyparams import find_free_port, create_sentinel_file

    app.include("egoist.experimental.serverprocess")  # for add_server_process

    app.add_server_process(
        "gofmtrpc -addr :{port} -sentinel {sentinel}",
        params=dict(port=find_free_port, sentinel=create_sentinel_file),
        name=NAME,
    )
Ejemplo n.º 7
0
def define_server_process(app: App):
    def _define(
        app: App,
        fmt: str,
        *,
        name: str,
        urlfmt: str = "http://{host}:{port}",
        host: str = "127.0.0.1",
        port: t.Optional[int] = None,
        params: t.Optional[t.Dict[str, t.Callable[[], object]]] = None,
    ):
        def _register():
            nonlocal host
            nonlocal port

            import shlex
            import atexit
            from discovery import get_discovery
            from rpcutil import find_free_port

            kwargs = {k: fn(app) for k, fn in (params or {}).items()}
            if port is None:
                port = kwargs.get("port") or find_free_port()
            if "host" in kwargs:
                host = kwargs.get["host"]

            argv = shlex.split(fmt.format(**kwargs))
            url = urlfmt.format(host=host, port=port)

            get_discovery().register(name, url=url)
            if app.registry.dry_run:
                logger.info("dry run, %s skipped", name)
                return

            from spawn import spawn_with_connection

            p, _ = spawn_with_connection(argv)

            def _shutdown():  # xxx:
                logger.info("terminate %s", name)
                with p:
                    p.terminate()

            atexit.register(_shutdown)

        app.action(("define_server_process", name), _register)

    app.include("discovery")
    app.add_directive("define_server_process", _define)
Ejemplo n.º 8
0
def includeme(app: App):
    app.include("discovery")

    # TODO: only spawn, when using
    def _register():
        import sys
        import pathlib
        from egoist.internal.netutil import find_free_port
        from discovery import get_discovery
        import util

        sentinel = util.create_sentinel_file()
        server_py = pathlib.Path(__file__).absolute().with_name("server.py")
        port = find_free_port()

        get_discovery().register("HELLO", url=f"http://127.0.0.1:{port}")

        argv = [
            sys.executable,
            server_py,
            "--port",
            str(port),
            "--host",
            "127.0.0.1",
            "--sentinel",
            sentinel,
        ]

        p = util.ConnectedProcess().spawn(argv, sentinel=sentinel)
        import atexit

        def _shutdown():
            logger.info("terminate HELLO")
            with p:
                p.terminate()

        atexit.register(_shutdown)

    app.action(NAME, _register)
Ejemplo n.º 9
0
from egoist.app import App, SettingsDict, parse_args

settings: SettingsDict = {"rootdir": "output", "here": __file__}
app = App(settings)

app.include("egoist.directives.define_file")


@app.define_file("egoist.generators.filekit:walk", suffix=".txt")
def hello() -> None:
    from egoist.generators.filekit import runtime

    with runtime.create_file() as wf:
        print("hello world", file=wf)


if __name__ == "__main__":
    for argv in parse_args(sep="-"):
        app.run(argv)
Ejemplo n.º 10
0
def setup_server(app: App) -> None:
    from egoist.ext.serverprocess.lazyparams import find_free_port, create_sentinel_file

    app.include("egoist.ext.serverprocess")  # for add_server_process
Ejemplo n.º 11
0
from __future__ import annotations
import typing as t
from egoist.app import App

app = App(settings={"here": __file__, "rootdir": ""})
app.include("egoist.directives.define_struct_set")


@app.define_struct_set("egoist.generators.structkit:walk")  # type: ignore
def models__models() -> None:
    from egoist.generators.structkit import runtime, structkit
    import objects

    @runtime.set_metadata_handler
    def metadata_handler(cls: t.Type[t.Any], *, name: str, info: t.Any,
                         metadata: runtime.Metadata) -> None:
        """with form"""
        metadata["tags"] = {
            "json": [name.rstrip("_")],
            "form": [name.rstrip("_")]
        }

    with runtime.generate(structkit, classes=[objects.Person]) as m:
        m.package("models")


if __name__ == "__main__":
    app.run()
Ejemplo n.º 12
0
def includeme(app: App) -> None:
    app.include("egoist.components.tracker")
    app.include("egoist.directives.add_subcommand")
    app.include("egoist.commands.generate")
    app.add_subcommand(setup, fn=makegen)
Ejemplo n.º 13
0
from egoist.app import App, SettingsDict

settings: SettingsDict = {"root": "cmd/", "here": __file__}
app = App(settings)

app.include("egoist.directives.define_cli")


@app.define_cli("egoist.generate.clikit:walk")
def hello(*, name: str) -> None:
    """hello message"""
    from egoist.generate.clikit import runtime, clikit

    with runtime.generate(clikit):
        runtime.printf("hello %s\n", name)


if __name__ == "__main__":
    app.run()
Ejemplo n.º 14
0
@directive(name="define_foo", requires=["egoist.commands.describe"])
def define_foo(app: App, fn: AnyFunction, name: str) -> AnyFunction:
    """*** define foo ***"""
    print(app, fn, "foo", name)

    def _register():
        print("!")

    app.registry.generators[name].append(fn)
    app.action(fn.__name__, _register)
    return fn


if __name__ == "__main__":
    app = App()
    app.include("__main__.define_foo")

    @app.define_foo("xxx")
    def yyy():
        print("yyy")

    @app.define_foo("xxx")
    def zzz():
        print("zzz")

    print("s")
    app.commit()
    yyy()
    zzz()
    print("e")
Ejemplo n.º 15
0
    def _define(
        app: App,
        fmt: str,
        *,
        name: str,
        urlfmt: str = "http://{host}:{port}",
        host: str = "127.0.0.1",
        port: t.Optional[t.Union[int, str]] = None,
        params: t.Optional[t.Dict[str, LazyParam]] = None,
        env: t.Optional[t.Dict[str, LazyParam]] = None,
        cwd: t.Union[str, pathlib.Path, None] = None,
        nowait: bool = False,
    ) -> None:
        app.include("egoist.experimental.serverprocess.components.discovery")
        app.include("egoist.experimental.serverprocess.components.httpclient")

        def _register() -> None:
            nonlocal host
            nonlocal port

            import shlex
            import atexit
            from .components.discovery import get_discovery
            from .lazyparams import find_free_port, create_sentinel_file

            if app.registry.dry_run:
                kwargs: t.Dict[str, t.Any] = {
                    k: "xxx"
                    for k in (params or {}).keys()
                }
                environ = {k: "xxx" for k in (env or {}).keys()}
                port = "xxx"
                sentinel = "xxx"
            else:
                kwargs = {k: fn(app) for k, fn in (params or {}).items()}
                environ = {k: fn(app) for k, fn in (env or {}).items()}

                if port is None:
                    port = (kwargs.get("port") or environ.get("port")
                            or environ.get("PORT") or find_free_port(app))
                elif "port" not in kwargs:
                    kwargs["port"] = port

                if "host" in kwargs:
                    host = kwargs["host"]
                elif "host" not in kwargs:
                    kwargs["host"] = host

                sentinel = (
                    # xxx
                    kwargs.get("sentinel") or environ.get("SENTINEL")
                    or ("" if nowait else create_sentinel_file(app)))

            argv = shlex.split(fmt.format(**kwargs))
            url = urlfmt.format(host=host, port=port)
            get_discovery().register(name, url=url)

            if app.registry.dry_run:
                logger.info("dry run, skip starting server process, %s", name)
                return

            from .spawn import spawn_with_connection

            p, _ = spawn_with_connection(argv,
                                         sentinel=sentinel,
                                         environ=environ,
                                         cwd=cwd,
                                         check=not nowait)

            def _shutdown() -> None:  # xxx:
                logger.info("terminate %s", name)
                with p:
                    p.terminate()

            atexit.register(_shutdown)

        app.action(("add_server_process", name), _register)
Ejemplo n.º 16
0
def includeme(app: App) -> None:
    app.include("egoist.directives.add_subcommand")
    app.include(".generate")
    app.add_subcommand(setup, fn=scan)
Ejemplo n.º 17
0
def includeme(app: App) -> None:
    app.include("egoist.components.fs")
Ejemplo n.º 18
0
def includeme(app: App) -> None:
    app.include("egoist.directives.add_subcommand")
    app.add_subcommand(setup, fn=hello)
Ejemplo n.º 19
0
from egoist.app import App, SettingsDict, parse_args

settings: SettingsDict = {"rootdir": "cmd/", "here": __file__}
app = App(settings)

app.include("egoist.commands.generate")
app.include("egoist.directives.define_cli")


@app.define_cli("egoist.generators.clikit:walk")
def hello(*, name: str) -> None:
    """hello message"""
    from egoist.generators.clikit import runtime, clikit

    with runtime.generate(clikit):
        runtime.printf("hello %s\n", name)


if __name__ == "__main__":
    for argv in parse_args(sep="-"):
        app.run(argv)
Ejemplo n.º 20
0
def includeme(app: App) -> None:
    app.include("./directives:add_server_process")  # for add_server_process()