From 9c82c61cf8acb63e387a6b2513e1b3554eb765b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Wed, 3 Dec 2014 10:55:32 +0100 Subject: [PATCH] Add some arguments --- iwla.py | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/iwla.py b/iwla.py index a82f858..8b02c79 100755 --- a/iwla.py +++ b/iwla.py @@ -1,11 +1,14 @@ #!/usr/bin/env python import os +import shutil +import sys import re import time import pickle import gzip import importlib +import argparse from calendar import monthrange from datetime import date @@ -439,7 +442,7 @@ class IWLA(object): return True - def start(self): + def start(self, _file): print '==> Load previous database' self.meta_infos = self._deserialize(conf.META_PATH) or self._clearMeta() @@ -454,17 +457,16 @@ class IWLA(object): print '==> Analysing log' - with open(conf.analyzed_filename) as f: - for l in f: - # print "line " + l + for l in _file: + # print "line " + l - groups = self.log_re.match(l) + groups = self.log_re.match(l) - if groups: - if not self._newHit(groups.groupdict()): - break - else: - print "No match for " + l + if groups: + if not self._newHit(groups.groupdict()): + break + else: + print "No match for " + l #break if self.analyse_started: @@ -477,5 +479,29 @@ class IWLA(object): self._generateMonthStats() if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Intelligent Web Log Analyzer') + + parser.add_argument('-c', '--clean-output', dest='clean_output', action='store_true', + default=False, + help='Clean output before starting') + + parser.add_argument('-i', '--stdin', dest='stdin', action='store_true', + default=False, + help='Read data from stdin instead of conf.analyzed_filename') + + args = parser.parse_args() + + if args.clean_output: + if os.path.exists(conf.DB_ROOT): shutil.rmtree(conf.DB_ROOT) + if os.path.exists(conf.DISPLAY_ROOT): shutil.rmtree(conf.DISPLAY_ROOT) + iwla = IWLA() - iwla.start() + + if args.stdin: + iwla.start(sys.stdin) + else: + if not os.path.exists(conf.analyzed_filename): + print 'No such file \'%s\'' % (conf.analyzed_filename) + sys.exit(-1) + with open(conf.analyzed_filename) as f: + iwla.start(f)