def _process_result(result, to_json=False): out, err, code = result if out and is_bytes(out): out = out.decode(get_locale_encoding()) if err and is_bytes(err): err = err.decode(get_locale_encoding()) text = ("%s\n\n%s" % (out, err)).strip() if code != 0: raise Exception(text) if not to_json: return text try: return json.loads(out) except ValueError as e: click.secho("%s => `%s`" % (e, out), fg="red", err=True) # if PIO Core prints unhandled warnings for line in out.split("\n"): line = line.strip() if not line: continue try: return json.loads(line) except ValueError: pass raise e
def read_safe_contents(self, path): error_reported = False for encoding in ( "utf-8", None, get_filesystem_encoding(), get_locale_encoding(), "latin-1", ): try: with io.open(path, encoding=encoding) as fp: contents = fp.read() self._safe_encoding = encoding return contents except UnicodeDecodeError: if not error_reported: error_reported = True click.secho( "Unicode decode error has occurred, please remove invalid " "(non-ASCII or non-UTF8) characters from %s file or convert it to UTF-8" % path, fg="yellow", err=True, ) return ""
def exec_command(*args, **kwargs): result = {"out": None, "err": None, "returncode": None} default = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE) default.update(kwargs) kwargs = default p = subprocess.Popen(*args, **kwargs) try: result["out"], result["err"] = p.communicate() result["returncode"] = p.returncode except KeyboardInterrupt: raise exception.AbortedByUser() finally: for s in ("stdout", "stderr"): if isinstance(kwargs[s], AsyncPipeBase): kwargs[s].close() for s in ("stdout", "stderr"): if isinstance(kwargs[s], AsyncPipeBase): result[s[3:]] = kwargs[s].get_buffer() for k, v in result.items(): if PY2 and isinstance(v, unicode): # pylint: disable=undefined-variable result[k] = v.encode() elif not PY2 and isinstance(result[k], bytes): try: result[k] = result[k].decode(get_locale_encoding() or get_filesystem_encoding()) except UnicodeDecodeError: result[k] = result[k].decode("latin-1") if v and isinstance(v, string_types): result[k] = result[k].strip() return result
def system_info(json_output): project_config = ProjectConfig() data = {} data["core_version"] = {"title": "PlatformIO Core", "value": __version__} data["python_version"] = { "title": "Python", "value": "{0}.{1}.{2}-{3}.{4}".format(*list(sys.version_info)), } data["system"] = {"title": "System Type", "value": util.get_systype()} data["platform"] = { "title": "Platform", "value": platform.platform(terse=True) } data["filesystem_encoding"] = { "title": "File System Encoding", "value": compat.get_filesystem_encoding(), } data["locale_encoding"] = { "title": "Locale Encoding", "value": compat.get_locale_encoding(), } data["core_dir"] = { "title": "PlatformIO Core Directory", "value": project_config.get_optional_dir("core"), } data["platformio_exe"] = { "title": "PlatformIO Core Executable", "value": proc.where_is_program( "platformio.exe" if proc.WINDOWS else "platformio"), } data["python_exe"] = { "title": "Python Executable", "value": proc.get_pythonexe_path(), } data["global_lib_nums"] = { "title": "Global Libraries", "value": len(LibraryPackageManager().get_installed()), } data["dev_platform_nums"] = { "title": "Development Platforms", "value": len(PlatformPackageManager().get_installed()), } data["package_tool_nums"] = { "title": "Tools & Toolchains", "value": len( ToolPackageManager( project_config.get_optional_dir("packages")).get_installed()), } click.echo( json.dumps(data) if json_output else tabulate([( item["title"], item["value"]) for item in data.values()]))