/usr/lib/python2.7/dist-packages/pipeline/packager.py is in python-django-pipeline 1.5.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | from __future__ import unicode_literals
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles.finders import find
from django.core.files.base import ContentFile
from django.utils.encoding import smart_bytes
from pipeline.compilers import Compiler
from pipeline.compressors import Compressor
from pipeline.conf import settings
from pipeline.exceptions import PackageNotFound
from pipeline.glob import glob
from pipeline.signals import css_compressed, js_compressed
class Package(object):
def __init__(self, config):
self.config = config
self._sources = []
@property
def sources(self):
if not self._sources:
paths = []
for pattern in self.config.get('source_filenames', []):
for path in glob(pattern):
if path not in paths and find(path):
paths.append(str(path))
self._sources = paths
return self._sources
@property
def paths(self):
return [path for path in self.sources
if not path.endswith(settings.PIPELINE_TEMPLATE_EXT)]
@property
def templates(self):
return [path for path in self.sources
if path.endswith(settings.PIPELINE_TEMPLATE_EXT)]
@property
def output_filename(self):
return self.config.get('output_filename')
@property
def extra_context(self):
return self.config.get('extra_context', {})
@property
def template_name(self):
return self.config.get('template_name')
@property
def variant(self):
return self.config.get('variant')
@property
def manifest(self):
return self.config.get('manifest', True)
class Packager(object):
def __init__(self, storage=None, verbose=False, css_packages=None, js_packages=None):
if storage is None:
storage = staticfiles_storage
self.storage = storage
self.verbose = verbose
self.compressor = Compressor(storage=storage, verbose=verbose)
self.compiler = Compiler(storage=storage, verbose=verbose)
if css_packages is None:
css_packages = settings.PIPELINE_CSS
if js_packages is None:
js_packages = settings.PIPELINE_JS
self.packages = {
'css': self.create_packages(css_packages),
'js': self.create_packages(js_packages),
}
def package_for(self, kind, package_name):
try:
return self.packages[kind][package_name]
except KeyError:
raise PackageNotFound(
"No corresponding package for %s package name : %s" % (
kind, package_name
)
)
def individual_url(self, filename):
return self.storage.url(filename)
def pack_stylesheets(self, package, **kwargs):
return self.pack(package, self.compressor.compress_css, css_compressed,
output_filename=package.output_filename,
variant=package.variant, **kwargs)
def compile(self, paths, force=False):
return self.compiler.compile(paths, force=force)
def pack(self, package, compress, signal, **kwargs):
output_filename = package.output_filename
if self.verbose:
print("Saving: %s" % output_filename)
paths = self.compile(package.paths, force=True)
content = compress(paths, **kwargs)
self.save_file(output_filename, content)
signal.send(sender=self, package=package, **kwargs)
return output_filename
def pack_javascripts(self, package, **kwargs):
return self.pack(package, self.compressor.compress_js, js_compressed, templates=package.templates, **kwargs)
def pack_templates(self, package):
return self.compressor.compile_templates(package.templates)
def save_file(self, path, content):
return self.storage.save(path, ContentFile(smart_bytes(content)))
def create_packages(self, config):
packages = {}
if not config:
return packages
for name in config:
packages[name] = Package(config[name])
return packages
|