1
0

cleanup compress.py

This commit is contained in:
Mike Schwörer 2020-01-16 10:07:14 +01:00
parent 588e9b089a
commit 76b4709d1a

View File

@ -2,27 +2,30 @@
import sys import sys
import os import os
from subprocess import call
import re import re
import subprocess import subprocess
def findnext(str, start, chr):
depth = 0
for i in range(start, len(str)):
if (str[i] == chr): return i;
def findclose(str, start): def findnext(strdata, start, searchchr):
depth = 0 depth = 0
for i in range(start, len(str)): for i in range(start, len(strdata)):
if (str[i] == '{'): depth = depth+1; if strdata[i] == searchchr: return i;
if (str[i] == '}'): depth = depth-1;
if (depth == 0): return i;
def countnl(str, start, end):
def findclose(strdata, start):
depth = 0
for i in range(start, len(strdata)):
if strdata[i] == '{': depth = depth + 1;
if strdata[i] == '}': depth = depth - 1;
if depth == 0: return i;
def countnl(strdata, start, end):
cnt = 0 cnt = 0
for i in range(start, end+1): for i in range(start, end + 1):
if (str[i] == '\n'): cnt = cnt+1; if strdata[i] == '\n': cnt = cnt + 1;
return cnt; return cnt
def comment_remover(text): def comment_remover(text):
def replacer(match): def replacer(match):
@ -31,30 +34,32 @@ def comment_remover(text):
return " " # note: a space and not an empty string return " " # note: a space and not an empty string
else: else:
return s return s
pattern = re.compile( pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE re.DOTALL | re.MULTILINE
) )
return re.sub(pattern, replacer, text) return re.sub(pattern, replacer, text)
fsource = str.replace(sys.argv[1], '\\', '/') # scss fsource = str.replace(sys.argv[1], '\\', '/') # scss
finput = str.replace(sys.argv[2], '\\', '/') # css finput = str.replace(sys.argv[2], '\\', '/') # css
foutput = str.replace(sys.argv[3], '\\', '/') # min.css foutput = str.replace(sys.argv[3], '\\', '/') # min.css
ftemp1 = '__temp_compresss_py_1.tmp.css'; ftemp1 = '__temp_compresss_py_1.tmp.css'
ftemp2 = '__temp_compresss_py_2.tmp.css'; ftemp2 = '__temp_compresss_py_2.tmp.css'
print('======== INPUT ========'); print('======== INPUT ========')
print(); print()
print(fsource); print(fsource)
print(finput); print(finput)
print(foutput); print(foutput)
print(); print()
print(); print()
print('======== DELETE OLD DATA ========'); print('======== DELETE OLD DATA ========')
if os.path.isfile(finput): if os.path.isfile(finput):
try: try:
os.remove(finput); os.remove(finput)
print(finput + ' deleted') print(finput + ' deleted')
except: except:
print(sys.exc_info()[0]) print(sys.exc_info()[0])
@ -62,18 +67,18 @@ else:
print(finput + ' does not exist') print(finput + ' does not exist')
if os.path.isfile(foutput): if os.path.isfile(foutput):
try: try:
os.remove(foutput); os.remove(foutput)
print(foutput + ' deleted') print(foutput + ' deleted')
except: except:
print(sys.exc_info()[0]) print(sys.exc_info()[0])
else: else:
print(foutput + ' does not exist') print(foutput + ' does not exist')
print(); print()
print(); print()
print('======== CALL SCSS ========')
print('======== CALL SCSS ========'); out = subprocess.run(['ruby', 'scss', '--style=expanded', '--no-cache', '--update', fsource + ':' + finput],
out = subprocess.run(['ruby', 'scss', '--style=expanded', '--no-cache', '--update', fsource + ':' + finput], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('> scss.bat --style=expanded --no-cache --update ' + fsource + ':' + finput) print('> scss.bat --style=expanded --no-cache --update ' + fsource + ':' + finput)
print('STDOUT:') print('STDOUT:')
print(out.stdout.decode('utf-8')) print(out.stdout.decode('utf-8'))
@ -83,13 +88,13 @@ print(out.stderr.decode('utf-8'))
print('') print('')
print('') print('')
print('======== CLEANUP COMMENTS ========'); print('======== CLEANUP COMMENTS ========')
with open(finput, 'r') as tf: with open(finput, 'r') as tf:
data1 = tf.read() data1 = tf.read()
print(str(len(data1)) + ' characters read from ' + os.path.basename(finput)) print(str(len(data1)) + ' characters read from ' + os.path.basename(finput))
data1 = comment_remover(data1) data1 = comment_remover(data1)
print('Comments in css removed'); print('Comments in css removed')
with open(ftemp1, "w") as tf: with open(ftemp1, "w") as tf:
tf.write(data1) tf.write(data1)
@ -98,19 +103,19 @@ with open(ftemp1, "w") as tf:
print('') print('')
print('') print('')
print('======== CALL YUI ========')
print('======== CALL YUI ========'); out = subprocess.run(['java', '-jar', 'yuicompressor.jar', '--verbose', ftemp1, '-o', ftemp2], stdout=subprocess.PIPE,
out = subprocess.run(['java', '-jar', 'yuicompressor.jar', '--verbose', ftemp1, '-o', ftemp2], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stderr=subprocess.PIPE)
print('> java -jar yuicompressor.jar --verbose "'+finput+'" -o "'+ftemp2+'"') print('> java -jar yuicompressor.jar --verbose "' + finput + '" -o "' + ftemp2 + '"')
print('STDOUT:'); print('STDOUT:')
print(out.stdout.decode('utf-8')) print(out.stdout.decode('utf-8'))
print('STDERR:'); print('STDERR:')
print(out.stderr.decode('utf-8')) print(out.stderr.decode('utf-8'))
print('') print('')
print('') print('')
print('======== READ ========'); print('======== READ ========')
with open(ftemp2, 'r') as tf: with open(ftemp2, 'r') as tf:
data = tf.read() data = tf.read()
print(str(len(data)) + ' characters read from ' + ftemp2) print(str(len(data)) + ' characters read from ' + ftemp2)
@ -118,11 +123,11 @@ with open(ftemp2, 'r') as tf:
print('') print('')
print('') print('')
print('======== REM ========'); print('======== REM ========')
try: try:
os.remove(ftemp1); os.remove(ftemp1)
print(ftemp1 + ' deleted') print(ftemp1 + ' deleted')
os.remove(ftemp2); os.remove(ftemp2)
print(ftemp2 + ' deleted') print(ftemp2 + ' deleted')
except: except:
print(sys.exc_info()[0]) print(sys.exc_info()[0])
@ -130,30 +135,29 @@ except:
print('') print('')
print('') print('')
print('======== REGEX ========'); print('======== REGEX ========')
data = re.sub(r'(\}*\})', '\g<1>\n', data); data = re.sub(r'(\}*\})', '\g<1>\n', data)
print('css data modified (1)') print('css data modified (1)')
print('') print('')
print('') print('')
print('======== MEDIA ========'); print('======== MEDIA ========')
ins = [] ins = []
for i in range(len(data)): for i in range(len(data)):
if data[i:].startswith('@media'): if data[i:].startswith('@media'):
copen = findnext(data, i, '{') copen = findnext(data, i, '{')
cclose = findclose(data, copen) cclose = findclose(data, copen)
if (countnl(data, copen, cclose) == 0): continue; if countnl(data, copen, cclose) == 0: continue;
ins.append( (copen+1, '\n\t') ) ins.append((copen + 1, '\n\t'))
for i in range(copen+1, cclose): for i2 in range(copen + 1, cclose):
if data[i] == '\n': if data[i2] == '\n':
tp =(i+1, '\t') tp = (i2 + 1, '\t')
ins.append( tp ) ins.append(tp)
ins.append((cclose, '\n')) ins.append((cclose, '\n'))
print('media query at idx:' + str(i) + ' formatted') print('media query at idx:' + str(i) + ' formatted')
@ -163,7 +167,7 @@ for (l, c) in reversed(ins):
print('') print('')
print('') print('')
print('======== WRITE ========'); print('======== WRITE ========')
with open(foutput, "w") as tf: with open(foutput, "w") as tf:
tf.write(data) tf.write(data)
print(str(len(data)) + ' characters written to ' + foutput) print(str(len(data)) + ' characters written to ' + foutput)
@ -171,12 +175,12 @@ with open(foutput, "w") as tf:
print('') print('')
print('') print('')
print('======== REMOVE MAP ========'); print('======== REMOVE MAP ========')
if os.path.isfile(finput + '.map'): if os.path.isfile(finput + '.map'):
try: try:
os.remove(finput + '.map'); os.remove(finput + '.map')
print(finput + '.map' + ' deleted') print(finput + '.map' + ' deleted')
except e: except Exception as e:
print(e) print(e)
else: else:
print(finput + '.map' + ' does not exist') print(finput + '.map' + ' does not exist')
@ -184,5 +188,4 @@ else:
print('') print('')
print('') print('')
print('Finished.') print('Finished.')