Exemplo n.º 1
0
def main(argv=None):
    args = parse_arguments(argv)
    logs_dedup_min_level = getattr(logging, args.logs_dedup_min_level)
    init_logging(level=args.verbosity,
                 fatal=args.fatal,
                 name=__name__,
                 logs_dedup_min_level=logs_dedup_min_level)

    logger.debug('Pelican version: %s', __version__)
    logger.debug('Python version: %s', sys.version.split()[0])

    try:
        pelican, settings = get_instance(args)

        if args.autoreload and args.listen:
            excqueue = multiprocessing.Queue()
            p1 = multiprocessing.Process(target=autoreload,
                                         args=(args, excqueue))
            p2 = multiprocessing.Process(target=listen,
                                         args=(settings.get('BIND'),
                                               settings.get('PORT'),
                                               settings.get("OUTPUT_PATH"),
                                               excqueue))
            p1.start()
            p2.start()
            exc = excqueue.get()
            p1.terminate()
            p2.terminate()
            if exc is not None:
                logger.critical(exc)
        elif args.autoreload:
            autoreload(args)
        elif args.listen:
            listen(settings.get('BIND'), settings.get('PORT'),
                   settings.get("OUTPUT_PATH"))
        else:
            watcher = FileSystemWatcher(args.settings, Readers, settings)
            watcher.check()
            with console.status("Generating..."):
                pelican.run()
    except KeyboardInterrupt:
        logger.warning('Keyboard interrupt received. Exiting.')
    except Exception as e:
        logger.critical("%s: %s", e.__class__.__name__, e)

        if args.verbosity == logging.DEBUG:
            console.print_exception()
        sys.exit(getattr(e, 'exitcode', 1))
Exemplo n.º 2
0
def autoreload(args, excqueue=None):
    console.print('  --- AutoReload Mode: Monitoring `content`, `theme` and'
                  ' `settings` for changes. ---')
    pelican, settings = get_instance(args)
    watcher = FileSystemWatcher(args.settings, Readers, settings)
    sleep = False
    while True:
        try:
            # Don't sleep first time, but sleep afterwards to reduce cpu load
            if sleep:
                time.sleep(0.5)
            else:
                sleep = True

            modified = watcher.check()

            if modified['settings']:
                pelican, settings = get_instance(args)
                watcher.update_watchers(settings)

            if any(modified.values()):
                console.print('\n-> Modified: {}. re-generating...'.format(
                    ', '.join(k for k, v in modified.items() if v)))
                pelican.run()

        except KeyboardInterrupt:
            if excqueue is not None:
                excqueue.put(None)
                return
            raise

        except Exception as e:
            if (args.verbosity == logging.DEBUG):
                if excqueue is not None:
                    excqueue.put(
                        traceback.format_exception_only(type(e), e)[-1])
                else:
                    raise
            logger.warning('Caught exception:\n"%s".',
                           e,
                           exc_info=settings.get('DEBUG', False))