Add some arguments

This commit is contained in:
Grégory Soutadé 2014-12-03 10:55:32 +01:00
parent 95023a5db3
commit 9c82c61cf8
1 changed files with 37 additions and 11 deletions

48
iwla.py
View File

@ -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)