Python - variable scope and modification -
i'm writing python script gui framework live forensic data collection on windows host. until now, haven't had many problems couldn't work through. 1 has me stumped. problem involving improper variable scope, life of me, can't find problem. script on 2 thousand lines long, i'll copy relevant portions unless otherwise instructed.
i made change code somewhere along line broke functionality. used work intended, , can't figure out why doesn't. lead me use versioning software projects in future, that's sure.
what's odd there no exceptions thrown @ all.
what i'm trying have user select tkinter checkbutton widgets corresponding optional function want perform. have set tkinter intvars each checkbutton widget, , placed these intvars array easy enumeration. gui has "start" button, when pressed, initiates start() function. function should enumerate checkbutton intvars in array , perform relevant functions if intvar set 1 (indicating selection).
all other parts of code function intended. having horrible time populating arrays. seems cannot modify arrays, if declared global within functions. if set values in array manually, functions intended. cannot iterate through intvars , set "global" arrays should correct values. seems 2 arrays never modified, acting if none of checkbutton intvars set '1' being selected.
#this array of integers corresponding checked #state of desired filetypes collection filetypes = [0]*34 #this array of integers corresponding optional collection items optionalitems = [0]*10 #... snipped ... #this function performs fine if filetypes array set manually... #extensions[] array containing file extension strings corresponding # checkbuttons on fileselector toplevel widget. #outputdirs[] array containing strings desired output directory. def collectfiles(): global filetypes n = 0 print("parsing checkboxes file collection...\r\n") item in filetypes: if item: print(extensions[n] + " selected...") #make output dir corresponding desired extension outputdest = cwd + "/output/" + computername + outputdirs[n] if not os.path.exists(outputdest): print(outputdest + " not exist, creating...") os.makedirs(outputdest) print("collection " + outputdirs[n] + " beginning...") drive in drives: print("searching drive " + drive + "...") filename in search(drive, extensions[n]): try: = 2 #outpath = cwd + "/output/" + username + outputdirs[n] tempbasename = os.path.basename(filename) testpath = os.path.join(outputdest, tempbasename) tempbasename2 = "" while os.path.exists(testpath): print(testpath + " exists in output directory.") tempbasename2 = str(i) + "_" + tempbasename += 1 testpath = os.path.join(outputdest, tempbasename2) shutil.copy2(filename,testpath) print("copied:\n\t" + filename + "\n to:\n\t" + testpath + "\n") logfile.write(str(datetime.datetime.now()) + "- copied:\r\n\t" + filename + "\r\n to:\r\n\t" + testpath + "\r\n\r\n") except: print("****problem copying: " + filename + "\n") logfile.write(str(datetime.datetime.now()) + "****problem copying: " + filename + "\r\n\r\n") pass n += 1 #... snipped ... #here's oddness begins. #the optionalitems array should set outside of # function, before called. def dooptionalcollection(): x = 0 item in optionalitems: if item: optionfunctions[x]() x = x + 1 collectfiles() #this routine called when "start" button pressed. # goal here enumerate checkboxes on gui, , # fill filetypes[] , optionalitems[] values corresponding # whether or not checkbuttons selected. def start(): global filetypes global optionalitems #... snipped ... #code here performs baseline forensic data collection, # , performs intended. #... snipped ... #get status of checkboxes , update optionalitems array. # optionarray array of intvars associated tkinter checkbuttons # obj.get() --> 0 = unchecked, 1 = checked = 0 obj in optionarray: optionalitems[i] = obj.get() = + 1 dooptionalcollection() #this dialog-style window pops when button pressed. # dialog contains 34 checkboxes, each variable hold state. def showfileselector(): global filetypes fs = toplevel(master=root) fs.title("collect files extension...") #set grid based on extensions dictionary keys. # have grid wrap on 6th element. #... snipped, setup of checkbutton tkinter windgets ... buttonarray = [txtbutton, pdfbutton, logbutton, docbutton, docxbutton, rarbutton, zipbutton, isobutton, jarbutton, jpgbutton, jpegbutton, bmpbutton, pngbutton, gifbutton, exebutton, pptbutton, pptxbutton, avibutton, mp4button, movbutton, flvbutton, mpgbutton, wmvbutton, mp3button, flacbutton, wmabutton, m4abutton, wavbutton, psdbutton, rawbutton, apkbutton, szipbutton, indexdatbutton, thumbsdbbutton] #using filetypes array, set status of checkbox. #this helpful if dialog re-opened, # re-populate dialog previous selections. x = 0 item in buttonarray: if filetypes[x]: item.select() x = x + 1 vararray = [txtvar, pdfvar, logvar, docvar, docxvar, rarvar, zipvar, isovar, jarvar, jpgvar, jpegvar, bmpvar, pngvar, gifvar, exevar, pptvar, pptxvar, avivar, mp4var, movvar, flvvar, mpgvar, wmvvar, mp3var, flacvar, wmavar, m4avar, wavvar, psdvar, rawvar, apkvar, szipvar, indexdatvar, thumbsdbvar] def accept(): global filetypes #user has possibly chosen files collect extension. # iterate vararray determine files collect, # , store result in filetypes[]. #this assignment not work. x = 0 var in vararray: #var.get() values of checkboxes # 0 = unchecked # 1 = checked filetypes[x] = var.get() x = x + 1 fs.destroy() def cancel(): #user has decided discard selections , close # window. fs.destroy() #... snipped, gui placement ... #back base-level indentation (no indents... not inside functions) #... snipped, optional item gui checkbutton setup .... optionarray = [productkeyvar, outlookvar, recyclebinvar, skypevar, prefetchvar, installlogvar, allmediavar, win7jumplistvar, win7thumbcachevar, recentfilesvar] optionfunctions = [collectproductkeys, collectoutlookattachments, collectrecyclebin, collectskypehistory, collectprefetchfolder, collectdeviceinstallationlogs, collectmediafiles, collectwin7jumplist, collectwin7thumbnailcache, collectshortcutrecentfiles] #... snipped, more gui setup ... #and then, obligatory... root.mainloop()
to attempt simplify process flow:
user sets options on gui, , may select filetypes collection well. filetypes[] should populated desired selections already, if user has chosen filetypes collect. modifying array not seem occur.
user presses start button, start button command start() function. globals defined - namely filetypes[] , optionalitems[].
inside start(), baseline forensic collection occurs, , functions intended.
still inside start(), status of checkboxes optional collection, , populate optionalitems[]. modifying array not seem occur.
start() calls dooptionalcollection().
dooptionalcollection() has globals defined - filetypes[] , optionalitems[]. when arrays enumerated, read initial values - zero. means none of desired collection functions being executed.
dooptionalcollection() calls collectfiles().
collectfiles() has global defined - artifact when parsing filetypes[] within function itself. since filetypes array zeroes, nothing ever gets collected, unless set manually in code.
this long post... first time posting here, apologize in advance huge wall of text. i'm not looking profit of code, i'm fine sharing code base if see full intended functionality. can make whole package available interested. in advance!
Comments
Post a Comment