Die Ergebnisse werden dan per E-Mail an eine vorher definierte Adresse geschickt.
Code: Alles auswählen
from Tkinter import *
import sys
import tkFileDialog
import tkSimpleDialog
import os,time
import smtplib
import pickle
import thread
class FolderScanner:
def __init__(self):
self.Status = 1
Mainwindow = Tk()
Mainwindow.geometry("400x250+0+0")
global RunScriptButton
RunScriptButton = Button(Mainwindow, text = "Run script", bg = "green", width = 25, height = 2, command = self.Thread)
RunScriptButton.place(x = 117.5, y = 30)
ChangeFolderButton = Button(Mainwindow, text = "Choose folder to scan", width = 25, command = self.ChangeFolder)
ChangeFolderButton.place(x = 25, y = 100)
ChangeMailAdressButton = Button(Mainwindow, text = "Choose mailadress", width = 25, command = self.EmailAdress)
ChangeMailAdressButton.place(x = 215, y = 100)
ChangeLogButton = Button(Mainwindow, text = "Choose destination of logfile", width = 25, command = self.DefineLogFolder)
ChangeLogButton.place(x = 215, y = 150)
ChangeTimeButton = Button(Mainwindow, text = "Choose repeattime of script(sek.)", width = 25, command = self.Repeattime)
ChangeTimeButton.place(x = 25, y = 150)
ChangeLogButton = Button(Mainwindow, text = "Create Log Files",bg = "yellow", width = 25, command = self.CreateLogFiles)
ChangeLogButton.place(x = 117.5 ,y = 200)
mainloop()
def ChangeFolder(self):
global FolderToScan
FolderToScan = tkFileDialog.askdirectory()
def EmailAdress(self):
global EmailAdress
EmailAdress = tkSimpleDialog.askstring("E-Mail","E-Mailadress:")
def CreateLogFiles(self):
global FolderToSaveLog
FolderToSaveLog = tkFileDialog.askdirectory()
FakeToStart = [('Fake.pdf', 1111111111), ('Fake.txt', 1111111111)]
os.chdir(FolderToSaveLog)
open('log.txt','w').close()
Txt = open('log2.txt','w')
pickle.dump(FakeToStart,Txt)
Txt.close()
def DefineLogFolder(self):
global FolderToSaveLog
FolderToSaveLog = tkFileDialog.askdirectory()
def Repeattime(self):
global Time
Time = tkSimpleDialog.askinteger("Repeattime","Repeattime(sek.):")
def StopScript(self):
global Status
Status = 0
def Thread(self):
Status = 1
thread.start_new_thread(self.Scanning,())
RunScriptButton.configure(bg = "red")
def Scanning (self):
global AccessTimeNew
global AccessTimeOld
Status = 0
while self.Status:
Status += 1
FilesNew = os.listdir(FolderToScan)
F = file(FolderToSaveLog + "/log.txt", "r")
Tempdata1 = F.read()
F.close()
FilesOld = Tempdata1.split("/n")
AccessTimeNew = [(f, os.path.getmtime(FolderToScan+ "/"+f)) for f in os.listdir(FolderToScan) if os.path.isfile(FolderToScan+"/"+f)]
F2 = file(FolderToSaveLog + "/log2.txt","r")
AccessTimeOld = pickle.load(F2)
F2.close()
if FilesNew == FilesOld and AccessTimeNew == AccessTimeOld:
time.sleep(Time)
continue
else:
FilesNew = os.listdir(FolderToScan)
AccessTimeNew = [(f, os.path.getmtime(FolderToScan+ "/"+f)) for f in os.listdir(FolderToScan) if os.path.isfile(FolderToScan+"/"+f)]
FilesChange = [i for i in FilesNew if not i in FilesOld]
FilesRemoved =[i for i in FilesOld if not i in FilesNew]
FilesAccessChanged =[i for i in AccessTimeNew if not i in AccessTimeOld]
Mailtext = "New Files:"+str(FilesChange)+"\n\n"+"Removed Files:"+str(FilesRemoved)+"\n\n"+"Changed Files:"+str(FilesAccessChanged)
Host = smtplib.SMTP('deinMailserver')
Host.sendmail(EmailAdress,EmailAdress,Mailtext)
{}
Host.quit()
FilesOld = FilesNew
Tempdata2 = "/n".join(FilesOld)
F = file(FolderToSaveLog + "/log.txt", "w")
F.write(Tempdata2)
F.close()
AccessTimeOld = AccessTimeNew
F2 = file(FolderToSaveLog + "/log2.txt","w")
Tempdata4 = pickle.dump(AccessTimeOld,F2)
F2.close()
time.sleep(Time)
continue
FolderScanner()