From 4f4e4648438819f3316d7ab4cae9a6589c22f752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Mon, 15 Dec 2014 21:28:25 +0100 Subject: [PATCH] Add file compression --- TODO | 4 ++-- conf.py | 2 ++ default_conf.py | 3 +++ iwla.py | 25 ++++++++++++++++++++++++- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 7f71f61..e18bc14 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,7 @@ doc auto generation doc enhancement Limit hits/pages/downloads by rate Automatic tests -quiet mode Add Licence Free memory as soon as possible -gzip output files \ No newline at end of file +gzip output files +different debug output levels \ No newline at end of file diff --git a/conf.py b/conf.py index 181f7b3..22d8e17 100644 --- a/conf.py +++ b/conf.py @@ -24,3 +24,5 @@ hit_to_page_conf = [r'^.+/category/.+$', r'^.+/tag/.+$', r'^.+/archive/.+$', r'^ # Because it's too long to build HTML when there is too much entries max_hits_displayed = 100 max_downloads_displayed = 100 + +compress_output_files = ['html', 'css', 'js'] diff --git a/default_conf.py b/default_conf.py index 0d1df53..7ccd2ff 100644 --- a/default_conf.py +++ b/default_conf.py @@ -48,3 +48,6 @@ resources_path = ['resources'] icon_path = '%s/%s' % (os.path.basename(resources_path[0]), 'icon') # CSS path (you can add yours) css_path = ['%s/%s/%s' % (os.path.basename(resources_path[0]), 'css', 'iwla.css')] + +# Extensions to compress in gzip during display build +compress_output_files = [] diff --git a/iwla.py b/iwla.py index bfdf62a..c5e55a6 100755 --- a/iwla.py +++ b/iwla.py @@ -31,6 +31,7 @@ from display import * # Conf values needed : # analyzed_filename # domain_name +# compress_output_files* # # Output files : # DB_ROOT/meta.db @@ -40,7 +41,7 @@ from display import * # # Statistics creation : # -# meta => +# meta : # last_time # start_analysis_time # stats => @@ -441,11 +442,33 @@ class IWLA(object): self.display.addPage(page) + def _compressFile(self, build_time, root, filename): + path = os.path.join(root, filename) + gz_path = path + '.gz' + #print 'Compress %s => %s' % (path, gz_path) + if not os.path.exists(gz_path) or\ + os.stat(filename).st_mtime > build_time: + with open(path, 'rb') as f_in: + with gzip.open(gz_path, 'wb') as f_out: + f_out.write(f_in.read()) + + def _compressFiles(self, build_time, root): + if not conf.compress_output_files: return + print root + for rootdir, subdirs, files in os.walk(root, followlinks=True): + for f in files: + for ext in conf.compress_output_files: + if f.endswith(ext): + self._compressFile(build_time, rootdir, f) + break + def _generateDisplay(self): self._generateDisplayDaysStats() self._callPlugins(conf.DISPLAY_HOOK_DIRECTORY) self._generateDisplayWholeMonthStats() + build_time = time.localtime() self.display.build(conf.DISPLAY_ROOT) + self._compressFiles(build_time, conf.DISPLAY_ROOT) def _createEmptyStats(self): stats = {}