variousscripts/pylogextractor/logextractor.py

105 lines
3.6 KiB
Python
Executable File

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import codecs
globalvars = {
"version": "0.92"
}
def searchuri():
prompt = ("File or directory for searching ["+
os.path.abspath(os.curdir)+"]:")
uri = input (prompt)
if uri == "":
uri = os.path.abspath(os.curdir)
return uri
if os.path.exists(uri):
if os.access(uri, os.R_OK):
return uri
else:
print ("You don't have permission to read "+uri+
". Try to change privileges or type another path.")
return searchuri()
else:
print ("File or directory not found. Plese type correct path.")
return searchuri()
def writefile():
prompt = ("File for writing[dump]:")
filename = input (prompt)
if filename == "":
filename = "dump"
try:
filedump = open(filename, 'x')
filedump.close()
return filename
except FileExistsError:
try:
filedump = open(filename, 'a')
filedump.close()
return filename
except PermissionError:
print ("You don't have permission to write "+filename)
return writefile()
except PermissionError:
print ("You don't have permission to write "+filename)
return writefile()
def substring():
prompt = ("Enter substring for search:")
substr = input (prompt)
if substr == "":
return substring()
return substr
if __name__ == "__main__":
print ("pyLogExtractor v",globalvars["version"])
uri = searchuri()
if os.path.isfile(uri):
print ("File for search: "+os.path.abspath(uri))
elif os.path.isdir(uri):
filesnumber = 0
for top, dirs, files in os.walk(uri):
filesnumber = (filesnumber + (len(files)))
print ("Directory for search: "+os.path.abspath(uri)+
" ("+str(filesnumber)+" files)")
filename = writefile()
print ("File for writing: "+filename)
substr = substring()
print ("Substring: "+substr)
filedump = open(filename, 'a')
stringsnumber = 0
if os.path.isdir(uri):
for top, dirs, files in os.walk(uri):
for nm in files:
with codecs.open(os.path.join(top, nm), "r",
encoding='utf-8', errors='ignore') as openedfile:
for currentstring in openedfile:
if currentstring.find(substr) > 0:
currentstring = (re.split(r': ',(re.sub(r'\<[^>]*\>',
'', currentstring))))[-1]
currentstring = (re.split(r'&gt; ',(re.sub(r'\<[^>]*\>',
'', currentstring))))[-1]
filedump.write(currentstring.rstrip() + '\n')
stringsnumber = stringsnumber + 1
openedfile.close()
elif os.path.isfile(uri):
print ("sdf")
with codecs.open(uri, "r",
encoding='utf-8', errors='ignore') as openedfile:
for currentstring in openedfile:
if currentstring.find(substr) > 0:
currentstring = (re.split(r': ',(re.sub(r'\<[^>]*\>',
'', currentstring))))[-1]
currentstring = (re.split(r'&gt; ',(re.sub(r'\<[^>]*\>',
'', currentstring))))[-1]
filedump.write(currentstring.rstrip() + '\n')
stringsnumber = stringsnumber + 1
openedfile.close()
filedump.close()
print (stringsnumber," strings added.")