From 507bf89d7392f1f986e77298554c616906005398 Mon Sep 17 00:00:00 2001 From: Shawn Bruce Date: Thu, 15 Jul 2021 10:29:37 -0400 Subject: [PATCH] Handle ImportError for unix specific imports. Only provide pty_command if import succeeds --- .../plugins/virtual_printer/__init__.py | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/octoprint/plugins/virtual_printer/__init__.py b/src/octoprint/plugins/virtual_printer/__init__.py index e1397ffca..198d31988 100644 --- a/src/octoprint/plugins/virtual_printer/__init__.py +++ b/src/octoprint/plugins/virtual_printer/__init__.py @@ -136,14 +136,11 @@ class VirtualPrinterPlugin( ] def cli_commands(self, cli_group, pass_octoprint_ctx, *args, **kwargs): - import fcntl import os - import pty import select import socket import stat import sys - import termios import threading import click @@ -344,40 +341,51 @@ class VirtualPrinterPlugin( ##~~ Unix PTY - @click.command("pty") - @click.option( - "--path", "path", type=click.Path(), default="/tmp/octoprint-virtual_printer" - ) - def pty_command(path): - if os.path.islink(path): - realpath = os.path.realpath(path) - if not os.path.exists(realpath): - os.unlink(path) - else: - print( - f"{path} already exists as a valid symlink. Unable to continue." - ) - sys.exit(1) - elif os.path.exists(path): - print(f"{path} already exists. Unable to continue.") - sys.exit(1) + try: + import fcntl + import pty + import termios + except ImportError: + pass + else: - click.echo(f"Creating Unix PTY on {path}...") - - mfd, sfd = pty.openpty() - sfd_path = os.ttyname(sfd) - os.symlink(sfd_path, path) - fcntl.fcntl( - mfd, fcntl.F_SETFL, fcntl.fcntl(mfd, fcntl.F_GETFL) | os.O_NONBLOCK + @click.command("pty") + @click.option( + "--path", + "path", + type=click.Path(), + default="/tmp/octoprint-virtual_printer", ) - attrs = termios.tcgetattr(mfd) - attrs[3] = attrs[3] & ~termios.ECHO - termios.tcsetattr(mfd, termios.TCSADRAIN, attrs) + def pty_command(path): + if os.path.islink(path): + realpath = os.path.realpath(path) + if not os.path.exists(realpath): + os.unlink(path) + else: + print( + f"{path} already exists as a valid symlink. Unable to continue." + ) + sys.exit(1) + elif os.path.exists(path): + print(f"{path} already exists. Unable to continue.") + sys.exit(1) - virtual = PTYVirtualPrinterWrapper(mfd, settings, datafolder) - virtual.wait() + click.echo(f"Creating Unix PTY on {path}...") - commands.append(pty_command) + mfd, sfd = pty.openpty() + sfd_path = os.ttyname(sfd) + os.symlink(sfd_path, path) + fcntl.fcntl( + mfd, fcntl.F_SETFL, fcntl.fcntl(mfd, fcntl.F_GETFL) | os.O_NONBLOCK + ) + attrs = termios.tcgetattr(mfd) + attrs[3] = attrs[3] & ~termios.ECHO + termios.tcsetattr(mfd, termios.TCSADRAIN, attrs) + + virtual = PTYVirtualPrinterWrapper(mfd, settings, datafolder) + virtual.wait() + + commands.append(pty_command) ##~~ Serial