From 31270a0286a216141f54d1ea620957477e91a40d Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 13 Dec 2023 18:17:36 -0600 Subject: [PATCH] is_deice_present() for WebBackend. pylint fixes. equalize behavior when device is not present. --- circup/__init__.py | 10 ++++++---- circup/backends.py | 42 ++++++++++++++++++++++++++++-------------- circup/shared.py | 1 + 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/circup/__init__.py b/circup/__init__.py index 714adf4..157b729 100644 --- a/circup/__init__.py +++ b/circup/__init__.py @@ -954,7 +954,7 @@ def libraries_from_code_py(code_py, mod_names): try: found_imports = findimports.find_imports(code_py) except Exception as ex: # broad exception because anything could go wrong - self.logger.exception(ex) + logger.exception(ex) click.secho('Unable to read the auto file: "{}"'.format(str(ex)), fg="red") sys.exit(2) # pylint: enable=broad-except @@ -1017,9 +1017,11 @@ def main(ctx, verbose, path, host, password, board_id, cpy_version): # pragma: if using_webworkflow: ctx.obj["backend"] = WebBackend(host=host, password=password, logger=logger) else: - ctx.obj["backend"] = DiskBackend(device_path, logger) + try: + ctx.obj["backend"] = DiskBackend(device_path, logger) + except ValueError as e: + print(e) - print(f'device is present ? {ctx.obj["backend"].is_device_present()}') if verbose: # Configure additional logging to stdout. global VERBOSE @@ -1046,7 +1048,7 @@ def main(ctx, verbose, path, host, password, board_id, cpy_version): # pragma: "https://github.com/adafruit/circuitpython/releases/latest" ) global CPY_VERSION - if device_path is None: + if device_path is None or not ctx.obj["backend"].is_device_present(): click.secho("Could not find a connected CircuitPython device.", fg="red") sys.exit(1) else: diff --git a/circup/backends.py b/circup/backends.py index 443b264..4b4c51a 100644 --- a/circup/backends.py +++ b/circup/backends.py @@ -10,9 +10,7 @@ import sys import tempfile from urllib.parse import urlparse - import click -import findimports import requests from requests.auth import HTTPBasicAuth @@ -133,11 +131,11 @@ def install_module( else: click.echo("Unknown module named, '{}'.".format(name)) - def libraries_from_imports(self, code_py, mod_names): - """ - To be overridden by subclass - """ - raise NotImplementedError + # def libraries_from_imports(self, code_py, mod_names): + # """ + # To be overridden by subclass + # """ + # raise NotImplementedError def uninstall(self, device_path, module_path): """ @@ -399,14 +397,16 @@ def _install_module_py(self, metadata): source_path = metadata["path"] # Path to Python source version. if os.path.isdir(source_path): - target = os.path.basename(os.path.dirname(source_path)) self.install_dir_http(source_path) else: - target = os.path.basename(source_path) self.install_file_http(source_path) def get_auto_file_path(self, auto_file_path): + """ + Make a local temp copy of the --auto file from the device. + Returns the path to the local copy. + """ url = auto_file_path auth = HTTPBasicAuth("", self.password) r = requests.get(url, auth=auth) @@ -454,6 +454,16 @@ def get_file_path(self, filename): """ return os.path.join(self.device_location, "fs", filename) + def is_device_present(self): + """ + returns True if the device is currently connected + """ + try: + _ = self.get_device_versions() + return True + except requests.exceptions.ConnectionError: + return False + class DiskBackend(Backend): """ @@ -463,7 +473,8 @@ class DiskBackend(Backend): def __init__(self, device_location, logger, version_info=None): if device_location is None: raise ValueError( - "Auto locating USB Disk based device failed. Please specify --path argument or ensure your device " + "Auto locating USB Disk based device failed. " + "Please specify --path argument or ensure your device " "is connected and mounted under the name CIRCUITPY." ) super().__init__(logger) @@ -510,8 +521,8 @@ def get_circuitpython_version(self): sys.exit(1) return circuit_python, board_id - else: - return self.version_info + + return self.version_info def _get_modules(self, device_lib_path): """ @@ -573,6 +584,9 @@ def _install_module_py(self, metadata): shutil.copyfile(source_path, target_path) def get_auto_file_path(self, auto_file_path): + """ + Returns the path on the device to the file to be read for --auto. + """ return auto_file_path def uninstall(self, device_path, module_path): @@ -615,12 +629,12 @@ def _update_file(self, module): def get_file_path(self, filename): """ - retuns the full path on the device to a given file name. + returns the full path on the device to a given file name. """ return os.path.join(self.device_location, filename) def is_device_present(self): """ - To be overriden by subclass + returns True if the device is currently connected """ return os.path.exists(self.device_location) diff --git a/circup/shared.py b/circup/shared.py index 786c0ca..0b37a23 100644 --- a/circup/shared.py +++ b/circup/shared.py @@ -79,6 +79,7 @@ def extract_metadata(path, logger): :param str path: The path to the file containing the metadata. :return: The dunder based metadata found in the file, as a dictionary. """ + # pylint: disable=too-many-locals result = {} logger.info("%s", path) if path.endswith(".py"):