def __init__(self, verbose=False, css_packages=None, js_packages=None): self.verbose = verbose self.compressor = Compressor(verbose) self.versioning = Versioning(verbose) self.compiler = Compiler(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 __init__(self, force=False, verbose=False): self.force = force self.verbose = verbose self.compressor = Compressor(verbose) self.versioning = Versioning(verbose) self.compiler = Compiler(verbose) self.packages = { 'css': self.create_packages(settings.PIPELINE_CSS), 'js': self.create_packages(settings.PIPELINE_JS), }
class Packager(object): def __init__(self, verbose=False, css_packages=None, js_packages=None): self.verbose = verbose self.compressor = Compressor(verbose) self.versioning = Versioning(verbose) self.compiler = Compiler(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].copy() except KeyError: raise PackageNotFound( "No corresponding package for %s package name : %s" % ( kind, package_name ) ) def individual_url(self, filename): relative_path = self.compressor.relative_path(filename)[1:] return urlparse.urljoin(settings.PIPELINE_URL, filepath_to_uri(relative_path)) def pack_stylesheets(self, package, **kwargs): variant = package.get('variant', None) absolute_asset_paths = package.get('absolute_asset_paths', True) return self.pack(package, self.compressor.compress_css, css_compressed, variant=variant, absolute_asset_paths=absolute_asset_paths, **kwargs) def compile(self, paths): return self.compiler.compile(paths) def pack(self, package, compress, signal, sync=False, force=False, **kwargs): if settings.PIPELINE_AUTO or (force and sync): need_update, version = self.versioning.need_update( package['output'], package['paths']) if need_update or force: output_filename = self.versioning.output_filename( package['output'], version ) self.versioning.cleanup(package['output']) if self.verbose: print "Version: %s" % version print "Saving: %s" % output_filename paths = self.compile(package['paths']) content = compress(paths, asset_url=self.individual_url(output_filename), **kwargs) self.save_file(output_filename, content) else: filename_base, filename = os.path.split(package['output']) version = self.versioning.version_from_file(filename_base, filename, force=force) signal.send(sender=self, package=package, version=version, **kwargs) return self.versioning.output_filename(package['output'], version) def pack_javascripts(self, package, **kwargs): if 'externals' in package: return 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 storage.save(path, ContentFile(smart_str(content))) def create_packages(self, config): packages = {} if not config: return packages for name in config: packages[name] = {} if 'external_urls' in config[name]: packages[name]['externals'] = config[name]['external_urls'] continue paths = [] for pattern in config[name]['source_filenames']: for path in glob(pattern): if not path in paths: paths.append(str(path)) packages[name]['paths'] = [path for path in paths if not path.endswith(settings.PIPELINE_TEMPLATE_EXT)] packages[name]['templates'] = [path for path in paths if path.endswith(settings.PIPELINE_TEMPLATE_EXT)] packages[name]['output'] = config[name]['output_filename'] packages[name]['context'] = {} packages[name]['manifest'] = True if 'absolute_asset_paths' in config[name]: packages[name]['absolute_asset_paths'] = \ config[name]['absolute_asset_paths'] if 'extra_context' in config[name]: packages[name]['context'] = config[name]['extra_context'] if 'template_name' in config[name]: packages[name]['template'] = config[name]['template_name'] if 'variant' in config[name]: packages[name]['variant'] = config[name]['variant'] if 'manifest' in config[name]: packages[name]['manifest'] = config[name]['manifest'] return packages
def setUp(self): self.versioning = Versioning() self.old_pipeline_version = settings.PIPELINE_VERSION
class VersioningTest(TestCase): def setUp(self): self.versioning = Versioning() self.old_pipeline_version = settings.PIPELINE_VERSION def test_versioner_class(self): self.assertTrue(isinstance(self.versioning.versioner, MTimeVersioning)) def test_output_filename(self): output_filename = self.versioning.output_filename( "css/master.?.css", "1308127956") self.assertEquals(output_filename, 'css/master.0.css') def test_output_filename_with_version(self): settings.PIPELINE_VERSION = True output_filename = self.versioning.output_filename( "css/master.?.css", "1308127956") self.assertEquals(output_filename, 'css/master.1308127956.css') @patch.object(MTimeVersioning, 'version') def test_need_update(self, mock): mock.return_value = "1307480052" need_update, version = self.versioning.need_update( 'css/master.1308127956.css', ['css/first.css'], ) self.assertEquals(need_update, True) self.assertEquals(version, "1307480052") @patch.object(MTimeVersioning, 'need_update') def test_no_update(self, mock): mock.return_value = (False, "123456") need_update, version = self.versioning.need_update( 'css/master.123456.css', ['css/first.css'], ) self.assertTrue(mock.called) self.assertEquals(need_update, False) self.assertEquals(version, "123456") @patch.object(MTimeVersioning, 'version') def test_version(self, mock): mock.return_value = "123456" version = self.versioning.version(['css/first.css']) self.assertTrue(mock.called) self.assertEquals(version, "123456") def test_version_from_file(self): settings.PIPELINE_VERSION = True version = self.versioning.version_from_file('css/', "master.?.css") self.assertEquals(version, "123456") @patch.object(storage, 'exists') def test_no_cleanup(self, mock): self.versioning.cleanup('css/master.?.css') self.assertFalse(mock.called) @patch.object(storage, 'delete') def test_cleanup(self, mock): settings.PIPELINE_VERSION = True self.versioning.cleanup('css/master.?.css') mock.assert_called_with('css/master.123456.css') def tearDown(self): settings.PIPELINE_VERSION = self.old_pipeline_version
class Packager(object): def __init__(self, verbose=False, css_packages=None, js_packages=None): self.verbose = verbose self.compressor = Compressor(verbose) self.versioning = Versioning(verbose) self.compiler = Compiler(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].copy() except KeyError: raise PackageNotFound( "No corresponding package for %s package name : %s" % (kind, package_name)) def individual_url(self, filename): relative_path = self.compressor.relative_path(filename)[1:] return urlparse.urljoin(settings.PIPELINE_URL, filepath_to_uri(relative_path)) def pack_stylesheets(self, package, **kwargs): variant = package.get('variant', None) absolute_asset_paths = package.get('absolute_asset_paths', True) return self.pack(package, self.compressor.compress_css, css_compressed, variant=variant, absolute_asset_paths=absolute_asset_paths, **kwargs) def compile(self, paths): return self.compiler.compile(paths) def pack(self, package, compress, signal, sync=False, force=False, **kwargs): if settings.PIPELINE_AUTO or (force and sync): need_update, version = self.versioning.need_update( package['output'], package['paths']) if need_update or force: output_filename = self.versioning.output_filename( package['output'], version) self.versioning.cleanup(package['output']) if self.verbose: print "Version: %s" % version print "Saving: %s" % output_filename paths = self.compile(package['paths']) content = compress( paths, asset_url=self.individual_url(output_filename), **kwargs) self.save_file(output_filename, content) else: filename_base, filename = os.path.split(package['output']) version = self.versioning.version_from_file(filename_base, filename, force=force) signal.send(sender=self, package=package, version=version, **kwargs) return self.versioning.output_filename(package['output'], version) def pack_javascripts(self, package, **kwargs): if 'externals' in package: return 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 storage.save(path, ContentFile(smart_str(content))) def create_packages(self, config): packages = {} if not config: return packages for name in config: packages[name] = {} if 'external_urls' in config[name]: packages[name]['externals'] = config[name]['external_urls'] continue paths = [] for pattern in config[name]['source_filenames']: for path in glob(pattern): if not path in paths: paths.append(str(path)) packages[name]['paths'] = [ path for path in paths if not path.endswith(settings.PIPELINE_TEMPLATE_EXT) ] packages[name]['templates'] = [ path for path in paths if path.endswith(settings.PIPELINE_TEMPLATE_EXT) ] packages[name]['output'] = config[name]['output_filename'] packages[name]['context'] = {} packages[name]['manifest'] = True if 'absolute_asset_paths' in config[name]: packages[name]['absolute_asset_paths'] = \ config[name]['absolute_asset_paths'] if 'extra_context' in config[name]: packages[name]['context'] = config[name]['extra_context'] if 'template_name' in config[name]: packages[name]['template'] = config[name]['template_name'] if 'variant' in config[name]: packages[name]['variant'] = config[name]['variant'] if 'manifest' in config[name]: packages[name]['manifest'] = config[name]['manifest'] return packages
class VersioningTest(TestCase): def setUp(self): self.versioning = Versioning() self.old_pipeline_version = settings.PIPELINE_VERSION def test_versioner_class(self): self.assertTrue(isinstance(self.versioning.versioner, MTimeVersioning)) def test_output_filename(self): output_filename = self.versioning.output_filename("css/master.?.css", "1308127956") self.assertEquals(output_filename, 'css/master.0.css') def test_output_filename_with_version(self): settings.PIPELINE_VERSION = True output_filename = self.versioning.output_filename("css/master.?.css", "1308127956") self.assertEquals(output_filename, 'css/master.1308127956.css') @patch.object(MTimeVersioning, 'version') def test_need_update(self, mock): mock.return_value = "1307480052" need_update, version = self.versioning.need_update('css/master.1308127956.css', ['css/first.css'], ) self.assertEquals(need_update, True) self.assertEquals(version, "1307480052") @patch.object(MTimeVersioning, 'need_update') def test_no_update(self, mock): mock.return_value = (False, "123456") need_update, version = self.versioning.need_update('css/master.123456.css', ['css/first.css'], ) self.assertTrue(mock.called) self.assertEquals(need_update, False) self.assertEquals(version, "123456") @patch.object(MTimeVersioning, 'version') def test_version(self, mock): mock.return_value = "123456" version = self.versioning.version(['css/first.css']) self.assertTrue(mock.called) self.assertEquals(version, "123456") def test_version_from_file(self): settings.PIPELINE_VERSION = True version = self.versioning.version_from_file('css/', "master.?.css") self.assertEquals(version, "123456") @patch.object(storage, 'exists') def test_no_cleanup(self, mock): self.versioning.cleanup('css/master.?.css') self.assertFalse(mock.called) @patch.object(storage, 'delete') def test_cleanup(self, mock): settings.PIPELINE_VERSION = True self.versioning.cleanup('css/master.?.css') mock.assert_called_with('css/master.123456.css') def tearDown(self): settings.PIPELINE_VERSION = self.old_pipeline_version