22
22
from doctest import testmod
23
23
from hashlib import sha1
24
24
from multiprocessing import cpu_count , Pool
25
+ from shutil import disk_usage
25
26
from tempfile import gettempdir
26
27
from time import sleep
27
28
28
29
try :
29
30
from urllib import request
30
31
from subprocess import getoutput
31
- import resource # windows dont have resource
32
+ from io import StringIO # pure-Python StringIO supports unicode.
32
33
except ImportError :
33
- request = getoutput = resource = None
34
+ request = getoutput = None
35
+ from StringIO import StringIO # lint:ok
34
36
try :
35
- from StringIO import StringIO # pure-Python StringIO supports unicode.
37
+ import resource # windows dont have resource
36
38
except ImportError :
37
- from io import StringIO # lint:ok
39
+ resource = None
38
40
39
41
40
- __version__ = "1.0.13 "
42
+ __version__ = "1.0.14 "
41
43
__license__ = "GPLv3+ LGPLv3+"
42
44
__author__ = "Juan Carlos"
43
45
__email__ = "juancarlospaco@gmail.com"
@@ -897,6 +899,7 @@ def process_multiple_files(file_path):
897
899
else :
898
900
previous = actual
899
901
log .debug ("Modification detected on {}." .format (file_path ))
902
+ check_working_folder (os .path .dirname (file_path ))
900
903
if file_path .endswith (".css" ):
901
904
process_single_css_file (file_path )
902
905
elif file_path .endswith (".js" ):
@@ -943,6 +946,7 @@ def process_single_css_file(css_file_path):
943
946
except : # Python2
944
947
with open (css_file_path ) as css_file :
945
948
original_css = css_file .read ()
949
+ log .debug ("INPUT: Reading CSS file {}." .format (css_file_path ))
946
950
minified_css = css_minify (original_css ,
947
951
wrap = args .wrap , comments = args .comments )
948
952
if args .timestamp :
@@ -955,20 +959,20 @@ def process_single_css_file(css_file_path):
955
959
gz_file_path = prefixer_extensioner (
956
960
css_file_path , ".css" ,
957
961
".css.gz" if args .overwrite else ".min.css.gz" , original_css )
962
+ log .debug ("OUTPUT: Writing gZIP CSS Minified {}." .format (gz_file_path ))
958
963
try :
959
964
with open (min_css_file_path , "w" , encoding = "utf-8" ) as output_file :
960
965
output_file .write (minified_css )
961
966
if only_on_py3 (args .gzip ):
962
967
with gzip .open (gz_file_path , "wt" , encoding = "utf-8" ) as output_gz :
963
968
output_gz .write (minified_css )
964
- log .debug ("Saving GZIPed Minified file {}." .format (gz_file_path ))
965
969
except :
966
970
with open (min_css_file_path , "w" ) as output_file :
967
971
output_file .write (minified_css )
968
972
if only_on_py3 (args .gzip ):
969
973
with gzip .open (gz_file_path , "w" ) as output_gz :
970
974
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 ))
972
976
973
977
974
978
def process_single_html_file (html_file_path ):
@@ -982,13 +986,16 @@ def process_single_html_file(html_file_path):
982
986
with open (html_file_path ) as html_file :
983
987
minified_html = html_minify (html_file .read (),
984
988
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" )
986
992
try : # Python3
987
993
with open (html_file_path , "w" , encoding = "utf-8" ) as output_file :
988
994
output_file .write (minified_html )
989
995
except : # Python2
990
996
with open (html_file_path , "w" ) as output_file :
991
997
output_file .write (minified_html )
998
+ log .debug ("OUTPUT: Writing HTML Minified {}." .format (html_file_path ))
992
999
993
1000
994
1001
def process_single_js_file (js_file_path ):
@@ -1000,6 +1007,7 @@ def process_single_js_file(js_file_path):
1000
1007
except : # Python2
1001
1008
with open (js_file_path ) as js_file :
1002
1009
original_js = js_file .read ()
1010
+ log .debug ("INPUT: Reading JS file {}." .format (js_file_path ))
1003
1011
if args .obfuscate : # with obfuscation
1004
1012
minified_js = simple_replacer_js (js_minify (js_minify2 (original_js )))
1005
1013
else : # without obfuscation
@@ -1014,20 +1022,20 @@ def process_single_js_file(js_file_path):
1014
1022
gz_file_path = prefixer_extensioner (
1015
1023
js_file_path , ".js" , ".js.gz" if args .overwrite else ".min.js.gz" ,
1016
1024
original_js )
1025
+ log .debug ("OUTPUT: Writing gZIP JS Minified {}." .format (gz_file_path ))
1017
1026
try : # Python3
1018
1027
with open (min_js_file_path , "w" , encoding = "utf-8" ) as output_file :
1019
1028
output_file .write (minified_js )
1020
1029
if only_on_py3 (args .gzip ):
1021
1030
with gzip .open (gz_file_path , "wt" , encoding = "utf-8" ) as output_gz :
1022
1031
output_gz .write (minified_js )
1023
- log .debug ("Saving GZIPed Minified file {}." .format (gz_file_path ))
1024
1032
except : # Python2
1025
1033
with open (min_js_file_path , "w" ) as output_file :
1026
1034
output_file .write (minified_js )
1027
1035
if only_on_py3 (args .gzip ):
1028
1036
with gzip .open (gz_file_path , "w" ) as output_gz :
1029
1037
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 ))
1031
1039
1032
1040
1033
1041
def check_for_updates ():
@@ -1054,6 +1062,25 @@ def only_on_py3(boolean_argument=True):
1054
1062
return False
1055
1063
1056
1064
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
+
1057
1084
def make_arguments_parser ():
1058
1085
"""Build and return a command line agument parser."""
1059
1086
if not sys .platform .startswith ("win" ) and sys .stderr .isatty ():
@@ -1162,27 +1189,30 @@ def main():
1162
1189
log .info (__doc__ + __version__ )
1163
1190
if only_on_py3 ((args .before , getoutput )):
1164
1191
log .info (getoutput (str (args .before )))
1192
+ check_working_folder (os .path .dirname (args .fullpath ))
1165
1193
# Work based on if argument is file or folder, folder is slower.
1166
1194
if os .path .isfile (args .fullpath ) and args .fullpath .endswith (".css" ):
1167
1195
log .info ("Target is a CSS File." )
1168
1196
list_of_files = str (args .fullpath )
1169
1197
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 "" ) )
1173
1201
list_of_files = str (args .fullpath )
1174
1202
process_single_html_file (args .fullpath )
1175
1203
elif os .path .isfile (args .fullpath ) and args .fullpath .endswith (".js" ):
1176
1204
log .info ("Target is a JS File." )
1177
1205
list_of_files = str (args .fullpath )
1178
1206
process_single_js_file (args .fullpath )
1179
1207
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 "" ))
1181
1210
log .warning ("Processing a whole Folder may take some time..." )
1182
1211
list_of_files = walkdir_to_filelist (
1183
1212
args .fullpath ,
1184
1213
(".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 ()))
1186
1216
pool = Pool (cpu_count ()) # Multiprocessing Async
1187
1217
pool .map_async (process_multiple_files , list_of_files )
1188
1218
pool .close ()
0 commit comments