From 2113f834a3f6622ef99a3bb91c779e5d00ef2b29 Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 20 Jul 2017 21:01:16 +0800 Subject: [PATCH] Rework the launcher to always call the installed copy of PyLink This should prevent cryptic errors caused by mismatched PyLink core/launcher versions. --- launcher.py | 89 +++++++++++++++++++++++++++++++++++++++++++++ pylink | 101 +++++----------------------------------------------- setup.py | 6 +++- 3 files changed, 102 insertions(+), 94 deletions(-) create mode 100644 launcher.py diff --git a/launcher.py b/launcher.py new file mode 100644 index 0000000..f4ed8d2 --- /dev/null +++ b/launcher.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +""" +PyLink IRC Services launcher. +""" + +import os +import sys +from pylinkirc import world, conf, __version__, real_version + +def main(): + import argparse + + parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.') + parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml') + parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true') + parser.add_argument("-c", "--check-pid", help="enables PID file checking", action='store_true') + parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true') + args = parser.parse_args() + + if args.version: # Display version and exit + print('PyLink %s (in VCS: %s)' % (__version__, real_version)) + sys.exit() + + # FIXME: we can't pass logging on to conf until we set up the config... + conf.loadConf(args.config) + + from pylinkirc.log import log + from pylinkirc import classes, utils, coremods + log.info('PyLink %s starting...', __version__) + + # Set terminal window title. See https://bbs.archlinux.org/viewtopic.php?id=85567 + # and https://stackoverflow.com/questions/7387276/ + if os.name == 'nt': + import ctypes + ctypes.windll.kernel32.SetConsoleTitleW("PyLink %s" % __version__) + elif os.name == 'posix': + sys.stdout.write("\x1b]2;PyLink %s\x07" % __version__) + + # Check for a pid file. This stops instance overlap and user mistakes, which are especially an + # issue for clientbot... + if args.check_pid: + config = conf.confname + if os.path.exists("%s.pid" % config): + log.error("PID file exists; aborting! If PyLink didn't shut down cleanly last time it " + "was run, or you're upgrading from PyLink < 1.1-dev, delete %s.pid and try " + "again." % config) + sys.exit(1) + + # Write a PID file unless specifically told not to. + if not args.no_pid: + with open('%s.pid' % conf.confname, 'w') as f: + f.write(str(os.getpid())) + + # Import plugins first globally, because they can listen for events + # that happen before the connection phase. + to_load = conf.conf['plugins'] + utils.resetModuleDirs() + # Here, we override the module lookup and import the plugins + # dynamically depending on which were configured. + for plugin in to_load: + try: + world.plugins[plugin] = pl = utils.loadPlugin(plugin) + except Exception as e: + log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e)) + else: + if hasattr(pl, 'main'): + log.debug('Calling main() function of plugin %r', pl) + pl.main() + + # Initialize all the networks one by one + for network, sdata in conf.conf['servers'].items(): + + try: + protoname = sdata['protocol'] + except (KeyError, TypeError): + log.error("(%s) Configuration error: No protocol module specified, aborting.", network) + else: + # Fetch the correct protocol module. + proto = utils.getProtocolModule(protoname) + + # Create and connect the network. + world.networkobjects[network] = irc = proto.Class(network) + log.debug('Connecting to network %r', network) + irc.connect() + + world.started.set() + log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys()))) + + coremods.permissions.resetPermissions() # XXX we should probably move this to run on import diff --git a/pylink b/pylink index 30a48f1..f6e3b88 100755 --- a/pylink +++ b/pylink @@ -1,98 +1,13 @@ #!/usr/bin/env python3 -""" -PyLink IRC Services launcher. -""" - -import os import sys try: - from pylinkirc import world + from pylinkirc import launcher except ImportError: - sys.stderr.write("ERROR: Failed to import PyLink main module (pylinkirc.world).\n\nIf you are " - "running PyLink from source, please install PyLink first using 'python3 " - "setup.py install' (global install) or 'python3 setup.py install --user'" - " (local install)\n") + print("ERROR: Failed to import PyLink launcher module (pylinkirc.launcher).\n\nIf you are " + "running PyLink from source, please install PyLink first using 'python3 " + "setup.py install' (global install) or 'python3 setup.py install --user'" + " (local install)\n") sys.exit(1) -from pylinkirc import conf, __version__, real_version - -if __name__ == '__main__': - import argparse - - parser = argparse.ArgumentParser(description='Starts an instance of PyLink IRC Services.') - parser.add_argument('config', help='specifies the path to the config file (defaults to pylink.yml)', nargs='?', default='pylink.yml') - parser.add_argument("-v", "--version", help="displays the program version and exits", action='store_true') - parser.add_argument("-c", "--check-pid", help="enables PID file checking", action='store_true') - parser.add_argument("-n", "--no-pid", help="skips generating PID files", action='store_true') - args = parser.parse_args() - - if args.version: # Display version and exit - print('PyLink %s (in VCS: %s)' % (__version__, real_version)) - sys.exit() - - # FIXME: we can't pass logging on to conf until we set up the config... - conf.loadConf(args.config) - - from pylinkirc.log import log - from pylinkirc import classes, utils, coremods - log.info('PyLink %s starting...', __version__) - - # Set terminal window title. See https://bbs.archlinux.org/viewtopic.php?id=85567 - # and https://stackoverflow.com/questions/7387276/ - if os.name == 'nt': - import ctypes - ctypes.windll.kernel32.SetConsoleTitleW("PyLink %s" % __version__) - elif os.name == 'posix': - sys.stdout.write("\x1b]2;PyLink %s\x07" % __version__) - - # Check for a pid file. This stops instance overlap and user mistakes, which are especially an - # issue for clientbot... - if args.check_pid: - config = conf.confname - if os.path.exists("%s.pid" % config): - log.error("PID file exists; aborting! If PyLink didn't shut down cleanly last time it " - "was run, or you're upgrading from PyLink < 1.1-dev, delete %s.pid and try " - "again." % config) - sys.exit(1) - - # Write a PID file unless specifically told not to. - if not args.no_pid: - with open('%s.pid' % conf.confname, 'w') as f: - f.write(str(os.getpid())) - - # Import plugins first globally, because they can listen for events - # that happen before the connection phase. - to_load = conf.conf['plugins'] - utils.resetModuleDirs() - # Here, we override the module lookup and import the plugins - # dynamically depending on which were configured. - for plugin in to_load: - try: - world.plugins[plugin] = pl = utils.loadPlugin(plugin) - except Exception as e: - log.exception('Failed to load plugin %r: %s: %s', plugin, type(e).__name__, str(e)) - else: - if hasattr(pl, 'main'): - log.debug('Calling main() function of plugin %r', pl) - pl.main() - - # Initialize all the networks one by one - for network, sdata in conf.conf['servers'].items(): - - try: - protoname = sdata['protocol'] - except (KeyError, TypeError): - log.error("(%s) Configuration error: No protocol module specified, aborting.", network) - else: - # Fetch the correct protocol module. - proto = utils.getProtocolModule(protoname) - - # Create and connect the network. - world.networkobjects[network] = irc = proto.Class(network) - log.debug('Connecting to network %r', network) - irc.connect() - - world.started.set() - log.info("Loaded plugins: %s", ', '.join(sorted(world.plugins.keys()))) - - from pylinkirc import coremods - coremods.permissions.resetPermissions() # XXX we should probably move this to run on import +else: + if __name__ == '__main__': + launcher.main() diff --git a/setup.py b/setup.py index e08ae43..04978ec 100644 --- a/setup.py +++ b/setup.py @@ -97,5 +97,9 @@ setup( package_dir = {'pylinkirc': '.'}, # Executable scripts - scripts=["pylink", "pylink-mkpasswd"], + scripts=["pylink-mkpasswd"], + + entry_points = { + 'console_scripts': ['pylink=pylinkirc.launcher:main'], + } )