def test_compile_file(self, tmpdir): from jac import CompressorExtension ext = CompressorExtension(mock.Mock(compressor_output_dir=tmpdir, compressor_static_prefix='/static', compressor_source_dirs=[str(tmpdir)])) static_file = os.path.join(str(tmpdir), 'test.sass') with open(static_file, 'w', encoding='utf-8') as f: f.write('''$blue: #3bbfce $margin: 16px .content-navigation border-color: $blue color: darken($blue, 9%) .border padding: $margin / 2 margin: $margin / 2 border-color: $blue''') html = '<link type="text/sass" rel="stylesheet" src="test.sass">' expected_hash = hashlib.md5(utf8_encode(html)) with open(static_file) as f: expected_hash.update(utf8_encode(f.read())) assert ext._compress_block('css', mock.Mock(return_value=html)) == \ '<link type="text/css" rel="stylesheet" href="/static/{}.css">'.format(expected_hash.hexdigest())
def test_compile_file(self, tmpdir): from jac import CompressorExtension ext = CompressorExtension( mock.Mock(compressor_output_dir=tmpdir, compressor_static_prefix='/static', compressor_source_dirs=[str(tmpdir)])) static_file = os.path.join(str(tmpdir), 'test.sass') with open(static_file, 'w', encoding='utf-8') as f: f.write('''$blue: #3bbfce $margin: 16px .content-navigation border-color: $blue color: darken($blue, 9%) .border padding: $margin / 2 margin: $margin / 2 border-color: $blue''') html = '<link type="text/sass" rel="stylesheet" src="test.sass" />' expected_hash = hashlib.md5(utf8_encode(html)) with open(static_file) as f: expected_hash.update(utf8_encode(f.read())) assert ext._compress_block( 'css', mock.Mock(return_value=html) ) == '<link type="text/css" rel="stylesheet" href="/static/{}.css" />'.format( expected_hash.hexdigest())
def compile(cls, what, mimetype='text/less', cwd=None, uri_cwd=None, debug=None): args = ['lessc'] if not debug: args += ['--compress'] if cwd: args += ['-ru'] args += ['--include-path={}'.format(cwd)] if uri_cwd: if not uri_cwd.endswith('/'): uri_cwd += '/' args += ['--rootpath={}'.format(uri_cwd)] args += ['-'] handler = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None) if isinstance(what, file): what = what.read() (stdout, stderr) = handler.communicate(input=utf8_encode(what)) stdout = u(stdout) if handler.returncode == 0: return stdout else: raise RuntimeError('Test this :S %s' % stderr)
def compile(cls, what, mimetype='text/coffeescript', cwd=None, uri_cwd=None, debug=None): args = ['coffee', '--compile', '--stdio'] handler = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None) if isinstance(what, file): what = what.read() (stdout, stderr) = handler.communicate(input=utf8_encode(what)) stdout = u(stdout) if not debug: stdout = jsmin(stdout) if handler.returncode == 0: return stdout else: raise RuntimeError('Test this :S %s' % stderr)
def compile(cls, what, mimetype='text/sass', cwd=None, uri_cwd=None, debug=None): args = ['sass', '-s'] if mimetype == 'text/scss': args.append('--scss') if cwd: args += ['-I', cwd] handler = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None) if isinstance(what, file): what = what.read() (stdout, stderr) = handler.communicate(input=utf8_encode(what)) stdout = u(stdout) if handler.returncode == 0: return stdout else: raise RuntimeError('Test this :S %s' % stderr)
def make_hash(self, html): soup = BeautifulSoup(html, PARSER) compilables = self.find_compilable_tags(soup) html_hash = hashlib.md5(utf8_encode(html)) for c in compilables: url = c.get('src') or c.get('href') if url: with open(self.find_file(url), 'r', encoding='utf-8') as f: while True: content = f.read(1024) if content: html_hash.update(utf8_encode(content)) else: break return html_hash.hexdigest()
def make_hash(self, html): soup = BeautifulSoup(html) compilables = self.find_compilable_tags(soup) html_hash = hashlib.md5(utf8_encode(html)) for c in compilables: url = c.get('src') or c.get('href') if url: with open(self.find_file(url), 'r', encoding='utf-8') as f: while True: content = f.read(1024) if content: html_hash.update(utf8_encode(content)) else: break return html_hash.hexdigest()
def compile(cls, what, mimetype='text/less', cwd=None, uri_cwd=None, debug=None): args = [] if not debug: args += ['--compress'] if cwd: args += ['-ru'] args += ['--include-path={}'.format(cwd)] if uri_cwd: if not uri_cwd.endswith('/'): uri_cwd += '/' args += ['--rootpath={}'.format(uri_cwd)] if cls.extra_args: args.extend(cls.extra_args) args += ['-'] args.insert(0, cls.binary) try: handler = subprocess.Popen(args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None) except OSError as e: msg = '{0} encountered an error when executing {1}: {2}'.format( cls.__name__, cls.binary, u(e), ) if e.errno == errno.ENOENT: msg += ' Make sure {0} is in your PATH.'.format(cls.binary) raise InvalidCompressorError(msg) if isinstance(what, file): what = what.read() (stdout, stderr) = handler.communicate(input=utf8_encode(what)) stdout = u(stdout) if handler.returncode == 0: return stdout else: raise RuntimeError('Test this :S %s' % stderr)
def make_hash(self, html): if self.config.compressor_offline_compress and html in self.offline_hash_cache: return self.offline_hash_cache[html] soup = BeautifulSoup(html, PARSER) compilables = self.find_compilable_tags(soup) html_hash = hashlib.md5(utf8_encode(html)) for c in compilables: url = c.get('src') or c.get('href') if url: with open(self.find_file(url), 'r', encoding='utf-8') as f: while True: content = f.read(1024) if content: html_hash.update(utf8_encode(content)) else: break digest = html_hash.hexdigest() if self.config.compressor_offline_compress: self.offline_hash_cache[html] = digest return digest
def compile(cls, what, mimetype="text/coffeescript", cwd=None, uri_cwd=None, debug=None): args = ["coffee", "--compile", "--stdio"] handler = subprocess.Popen( args, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, cwd=None ) if isinstance(what, file): what = what.read() (stdout, stderr) = handler.communicate(input=utf8_encode(what)) stdout = u(stdout) if not debug: stdout = jsmin(stdout) if handler.returncode == 0: return stdout else: raise RuntimeError("Test this :S %s" % stderr)