Example #1
0
def run_platformio(*cmd, **kwargs):
    def mock_exit(return_code):
        raise SystemExit(return_code)

    orig_argv = sys.argv
    orig_exit = sys.exit  # mock sys.exit
    full_cmd = u' '.join(quote(x) for x in cmd)
    _LOGGER.info(u"Running:  %s", full_cmd)
    try:
        func = kwargs.get('main')
        if func is None:
            import platformio.__main__
            func = platformio.__main__.main
        sys.argv = list(cmd)
        sys.exit = mock_exit
        return func() or 0
    except KeyboardInterrupt:
        return 1
    except SystemExit as err:
        return err.args[0]
    except Exception as err:  # pylint: disable=broad-except
        _LOGGER.error(u"Running platformio failed: %s", err)
        _LOGGER.error(u"Please try running %s locally.", full_cmd)
    finally:
        sys.argv = orig_argv
        sys.exit = orig_exit
Example #2
0
 def on_message(self, message):
     if self.proc is not None:
         return
     command = self.build_command(message)
     _LOGGER.debug(u"WebSocket opened for command %s", [quote(x) for x in command])
     self.proc = tornado.process.Subprocess(command,
                                            stdout=tornado.process.Subprocess.STREAM,
                                            stderr=subprocess.STDOUT)
     self.proc.set_exit_callback(self.proc_on_exit)
     tornado.ioloop.IOLoop.current().spawn_callback(self.redirect_stream)