diff --git a/platformio/app.py b/platformio/app.py index cda86dc4..f7fcce2a 100644 --- a/platformio/app.py +++ b/platformio/app.py @@ -20,9 +20,8 @@ from time import time from lockfile import LockFile -from platformio import __version__ +from platformio import __version__, util from platformio.exception import InvalidSettingName, InvalidSettingValue -from platformio.util import get_home_dir, is_ci DEFAULT_SETTINGS = { "check_platformio_interval": { @@ -76,7 +75,7 @@ class State(object): self.path = path self.lock = lock if not self.path: - self.path = join(get_home_dir(), "appstate.json") + self.path = join(util.get_home_dir(), "appstate.json") self._state = {} self._prev_state = {} self._lockfile = None @@ -85,8 +84,7 @@ class State(object): try: self._lock_state_file() if isfile(self.path): - with open(self.path, "r") as fp: - self._state = json.load(fp) + self._state = util.load_json(self.path) except ValueError: self._state = {} self._prev_state = deepcopy(self._state) @@ -149,7 +147,7 @@ def get_setting(name): if name == "enable_prompts": # disable prompts for Continuous Integration systems # and when global "--force" option is set - if any([is_ci(), get_session_var("force_option")]): + if any([util.is_ci(), get_session_var("force_option")]): return False _env_name = "PLATFORMIO_SETTING_%s" % name.upper() diff --git a/platformio/commands/upgrade.py b/platformio/commands/upgrade.py index 894e8182..f8b48950 100644 --- a/platformio/commands/upgrade.py +++ b/platformio/commands/upgrade.py @@ -94,6 +94,9 @@ WARNING! Don't use `sudo` for the rest PlatformIO commands. else: raise exception.UpgradeError( "\n".join([str(cmd), r['out'], r['err']])) + finally: + if r: + r.close() def get_latest_version(): diff --git a/platformio/libmanager.py b/platformio/libmanager.py index 74e68c8f..01631b86 100644 --- a/platformio/libmanager.py +++ b/platformio/libmanager.py @@ -51,8 +51,7 @@ class LibraryManager(object): conf_path = join(self.lib_dir, dirname, self.CONFIG_NAME) if not isfile(conf_path): continue - with open(conf_path, "r") as f: - items[dirname] = json.load(f) + items[dirname] = util.load_json(conf_path) return items def get_latest_versions(self): diff --git a/platformio/util.py b/platformio/util.py index 8af2e859..579f44b8 100644 --- a/platformio/util.py +++ b/platformio/util.py @@ -124,6 +124,11 @@ def singleton(cls): return get_instance +def load_json(file_path): + with open(file_path, "r") as f: + return json.load(f) + + def get_systype(): data = uname() type_ = data[0].lower() @@ -343,21 +348,19 @@ def get_api_result(path, params=None, data=None, skipdns=False): try: if data: r = requests.post( - url + path, params=params, data=data, headers=headers, - timeout=(5, 13) - ) + url + path, params=params, data=data, headers=headers) else: - r = requests.get( - url + path, params=params, headers=headers, timeout=(5, 13)) - result = r.json() + r = requests.get(url + path, params=params, headers=headers) r.raise_for_status() + result = r.json() except requests.exceptions.HTTPError as e: if result and "errors" in result: raise exception.APIRequestError(result['errors'][0]['title']) else: raise exception.APIRequestError(e) except (requests.exceptions.ConnectionError, - requests.exceptions.ConnectTimeout): + requests.exceptions.ConnectTimeout, + requests.exceptions.ReadTimeout): if not skipdns: return get_api_result(path, params, data, skipdns=True) raise exception.APIRequestError( @@ -383,8 +386,7 @@ def _lookup_boards(): for json_file in sorted(os.listdir(bdir)): if not json_file.endswith(".json"): continue - with open(join(bdir, json_file)) as f: - boards.update(json.load(f)) + boards.update(load_json(join(bdir, json_file))) return boards