Speedup PIO Home via internal calling of PIO Core CLI

This commit is contained in:
Ivan Kravets
2019-07-01 15:55:42 +03:00
parent 500b3e08fe
commit dfca7f0b68
8 changed files with 41 additions and 31 deletions

View File

@ -119,8 +119,7 @@ An unexpected error occurred. Further steps:
def debug_gdb_main(): def debug_gdb_main():
sys.argv = [sys.argv[0], "debug", "--interface", "gdb"] + sys.argv[1:] return main([sys.argv[0], "debug", "--interface", "gdb"] + sys.argv[1:])
return main()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -123,7 +123,7 @@ def cli(ctx, project_dir, project_conf, environment, verbose, interface,
if helpers.is_mi_mode(__unprocessed): if helpers.is_mi_mode(__unprocessed):
click.echo('~"Preparing firmware for debugging...\\n"') click.echo('~"Preparing firmware for debugging...\\n"')
output = helpers.GDBBytesIO() output = helpers.GDBBytesIO()
with helpers.capture_std_streams(output): with util.capture_std_streams(output):
helpers.predebug_project(ctx, project_dir, env_name, preload, helpers.predebug_project(ctx, project_dir, env_name, preload,
verbose) verbose)
output.close() output.close()

View File

@ -14,7 +14,6 @@
import sys import sys
import time import time
from contextlib import contextmanager
from fnmatch import fnmatch from fnmatch import fnmatch
from hashlib import sha1 from hashlib import sha1
from io import BytesIO from io import BytesIO
@ -41,17 +40,6 @@ class GDBBytesIO(BytesIO): # pylint: disable=too-few-public-methods
self.STDOUT.flush() self.STDOUT.flush()
@contextmanager
def capture_std_streams(stdout, stderr=None):
_stdout = sys.stdout
_stderr = sys.stderr
sys.stdout = stdout
sys.stderr = stderr or stdout
yield
sys.stdout = _stdout
sys.stderr = _stderr
def is_mi_mode(args): def is_mi_mode(args):
return "--interpreter" in " ".join(args) return "--interpreter" in " ".join(args)

View File

@ -14,6 +14,7 @@
from __future__ import absolute_import from __future__ import absolute_import
import codecs
import glob import glob
import os import os
import shutil import shutil
@ -67,10 +68,10 @@ class OSRPC(object):
def request_content(self, uri, data=None, headers=None, cache_valid=None): def request_content(self, uri, data=None, headers=None, cache_valid=None):
if uri.startswith('http'): if uri.startswith('http'):
return self.fetch_content(uri, data, headers, cache_valid) return self.fetch_content(uri, data, headers, cache_valid)
if isfile(uri): if not isfile(uri):
with open(uri) as fp: return None
return fp.read() with codecs.open(uri, encoding="utf-8") as fp:
return None return fp.read()
@staticmethod @staticmethod
def open_url(url): def open_url(url):

View File

@ -17,12 +17,12 @@ from __future__ import absolute_import
import json import json
import os import os
import re import re
from io import BytesIO
import jsonrpc # pylint: disable=import-error import jsonrpc # pylint: disable=import-error
from twisted.internet import utils # pylint: disable=import-error from twisted.internet import threads # pylint: disable=import-error
from platformio import __version__ from platformio import __main__, __version__, util
from platformio.commands.home import helpers
from platformio.compat import string_types from platformio.compat import string_types
@ -30,7 +30,6 @@ class PIOCoreRPC(object):
@staticmethod @staticmethod
def call(args, options=None): def call(args, options=None):
json_output = "--json-output" in args
try: try:
args = [ args = [
str(arg) if not isinstance(arg, string_types) else arg str(arg) if not isinstance(arg, string_types) else arg
@ -39,13 +38,20 @@ class PIOCoreRPC(object):
except UnicodeError: except UnicodeError:
raise jsonrpc.exceptions.JSONRPCDispatchException( raise jsonrpc.exceptions.JSONRPCDispatchException(
code=4002, message="PIO Core: non-ASCII chars in arguments") code=4002, message="PIO Core: non-ASCII chars in arguments")
d = utils.getProcessOutputAndValue(
helpers.get_core_fullpath(), def _call_cli():
args, outbuff = BytesIO()
path=(options or {}).get("cwd"), errbuff = BytesIO()
env={k: v with util.capture_std_streams(outbuff, errbuff):
for k, v in os.environ.items() if "%" not in k}) with util.cd((options or {}).get("cwd") or os.getcwd()):
d.addCallback(PIOCoreRPC._call_callback, json_output) exit_code = __main__.main(["-c"] + args)
result = (outbuff.getvalue(), errbuff.getvalue(), exit_code)
outbuff.close()
errbuff.close()
return result
d = threads.deferToThread(_call_cli)
d.addCallback(PIOCoreRPC._call_callback, "--json-output" in args)
d.addErrback(PIOCoreRPC._call_errback) d.addErrback(PIOCoreRPC._call_errback)
return d return d

View File

@ -182,7 +182,7 @@ def platform_frameworks(query, json_output):
for framework in util.get_api_result("/frameworks", cache_valid="7d"): for framework in util.get_api_result("/frameworks", cache_valid="7d"):
if query == "all": if query == "all":
query = "" query = ""
search_data = dump_json_to_unicode(framework) search_data = framework
if query and query.lower() not in search_data.lower(): if query and query.lower() not in search_data.lower():
continue continue
framework['homepage'] = ("https://platformio.org/frameworks/" + framework['homepage'] = ("https://platformio.org/frameworks/" +

View File

@ -54,6 +54,8 @@ if PY2:
return data return data
def dump_json_to_unicode(obj): def dump_json_to_unicode(obj):
if isinstance(obj, unicode):
return obj
return json.dumps(obj, return json.dumps(obj,
encoding=get_filesystem_encoding(), encoding=get_filesystem_encoding(),
ensure_ascii=False).encode("utf8") ensure_ascii=False).encode("utf8")
@ -100,4 +102,6 @@ else:
return data.encode() return data.encode()
def dump_json_to_unicode(obj): def dump_json_to_unicode(obj):
if isinstance(obj, string_types):
return obj
return json.dumps(obj, ensure_ascii=False) return json.dumps(obj, ensure_ascii=False)

View File

@ -20,6 +20,7 @@ import socket
import stat import stat
import sys import sys
import time import time
from contextlib import contextmanager
from functools import wraps from functools import wraps
from glob import glob from glob import glob
from os.path import abspath, basename, dirname, isfile, join from os.path import abspath, basename, dirname, isfile, join
@ -107,6 +108,17 @@ def singleton(cls):
return get_instance return get_instance
@contextmanager
def capture_std_streams(stdout, stderr=None):
_stdout = sys.stdout
_stderr = sys.stderr
sys.stdout = stdout
sys.stderr = stderr or stdout
yield
sys.stdout = _stdout
sys.stderr = _stderr
def load_json(file_path): def load_json(file_path):
try: try:
with open(file_path, "r") as f: with open(file_path, "r") as f: