Commit 00590590 authored by Michał Woźniak's avatar Michał Woźniak

WIP: implementation started, nowhere near ready

parent 088a2e0d
#!/usr/bin/python
# vim: et sw=4 ts=4:
from inotify_simple import INotify, flags
import import_logs
from optparse import OptionParser
import sys
import os.path
from glob import glob
from enum import Enum
class states(Enum):
NEW = 1
OPEN = 2
READY = 3
class Configuration(import_logs.Configuration):
def __init__(self, argv = None):
# in case we want to add options
self.parser = self._create_parser()
self.parser.add_option(
'--ingestion-start-delay',
dest='ingestion_start_delay',
type='int',
default=200,
help="Delay (in ms) between noticing a logfile to be processed and starting ingesting it."
"This is part of the built-in heuristic for determining that a file is not being modified "
"or moved anymore and can be safely ingested."
)
# parse the stuff
self._parse_args(self.parser, argv)
# sanitize the paths (all paths should be existing directories)
sanitized_filenames = []
for f in self.filenames:
f = os.path.realpath(f)
if not os.path.exists(f):
raise IOError("Path does not exist: " % f)
if not os.path.isdir(f):
raise IOError("Path is not a directory: " % f)
sanitized_filenames.append(f)
self.filenames = sanitized_filenames
if len(self.filenames) == 0:
raise Exception("No existing directories to watch!")
config = Configuration()
inotify = INotify()
watch_flags = flags.CREATE | flags.MODIFY | flags.DELETE_SELF | flags.CLOSE_WRITE | flags.OPEN | flags.MOVED_TO | flags.MOVED_FROM
# files to process
logfile_glob = "*.log"
logfiles = [] # array of dicts, each with "path", "state"
# set up watches
print("+-- watching:")
watches = {}
for path in config.filenames:
print(" - %s" % path)
# set up watches
wd = inotify.add_watch(path, watch_flags)
watches[wd] = path
# glob any files already existing in the directory
for p in glob(os.path.join(path, logfile_glob)):
logfiles.append({
"path": p,
"state": states.NEW
})
print(logfiles)
# And see the corresponding events:
while True:
print("+-- loop...")
for event in inotify.read(config.options.ingestion_start_delay):
print(' +-- event! %s: %s' % (os.path.join(watches[event.wd], event.name), ', '.join([str(f) for f in flags.from_mask(event.mask)])))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment