def _run_chef(pre_args, argv): # Setup the app. root = None if pre_args.root: root = os.path.expanduser(pre_args.root) else: try: root = find_app_root(theme=pre_args.theme) except SiteNotFoundError: root = None # Can't apply custom configuration stuff if there's no website. if (pre_args.config_variants or pre_args.config_values) and not root: raise SiteNotFoundError( "Can't apply any configuration variant or value overrides, " "there is no website here.") if root: cache_key = None if not pre_args.no_cache: cache_key = _build_cache_key(pre_args) appfactory = PieCrustFactory( root, theme_site=pre_args.theme, cache=(not pre_args.no_cache), cache_key=cache_key, debug=pre_args.debug, config_variants=pre_args.config_variants, config_values=pre_args.config_values) app = appfactory.create() else: appfactory = None app = NullPieCrust( theme_site=pre_args.theme) # Setup the arg parser. parser = argparse.ArgumentParser( prog='chef', description="The PieCrust chef manages your website.", formatter_class=argparse.RawDescriptionHelpFormatter) _setup_main_parser_arguments(parser) commands = sorted(app.plugin_loader.getCommands(), key=lambda c: c.name) subparsers = parser.add_subparsers(title='list of commands') for c in commands: p = subparsers.add_parser(c.name, help=c.description) c.setupParser(p, app) p.set_defaults(func=c.checkedRun) p.set_defaults(cache_name=c.cache_name) help_cmd = next(filter(lambda c: c.name == 'help', commands), None) if help_cmd and help_cmd.has_topics: with io.StringIO() as epilog: epilog.write("additional help topics:\n") for name, desc in help_cmd.getTopics(): print_help_item(epilog, name, desc) parser.epilog = epilog.getvalue() # Parse the command line. result = parser.parse_args(argv) logger.debug(format_timed(_chef_start_time, 'initialized PieCrust', colored=False)) # Print the help if no command was specified. if not hasattr(result, 'func'): parser.print_help() return 0 # Do any custom setup the user wants. custom_env = app.config.get('chef/env') if custom_env: _setup_app_environment(app, custom_env) # Add some timing information. if app.env: app.env.stats.registerTimer('ChefStartup') app.env.stats.stepTimerSince('ChefStartup', _chef_start_time) # Run the command! ctx = CommandContext(appfactory, app, parser, result) exit_code = result.func(ctx) if exit_code is None: return 0 if not isinstance(exit_code, int): logger.error("Got non-integer exit code: %s" % exit_code) return -1 return exit_code
def _run_chef(pre_args, argv): # Setup the app. start_time = time.perf_counter() root = None if pre_args.root: root = os.path.expanduser(pre_args.root) else: try: root = find_app_root(theme=pre_args.theme) except SiteNotFoundError: root = None # Can't apply custom configuration stuff if there's no website. if (pre_args.config_variant or pre_args.config_values) and not root: raise SiteNotFoundError( "Can't apply any configuration variant or value overrides, " "there is no website here.") if root: cache_key = None if not pre_args.no_cache: cache_key = _build_cache_key(pre_args) app = PieCrust( root, theme_site=pre_args.theme, cache=(not pre_args.no_cache), cache_key=cache_key, debug=pre_args.debug) apply_variant_and_values( app, pre_args.config_variant, pre_args.config_values) else: app = NullPieCrust( theme_site=pre_args.theme) # Setup the arg parser. parser = argparse.ArgumentParser( prog='chef', description="The PieCrust chef manages your website.", formatter_class=argparse.RawDescriptionHelpFormatter) _setup_main_parser_arguments(parser) commands = sorted(app.plugin_loader.getCommands(), key=lambda c: c.name) subparsers = parser.add_subparsers(title='list of commands') for c in commands: p = subparsers.add_parser(c.name, help=c.description) c.setupParser(p, app) p.set_defaults(func=c.checkedRun) p.set_defaults(cache_name=c.cache_name) help_cmd = next(filter(lambda c: c.name == 'help', commands), None) if help_cmd and help_cmd.has_topics: with io.StringIO() as epilog: epilog.write("additional help topics:\n") for name, desc in help_cmd.getTopics(): print_help_item(epilog, name, desc) parser.epilog = epilog.getvalue() # Parse the command line. result = parser.parse_args(argv) logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) # Print the help if no command was specified. if not hasattr(result, 'func'): parser.print_help() return 0 # Run the command! ctx = CommandContext(app, parser, result) ctx.config_variant = pre_args.config_variant ctx.config_values = pre_args.config_values exit_code = result.func(ctx) if exit_code is None: return 0 if not isinstance(exit_code, int): logger.error("Got non-integer exit code: %s" % exit_code) return -1 return exit_code
def _run_chef(pre_args): # Setup the app. start_time = time.clock() root = pre_args.root if root is None: try: root = find_app_root() except SiteNotFoundError: root = None if not root: app = NullPieCrust() else: app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug) # Build a hash for a custom cache directory. cache_key = 'default' # Handle a configuration variant. if pre_args.config_variant is not None: if not root: raise SiteNotFoundError("Can't apply any variant.") app.config.applyVariant('variants/' + pre_args.config_variant) cache_key += ',variant=%s' % pre_args.config_variant for name, value in pre_args.config_values: logger.debug("Setting configuration '%s' to: %s" % (name, value)) app.config.set(name, value) cache_key += ',%s=%s' % (name, value) # Setup the arg parser. parser = argparse.ArgumentParser( prog='chef', description="The PieCrust chef manages your website.", formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('--version', action='version', version=('%(prog)s ' + APP_VERSION)) parser.add_argument('--root', help="The root directory of the website.") parser.add_argument( '--config', help="The configuration variant to use for this command.") parser.add_argument('--config-set', help="Sets a specific site configuration setting.") parser.add_argument('--debug', help="Show debug information.", action='store_true') parser.add_argument('--no-cache', help="When applicable, disable caching.", action='store_true') parser.add_argument('--quiet', help="Print only important information.", action='store_true') parser.add_argument('--log', help="Send log messages to the specified file.") parser.add_argument('--log-debug', help="Log debug messages to the log file.", action='store_true') commands = sorted(app.plugin_loader.getCommands(), key=lambda c: c.name) subparsers = parser.add_subparsers(title='list of commands') for c in commands: p = subparsers.add_parser(c.name, help=c.description) c.setupParser(p, app) p.set_defaults(func=c.checkedRun) p.set_defaults(cache_name=c.cache_name) help_cmd = next(filter(lambda c: c.name == 'help', commands), None) if help_cmd and help_cmd.has_topics: with io.StringIO() as epilog: epilog.write("additional help topics:\n") for name, desc in help_cmd.getTopics(): print_help_item(epilog, name, desc) parser.epilog = epilog.getvalue() # Parse the command line. result = parser.parse_args() logger.debug( format_timed(start_time, 'initialized PieCrust', colored=False)) # Print the help if no command was specified. if not hasattr(result, 'func'): parser.print_help() return 0 # Use a customized cache for the command and current config. if result.cache_name != 'default' or cache_key != 'default': app.useSubCache(result.cache_name, cache_key) # Run the command! ctx = CommandContext(app, parser, result) exit_code = result.func(ctx) if exit_code is None: return 0 if not isinstance(exit_code, int): logger.error("Got non-integer exit code: %s" % exit_code) return -1 return exit_code
def _run_chef(pre_args, argv): # Setup the app. start_time = time.clock() root = pre_args.root if root is None: try: root = find_app_root() except SiteNotFoundError: root = None if not root: app = NullPieCrust() else: app = PieCrust(root, cache=pre_args.cache, debug=pre_args.debug) # Build a hash for a custom cache directory. cache_key = 'default' # Handle custom configurations. if pre_args.config_variant is not None and not root: raise SiteNotFoundError("Can't apply any variant.") apply_variant_and_values(app, pre_args.config_variant, pre_args.config_values) # Adjust the cache key. if pre_args.config_variant is not None: cache_key += ',variant=%s' % pre_args.config_variant for name, value in pre_args.config_values: cache_key += ',%s=%s' % (name, value) # Setup the arg parser. parser = argparse.ArgumentParser( prog='chef', description="The PieCrust chef manages your website.", formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument( '--version', action='version', version=('%(prog)s ' + APP_VERSION)) parser.add_argument( '--root', help="The root directory of the website.") parser.add_argument( '--config', help="The configuration variant to use for this command.") parser.add_argument( '--config-set', help="Sets a specific site configuration setting.") parser.add_argument( '--debug', help="Show debug information.", action='store_true') parser.add_argument( '--no-cache', help="When applicable, disable caching.", action='store_true') parser.add_argument( '--quiet', help="Print only important information.", action='store_true') parser.add_argument( '--log', help="Send log messages to the specified file.") parser.add_argument( '--log-debug', help="Log debug messages to the log file.", action='store_true') commands = sorted(app.plugin_loader.getCommands(), key=lambda c: c.name) subparsers = parser.add_subparsers(title='list of commands') for c in commands: p = subparsers.add_parser(c.name, help=c.description) c.setupParser(p, app) p.set_defaults(func=c.checkedRun) p.set_defaults(cache_name=c.cache_name) help_cmd = next(filter(lambda c: c.name == 'help', commands), None) if help_cmd and help_cmd.has_topics: with io.StringIO() as epilog: epilog.write("additional help topics:\n") for name, desc in help_cmd.getTopics(): print_help_item(epilog, name, desc) parser.epilog = epilog.getvalue() # Parse the command line. result = parser.parse_args(argv) logger.debug(format_timed(start_time, 'initialized PieCrust', colored=False)) # Print the help if no command was specified. if not hasattr(result, 'func'): parser.print_help() return 0 # Use a customized cache for the command and current config. if result.cache_name != 'default' or cache_key != 'default': app.useSubCache(result.cache_name, cache_key) # Run the command! ctx = CommandContext(app, parser, result) ctx.config_variant = pre_args.config_variant ctx.config_values = pre_args.config_values exit_code = result.func(ctx) if exit_code is None: return 0 if not isinstance(exit_code, int): logger.error("Got non-integer exit code: %s" % exit_code) return -1 return exit_code