Example #1
0
 def init(self, board, framework, project_dir):
     assert project_dir
     state = AppRPC.load_state()
     if not isdir(project_dir):
         os.makedirs(project_dir)
     args = ["init", "--board", board]
     if framework:
         args.extend(["--project-option", "framework = %s" % framework])
     if (state['storage']['coreCaller'] and state['storage']['coreCaller']
             in ProjectGenerator.get_supported_ides()):
         args.extend(["--ide", state['storage']['coreCaller']])
     d = PIOCoreRPC.call(args, options={"cwd": project_dir})
     d.addCallback(self._generate_project_main, project_dir, framework)
     return d
Example #2
0
    def import_pio(project_dir):
        if not project_dir or not is_platformio_project(project_dir):
            raise jsonrpc.exceptions.JSONRPCDispatchException(
                code=4001,
                message="Not an PlatformIO project: %s" % project_dir)
        new_project_dir = join(
            AppRPC.load_state()['storage']['projectsDir'],
            time.strftime("%y%m%d-%H%M%S-") + basename(project_dir))
        shutil.copytree(project_dir, new_project_dir)

        state = AppRPC.load_state()
        args = ["init"]
        if (state['storage']['coreCaller'] and state['storage']['coreCaller']
                in ProjectGenerator.get_supported_ides()):
            args.extend(["--ide", state['storage']['coreCaller']])
        d = PIOCoreRPC.call(args, options={"cwd": new_project_dir})
        d.addCallback(lambda _: new_project_dir)
        return d
Example #3
0
    def import_arduino(self, board, use_arduino_libs, arduino_project_dir):
        board = str(board)
        if arduino_project_dir and PY2:
            arduino_project_dir = arduino_project_dir.encode(
                get_filesystem_encoding())
        # don't import PIO Project
        if is_platformio_project(arduino_project_dir):
            return arduino_project_dir

        is_arduino_project = any([
            os.path.isfile(
                os.path.join(
                    arduino_project_dir,
                    "%s.%s" % (os.path.basename(arduino_project_dir), ext),
                )) for ext in ("ino", "pde")
        ])
        if not is_arduino_project:
            raise jsonrpc.exceptions.JSONRPCDispatchException(
                code=4000,
                message="Not an Arduino project: %s" % arduino_project_dir)

        state = AppRPC.load_state()
        project_dir = os.path.join(state["storage"]["projectsDir"],
                                   time.strftime("%y%m%d-%H%M%S-") + board)
        if not os.path.isdir(project_dir):
            os.makedirs(project_dir)
        args = ["init", "--board", board]
        args.extend(["--project-option", "framework = arduino"])
        if use_arduino_libs:
            args.extend([
                "--project-option",
                "lib_extra_dirs = ~/Documents/Arduino/libraries"
            ])
        if (state["storage"]["coreCaller"] and state["storage"]["coreCaller"]
                in ProjectGenerator.get_supported_ides()):
            args.extend(["--ide", state["storage"]["coreCaller"]])
        d = PIOCoreRPC.call(args,
                            options={
                                "cwd": project_dir,
                                "force_subprocess": True
                            })
        d.addCallback(self._finalize_arduino_import, project_dir,
                      arduino_project_dir)
        return d
Example #4
0
def run_server(host, port, no_open, shutdown_timeout, home_url):
    contrib_dir = get_core_package_dir("contrib-piohome")
    if not os.path.isdir(contrib_dir):
        raise PlatformioException("Invalid path to PIO Home Contrib")

    ws_rpc_factory = WebSocketJSONRPCServerFactory(shutdown_timeout)
    ws_rpc_factory.addHandler(AccountRPC(), namespace="account")
    ws_rpc_factory.addHandler(AppRPC(), namespace="app")
    ws_rpc_factory.addHandler(IDERPC(), namespace="ide")
    ws_rpc_factory.addHandler(MiscRPC(), namespace="misc")
    ws_rpc_factory.addHandler(OSRPC(), namespace="os")
    ws_rpc_factory.addHandler(PIOCoreRPC(), namespace="core")
    ws_rpc_factory.addHandler(ProjectRPC(), namespace="project")

    path = urlparse(home_url).path
    routes = [
        WebSocketRoute(path + "wsrpc", ws_rpc_factory, name="wsrpc"),
        Route(path + "__shutdown__", shutdown_server, methods=["POST"]),
        Mount(path,
              StaticFiles(directory=contrib_dir, html=True),
              name="static"),
    ]
    if path != "/":
        routes.append(Route("/", protected_page))

    uvicorn.run(
        Starlette(
            middleware=[Middleware(ShutdownMiddleware)],
            routes=routes,
            on_startup=[
                lambda: click.echo(
                    "PIO Home has been started. Press Ctrl+C to shutdown."),
                lambda: None if no_open else click.launch(home_url),
            ],
        ),
        host=host,
        port=port,
        log_level="warning",
    )
Example #5
0
    def import_pio(project_dir):
        if not project_dir or not is_platformio_project(project_dir):
            raise jsonrpc.exceptions.JSONRPCDispatchException(
                code=4001,
                message="Not an PlatformIO project: %s" % project_dir)
        new_project_dir = os.path.join(
            AppRPC.load_state()["storage"]["projectsDir"],
            time.strftime("%y%m%d-%H%M%S-") + os.path.basename(project_dir),
        )
        shutil.copytree(project_dir, new_project_dir, symlinks=True)

        state = AppRPC.load_state()
        args = ["init"]
        if (state["storage"]["coreCaller"] and state["storage"]["coreCaller"]
                in ProjectGenerator.get_supported_ides()):
            args.extend(["--ide", state["storage"]["coreCaller"]])
        d = PIOCoreRPC.call(args,
                            options={
                                "cwd": new_project_dir,
                                "force_subprocess": True
                            })
        d.addCallback(lambda _: new_project_dir)
        return d
Example #6
0
def cli(port, host, no_open, shutdown_timeout):
    # pylint: disable=import-error, import-outside-toplevel

    # import contrib modules
    inject_contrib_pysite()
    from autobahn.twisted.resource import WebSocketResource
    from twisted.internet import reactor
    from twisted.web import server

    from platformio.commands.home.rpc.handlers.app import AppRPC
    from platformio.commands.home.rpc.handlers.ide import IDERPC
    from platformio.commands.home.rpc.handlers.misc import MiscRPC
    from platformio.commands.home.rpc.handlers.os import OSRPC
    from platformio.commands.home.rpc.handlers.piocore import PIOCoreRPC
    from platformio.commands.home.rpc.handlers.project import ProjectRPC
    from platformio.commands.home.rpc.server import JSONRPCServerFactory
    from platformio.commands.home.web import WebRoot

    factory = JSONRPCServerFactory(shutdown_timeout)
    factory.addHandler(AppRPC(), namespace="app")
    factory.addHandler(IDERPC(), namespace="ide")
    factory.addHandler(MiscRPC(), namespace="misc")
    factory.addHandler(OSRPC(), namespace="os")
    factory.addHandler(PIOCoreRPC(), namespace="core")
    factory.addHandler(ProjectRPC(), namespace="project")

    contrib_dir = get_core_package_dir("contrib-piohome")
    if not isdir(contrib_dir):
        raise exception.PlatformioException("Invalid path to PIO Home Contrib")

    # Ensure PIO Home mimetypes are known
    mimetypes.add_type("text/html", ".html")
    mimetypes.add_type("text/css", ".css")
    mimetypes.add_type("application/javascript", ".js")

    root = WebRoot(contrib_dir)
    root.putChild(b"wsrpc", WebSocketResource(factory))
    site = server.Site(root)

    # hook for `platformio-node-helpers`
    if host == "__do_not_start__":
        return

    # if already started
    already_started = False
    socket.setdefaulttimeout(1)
    try:
        socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
        already_started = True
    except:  # pylint: disable=bare-except
        pass

    home_url = "http://%s:%d" % (host, port)
    if not no_open:
        if already_started:
            click.launch(home_url)
        else:
            reactor.callLater(1, lambda: click.launch(home_url))

    click.echo("\n".join([
        "",
        "  ___I_",
        " /\\-_--\\   PlatformIO Home",
        "/  \\_-__\\",
        "|[]| [] |  %s" % home_url,
        "|__|____|______________%s" % ("_" * len(host)),
    ]))
    click.echo("")
    click.echo("Open PIO Home in your browser by this URL => %s" % home_url)

    if already_started:
        return

    click.echo("PIO Home has been started. Press Ctrl+C to shutdown.")

    reactor.listenTCP(port, site, interface=host)
    reactor.run()