minify3.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python3
  2. # Minify tableExport.jquery.plugin file(s)
  3. # using Simon Georget's python script to minify javascript files
  4. # https://github.com/simogeo/Filemanager/blob/master/utils/minify.py
  5. # Usage : $ python ./tools/minify.py
  6. class bcolors:
  7. HEADER = '\033[95m'
  8. OKBLUE = '\033[94m'
  9. OKGREEN = '\033[92m'
  10. WARNING = '\033[93m'
  11. FAIL = '\033[91m'
  12. ENDC = '\033[0m'
  13. def disable(self):
  14. self.HEADER = ''
  15. self.OKBLUE = ''
  16. self.OKGREEN = ''
  17. self.WARNING = ''
  18. self.FAIL = ''
  19. self.ENDC = ''
  20. import http.client, urllib.request, urllib.parse, urllib.error, sys, os
  21. fmRootFolder = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + "/"
  22. os.chdir(fmRootFolder) # set working directory
  23. toMinify = ["tableExport.js"]
  24. print(bcolors.HEADER + "-------------------------------------" + bcolors.ENDC)
  25. # we loop on JS languages files
  26. for index, item in enumerate(toMinify):
  27. # print index, item
  28. dir = os.path.dirname(item)
  29. file = os.path.basename(item)
  30. with open (fmRootFolder + item, "r") as myfile:
  31. js_input=myfile.read()
  32. # Define the parameters for the POST request and encode them in
  33. # a URL-safe format.
  34. params = urllib.parse.urlencode([
  35. ('js_code', js_input),
  36. # ('compilation_level', 'WHITESPACE_ONLY'),
  37. ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
  38. ('output_format', 'text'),
  39. ('output_info', 'compiled_code'),
  40. ])
  41. params2 = urllib.parse.urlencode([
  42. ('js_code', js_input),
  43. # ('compilation_level', 'WHITESPACE_ONLY'),
  44. ('compilation_level', 'SIMPLE_OPTIMIZATIONS'),
  45. ('output_format', 'text'),
  46. ('output_info', 'errors'),
  47. ])
  48. # Always use the following value for the Content-type header.
  49. headers = { "Content-type": "application/x-www-form-urlencoded" }
  50. conn = http.client.HTTPConnection('closure-compiler.appspot.com')
  51. conn.request('POST', '/compile', params, headers)
  52. response = conn.getresponse()
  53. data = response.read()
  54. # we write the minified file - os.path.splitext(file)[0] return filename without extension
  55. with open(fmRootFolder + dir + '/' + os.path.splitext(file)[0] + ".min.js", "w") as text_file:
  56. text_file.write(data.decode("utf-8"))
  57. # We retrieve errors
  58. conn.request('POST', '/compile', params2, headers)
  59. response = conn.getresponse()
  60. errors = response.read().decode("utf-8")
  61. if errors == "":
  62. print(bcolors.OKBLUE + file + " has been minified. No error found.")
  63. else:
  64. print(bcolors.FAIL + file + " : the code contains errors : ")
  65. print("")
  66. print(errors + bcolors.ENDC)
  67. conn.close()
  68. print(bcolors.HEADER + "-------------------------------------" + bcolors.ENDC)