comparison mercurial/statprof.py @ 30255:f42cd5434cc2

statprof: require paths to save or load profile data Upstream appears to aggressively save statprof data in a well-defined home directory path. Change the code to not do that. We also change file saving to fail if an error has occurred instead of silently failing. Callers can catch the exception. This behavior is more suitable for a generic "library" module.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sun, 14 Aug 2016 19:14:05 -0700
parents ad4c0236168d
children 2ed0b3f9f79e
comparison
equal deleted inserted replaced
30254:ad4c0236168d 30255:f42cd5434cc2
309 state.thread.join() 309 state.thread.join()
310 310
311 state.accumulate_time(clock()) 311 state.accumulate_time(clock())
312 state.last_start_time = None 312 state.last_start_time = None
313 statprofpath = os.environ.get('STATPROF_DEST') 313 statprofpath = os.environ.get('STATPROF_DEST')
314 save_data(statprofpath) 314 if statprofpath:
315 315 save_data(statprofpath)
316 def save_data(path=None): 316
317 try: 317 def save_data(path):
318 path = path or (os.environ['HOME'] + '/statprof.data') 318 with open(path, 'w+') as file:
319 file = open(path, "w+")
320
321 file.write(str(state.accumulated_time) + '\n') 319 file.write(str(state.accumulated_time) + '\n')
322 for sample in state.samples: 320 for sample in state.samples:
323 time = str(sample.time) 321 time = str(sample.time)
324 stack = sample.stack 322 stack = sample.stack
325 sites = ['\1'.join([s.path, str(s.lineno), s.function]) 323 sites = ['\1'.join([s.path, str(s.lineno), s.function])
326 for s in stack] 324 for s in stack]
327 file.write(time + '\0' + '\0'.join(sites) + '\n') 325 file.write(time + '\0' + '\0'.join(sites) + '\n')
328 326
329 file.close() 327 def load_data(path):
330 except (IOError, OSError):
331 # The home directory probably didn't exist, or wasn't writable. Oh well.
332 pass
333
334 def load_data(path=None):
335 path = path or (os.environ['HOME'] + '/statprof.data')
336 lines = open(path, 'r').read().splitlines() 328 lines = open(path, 'r').read().splitlines()
337 329
338 state.accumulated_time = float(lines[0]) 330 state.accumulated_time = float(lines[0])
339 state.samples = [] 331 state.samples = []
340 for line in lines[1:]: 332 for line in lines[1:]: