def handle(self, *args, **options): # Due to the use of LaxOptionParser ``args`` now contains all # unparsed options, and ``options`` those that the Django command # has declared. DjangoConfigStorage.force_debug = True # Create log log = logging.getLogger('django-assets') log.setLevel({0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}[int(options.get('verbosity', 1))]) log.addHandler(logging.StreamHandler()) # If the user requested it, search for bundles defined in templates if options.get('parse_templates'): log.info('Searching templates...') # Note that we exclude container bundles. By their very nature, # they are guaranteed to have been created by solely referencing # other bundles which are already registered. get_env().add(*[b for b in self.load_from_templates() if not b.is_container]) if len(get_env()) == 0: DjangoConfigStorage.force_debug = False raise CommandError('No asset bundles were found. ' 'If you are defining assets directly within your ' 'templates, you want to use the --parse-templates ' 'option.') prog = "%s assets" % path.basename(sys.argv[0]) impl = GenericArgparseImplementation( env=get_env(), log=log, no_global_options=True, prog=prog) try: impl.run_with_argv(args) except AssetCommandError, e: raise CommandError(e)
def handle(self, *args, **options): # Due to the use of LaxOptionParser ``args`` now contains all # unparsed options, and ``options`` those that the Django command # has declared. # Create log log = logging.getLogger('django-assets') log.setLevel({0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}[int(options.get('verbosity', 1))]) log.addHandler(logging.StreamHandler()) # If the user requested it, search for bundles defined in templates if options.get('parse_templates'): log.info('Searching templates...') # Note that we exclude container bundles. By their very nature, # they are guaranteed to have been created by solely referencing # other bundles which are already registered. get_env().add(*[b for b in self.load_from_templates() if not b.is_container]) if len(get_env()) == 0: raise CommandError('No asset bundles were found. ' 'If you are defining assets directly within your ' 'templates, you want to use the --parse-templates ' 'option.') prog = "%s assets" % path.basename(sys.argv[0]) impl = GenericArgparseImplementation( env=get_env(), log=log, no_global_options=True, prog=prog) try: impl.run_with_argv(args) except AssetCommandError, e: raise CommandError(e)
def build_assets(): """Build assets like ./manage.py assets build does""" if settings.ASSETS_AUTO_BUILD: return env = assets_env.get_env() argparser = GenericArgparseImplementation(env=env, no_global_options=False) errored = argparser.run_with_argv(["build"]) or 0 if errored != 0: raise Exception("Asset building failed with error code %d" % errored)
def build_assets(): """Call django_assets ./manage.py assets build if the app is present.""" cwd = os.getcwd() try: from webassets.script import GenericArgparseImplementation from django_assets.env import get_env prog = "%s assets" % os.path.basename(sys.argv[0]) impl = GenericArgparseImplementation( env=get_env(), log=LOGGER, no_global_options=True, prog=prog) impl.run_with_argv(["build"]) except ImportError: pass os.chdir(cwd)
def build_assets(): """Call django_assets ./manage.py assets build if the app is present.""" cwd = os.getcwd() try: from webassets.script import GenericArgparseImplementation from django_assets.env import get_env prog = "%s assets" % os.path.basename(sys.argv[0]) impl = GenericArgparseImplementation(env=get_env(), log=LOGGER, no_global_options=True, prog=prog) impl.run_with_argv(["build"]) except ImportError: pass os.chdir(cwd)
def test_watch_config_file(self): """The watch command has an eye on the config file. This is an extension to the base watch command.""" try: import yaml except ImportError: raise SkipTest() self.cmd_env = CommandLineEnvironment(self.env, logging) self.cmd_env.commands['watch'] = \ GenericArgparseImplementation.WatchCommand( self.cmd_env, argparse.Namespace(config=self.path('config.yml'))) self.create_files({'in': 'foo'}) template = """ directory: . bundles: foo: contents: - in output: %s """ self.create_files({'config.yml': template % 'outA'}) with self: time.sleep(0.1) # Change the config file; this change is detected; we update # the timestamp explicitly or we might not have enough precision self.create_files({'config.yml': template % 'outB'}) self.setmtime('config.yml', mod=100) time.sleep(0.2) # The second output file has been built assert self.get('outB') == 'foo'
def handle(self, *args, **options): # Due to the use of LaxOptionParser ``args`` now contains all # unparsed options, and ``options`` those that the Django command # has declared. # Create log log = logging.getLogger('django-assets') log.setLevel({ 0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG }[int(options.get('verbosity', 1))]) log.addHandler(logging.StreamHandler()) # If the user requested it, search for bundles defined in templates if options.get('parse_templates'): log.info('Searching templates...') # Note that we exclude container bundles. By their very nature, # they are guaranteed to have been created by solely referencing # other bundles which are already registered. get_env().add( *[b for b in self.load_from_templates() if not b.is_container]) if len(get_env()) == 0: log.info("No asset bundles were found. " "If you are defining assets directly within your " "templates, you want to use the --parse-templates " "option.") return prog = "%s assets" % path.basename(sys.argv[0]) impl = GenericArgparseImplementation(env=get_env(), log=log, no_global_options=True, prog=prog) try: # The webassets script runner may either return None on success (so # map that to zero) or a return code on build failure (so raise # a Django CommandError exception when that happens) retval = impl.run_with_argv(args) or 0 if retval != 0: raise CommandError('The webassets build script exited with ' 'a non-zero exit code (%d).' % retval) except AssetCommandError as e: raise CommandError(e)
def handle(self, *args, **options): # For django to work, we need to populate apps and models as soon as possible # If the apps have not yet been populated by now (as is the case when using uwsgi), # populate. from django.db.models.loading import cache cache._populate() # Due to the use of LaxOptionParser ``args`` now contains all # unparsed options, and ``options`` those that the Django command # has declared. # Create log log = logging.getLogger('django-assets') log.setLevel({0: logging.WARNING, 1: logging.INFO, 2: logging.DEBUG}[int(options.get('verbosity', 1))]) log.addHandler(logging.StreamHandler()) # If the user requested it, search for bundles defined in templates if options.get('parse_templates'): log.info('Searching templates...') # Note that we exclude container bundles. By their very nature, # they are guaranteed to have been created by solely referencing # other bundles which are already registered. get_env().add(*[b for b in self.load_from_templates() if not b.is_container]) if len(get_env()) == 0: raise CommandError('No asset bundles were found. ' 'If you are defining assets directly within your ' 'templates, you want to use the --parse-templates ' 'option.') prog = "%s assets" % path.basename(sys.argv[0]) impl = GenericArgparseImplementation( env=get_env(), log=log, no_global_options=True, prog=prog) try: # The webassets script runner may either return None on success (so # map that to zero) or a return code on build failure (so raise # a Django CommandError exception when that happens) retval = impl.run_with_argv(args) or 0 if retval != 0: raise CommandError('The webassets build script exited with ' 'a non-zero exit code (%d).' % retval) except AssetCommandError as e: raise CommandError(e)
def test_watch_with_fixed_env_and_no_config(self): """[Regression[ The custom 'watch' command does not break if the CLI is initialized via fixed environment, instead of reading one from a configuration file. """ self.cmd_env = CommandLineEnvironment(self.env, logging) self.cmd_env.commands['watch'] = \ GenericArgparseImplementation.WatchCommand( self.cmd_env, argparse.Namespace()) with self: time.sleep(0.1)
def test_no_env(self): """[Regression] If no env is hardcoded, nor one given via the commandline, we fail with a clean error. """ impl = GenericArgparseImplementation(env=None) assert_raises(CommandError, impl.run_with_argv, ['build'])