Don't use global variable profiledir

Rather than setting a global threadutil/profiledir variable, we make
set_profiledir a class function that sets the class variable profiledir.

While touching theprofiledir code, add warning to the user if the
profile directory existed before.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-09-29 16:02:51 +02:00
parent 8970a1500b
commit 642880b404
2 changed files with 23 additions and 20 deletions

View File

@ -1,6 +1,5 @@
# OfflineIMAP initialization code # OfflineIMAP initialization code
# Copyright (C) 2002-2007 John Goerzen # Copyright (C) 2002-2011 John Goerzen & contributors
# <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -157,11 +156,14 @@ class OfflineImap:
if not options.singlethreading: if not options.singlethreading:
logging.warn("Profile mode: Forcing to singlethreaded.") logging.warn("Profile mode: Forcing to singlethreaded.")
options.singlethreading = True options.singlethreading = True
profiledir = options.profiledir if os.path.exists(options.profiledir):
os.mkdir(profiledir) logging.warn("Profile mode: Directory '%s' already exists!" %
threadutil.setprofiledir(profiledir) options.profiledir)
else:
os.mkdir(options.profiledir)
threadutil.ExitNotifyThread.set_profiledir(options.profiledir)
logging.warn("Profile mode: Potentially large data will be " logging.warn("Profile mode: Potentially large data will be "
"created in '%s'" % profiledir) "created in '%s'" % options.profiledir)
#override a config value #override a config value
if options.configoverride: if options.configoverride:

View File

@ -1,6 +1,5 @@
# Copyright (C) 2002, 2003 John Goerzen # Copyright (C) 2002-2011 John Goerzen & contributors
# Thread support module # Thread support module
# <jgoerzen@complete.org>
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -20,15 +19,10 @@ from threading import Lock, Thread, BoundedSemaphore
from Queue import Queue, Empty from Queue import Queue, Empty
import traceback import traceback
from thread import get_ident # python < 2.6 support from thread import get_ident # python < 2.6 support
import os.path
import sys import sys
from offlineimap.ui import getglobalui from offlineimap.ui import getglobalui
profiledir = None
def setprofiledir(newdir):
global profiledir
profiledir = newdir
###################################################################### ######################################################################
# General utilities # General utilities
###################################################################### ######################################################################
@ -132,11 +126,14 @@ def threadexited(thread):
class ExitNotifyThread(Thread): class ExitNotifyThread(Thread):
"""This class is designed to alert a "monitor" to the fact that a thread has """This class is designed to alert a "monitor" to the fact that a thread has
exited and to provide for the ability for it to find out why.""" exited and to provide for the ability for it to find out why."""
profiledir = None
"""class variable that is set to the profile directory if required"""
def run(self): def run(self):
global exitthreads, profiledir global exitthreads
self.threadid = get_ident() self.threadid = get_ident()
try: try:
if not profiledir: # normal case if not ExitNotifyThread.profiledir: # normal case
Thread.run(self) Thread.run(self)
else: else:
try: try:
@ -148,9 +145,8 @@ class ExitNotifyThread(Thread):
prof = prof.runctx("Thread.run(self)", globals(), locals()) prof = prof.runctx("Thread.run(self)", globals(), locals())
except SystemExit: except SystemExit:
pass pass
prof.dump_stats( \ prof.dump_stats(os.path.join(ExitNotifyThread.profiledir,
profiledir + "/" + str(self.threadid) + "_" + \ "%s_%s.prof" % (self.threadid, self.getName())))
self.getName() + ".prof")
except: except:
self.setExitCause('EXCEPTION') self.setExitCause('EXCEPTION')
if sys: if sys:
@ -195,6 +191,11 @@ class ExitNotifyThread(Thread):
set.""" set."""
return self.exitmessage return self.exitmessage
@classmethod
def set_profiledir(cls, directory):
"""If set, will output profile information to 'directory'"""
cls.profiledir = directory
###################################################################### ######################################################################
# Instance-limited threads # Instance-limited threads