Example #1
0
    def test_read_deps(self, mock_json_load):
        _file = open(os.path.join(settings.SYSTEMJS_CACHE_DIR, 'deps.json'),
                     'w')
        _file.write(json.dumps(self._depcache))
        self.addCleanup(_file.close)
        self.addCleanup(lambda: os.remove(_file.name))

        mock_json_load.return_value = self._depcache
        tracer = SystemTracer()

        # checking reading the hashes
        hashes = tracer.get_hashes()
        self.assertEqual(
            hashes, {
                'app/dummy.js': '65d75b61cae058018d3de1fa433a43da',
                'app/dependency.js': 'd41d8cd98f00b204e9800998ecf8427e'
            })

        # check reading the depcache for an app
        depcache = tracer.get_depcache('app/dummy')
        self.assertEqual(
            depcache, {
                'app/dummy.js': {
                    'name': 'app/dummy.js',
                    'timestamp': self.now,
                    'path': 'app/dummy.js',
                    'skip': False,
                },
                'app/dependency.js': {
                    'name': 'app/dependency.js',
                    'timestamp': self.now,
                    'path': 'app/dependency.js',
                    'skip': False,
                }
            })

        # check retrieving bundle options
        bundle_options = tracer.get_bundle_options()
        self.assertEqual(bundle_options, {
            'option1': True,
            'option2': False,
        })

        self.assertEqual(mock_json_load.call_count, 1)
    def test_read_deps(self, mock_json_load):
        _file = open(os.path.join(settings.SYSTEMJS_CACHE_DIR, 'deps.json'), 'w')
        _file.write(json.dumps(self._depcache))
        self.addCleanup(_file.close)
        self.addCleanup(lambda: os.remove(_file.name))

        mock_json_load.return_value = self._depcache
        tracer = SystemTracer()

        # checking reading the hashes
        hashes = tracer.get_hashes()
        self.assertEqual(hashes, {
            'app/dummy.js': '65d75b61cae058018d3de1fa433a43da',
            'app/dependency.js': 'd41d8cd98f00b204e9800998ecf8427e'
        })

        # check reading the depcache for an app
        depcache = tracer.get_depcache('app/dummy')
        self.assertEqual(depcache, {
            'app/dummy.js': {
                'name': 'app/dummy.js',
                'timestamp': self.now,
                'path': 'app/dummy.js',
                'skip': False,
            },
            'app/dependency.js': {
                'name': 'app/dependency.js',
                'timestamp': self.now,
                'path': 'app/dependency.js',
                'skip': False,
            }
        })

        # check retrieving bundle options
        bundle_options = tracer.get_bundle_options()
        self.assertEqual(bundle_options, {
            'option1': True,
            'option2': False,
        })

        self.assertEqual(mock_json_load.call_count, 1)
    def handle(self, **options):
        super(Command, self).handle(**options)

        self.post_process = options['post_process']
        self.minimal = options.get('minimal')

        self.verbosity = 2
        self.storage = copy(staticfiles_storage)
        self.storage.systemjs_bundling = True  # set flag to check later

        # initialize SystemJS specific objects to process the bundles
        tracer = SystemTracer(node_path=options.get('node_path'))
        system_opts = self.get_system_opts(options)
        system = System(**system_opts)

        has_different_options = self.minimal and tracer.get_bundle_options() != system_opts

        # discover the apps being imported in the templates
        all_apps = self.find_apps(templates=options.get('templates'))
        all_apps = set(sum(all_apps.values(), []))

        bundled_files = OrderedDict()
        # FIXME: this should be configurable, if people use S3BotoStorage for example, it needs to end up there
        storage = FileSystemStorage(settings.STATIC_ROOT, base_url=settings.STATIC_URL)
        for app in all_apps:
            # do we need to generate the bundle for this app?
            if self.minimal and not (has_different_options or tracer.check_needs_update(app)):
                # check if the bundle actually exists - if it doesn't, don't skip it
                # this happens on the first ever bundle
                bundle_path = System.get_bundle_path(app)
                if self.storage.exists(bundle_path):
                    self.stdout.write('Checked bundle for app \'{app}\', no changes found'.format(app=app))
                    continue

            rel_path = system.bundle(app)
            if not self.storage.exists(rel_path):
                self.stderr.write('Could not bundle {app}'.format(app=app))
            else:
                self.stdout.write('Bundled {app} into {out}'.format(app=app, out=rel_path))
            bundled_files[rel_path] = (storage, rel_path)

        if self.minimal and bundled_files:
            self.stdout.write('Generating the new depcache and writing to file...')
            all_deps = {app: tracer.trace(app) for app in all_apps}
            tracer.write_depcache(all_deps, system_opts)

        if self.post_process and hasattr(self.storage, 'post_process'):
            # post-process system.js if it's within settings.STATIC_ROOT
            systemjs_path = find_systemjs_location()
            try:
                within_static_root = self.storage.exists(systemjs_path)
            except SuspiciousFileOperation:
                within_static_root = False
            if within_static_root:
                relative = os.path.relpath(systemjs_path, settings.STATIC_ROOT)
                bundled_files[relative] = (storage, relative)

            processor = self.storage.post_process(bundled_files, dry_run=False)
            for original_path, processed_path, processed in processor:
                if isinstance(processed, Exception):  # pragma: no cover
                    self.stderr.write("Post-processing '%s' failed!" % original_path)
                    # Add a blank line before the traceback, otherwise it's
                    # too easy to miss the relevant part of the error message.
                    self.stderr.write("")
                    raise processed
                if processed:  # pragma: no cover
                    self.log("Post-processed '%s' as '%s'" % (original_path, processed_path), level=1)
                else:
                    self.log("Skipped post-processing '%s'" % original_path)  # pragma: no cover