Пример #1
0
class LogSync(object):
    def __init__(self, withSuffix=None, logDir=None, maxProc=multiprocessing.cpu_count(), configFile='./logsync.json'):
        """
        Inital method for base class LogSync

        :param withSuffix:      if is False - not use suffix in self.logDir
        :param logDir:          define self.logPath (without suffix, it will be added)
        :param maxProc:         max proccesses in pool of child proc for async calls
        :param configFile:      config file location (logsync.json)
        """

        try:
            with open(configFile, 'rb') as f:
                self.cfg = json.load(f)
        except IOError:
            raise ConfigNotFound(configFile)
        except ValueError:
            raise ErrorConfigParse(configFile)
        except Exception as e:
            raise e

        if logDir:
            self.logPath = logDir
        elif withSuffix:
            self.logPath = os.path.join(logDir, withSuffix)
        else:
            raise noSyncError

        self.procpool = Pool(processes = maxProc)
        self.fList=[]

    def handler(self, body):
        self.listLogs(body)
        self.syncWrapper()

    def syncWrapper(self):
        for logFile in self.fList:
            self.procpool.apply_acync(self.syncWrapper, args = logFile)

    def listLogs(self, body):
        fileMask = self.cfg['logger']['fmask']['prefix'] + str(body) + self.cfg['logger']['fmask']['suffix']
        for root, dirs, files in os.walk(self.logPath):
            for filename in fnmatch.filter(files, fileMask):
                self.listReplays(os.path.join(root, filename))
                self.fList.append = os.path.join(root, filename)

    def listReplays(self, logFile):
        if self.cfg['logger']['parseReplays']:
            try:
                with open(logFile, 'r+') as f:
                    fdata = mmap.mmap(f.fileno(), 0)
                    m = re.search(self.cfg['logger']['replayPattern'], fdata)
                    if m:
                        fmaskBody = m.groups()[0]
                        fmask = self.cfg['logger']['rmask']['prefix'] + fmaskBody + self.cfg['logger']['rmask'][
                            'suffix']
                        for root, dirs, files in os.walk(self.logPath):
                            for filename in fnmatch.filter(fmask):
                                self.fList.append = os.path.join(root, filename)
            except IOError:
                raise ConfigNotFound(logFile)
            except AttributeError:
                raise ErrorConfigParse

    def sync(self, logFile):
        """
        Abstract method just remove log file from query
        :param logFile: file to operate
        """
        self.fList.remove(logFile)
Пример #2
0
class LogSync(object):
    def __init__(self,
                 withSuffix=None,
                 logDir=None,
                 maxProc=multiprocessing.cpu_count(),
                 configFile='./logsync.json'):
        """
        Inital method for base class LogSync

        :param withSuffix:      if is False - not use suffix in self.logDir
        :param logDir:          define self.logPath (without suffix, it will be added)
        :param maxProc:         max proccesses in pool of child proc for async calls
        :param configFile:      config file location (logsync.json)
        """

        try:
            with open(configFile, 'rb') as f:
                self.cfg = json.load(f)
        except IOError:
            raise ConfigNotFound(configFile)
        except ValueError:
            raise ErrorConfigParse(configFile)
        except Exception as e:
            raise e

        if logDir:
            self.logPath = logDir
        elif withSuffix:
            self.logPath = os.path.join(logDir, withSuffix)
        else:
            raise noSyncError

        self.procpool = Pool(processes=maxProc)
        self.fList = []

    def handler(self, body):
        self.listLogs(body)
        self.syncWrapper()

    def syncWrapper(self):
        for logFile in self.fList:
            self.procpool.apply_acync(self.syncWrapper, args=logFile)

    def listLogs(self, body):
        fileMask = self.cfg['logger']['fmask']['prefix'] + str(
            body) + self.cfg['logger']['fmask']['suffix']
        for root, dirs, files in os.walk(self.logPath):
            for filename in fnmatch.filter(files, fileMask):
                self.listReplays(os.path.join(root, filename))
                self.fList.append = os.path.join(root, filename)

    def listReplays(self, logFile):
        if self.cfg['logger']['parseReplays']:
            try:
                with open(logFile, 'r+') as f:
                    fdata = mmap.mmap(f.fileno(), 0)
                    m = re.search(self.cfg['logger']['replayPattern'], fdata)
                    if m:
                        fmaskBody = m.groups()[0]
                        fmask = self.cfg['logger']['rmask'][
                            'prefix'] + fmaskBody + self.cfg['logger'][
                                'rmask']['suffix']
                        for root, dirs, files in os.walk(self.logPath):
                            for filename in fnmatch.filter(fmask):
                                self.fList.append = os.path.join(
                                    root, filename)
            except IOError:
                raise ConfigNotFound(logFile)
            except AttributeError:
                raise ErrorConfigParse

    def sync(self, logFile):
        """
        Abstract method just remove log file from query
        :param logFile: file to operate
        """
        self.fList.remove(logFile)