patternjavascriptMinor
Python code to minify JS and CSS on the fly
Viewed 0 times
thecssandpythoncodeminifyfly
Problem
Below is the python code to compress js and css files.
```
import sys
import os
import glob
import shutil
import fnmatch
import os.path
import re
import argparse
import time
#Store the time the script starts
start_time = time.time()
total_files_compressed = 0
total_js_files_compressed = 0
total_css_files_compressed = 0
jsMinPath = 'min/js'
jsDevPath = 'js'
cssDevPath = 'css'
cssMinPath = 'min/css'
yuicompressorPath = 'yuicompressor.jar'
enable_log = False
retval = os.getcwd()
total_warnings = 0
total_errors = 0
print ("Minification script started")
def logErrorWarning(filepath):
global total_warnings
global total_errors
logf = open(filepath)
warning_count = 0
error_count = 0
for line in logf:
if "[ERROR]" in line:
error_count = error_count + 1
total_errors = total_errors + 1
if "[WARNING]" in line:
warning_count = warning_count + 1
total_warnings = total_warnings + 1
logf.close()
logOutput("Errors " + str(error_count) + " Total Warnings " + str(warning_count))
def logOutput(text):
print (text)
def incrementCompressedFileCount():
global total_files_compressed
total_files_compressed = total_files_compressed + 1
def incrementCompressedJSFileCount():
global total_js_files_compressed
total_js_files_compressed = total_js_files_compressed + 1
def incrementCompressedCSSFileCount():
global total_css_files_compressed
total_css_files_compressed = total_css_files_compressed + 1
def minifyJs():
if not os.path.exists(jsMinPath + "/log"):
os.makedirs(jsMinPath + "/log")
else:
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")
logfile = open(jsMinPath + "/log/fulllog.txt", 'w')
logfile.write('Minify script started for internal js
```
import sys
import os
import glob
import shutil
import fnmatch
import os.path
import re
import argparse
import time
#Store the time the script starts
start_time = time.time()
total_files_compressed = 0
total_js_files_compressed = 0
total_css_files_compressed = 0
jsMinPath = 'min/js'
jsDevPath = 'js'
cssDevPath = 'css'
cssMinPath = 'min/css'
yuicompressorPath = 'yuicompressor.jar'
enable_log = False
retval = os.getcwd()
total_warnings = 0
total_errors = 0
print ("Minification script started")
def logErrorWarning(filepath):
global total_warnings
global total_errors
logf = open(filepath)
warning_count = 0
error_count = 0
for line in logf:
if "[ERROR]" in line:
error_count = error_count + 1
total_errors = total_errors + 1
if "[WARNING]" in line:
warning_count = warning_count + 1
total_warnings = total_warnings + 1
logf.close()
logOutput("Errors " + str(error_count) + " Total Warnings " + str(warning_count))
def logOutput(text):
print (text)
def incrementCompressedFileCount():
global total_files_compressed
total_files_compressed = total_files_compressed + 1
def incrementCompressedJSFileCount():
global total_js_files_compressed
total_js_files_compressed = total_js_files_compressed + 1
def incrementCompressedCSSFileCount():
global total_css_files_compressed
total_css_files_compressed = total_css_files_compressed + 1
def minifyJs():
if not os.path.exists(jsMinPath + "/log"):
os.makedirs(jsMinPath + "/log")
else:
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")
logfile = open(jsMinPath + "/log/fulllog.txt", 'w')
logfile.write('Minify script started for internal js
Solution
if not os.path.exists(jsMinPath + "/log"):
os.makedirs(jsMinPath + "/log")
else:
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")could be
if os.path.exists(jsMinPath + "/log"):
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")Also
def contains(list, element):
flag = False
for x in list:
if x == element:
flag = True
return flagcould be
def contains(list, element):
return element in listand for that reason is probably not required (it doesn't seem to be used anyway).
No time to go further at the moment. I might edit my answer later on.
Code Snippets
if not os.path.exists(jsMinPath + "/log"):
os.makedirs(jsMinPath + "/log")
else:
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")if os.path.exists(jsMinPath + "/log"):
shutil.rmtree(jsMinPath + "/log")
os.makedirs(jsMinPath + "/log")def contains(list, element):
flag = False
for x in list:
if x == element:
flag = True
return flagdef contains(list, element):
return element in listContext
StackExchange Code Review Q#38011, answer score: 2
Revisions (0)
No revisions yet.