<s> from less import LessException <EOL> from less . settings import LESS_EXECUTABLE , LESS_ROOT , LESS_OUTPUT_DIR , LESS_OPTIONS <EOL> from django . conf import settings <EOL> import logging <EOL> import urlparse <EOL> import re <EOL> import os <EOL> import subprocess <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> STATIC_URL = getattr ( settings , "<STR_LIT>" , getattr ( settings , "<STR_LIT>" ) ) <EOL> class URLConverter ( object ) : <EOL> URL_PATTERN = re . compile ( r'<STR_LIT>' ) <EOL> def __init__ ( self , content , source_path ) : <EOL> self . content = content <EOL> self . source_dir = os . path . dirname ( source_path ) <EOL> if not self . source_dir . endswith ( '<STR_LIT:/>' ) : <EOL> self . source_dir = self . source_dir + '<STR_LIT:/>' <EOL> def convert_url ( self , matchobj ) : <EOL> url = matchobj . group ( <NUM_LIT:1> ) <EOL> url = url . strip ( '<STR_LIT>' ) <EOL> if url . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:/>' , '<STR_LIT>' ) ) : <EOL> return "<STR_LIT>" % url <EOL> return "<STR_LIT>" % urlparse . urljoin ( self . source_dir , url ) <EOL> def convert ( self ) : <EOL> return self . URL_PATTERN . sub ( self . convert_url , self . content ) <EOL> def compile_less ( input , output , less_path ) : <EOL> less_root = os . path . join ( LESS_ROOT , LESS_OUTPUT_DIR ) <EOL> if not os . path . exists ( less_root ) : <EOL> os . makedirs ( less_root ) <EOL> args = [ LESS_EXECUTABLE ] + LESS_OPTIONS + [ input ] <EOL> popen_kwargs = dict ( <EOL> stdout = subprocess . PIPE , <EOL> stderr = subprocess . PIPE , <EOL> ) <EOL> if os . name == "<STR_LIT>" : <EOL> popen_kwargs [ "<STR_LIT>" ] = True <EOL> p = subprocess . Popen ( args , ** popen_kwargs ) <EOL> out , errors = p . communicate ( ) <EOL> if errors : <EOL> logger . error ( errors ) <EOL> raise LessException ( errors ) <EOL> output_directory = os . path . dirname ( output ) <EOL> if not os . path . exists ( output_directory ) : <EOL> os . makedirs ( output_directory ) <EOL> compiled_css = URLConverter ( <EOL> out . decode ( settings . FILE_CHARSET ) , <EOL> os . path . join ( STATIC_URL , less_path ) <EOL> ) . convert ( ) <EOL> compiled_file = open ( output , "<STR_LIT>" ) <EOL> compiled_file . write ( compiled_css . encode ( settings . FILE_CHARSET ) ) <EOL> compiled_file . close ( ) </s>
