Skip to content

Commit 0bc67e7

Browse files
Fix #8, Logging improvements, Check working folder and free space [SKIP CI]
1 parent 3805dcd commit 0bc67e7

File tree

1 file changed

+45
-15
lines changed

1 file changed

+45
-15
lines changed

css-html-js-minify.py

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,24 @@
2222
from doctest import testmod
2323
from hashlib import sha1
2424
from multiprocessing import cpu_count, Pool
25+
from shutil import disk_usage
2526
from tempfile import gettempdir
2627
from time import sleep
2728

2829
try:
2930
from urllib import request
3031
from subprocess import getoutput
31-
import resource # windows dont have resource
32+
from io import StringIO # pure-Python StringIO supports unicode.
3233
except ImportError:
33-
request = getoutput = resource = None
34+
request = getoutput = None
35+
from StringIO import StringIO # lint:ok
3436
try:
35-
from StringIO import StringIO # pure-Python StringIO supports unicode.
37+
import resource # windows dont have resource
3638
except ImportError:
37-
from io import StringIO # lint:ok
39+
resource = None
3840

3941

40-
__version__ = "1.0.13"
42+
__version__ = "1.0.14"
4143
__license__ = "GPLv3+ LGPLv3+"
4244
__author__ = "Juan Carlos"
4345
__email__ = "juancarlospaco@gmail.com"
@@ -897,6 +899,7 @@ def process_multiple_files(file_path):
897899
else:
898900
previous = actual
899901
log.debug("Modification detected on {}.".format(file_path))
902+
check_working_folder(os.path.dirname(file_path))
900903
if file_path.endswith(".css"):
901904
process_single_css_file(file_path)
902905
elif file_path.endswith(".js"):
@@ -943,6 +946,7 @@ def process_single_css_file(css_file_path):
943946
except: # Python2
944947
with open(css_file_path) as css_file:
945948
original_css = css_file.read()
949+
log.debug("INPUT: Reading CSS file {}.".format(css_file_path))
946950
minified_css = css_minify(original_css,
947951
wrap=args.wrap, comments=args.comments)
948952
if args.timestamp:
@@ -955,20 +959,20 @@ def process_single_css_file(css_file_path):
955959
gz_file_path = prefixer_extensioner(
956960
css_file_path, ".css",
957961
".css.gz" if args.overwrite else ".min.css.gz", original_css)
962+
log.debug("OUTPUT: Writing gZIP CSS Minified {}.".format(gz_file_path))
958963
try:
959964
with open(min_css_file_path, "w", encoding="utf-8") as output_file:
960965
output_file.write(minified_css)
961966
if only_on_py3(args.gzip):
962967
with gzip.open(gz_file_path, "wt", encoding="utf-8") as output_gz:
963968
output_gz.write(minified_css)
964-
log.debug("Saving GZIPed Minified file {}.".format(gz_file_path))
965969
except:
966970
with open(min_css_file_path, "w") as output_file:
967971
output_file.write(minified_css)
968972
if only_on_py3(args.gzip):
969973
with gzip.open(gz_file_path, "w") as output_gz:
970974
output_gz.write(minified_css)
971-
log.debug("Saving GZIPed Minified file {}.".format(gz_file_path))
975+
log.debug("OUTPUT: Writing CSS Minified {}.".format(min_css_file_path))
972976

973977

974978
def process_single_html_file(html_file_path):
@@ -982,13 +986,16 @@ def process_single_html_file(html_file_path):
982986
with open(html_file_path) as html_file:
983987
minified_html = html_minify(html_file.read(),
984988
comments=only_on_py3(args.comments))
985-
html_file_path = prefixer_extensioner(html_file_path, ".html", ".html")
989+
log.debug("INPUT: Reading HTML file {}.".format(html_file_path))
990+
html_file_path = prefixer_extensioner(
991+
html_file_path, ".html" if args.overwrite else ".htm", ".html")
986992
try: # Python3
987993
with open(html_file_path, "w", encoding="utf-8") as output_file:
988994
output_file.write(minified_html)
989995
except: # Python2
990996
with open(html_file_path, "w") as output_file:
991997
output_file.write(minified_html)
998+
log.debug("OUTPUT: Writing HTML Minified {}.".format(html_file_path))
992999

9931000

9941001
def process_single_js_file(js_file_path):
@@ -1000,6 +1007,7 @@ def process_single_js_file(js_file_path):
10001007
except: # Python2
10011008
with open(js_file_path) as js_file:
10021009
original_js = js_file.read()
1010+
log.debug("INPUT: Reading JS file {}.".format(js_file_path))
10031011
if args.obfuscate: # with obfuscation
10041012
minified_js = simple_replacer_js(js_minify(js_minify2(original_js)))
10051013
else: # without obfuscation
@@ -1014,20 +1022,20 @@ def process_single_js_file(js_file_path):
10141022
gz_file_path = prefixer_extensioner(
10151023
js_file_path, ".js", ".js.gz" if args.overwrite else ".min.js.gz",
10161024
original_js)
1025+
log.debug("OUTPUT: Writing gZIP JS Minified {}.".format(gz_file_path))
10171026
try: # Python3
10181027
with open(min_js_file_path, "w", encoding="utf-8") as output_file:
10191028
output_file.write(minified_js)
10201029
if only_on_py3(args.gzip):
10211030
with gzip.open(gz_file_path, "wt", encoding="utf-8") as output_gz:
10221031
output_gz.write(minified_js)
1023-
log.debug("Saving GZIPed Minified file {}.".format(gz_file_path))
10241032
except: # Python2
10251033
with open(min_js_file_path, "w") as output_file:
10261034
output_file.write(minified_js)
10271035
if only_on_py3(args.gzip):
10281036
with gzip.open(gz_file_path, "w") as output_gz:
10291037
output_gz.write(minified_js)
1030-
log.debug("Saving GZIPed Minified file {}.".format(gz_file_path))
1038+
log.debug("OUTPUT: Writing JS Minified {}.".format(min_js_file_path))
10311039

10321040

10331041
def check_for_updates():
@@ -1054,6 +1062,25 @@ def only_on_py3(boolean_argument=True):
10541062
return False
10551063

10561064

1065+
def check_working_folder(folder_to_check):
1066+
"""Check destination folder."""
1067+
log.debug("Checking the Working Folder: {}.".format(folder_to_check))
1068+
# What if folder is not a folder.
1069+
if not os.path.isdir(folder_to_check):
1070+
log.critical("Folder {} does not exist !.".format(folder_to_check))
1071+
# What if destination folder is not Readable by the user.
1072+
elif not os.access(folder_to_check, os.R_OK):
1073+
log.critical("Folder {} not Readable !.".format(folder_to_check))
1074+
# What if destination folder is not Writable by the user.
1075+
elif not os.access(folder_to_check, os.W_OK):
1076+
log.critical("Folder {} Not Writable !.".format(folder_to_check))
1077+
hdd = int(disk_usage(folder_to_check).free / 1024 / 1024 / 1024)
1078+
if hdd: # > 1 Gb
1079+
log.info("Total Free Space: ~{} GigaBytes.".format(hdd))
1080+
else: # < 1 Gb
1081+
log.critical("Total Free Space is < 1 GigaByte; Epic Fail !.")
1082+
1083+
10571084
def make_arguments_parser():
10581085
"""Build and return a command line agument parser."""
10591086
if not sys.platform.startswith("win") and sys.stderr.isatty():
@@ -1162,27 +1189,30 @@ def main():
11621189
log.info(__doc__ + __version__)
11631190
if only_on_py3((args.before, getoutput)):
11641191
log.info(getoutput(str(args.before)))
1192+
check_working_folder(os.path.dirname(args.fullpath))
11651193
# Work based on if argument is file or folder, folder is slower.
11661194
if os.path.isfile(args.fullpath) and args.fullpath.endswith(".css"):
11671195
log.info("Target is a CSS File.")
11681196
list_of_files = str(args.fullpath)
11691197
process_single_css_file(args.fullpath)
1170-
elif os.path.isfile(args.fullpath
1171-
) and args.fullpath.endswith((".htm", ".html")):
1172-
log.info("Target is a HTML File.")
1198+
elif os.path.isfile(args.fullpath) and args.fullpath.endswith(
1199+
".html" if args.overwrite else ".htm"):
1200+
log.info("Target is HTM{} File.".format("L" if args.overwrite else ""))
11731201
list_of_files = str(args.fullpath)
11741202
process_single_html_file(args.fullpath)
11751203
elif os.path.isfile(args.fullpath) and args.fullpath.endswith(".js"):
11761204
log.info("Target is a JS File.")
11771205
list_of_files = str(args.fullpath)
11781206
process_single_js_file(args.fullpath)
11791207
elif os.path.isdir(args.fullpath):
1180-
log.info("Target is a Folder with CSS, HTML, JS.")
1208+
log.info("Target is a Folder with CSS, HTM{}, JS files !.".format(
1209+
"L" if args.overwrite else ""))
11811210
log.warning("Processing a whole Folder may take some time...")
11821211
list_of_files = walkdir_to_filelist(
11831212
args.fullpath,
11841213
(".css", ".js", ".html" if args.overwrite else ".htm"),
1185-
tuple() if args.overwrite else (".min.css", ".min.js", ".htm"))
1214+
(".min.css", ".min.js", ".htm" if args.overwrite else ".html"))
1215+
log.info('Total Maximum CPUs used: ~{} Cores.'.format(cpu_count()))
11861216
pool = Pool(cpu_count()) # Multiprocessing Async
11871217
pool.map_async(process_multiple_files, list_of_files)
11881218
pool.close()

0 commit comments

Comments
 (0)