Package vivisect :: Package storage :: Module basicfile
[hide private]
[frames] | no frames]

Source Code for Module vivisect.storage.basicfile

 1  import cPickle as pickle 
 2  import vivisect 
 3   
 4  vivsig_cpickle = 'VIV'.ljust(8,'\x00') 
 5   
6 -def saveWorkspaceChanges(vw, filename):
7 elist = vw.exportWorkspaceChanges() 8 if len(elist): 9 f = file(filename, 'ab') 10 pickle.dump(elist, f, protocol=2) 11 f.close()
12
13 -def saveWorkspace(vw, filename):
14 events = vw.exportWorkspace() 15 vivEventsToFile(filename, events)
16
17 -def vivEventsAppendFile(filename, events):
18 f = file(filename, 'ab') 19 # Mime type for the basic workspace 20 pickle.dump(events, f, protocol=2) 21 f.close()
22
23 -def vivEventsToFile(filename, events):
24 f = file(filename, 'wb') 25 # Mime type for the basic workspace 26 f.write(vivsig_cpickle) 27 pickle.dump(events, f, protocol=2) 28 f.close()
29
30 -def vivEventsFromFile(filename):
31 f = file(filename, "rb") 32 vivsig = f.read(8) 33 34 # check for various viv serial formats 35 if vivsig == vivsig_cpickle: 36 pass 37 38 else: # FIXME legacy file format.... ( eventually remove ) 39 f.seek(0) 40 41 events = [] 42 # Incremental changes are saved to the file by appending more pickled 43 # lists of exported events 44 while True: 45 try: 46 events.extend( pickle.load(f) ) 47 except EOFError, e: 48 break 49 except pickle.UnpicklingError, e: 50 raise vivisect.InvalidWorkspace(filename, "invalid workspace file") 51 52 f.close() 53 54 # FIXME - diagnostics to hunt msgpack unsave values 55 #for event in events: 56 #import msgpack 57 #try: 58 #msgpack.dumps(event) 59 #except Exception, e: 60 #print('Unsafe Event: %d %r' % event) 61 62 return events
63
64 -def loadWorkspace(vw, filename):
65 events = vivEventsFromFile(filename) 66 vw.importWorkspace(events) 67 return
68