/
utils.py
206 lines (148 loc) · 5.65 KB
/
utils.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import os
import re
import socket
import subprocess
from hashlib import md5
from warnings import warn
from django.core.cache import cache as default_cache
from django.core.cache import get_cache as base_get_cache
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
from django.utils.encoding import smart_bytes, smart_str
from static_precompiler.exceptions import CompilerNotFound, UnsupportedFile
from static_precompiler.settings import CACHE_NAME, COMPILERS, MTIME_DELAY, POSIX_COMPATIBLE, STATIC_URL
try:
from importlib import import_module
except ImportError:
from django.utils.importlib import import_module
if six.PY2:
# noinspection PyUnresolvedReferences
from urlparse import urljoin
else:
# noinspection PyUnresolvedReferences
from urllib.parse import urljoin
def normalize_path(posix_path):
""" Convert posix style path to OS-dependent path.
"""
if POSIX_COMPATIBLE:
return posix_path
return os.path.join(*posix_path.split("/"))
def fix_line_breaks(text):
""" Convert Win line breaks to Unix
"""
return text.replace("\r\n", "\n")
def get_hexdigest(plaintext, length=None):
digest = md5(smart_bytes(plaintext)).hexdigest()
if length:
return digest[:length]
return digest
def get_cache():
if CACHE_NAME:
return base_get_cache(CACHE_NAME)
return default_cache
def get_cache_key(key):
return "static_precompiler.{0}.{1}".format(socket.gethostname(), key)
def get_mtime_cachekey(filename):
return get_cache_key("mtime.{0}".format(get_hexdigest(filename)))
def get_mtime(filename):
if MTIME_DELAY:
key = get_mtime_cachekey(filename)
cache = get_cache()
mtime = cache.get(key)
if mtime is None:
mtime = os.path.getmtime(filename)
cache.set(key, mtime, MTIME_DELAY)
return mtime
return os.path.getmtime(filename)
# noinspection PyShadowingBuiltins
def run_command(args, input=None, cwd=None):
popen_kwargs = dict(
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if cwd is not None:
popen_kwargs["cwd"] = cwd
if input is not None:
popen_kwargs["stdin"] = subprocess.PIPE
if os.name == "nt":
popen_kwargs["shell"] = True
p = subprocess.Popen(args, **popen_kwargs)
if input:
input = smart_bytes(input)
output, error = p.communicate(input)
return smart_str(output), smart_str(error)
class URLConverter(object):
URL_PATTERN = re.compile(r"url\(([^\)]+)\)")
@staticmethod
def convert_url(url, source_dir):
assert source_dir[-1] == "/"
url = url.strip(' \'"')
if url.startswith(('http://', 'https://', '/', 'data:')):
return url
return urljoin(STATIC_URL, urljoin(source_dir, url))
def convert(self, content, path):
source_dir = os.path.dirname(path)
if not source_dir.endswith("/"):
source_dir += "/"
return self.URL_PATTERN.sub(
lambda matchobj: "url('{0}')".format(
self.convert_url(matchobj.group(1), source_dir)
),
content
)
url_converter = URLConverter()
def convert_urls(content, path):
return url_converter.convert(content, path)
def _build_compilers():
# noinspection PyShadowingNames
compilers = {}
for compiler_path in COMPILERS:
compiler_options = {}
if isinstance(compiler_path, (tuple, list)):
if len(compiler_path) != 2:
raise ImproperlyConfigured(
'Compiler must be specified in the format ("path.to.CompilerClass", {{compiler options...}}),'
' got {0}'.format(compiler_path)
)
compiler_path, compiler_options = compiler_path
if not isinstance(compiler_options, dict):
raise ImproperlyConfigured('Compiler options must be a dict, got {0}'.format(compiler_options))
try:
compiler_module, compiler_classname = compiler_path.rsplit('.', 1)
except ValueError:
raise ImproperlyConfigured('{0} isn\'t a compiler module'.format(compiler_path))
try:
mod = import_module(compiler_module)
except ImportError as e:
raise ImproperlyConfigured('Error importing compiler {0}: "{1}"'.format(compiler_module, e))
try:
compiler_class = getattr(mod, compiler_classname)
except AttributeError:
raise ImproperlyConfigured(
'Compiler module "{0}" does not define a "{1}" class'.format(compiler_module, compiler_classname)
)
compiler_to_add = compiler_class(**compiler_options)
compiler = compilers.setdefault(compiler_class.name, compiler_to_add)
if compiler_to_add != compiler:
warn("Both compilers {0} and {1} have the same name.".format(compiler_to_add, compiler))
return compilers
compilers = None
def get_compilers():
global compilers
if compilers is None:
compilers = _build_compilers()
return compilers
def get_compiler_by_name(name):
try:
return get_compilers()[name]
except KeyError:
raise CompilerNotFound("There is no compiler with name '{0}'.".format(name))
def get_compiler_by_path(path):
for compiler in six.itervalues(get_compilers()):
if compiler.is_supported(path):
return compiler
raise UnsupportedFile("The source file '{0}' is not supported by any of available compilers.".format(path))
def compile_static(path):
return get_compiler_by_path(path).compile(path)
def compile_static_lazy(path):
return get_compiler_by_path(path).compile_lazy(path)