Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
check for multiple connected supported printers
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Jan 14, 2020
1 parent 7fd001e commit 2afd493
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions securedrop_export/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ExportStatus(Enum):
USB_DISK_ERROR = 'USB_DISK_ERROR'

# Printer preflight related errors
ERROR_MULTIPLE_PRINTERS_FOUND = 'ERROR_MULTIPLE_PRINTERS_FOUND'
ERROR_PRINTER_NOT_FOUND = 'ERROR_PRINTER_NOT_FOUND'
ERROR_PRINTER_NOT_SUPPORTED = 'ERROR_PRINTER_NOT_SUPPORTED'
ERROR_PRINTER_DRIVER_UNAVAILABLE = 'ERROR_PRINTER_DRIVER_UNAVAILABLE'
Expand Down
26 changes: 26 additions & 0 deletions securedrop_export/print/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ def wait_for_print(self):
self.submission.exit_gracefully(ExportStatus.ERROR_PRINT.value)
return True

def check_printer_setup(self) -> None:
try:
output = subprocess.check_output(["sudo", "lpinfo", "-v"])
printers = [x for x in output.decode('utf-8').split() if "usb://" in x]
if not printers:
logger.info('No usb printers connected')
self.exit_gracefully(ExportStatus.ERROR_PRINTER_NOT_FOUND.value)

supported_printers = [x for x in printers if x in ("Brother", "LaserJet")]
if not supported_printers:
logger.info('{} are unsupported printers'.format(printers))
self.exit_gracefully(ExportStatus.ERROR_PRINTER_NOT_SUPPORTED.value)

if len(supported_printers) > 1:
logger.info('Too many usb printers connected')
self.exit_gracefully(ExportStatus.ERROR_MULTIPLE_PRINTERS_FOUND.value)

printer_uri = printers[0]
printer_ppd = self._install_printer_ppd(printer_uri)
self._setup_printer(printer_uri, printer_ppd)
except subprocess.CalledProcessError:
self.submission.exit_gracefully(ExportStatus.ERROR_GENERIC.value)

def get_printer_uri(self):
# Get the URI via lpinfo and only accept URIs of supported printers
printer_uri = ""
Expand Down Expand Up @@ -204,6 +227,7 @@ def __init__(self, *args, **kwargs):

def run(self):
logger.info('Export archive is printer')
self.check_printer_setup()
# prints all documents in the archive
logger.info('Searching for printer')
printer_uri = self.get_printer_uri()
Expand All @@ -220,6 +244,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def run(self):
logger.info('Export archive is printer-test')
self.check_printer_setup()
# Prints a test page to ensure the printer is functional
printer_uri = self.get_printer_uri()
printer_ppd = self.install_printer_ppd(printer_uri)
Expand Down

0 comments on commit 2afd493

Please sign in to comment.