def handle(self, **options):
        self.force_compress = options.get('force', False)

        # This will loop through every bundle, and do the following:
        # - Concat all files into one
        # - Cache bust all images in CSS files
        # - Minify the concatted files
        for ftype, bundle in settings.MINIFY_BUNDLES.iteritems():
            for name, files in bundle.iteritems():
                # Set the paths to the files.
                concatted_file = os.path.join(
                    settings.ROOT, 'static',
                    ftype, '%s-all.%s' % (name, ftype,))
                compressed_file = os.path.join(
                    settings.ROOT, 'static',
                    ftype, '%s-min.%s' % (name, ftype,))

                ensure_path_exists(concatted_file)
                ensure_path_exists(compressed_file)

                files_all = []
                for fn in files:
                    processed = self._preprocess_file(fn)
                    # If the file can't be processed, we skip it.
                    if processed is not None:
                        files_all.append(processed)

                # Concat all the files.
                tmp_concatted = '%s.tmp' % concatted_file
                if len(files_all) == 0:
                    raise CommandError(
                        'No input files specified in '
                        'MINIFY_BUNDLES["%s"]["%s"] in settings.py!' %
                        (ftype, name)
                    )
                run_command('cat {files} > {tmp}'.format(
                    files=' '.join(files_all),
                    tmp=tmp_concatted
                ))

                # Cache bust individual images in the CSS.
                if ftype == 'css':
                    bundle_hash = self._cachebust(tmp_concatted, name)
                    self.bundle_hashes['%s:%s' % (ftype, name)] = bundle_hash

                # Compresses the concatenations.
                is_changed = self._is_changed(concatted_file)
                self._clean_tmp(concatted_file)
                if is_changed or not os.path.isfile(compressed_file):
                    self._minify(ftype, concatted_file, compressed_file)
                else:
                    print(
                        'File unchanged, skipping minification of %s' % (
                            concatted_file))
                    self.minify_skipped += 1

        # Write out the hashes
        self.update_hashes()

        if self.minify_skipped:
            print(
                'Unchanged files skipped for minification: %s' % (
                    self.minify_skipped))
예제 #2
0
    def handle(self, **options):
        self.force_compress = options.get('force', False)

        # This will loop through every bundle, and do the following:
        # - Concat all files into one
        # - Cache bust all images in CSS files
        # - Minify the concatted files
        for ftype, bundle in six.iteritems(settings.MINIFY_BUNDLES):
            for name, files in six.iteritems(bundle):
                # Set the paths to the files.
                concatted_file = os.path.join(settings.ROOT, 'static', ftype,
                                              '%s-all.%s' % (
                                                  name,
                                                  ftype,
                                              ))
                compressed_file = os.path.join(settings.ROOT, 'static', ftype,
                                               '%s-min.%s' % (
                                                   name,
                                                   ftype,
                                               ))

                ensure_path_exists(concatted_file)
                ensure_path_exists(compressed_file)

                files_all = []
                for fn in files:
                    processed = self._preprocess_file(fn)
                    # If the file can't be processed, we skip it.
                    if processed is not None:
                        files_all.append(processed)

                # Concat all the files.
                tmp_concatted = '%s.tmp' % concatted_file
                if len(files_all) == 0:
                    raise CommandError(
                        'No input files specified in '
                        'MINIFY_BUNDLES["%s"]["%s"] in settings.py!' %
                        (ftype, name))
                run_command('cat {files} > {tmp}'.format(
                    files=' '.join(files_all), tmp=tmp_concatted))

                # Cache bust individual images in the CSS.
                if ftype == 'css':
                    bundle_hash = self._cachebust(tmp_concatted, name)
                    self.bundle_hashes['%s:%s' % (ftype, name)] = bundle_hash

                # Compresses the concatenations.
                is_changed = self._is_changed(concatted_file)
                self._clean_tmp(concatted_file)
                if is_changed or not os.path.isfile(compressed_file):
                    self._minify(ftype, concatted_file, compressed_file)
                else:
                    print('File unchanged, skipping minification of %s' %
                          (concatted_file))
                    self.minify_skipped += 1

        # Write out the hashes
        self.update_hashes()

        if self.minify_skipped:
            print('Unchanged files skipped for minification: %s' %
                  (self.minify_skipped))
예제 #3
0
    def handle(self, **options):
        self.force_compress = options.get('force', False)

        # This will loop through every bundle, and do the following:
        # - Concat all files into one
        # - Cache bust all images in CSS files
        # - Minify the concatted files
        for ftype, bundle in settings.MINIFY_BUNDLES.items():
            for name, files in bundle.items():
                # Set the paths to the files.
                concatted_file = os.path.join(
                    settings.ROOT,
                    'static',
                    ftype,
                    '%s-all.%s' % (
                        name,
                        ftype,
                    ),
                )
                compressed_file = os.path.join(
                    settings.ROOT,
                    'static',
                    ftype,
                    '%s-min.%s' % (
                        name,
                        ftype,
                    ),
                )

                ensure_path_exists(concatted_file)
                ensure_path_exists(compressed_file)

                files_all = []
                contents = []
                for filename in files:
                    processed = self._preprocess_file(filename)
                    # If the file can't be processed, we skip it.
                    if processed is not None:
                        files_all.append(processed)
                    with open(processed) as f:
                        contents.append(f.read())

                # Concat all the files.
                tmp_concatted = '%s.tmp' % concatted_file
                if len(files_all) == 0:
                    raise CommandError(
                        'No input files specified in '
                        'MINIFY_BUNDLES["%s"]["%s"] in settings.py!' %
                        (ftype, name))
                with open(tmp_concatted, 'w') as f:
                    f.write(''.join(contents))

                # Compresses the concatenations.
                is_changed = self._is_changed(concatted_file)
                self._clean_tmp(concatted_file)
                if is_changed or not os.path.isfile(compressed_file):
                    self._minify(ftype, concatted_file, compressed_file)
                else:
                    print('File unchanged, skipping minification of %s' %
                          (concatted_file))
                    self.minify_skipped += 1

        if self.minify_skipped:
            print('Unchanged files skipped for minification: %s' %
                  (self.minify_skipped))