Skip to content

Commit

Permalink
Fixing a duplicate firmware fetching class and makeing logging real
Browse files Browse the repository at this point in the history
  • Loading branch information
james townley committed Feb 24, 2016
1 parent bbc7cf4 commit a124606
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 51 deletions.
36 changes: 25 additions & 11 deletions src/firmware/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
import os
import sys
import logging

from firmware import MacFirmwareUpdater, LinuxFirmwareUpdater, WindowsFirmwareUpdater


def get_firmware_updater(logger=None, bootloader_idvendor=0x0483, bootloader_idproduct=0xdf11, peachy_idvendor=0x16d0, peachy_idproduct=0x0af3):
path = os.path.dirname(os.path.abspath(__file__))
logger = logging.getLogger('peachy')

def get_firmware_updater(bootloader_idvendor=0x0483, bootloader_idproduct=0xdf11, peachy_idvendor=0x16d0, peachy_idproduct=0x0af3):
logger.info("Firmware Flash Is Frozen: {}".format(str(getattr(sys, 'frozen', False))))
if 'darwin' in sys.platform:
dependancies_path = os.path.join(path, 'dependancies', 'mac')
return MacFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'mac')
if logger:
logger.info("Firmware Flash Dependancies Path: {}".format(dependancies_path))
return MacFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct)
elif 'win' in sys.platform:
dependancies_path = os.path.join(path, 'dependancies', 'windows')
return WindowsFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'windows')
return WindowsFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct)
elif 'linux' in sys.platform:
dependancies_path = os.path.join(path, 'dependancies', 'linux')
return LinuxFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'linux')
return LinuxFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct)
else:
if logger:
logger.error("Platform {} is unsupported for firmware updates".format(sys.platform))
raise Exception("Unsupported Platform")
logger.error("Platform {} is unsupported for firmware updates".format(sys.platform))
raise Exception("Unsupported Platform")
53 changes: 13 additions & 40 deletions src/firmware/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import os
import stat
from subprocess import Popen, PIPE
import logging

logger = logging.getLogger('peachy')

class FirmwareUpdater(object):
def __init__(self, dependancy_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger=None):
def __init__(self, dependancy_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct):
self._bootloader_idvendor = bootloader_idvendor
self._bootloader_idproduct = bootloader_idproduct
self._peachy_idvendor = peachy_idvendor
self._peachy_idproduct = peachy_idproduct
self._logger = logger

self.dependancy_path = dependancy_path

Expand All @@ -31,10 +32,9 @@ def list_usb_devices(self):
(out, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
if self._logger:
self._logger.error("Output: {}".format(out))
self._logger.error("Error: {}".format(err))
self._logger.error("Exit Code: {}".format(exit_code))
logger.error("Output: {}".format(out))
logger.error("Error: {}".format(err))
logger.error("Exit Code: {}".format(exit_code))
raise Exception("Command failed")
else:
peachys = out.count(self.peachy_usb_address)
Expand All @@ -48,8 +48,7 @@ def check_ready(self):
elif (bootloaders == 0) and (peachy_printers <= 1):
return False
else:
if self._logger:
self._logger.error("{0} peachy printers and {1} bootloaders found".format(peachy_printers, bootloaders))
logger.error("{0} peachy printers and {1} bootloaders found".format(peachy_printers, bootloaders))
raise Exception("{0} peachy printers and {1} bootloaders found".format(peachy_printers, bootloaders))

def update(self, firmware_path):
Expand Down Expand Up @@ -86,14 +85,14 @@ def update(self, firmware_path):
(out, err) = process.communicate()
exit_code = process.wait()
if exit_code != 0:
if self._logger:
self._logger.error("Output: {}".format(out))
self._logger.error("Error: {}".format(err))
self._logger.error("Exit Code: {}".format(exit_code))
logger.error("Output: {}".format(out))
logger.error("Error: {}".format(err))
logger.error("Exit Code: {}".format(exit_code))
return False
else:
return True


class MacFirmwareUpdater(LinuxFirmwareUpdater):

@property
Expand All @@ -107,7 +106,7 @@ def peachy_usb_address(self):
@property
def check_usb_command(self):
command = [os.path.join(self.dependancy_path, 'check_usb.sh')]
print command
logger.info("Check usb command: {}".format(command))
return [os.path.join(self.dependancy_path, 'check_usb.sh')]


Expand Down Expand Up @@ -176,30 +175,4 @@ def update(self, firmware_path):
raise Exception('Failed to switch driver')


def get_firmware_updater(logger=None, bootloader_idvendor=0x0483, bootloader_idproduct=0xdf11, peachy_idvendor=0x16d0, peachy_idproduct=0x0af3):
if logger:
logger.info("Firmware Flash Is Frozen: {}".format(str(getattr(sys, 'frozen', False))))
if 'darwin' in sys.platform:
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'mac')
if logger:
logger.info("Firmware Flash Dependancies Path: {}".format(dependancies_path))
return MacFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
elif 'win' in sys.platform:
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'windows')
return WindowsFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
elif 'linux' in sys.platform:
if getattr(sys, 'frozen', False):
dependancies_path = sys._MEIPASS
else:
dependancies_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dependancies', 'linux')
return LinuxFirmwareUpdater(dependancies_path, bootloader_idvendor, bootloader_idproduct, peachy_idvendor, peachy_idproduct, logger)
else:
if logger:
logger.error("Platform {} is unsupported for firmware updates".format(sys.platform))
raise Exception("Unsupported Platform")

0 comments on commit a124606

Please sign in to comment.