Example #1
0
    def __init__(self, args):
        """
        :param args: CLI args
        """
        global manager
        assert not manager, 'Only one instance of Manager should be created at a time!'

        if args is None:
            # Decode all arguments to unicode before parsing
            args = unicode_argv()[1:]
        self.args = args
        self.config_base = None
        self.config_name = None
        self.config_path = None
        self.db_filename = None
        self.engine = None
        self.lockfile = None
        self.database_uri = None
        self.db_upgraded = False
        self._has_lock = False
        self.is_daemon = False
        self.ipc_server = None
        self.task_queue = None
        self.persist = None
        self.initialized = False

        self.config = {}

        if '--help' in args or '-h' in args:
            # TODO: This is a bit hacky, but we can't call parse on real arguments when --help is used because it will
            # cause a system exit before plugins are loaded and print incomplete help. This will get us a default
            # options object and we'll parse the real args later, or send them to daemon. #2807
            self.options, extra = CoreArgumentParser().parse_known_args(['execute'])
        else:
            try:
                self.options, extra = CoreArgumentParser().parse_known_args(args)
            except ParserError:
                # If a non-built-in command was used, we need to parse with a parser that doesn't define the subparsers
                self.options, extra = manager_parser.parse_known_args(args)
        try:
            self.find_config(create=False)
        except:
            logger.start(level=self.options.loglevel.upper(), to_file=False)
            raise
        else:
            log_file = os.path.expanduser(self.options.logfile)
            # If an absolute path is not specified, use the config directory.
            if not os.path.isabs(log_file):
                log_file = os.path.join(self.config_base, log_file)
            logger.start(log_file, self.options.loglevel.upper(), to_console=not self.options.cron)

        manager = self

        log.debug('sys.defaultencoding: %s' % sys.getdefaultencoding())
        log.debug('sys.getfilesystemencoding: %s' % sys.getfilesystemencoding())
        log.debug('os.path.supports_unicode_filenames: %s' % os.path.supports_unicode_filenames)
        if codecs.lookup(sys.getfilesystemencoding()).name == 'ascii' and not os.path.supports_unicode_filenames:
            log.warning('Your locale declares ascii as the filesystem encoding. Any plugins reading filenames from '
                        'disk will not work properly for filenames containing non-ascii characters. Make sure your '
                        'locale env variables are set up correctly for the environment which is launching FlexGet.')
Example #2
0
def main():
    """Main entry point for Command Line Interface"""

    logger.initialize()

    parser = CoreArgumentParser()
    plugin.load_plugins(parser)

    options = parser.parse_args()

    try:
        manager = Manager(options)
    except IOError as e:
        # failed to load config, TODO: why should it be handled here? So sys.exit isn't called in webui?
        log.critical(e)
        logger.flush_logging_to_console()
        sys.exit(1)

    log_level = logging.getLevelName(options.loglevel.upper())
    log_file = os.path.expanduser(manager.options.logfile)
    # If an absolute path is not specified, use the config directory.
    if not os.path.isabs(log_file):
        log_file = os.path.join(manager.config_base, log_file)
    logger.start(log_file, log_level)

    if options.profile:
        try:
            import cProfile as profile
        except ImportError:
            import profile
        profile.runctx('manager.execute()', globals(), locals(), os.path.join(manager.config_base, 'flexget.profile'))
    else:
        manager.execute()
    manager.shutdown()
Example #3
0
def setup_once():
    global plugins_loaded, test_arguments
    if not plugins_loaded:
        flexget.logger.initialize(True)
        setup_logging_level()
        parser = CoreArgumentParser(True)
        load_plugins(parser)
        # store options for MockManager
        test_arguments = parser.parse_args()
        plugins_loaded = True
Example #4
0
def setup_once():
    global plugins_loaded, test_arguments
    if not plugins_loaded:
        flexget.logger.initialize(True)
        setup_logging_level()
        parser = CoreArgumentParser(True)
        load_plugins(parser)
        # store options for MockManager
        test_arguments = parser.parse_args()
        plugins_loaded = True
Example #5
0
    def test_load(self):
        from flexget.options import CoreArgumentParser

        plugin.load_plugins(CoreArgumentParser())
        assert 0 == plugin.load_plugins(CoreArgumentParser())
        plugin_path = os.path.dirname(plugins.__file__)
        plugin_modules = set(
            os.path.basename(i) for k in ("/*.py", "/*/*.py")
            for i in glob.glob(plugin_path + k))
        assert len(
            plugin_modules) >= 10, "Less than 10 plugin modules looks fishy"
        assert len(
            plugin.plugins
        ) >= len(plugin_modules) - 1, "Less plugins than plugin modules"
Example #6
0
def main():
    """Main entry point for Command Line Interface"""

    logger.initialize()

    parser = CoreArgumentParser()
    plugin.load_plugins(parser)

    options = parser.parse_args()

    try:
        manager = Manager(options)
    except IOError, e:
        # failed to load config, TODO: why should it be handled here? So sys.exit isn't called in webui?
        log.critical(e)
        logger.flush_logging_to_console()
        sys.exit(1)
Example #7
0
 def _init_options(self, args):
     """
     Initialize argument parsing
     """
     if '--help' in args or '-h' in args:
         # TODO: This is a bit hacky, but we can't call parse on real arguments when --help is used because it will
         # cause a system exit before plugins are loaded and print incomplete help. This will get us a default
         # options object and we'll parse the real args later, or send them to daemon. #2807
         # TODO: this will cause command failure in case of config.yml does not
         # exists and user runs "flexget -c some.yml --help"
         options = CoreArgumentParser().parse_known_args(['execute'])[0]
     else:
         try:
             options = CoreArgumentParser().parse_known_args(args)[0]
         except ParserError:
             try:
                 # If a non-built-in command was used, we need to parse with a parser that
                 # doesn't define the subparsers
                 options = manager_parser.parse_known_args(args)[0]
             except ParserError as e:
                 manager_parser.print_help()
                 print('\nError: %s' % e.message)
                 sys.exit(1)
     try:
         if options.cli_command is None:
             # TODO: another hack ...
             # simply running "flexget -c config.yml" fails, let's fix that
             manager_parser.print_help()
             print('\nCommand missing, eg. execute or daemon ...')
             # TODO: oh dear ...
             if '--help' in args or '-h' in args:
                 print(
                     'NOTE: The help may be incomplete due issues with argparse. Try without --help.'
                 )
             else:
                 print(
                     'NOTE: The help may be incomplete due issues with argparse. Try with --help.'
                 )
             sys.exit(1)
     except AttributeError:
         pass  # TODO: hack .. this is getting out of hand
     return options
Example #8
0
 def _init_options(args: List[str]) -> argparse.Namespace:
     """Initialize argument parsing"""
     try:
         options = CoreArgumentParser().parse_known_args(args, do_help=False)[0]
     except ParserError as exc:
         try:
             # If a non-built-in command was used, we need to parse with a parser that
             # doesn't define the subparsers
             options = manager_parser.parse_known_args(args, do_help=False)[0]
         except ParserError:
             manager_parser.print_help()
             print(f'\nError: {exc.message}')
             sys.exit(1)
     return options
Example #9
0
def main():
    """Main entry point for FlexGet UI"""

    logger.initialize()

    # The core plugins need a core parser to add their options to
    core_parser = CoreArgumentParser()
    plugin.load_plugins(core_parser)

    # Use the ui options parser to parse the cli
    parser = UIArgumentParser(core_parser)
    options = parser.parse_args()
    try:
        manager = UIManager(options, core_parser)
    except IOError, e:
        # failed to load config
        log.critical(e.message)
        logger.flush_logging_to_console()
        sys.exit(1)
Example #10
0
def main():
    """Main entry point for FlexGet UI"""

    logger.initialize()

    # The core plugins need a core parser to add their options to
    core_parser = CoreArgumentParser()
    plugin.load_plugins(core_parser)

    # Use the ui options parser to parse the cli
    parser = UIArgumentParser(core_parser)
    options = parser.parse_args()
    try:
        manager = UIManager(options, core_parser)
    except IOError as e:
        # failed to load config
        log.critical(e.message)
        logger.flush_logging_to_console()
        sys.exit(1)

    log_level = logging.getLevelName(options.loglevel.upper())
    logger.start(os.path.join(manager.config_base, 'flexget.log'), log_level)

    flexget.ui.webui.start(manager)
Example #11
0
from Queue import Empty

from flask import render_template, request, flash
from flask import Blueprint, escape, jsonify

from flexget.options import CoreArgumentParser, RaiseErrorArgumentParser
from flexget.ui.webui import register_plugin
from flexget.scheduler import BufferQueue

execute = Blueprint('execute', __name__)

log = logging.getLogger('ui.execute')

bufferqueue = BufferQueue()
exec_parser = RaiseErrorArgumentParser(
    parents=[CoreArgumentParser().get_subparser('execute')])


@execute.route('/', methods=['POST', 'GET'])
def index():
    context = {'progress': exec_parser.format_help().split('\n')}
    if request.method == 'POST':
        try:
            options = exec_parser.parse_args(request.form.get('options', ''))
        except ValueError as e:
            flash(escape(e.message), 'error')
            context['options'] = request.form['options']
        else:
            executor.execute(options=options, output=bufferqueue)
            context['execute_progress'] = True
            context['progress'] = progress(as_list=True)
Example #12
0
    def test_no_dupes(self):
        from flexget.options import CoreArgumentParser
        plugin.load_plugins(CoreArgumentParser())

        assert plugin.PluginInfo.dupe_counter == 0, "Duplicate plugin names, see log"