Add --dry-run (-D) argument

This commit is contained in:
Gregory Soutade 2017-05-25 21:03:46 +02:00
parent d08085faf0
commit 4bc2c1ad4b
2 changed files with 26 additions and 13 deletions

View File

@ -351,6 +351,8 @@ class DisplayHTMLPage(object):
self.logger.debug('Write %s' % (filename))
if self.iwla.dry_run: return
f = codecs.open(filename, 'w', 'utf-8')
f.write(u'<!DOCTYPE html>')
f.write(u'<html>')
@ -398,14 +400,15 @@ class DisplayHTMLBuild(object):
self.pages.append(page)
def build(self, root):
display_root = self.iwla.getConfValue('DISPLAY_ROOT', '')
if not os.path.exists(display_root):
os.makedirs(display_root)
for res_path in self.iwla.getResourcesPath():
target = os.path.abspath(res_path)
link_name = os.path.join(display_root, res_path)
if not os.path.exists(link_name):
os.symlink(target, link_name)
if not self.iwla.dry_run:
display_root = self.iwla.getConfValue('DISPLAY_ROOT', '')
if not os.path.exists(display_root):
os.makedirs(display_root)
for res_path in self.iwla.getResourcesPath():
target = os.path.abspath(res_path)
link_name = os.path.join(display_root, res_path)
if not os.path.exists(link_name):
os.symlink(target, link_name)
for page in self.pages:
page.build(root, filters=self.filters)

20
iwla.py
View File

@ -133,7 +133,7 @@ class IWLA(object):
API_VERSION = 1
IWLA_VERSION = '0.5-dev'
def __init__(self, logLevel):
def __init__(self, logLevel, dry_run):
self.meta_infos = {}
self.analyse_started = False
self.current_analysis = {}
@ -141,6 +141,7 @@ class IWLA(object):
self.cache_plugins = {}
self.display = DisplayHTMLBuild(self)
self.valid_visitors = None
self.dry_run = dry_run
self.log_format_extracted = re.sub(r'([^\$\w])', r'\\\g<1>', conf.log_format)
self.log_format_extracted = re.sub(r'\$(\w+)', '(?P<\g<1>>.+)', self.log_format_extracted)
@ -155,7 +156,10 @@ class IWLA(object):
logging.basicConfig(format='%(name)s %(message)s', level=logLevel)
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.info('==> Start')
if self.dry_run:
self.logger.info('==> Start (DRY RUN)')
else:
self.logger.info('==> Start')
try:
t = gettext.translation('iwla', localedir=conf.locales_path, languages=[conf.locale], codeset='utf8')
self.logger.info('\tUsing locale %s' % (conf.locale))
@ -243,6 +247,7 @@ class IWLA(object):
return gzip.open(filename, prot)
def _serialize(self, obj, filename):
if self.dry_run: return
base = os.path.dirname(filename)
if not os.path.exists(base):
os.makedirs(base)
@ -556,6 +561,7 @@ class IWLA(object):
if not os.path.exists(gz_path) or\
os.stat(path).st_mtime >= build_time:
if self.dry_run: return
with open(path, 'rb') as f_in, gzip.open(gz_path, 'wb') as f_out:
f_out.write(f_in.read())
@ -622,7 +628,7 @@ class IWLA(object):
self._callPlugins(conf.POST_HOOK_DIRECTORY)
path = self.getDBFilename(cur_time)
if os.path.exists(path):
if os.path.exists(path) and not self.dry_run:
os.remove(path)
self.logger.info("==> Serialize to %s" % (path))
@ -853,6 +859,10 @@ if __name__ == '__main__':
default=False,
help='Only generate display')
parser.add_argument('-D', '--dry-run', dest='dry_run', action='store_true',
default=False,
help='Process log but don\'t write files (database and HTML) to disk')
args = parser.parse_args()
# Load user conf
@ -872,7 +882,7 @@ if __name__ == '__main__':
else:
conf.__dict__.update({k:v})
if args.clean_output:
if args.clean_output and not args.dry_run:
if os.path.exists(conf.DB_ROOT): shutil.rmtree(conf.DB_ROOT)
if os.path.exists(conf.DISPLAY_ROOT): shutil.rmtree(conf.DISPLAY_ROOT)
@ -880,7 +890,7 @@ if __name__ == '__main__':
if not isinstance(loglevel, int):
raise ValueError('Invalid log level: %s' % (args.loglevel))
iwla = IWLA(loglevel)
iwla = IWLA(loglevel, args.dry_run)
required_conf = ['analyzed_filename', 'domain_name']
if not validConfRequirements(required_conf, iwla, 'Main Conf'):