[Pymoul-svn] SF.net SVN: pymoul: [98] pymoul/trunk
Status: Alpha
Brought to you by:
tiran
|
From: <ti...@us...> - 2007-01-29 18:46:36
|
Revision: 98
http://pymoul.svn.sourceforge.net/pymoul/?rev=98&view=rev
Author: tiran
Date: 2007-01-29 10:46:30 -0800 (Mon, 29 Jan 2007)
Log Message:
-----------
Some work on chatlogs and plasma logs
Modified Paths:
--------------
pymoul/trunk/setup_win32.py
pymoul/trunk/src/moul/file/chatlog.py
pymoul/trunk/src/moul/file/plasmalog.py
Modified: pymoul/trunk/setup_win32.py
===================================================================
--- pymoul/trunk/setup_win32.py 2007-01-29 13:15:59 UTC (rev 97)
+++ pymoul/trunk/setup_win32.py 2007-01-29 18:46:30 UTC (rev 98)
@@ -78,8 +78,8 @@
pexe['upx_args'] = '--mono --best'
# InnoSetup
pexe['innosetup'] = os.environ.get('INNOSETUP') # TODO:
+ pexe['inno_templates'] = "template.iss"
pexe['app_name'] = 'pyMoul'
- # not required at the moment
pexe['includes'].extend(findPyTz())
kw['zipfile'] = 'library.zip'
Modified: pymoul/trunk/src/moul/file/chatlog.py
===================================================================
--- pymoul/trunk/src/moul/file/chatlog.py 2007-01-29 13:15:59 UTC (rev 97)
+++ pymoul/trunk/src/moul/file/chatlog.py 2007-01-29 18:46:30 UTC (rev 98)
@@ -16,6 +16,17 @@
# Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""Chat log parser
+
+Chatlog format
+==============
+
+(MM/DD hh:mm:ss) Chat.log started...
+(MM/DD hh:mm:ss) ...Chat.log stopped.
+(MM/DD hh:mm:ss) From USER in LOCATION: msg
+(MM/DD hh:mm:ss) Error: ERRORMSG
+(MM/DD hh:mm:ss) USER: TEXT (note the two spaces!)
+(MM/DD hh:mm:ss) To USER: TEXT
+(MM/DD hh:mm:ss) USER action
"""
__author__ = "Christian Heimes"
__version__ = "$Id$"
@@ -23,22 +34,127 @@
import os
import re
-from glob import glob
+from fnmatch import fnmatch
+from stat import ST_MTIME
+from shutil import move
+from time import localtime
-CHAT_RE_TXT = r"^\((?P<M>\d{1,2})/(?P<D>\d{1,2})\ " \
- r"(?P<h>\d{1,2}):(?P<m>\d\d{1,2}):(?P<s>\d{1,2})\)" \
- r"(?P<space>\ {1,2})(?P<text>.*)$"
-CHAT_RE = re.compile(CHAT_RE_TXT)
+from moul.log import getLogger
+CHAT_RE = re.compile(
+ r"^\((?P<M>\d{1,2})/(?P<D>\d{1,2})\ " # MM/DD
+ r"(?P<h>\d{1,2}):(?P<m>\d\d{1,2}):(?P<s>\d{1,2})\)" # hh:mm:ss
+ r"(?P<space>\ {1,2})(?P<msg>.*)$") # text
+CHAT_STARTED = "Chat.log started..."
+CHAT_STOPPED = "...Chat.log stopped."
+TEXT_MSGFROM = re.compile(
+ r"From (?P<user>.*) in (?P<location>.*): (?P<msg>.*)"
+ # From USER in LOCATION: msg
+ )
+TEXT_MSGTO = re.compile(
+ r"To (?P<user>.*): (?P<msg>.*)"
+ # From USER in LOCATION: msg
+ )
+TEXT_ERROR = re.compile(
+ r"Error: (?P<msg>.*)"
+ # Error: message
+ )
+TEXT = re.compile(
+ r"(?P<user>.*): (?P<msg>.*)"
+ # User: message
+ )
+
+
+LOG = getLogger('moul.chat')
+
+def modtime(pathname):
+ """Modification time
+ """
+ return os.stat(pathname)[ST_MTIME]
+
class ChatlogMover(object):
"""
"""
+ _pat = "chat*.log"
+ _filename = "chatlog_%(sY)i%(sM)i%(sD)i_%(ch)i%(cm)i_%(sh)i%(sm)i.txt"
- def __init__(self, path, dest):
- self._path = path
- self._dest = dest
+ def __init__(self, srcdir, destdir):
+ self._path = srcdir
+ self._dest = destdir
self._logs = []
+ if not os.path.isdir(srcdir):
+ LOG.critical("%s is not a directory" % srcdir)
+ if not os.path.isdir(destdir):
+ LOG.info("Creating chatlog directory %s" % destdir)
+ os.mkdir(dest)
def findLogs(self):
- self._logs.append(os.path.join(self._path, 'chat*.log'))
+ """Find log files in self._path
+ Also calls and stores _findCreated(), modtime() and _makeFile()
+ for the file.
+ """
+ for file in os.listdir(self._path):
+ if not fnmatch(file, self._pat):
+ continue
+
+ fpath = os.path.join(self._path, file)
+ if not os.path.isfile(fpath):
+ continue
+ LOG.debug("Found chatlog %s" % fpath)
+ fd = open(fpath, 'r')
+ created = self._findCreated(fd)
+ if not created:
+ LOG.warning("Failed to parse chatlog %s" % fpath)
+ modtime = localtime(modtime(fpath))
+ newname = self._makeFilename(created, modtime)
+
+ details = {
+ 'fpath' : fpath,
+ 'filename' : file,
+ 'modtime' : localtime(modtime(fpath)),
+ 'created' : created,
+ 'newname' : newname,
+ 'newpath' : os.path.join(self._dest, newname),
+ }
+
+ self._logs.append(details)
+
+ def _findCreated(self, fd):
+ """Parse chatlog to find "started" date
+ """
+ created = None
+ while True:
+ line = fd.readline()
+ if not line:
+ break
+ mo = CHAT_RE.match(line)
+ if mo:
+ data = mo.groupdict()
+ if data['msg'].startswith(CHATLOG_STARTED):
+ created = (None, mo['M'], mo['D'], mo['h'], mo['m'],
+ mo['s'], None, None, None)
+ return created
+
+ def _makeFilename(self, created, stopped):
+ """Build a filename from created and stopped information
+ """
+ c, s = created, stopped
+ mapping = {
+ 'sY' : s['Y'], 'sM' : s['YM'], 'sD' : s['D'],
+ 'sh' : s['h'], 'sm' : s['m'], 'ss' : s['s'],
+ 'cM' : c['YM'], 'cD' : c['D'],
+ 'ch' : c['h'], 'cm' : c['m'], 'cs' : c['s'],
+ }
+ return self._filename % mapping
+
+ def moveChatlogs(self):
+ """Move chatlogs from old to new location and name
+ """
+ for details in self._logs:
+ old = details['fpath']
+ new = details['newpath']
+ if os.path.isfile(new):
+ LOG.warning("%s exists - overwriting" % new)
+ LOG.info("Move chatlog %s to %s" % (old, new))
+ move(old, new)
Modified: pymoul/trunk/src/moul/file/plasmalog.py
===================================================================
--- pymoul/trunk/src/moul/file/plasmalog.py 2007-01-29 13:15:59 UTC (rev 97)
+++ pymoul/trunk/src/moul/file/plasmalog.py 2007-01-29 18:46:30 UTC (rev 98)
@@ -25,76 +25,82 @@
from stat import ST_MTIME
import time
import zipfile
-import re
+from fnmatch import fnmatch
from moul.crypt.elf import decryptElf
PLASMA_LOG = "plasmalog.txt"
_marker = object()
-RE_SAFEEXT_TEXT = "\.(elf|txt|log|zip|jpg|jpeg)$"
-RE_SAFEXT = re.compile(RE_SAFEEXT_TEXT, re.IGNORECASE)
-def getTimeStamp(path):
- """Get time stamp yyyymmdd_hhmm based in the modification time
+class PlasmalogZipper(object):
+ """Zip plasma logs
"""
- sec = os.stat(path)[ST_MTIME]
- return time.strftime("%Y%m%d_%H%M", time.gmtime(sec))
+ _save_patterns = ['*.elf', '*.txt', '*.log', '*.zip', '*.jpg']
+
+ def __init__(self, srcdir, destdir):
+ self._srcdir = srcdir
+ self._destdir = destdir
-def isLogDir(path):
- """Check if a path is a valid plasma log directory
-
- Just checks for the plasmalog.txt file
-
- Returns either False or a time stamp
- """
- pl = os.path.join(path, PLASMA_LOG)
- if not os.path.isfile(pl):
- return False
- return getTimeStamp(pl)
-
-def zipLogDir(logdir, destdir, remove):
- """Zip all log files
-
- This function also zips subdirectories.
- """
- stored_dirs = []
-
- for root, dirs, files in os.walk(logdir):
- stamp = isLogDir(root)
- if not stamp:
- continue
- name = os.path.basename(root)
- zipname = "%s_%s.zip" % (name, stamp)
- zipfd = zipfile.ZipFile(os.path.join(destdir, zipname),
- 'w', zipfile.ZIP_DEFLATED)
- for file in files:
- if file.lower().startswith('chat.'):
- # omit chatlogs from the logs
+ def getTimeStamp(self, path):
+ """Get time stamp yyyymmdd_hhmm based in the modification time
+ """
+ sec = os.stat(path)[ST_MTIME]
+ return time.strftime("%Y%m%d_%H%M", time.localtime(sec))
+
+ def isLogDir(self, path):
+ """Check if a path is a valid plasma log directory
+
+ Just checks for the plasmalog.txt file
+
+ Returns either False or a time stamp
+ """
+ pl = os.path.join(path, PLASMA_LOG)
+ if not os.path.isfile(pl):
+ return False
+ return getTimeStamp(pl)
+
+ def zipLogDir(self):
+ """Zip all log files
+
+ This function also zips subdirectories.
+ """
+ stored_dirs = []
+
+ for root, dirs, files in os.walk(self._srcdir):
+ stamp = isLogDir(root)
+ if not stamp:
continue
- fname = os.path.join(root, file)
- arcname = os.path.join(name, file)
- zipfd.write(fname, arcname)
- #zipfd.printdir()
- zipfd.close()
- stored_dirs.append((root, zipname))
+ name = os.path.basename(root)
+ zipname = "%s_%s.zip" % (name, stamp)
+ zipfd = zipfile.ZipFile(os.path.join(self._destdir, zipname),
+ 'w', zipfile.ZIP_DEFLATED)
+ for file in files:
+ if file.lower().startswith('chat.'):
+ # omit chatlogs from the logs
+ continue
+ fname = os.path.join(root, file)
+ arcname = os.path.join(name, file)
+ zipfd.write(fname, arcname)
+ #zipfd.printdir()
+ zipfd.close()
+ stored_dirs.append((root, zipname))
- if remove:
- removeLogs(logdir)
+ return stored_dirs
- return stored_dirs
+ def removeLogs(self, logdir):
+ """Removes log directories
+
+ The removeLogs function removes only files considered as safe
+ """
+ for root, dirs, files in os.walk(logdir, topdown=False):
+ for name in files:
+ matches = [pat for pat in self._save_patterns
+ if fnmatch(basename, pat)]
+ if match:
+ os.remove(os.path.join(root, name))
+ else:
+ LOG.warning("Won't remove %s from %s" % (name, root))
+ for name in dirs:
+ os.rmdir(os.path.join(root, name))
-def removeLogs(logdir):
- """Removes log directories
-
- The removeLogs function removes only files considered as safe
- """
- for root, dirs, files in os.walk(logdir, topdown=False):
- for name in files:
- if RE_SAFEXT.search(name):
- os.remove(os.path.join(root, name))
- else:
- print name # XXX
- for name in dirs:
- os.rmdir(os.path.join(root, name))
-
- os.rmdir(logdir)
+ os.rmdir(logdir)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|