From 497e11b102e5b8e12941dceead2c50b8c1edea6d Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Fri, 10 Aug 2018 12:12:32 +0200 Subject: [PATCH] Refactor console scripts into entry points Change-Id: I521ab193bb65dc4d53e84d4f289194bff85b0685 --- pyghmi/cmd/__init__.py | 0 bin/fakebmc => pyghmi/cmd/fakebmc.py | 6 +- bin/pyghmicons => pyghmi/cmd/pyghmicons.py | 69 +++++++++++---------- bin/pyghmiutil => pyghmi/cmd/pyghmiutil.py | 71 ++++++++++++---------- bin/virshbmc => pyghmi/cmd/virshbmc.py | 6 +- setup.cfg | 7 +++ setup.py | 1 - tox.ini | 1 - 8 files changed, 94 insertions(+), 67 deletions(-) create mode 100644 pyghmi/cmd/__init__.py rename bin/fakebmc => pyghmi/cmd/fakebmc.py (98%) rename bin/pyghmicons => pyghmi/cmd/pyghmicons.py (53%) rename bin/pyghmiutil => pyghmi/cmd/pyghmiutil.py (64%) rename bin/virshbmc => pyghmi/cmd/virshbmc.py (99%) diff --git a/pyghmi/cmd/__init__.py b/pyghmi/cmd/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/bin/fakebmc b/pyghmi/cmd/fakebmc.py similarity index 98% rename from bin/fakebmc rename to pyghmi/cmd/fakebmc.py index e2b25ebc..14d1ff91 100755 --- a/bin/fakebmc +++ b/pyghmi/cmd/fakebmc.py @@ -77,7 +77,7 @@ class FakeBmc(bmc.Bmc): self.sol.send_data(data) -if __name__ == '__main__': +def main(): parser = argparse.ArgumentParser( prog='fakebmc', description='Pretend to be a BMC', @@ -90,3 +90,7 @@ if __name__ == '__main__': args = parser.parse_args() mybmc = FakeBmc({'admin': 'password'}, port=args.port) mybmc.listen() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/bin/pyghmicons b/pyghmi/cmd/pyghmicons.py similarity index 53% rename from bin/pyghmicons rename to pyghmi/cmd/pyghmicons.py index 41e37dc2..4383d830 100755 --- a/bin/pyghmicons +++ b/pyghmi/cmd/pyghmicons.py @@ -29,21 +29,8 @@ import tty from pyghmi.ipmi import console import threading -tcattr = termios.tcgetattr(sys.stdin) -newtcattr = tcattr -# TODO(jbjohnso): add our exit handler -newtcattr[-1][termios.VINTR] = 0 -newtcattr[-1][termios.VSUSP] = 0 -termios.tcsetattr(sys.stdin, termios.TCSADRAIN, newtcattr) -tty.setraw(sys.stdin.fileno()) -currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) -fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK) - -sol = None - - -def _doinput(): +def _doinput(sol): while True: select.select((sys.stdin,), (), (), 600) try: @@ -51,6 +38,8 @@ def _doinput(): except (IOError, OSError) as e: if e.errno == 11: continue + raise + sol.send_data(data) @@ -64,22 +53,40 @@ def _print(data): if bailout: raise Exception(data) -try: - if sys.argv[3] is None: - passwd = os.environ['IPMIPASSWORD'] - else: - passwd_file = sys.argv[3] - with open(passwd_file, "r") as f: - passwd = f.read() - sol = console.Console(bmc=sys.argv[1], userid=sys.argv[2], password=passwd, - iohandler=_print, force=True) - inputthread = threading.Thread(target=_doinput) - inputthread.daemon = True - inputthread.start() - sol.main_loop() -except Exception: +def main(): + tcattr = termios.tcgetattr(sys.stdin) + newtcattr = tcattr + # TODO(jbjohnso): add our exit handler + newtcattr[-1][termios.VINTR] = 0 + newtcattr[-1][termios.VSUSP] = 0 + termios.tcsetattr(sys.stdin, termios.TCSADRAIN, newtcattr) + + tty.setraw(sys.stdin.fileno()) currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) - fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK) - termios.tcsetattr(sys.stdin, termios.TCSANOW, tcattr) - sys.exit(0) + fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl | os.O_NONBLOCK) + + try: + if sys.argv[3] is None: + passwd = os.environ['IPMIPASSWORD'] + else: + passwd_file = sys.argv[3] + with open(passwd_file, "r") as f: + passwd = f.read() + + sol = console.Console(bmc=sys.argv[1], userid=sys.argv[2], password=passwd, + iohandler=_print, force=True) + inputthread = threading.Thread(target=_doinput, args=(sol,)) + inputthread.daemon = True + inputthread.start() + sol.main_loop() + + except Exception: + currfl = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) + fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, currfl ^ os.O_NONBLOCK) + termios.tcsetattr(sys.stdin, termios.TCSANOW, tcattr) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/bin/pyghmiutil b/pyghmi/cmd/pyghmiutil.py similarity index 64% rename from bin/pyghmiutil rename to pyghmi/cmd/pyghmiutil.py index 1410ffdd..1e527cac 100755 --- a/bin/pyghmiutil +++ b/pyghmi/cmd/pyghmiutil.py @@ -20,69 +20,76 @@ # it isn't conceived as a general utility to actually use, just help developers # understand how the ipmi_command class workes. # """ +import functools import os import string import sys from pyghmi.ipmi import command -if (len(sys.argv) < 3) or 'IPMIPASSWORD' not in os.environ: - print("Usage:") - print(" IPMIPASSWORD=password %s bmc username " - % sys.argv[0]) - sys.exit(1) -password = os.environ['IPMIPASSWORD'] -os.environ['IPMIPASSWORD'] = "" -bmc = sys.argv[1] -userid = sys.argv[2] -args = None -if len(sys.argv) >= 5: - args = sys.argv[4:] - -ipmicmd = None - - -def docommand(result, ipmisession): - cmmand = sys.argv[3] +def docommand(args, result, ipmisession): + command = args[0] + args = args[1:] print("Logged into %s" % ipmisession.bmc) if 'error' in result: print(result['error']) return - if cmmand == 'power': + if command == 'power': if args: print(ipmisession.set_power(args[0], wait=True)) else: value = ipmisession.get_power() print("%s: %s" % (ipmisession.bmc, value['powerstate'])) - elif cmmand == 'bootdev': + elif command == 'bootdev': if args: print(ipmisession.set_bootdev(args[0])) else: print(ipmisession.get_bootdev()) - elif cmmand == 'sensors': + elif command == 'sensors': for reading in ipmisession.get_sensor_data(): print(reading) - elif cmmand == 'health': + elif command == 'health': print(ipmisession.get_health()) - elif cmmand == 'inventory': + elif command == 'inventory': for item in ipmisession.get_inventory(): print(item) - elif cmmand == 'leds': + elif command == 'leds': for led in ipmisession.get_leds(): print(led) - elif cmmand == 'graphical': + elif command == 'graphical': print(ipmisession.get_graphical_console()) - elif cmmand == 'net': + elif command == 'net': print(ipmisession.get_net_configuration()) - elif cmmand == 'raw': + elif command == 'raw': print(ipmisession.raw_command( netfn=int(args[0]), command=int(args[1]), data=map(lambda x: int(x, 16), args[2:]))) -bmcs = string.split(bmc, ",") -for bmc in bmcs: - ipmicmd = command.Command(bmc=bmc, userid=userid, password=password, - onlogon=docommand) -ipmicmd.eventloop() + +def main(): + if (len(sys.argv) < 3) or 'IPMIPASSWORD' not in os.environ: + print("Usage:") + print(" IPMIPASSWORD=password %s bmc username " % sys.argv[0]) + return 1 + + password = os.environ['IPMIPASSWORD'] + os.environ['IPMIPASSWORD'] = "" + bmc = sys.argv[1] + userid = sys.argv[2] + + bmcs = string.split(bmc, ",") + ipmicmd = None + for bmc in bmcs: + # NOTE(etingof): is it right to have `ipmicmd` overridden? + ipmicmd = command.Command( + bmc=bmc, userid=userid, password=password, + onlogon=functools.partial(docommand, sys.argv[3:])) + + if ipmicmd: + ipmicmd.eventloop() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/bin/virshbmc b/pyghmi/cmd/virshbmc.py similarity index 99% rename from bin/virshbmc rename to pyghmi/cmd/virshbmc.py index fca2ffac..83e5d3bc 100755 --- a/bin/virshbmc +++ b/pyghmi/cmd/virshbmc.py @@ -127,7 +127,7 @@ class LibvirtBmc(bmc.Bmc): libvirt.virEventRunDefaultImpl() -if __name__ == '__main__': +def main(): parser = argparse.ArgumentParser( prog='virshbmc', description='Pretend to be a BMC and proxy to virsh', @@ -156,3 +156,7 @@ if __name__ == '__main__': domain=args.domain, port=args.port) mybmc.listen() + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/setup.cfg b/setup.cfg index e63df42d..ac9f1bcd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -28,3 +28,10 @@ packages = [global] setup-hooks = pbr.hooks.setup_hook + +[entry_points] +console_scripts = + pyghmicons = pyghmi.cmd.pyghmicons:main + pyghmiutil = pyghmi.cmd.pyghmiutil:main + virshbmc = pyghmi.cmd.virshbmc:main + fakebmc = pyghmi.cmd.fakebmc:main diff --git a/setup.py b/setup.py index a6dfecf5..f9dec207 100755 --- a/setup.py +++ b/setup.py @@ -20,6 +20,5 @@ import setuptools setuptools.setup( license='Apache License, Version 2.0', - scripts=['bin/pyghmicons', 'bin/pyghmiutil', 'bin/virshbmc'], setup_requires=['pbr'], pbr=True) diff --git a/tox.ini b/tox.ini index fc4f75ec..7e9fc47f 100644 --- a/tox.ini +++ b/tox.ini @@ -17,7 +17,6 @@ sitepackages = True [testenv:pep8] basepython = python3 whitelist_externals = bash -commands = bash -c 'pycodestyle pyghmi bin/*' [testenv:cover] basepython = python3