/usr/share/pyshared/livereload/compiler.py is in python-livereload 1.0.1-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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #!/usr/bin/python
# -*- coding: utf-8 -*-
"""livereload.compiler
Provides a set of compilers for web developers.
Available compilers now:
+ less
+ coffee
+ uglifyjs
+ slimmer
"""
import os
import functools
import logging
from subprocess import Popen, PIPE
def make_folder(dest):
folder = os.path.split(dest)[0]
if not folder:
return
if os.path.isdir(folder):
return
try:
os.makedirs(folder)
except:
pass
def _get_http_file(url, build_dir='build/assets'):
import hashlib
key = hashlib.md5(url).hexdigest()
filename = os.path.join(os.getcwd(), build_dir, key)
if os.path.exists(filename):
return filename
make_folder(filename)
import urllib
print('Downloading: %s' % url)
urllib.urlretrieve(url, filename)
return filename
class BaseCompiler(object):
"""BaseCompiler
BaseCompiler defines the basic syntax of a Compiler.
>>> c = BaseCompiler('a')
>>> c.write('b') #: write compiled code to 'b'
>>> c.append('c') #: append compiled code to 'c'
"""
def __init__(self, path=None):
if path:
if path.startswith('http://') or path.startswith('https://'):
path = _get_http_file(path)
self.filetype = os.path.splitext(path)[1]
self.path = path
def get_code(self):
f = open(self.path)
code = f.read()
f.close()
return code
def write(self, output):
"""write code to output"""
logging.info('write %s' % output)
make_folder(output)
f = open(output, 'w')
code = self.get_code()
if code:
f.write(code)
f.close()
def append(self, output):
"""append code to output"""
logging.info('append %s' % output)
make_folder(output)
f = open(output, 'a')
f.write(self.get_code())
f.close()
def __call__(self, output, mode='w'):
if mode == 'a':
self.append(output)
return
self.write(output)
return
class CommandCompiler(BaseCompiler):
def init_command(self, command, source=None):
self.command = command
self.source = source
def get_code(self):
cmd = self.command.split()
if self.path:
cmd.append(self.path)
try:
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
except OSError as e:
logging.error(e)
if e.errno == os.errno.ENOENT: # file (command) not found
logging.error("maybe you haven't installed %s", cmd[0])
return None
if self.source:
stdout, stderr = p.communicate(input=self.source)
else:
stdout, stderr = p.communicate()
if stderr:
logging.error(stderr)
return None
#: stdout is bytes, decode for python3
return stdout.decode()
def lessc(path, output, mode='w'):
_compile = CommandCompiler(path)
_compile.init_command('lessc --compress')
return functools.partial(_compile, output, mode)
def uglifyjs(path, output, mode='w'):
_compile = CommandCompiler(path)
_compile.init_command('uglifyjs --nc')
return functools.partial(_compile, output, mode)
class SlimmerCompiler(BaseCompiler):
def get_code(self):
import slimmer
f = open(self.path)
code = f.read()
f.close()
if self.filetype == '.css':
return slimmer.css_slimmer(code)
if self.filetype == '.js':
return slimmer.js_slimmer(code)
if self.filetype == '.html':
return slimmer.xhtml_slimmer(code)
return code
def slimmer(path, output, mode='w'):
_compile = SlimmerCompiler(path)
return functools.partial(_compile, output, mode)
def rstc(path, output, mode='w'):
_compile = CommandCompiler(path)
_compile.init_command('rst2html.py')
return functools.partial(_compile, output, mode)
def shell(command, path=None, output=os.devnull, mode='w'):
_compile = CommandCompiler(path)
_compile.init_command(command)
return functools.partial(_compile, output, mode)
def coffee(path, output, mode='w'):
_compile = CommandCompiler(path)
f = open(path)
code = f.read()
f.close()
_compile.init_command('coffee --compile --stdio', code)
return functools.partial(_compile, output, mode)
|