def threadutils_add(thread): global _threadutils_threads_pool devlog('threadutils_add', "adding thread %s to thread pool" % thread.getName()) if thread.isDaemon(): devlog('threadutils_add', "thread %s IS daemon" % thread.getName()) _threadutils_threads_pool.append(thread)
def threadutils_cleanup(): global _threadutils_threads_pool if _threadutils_threads_pool == []: devlog('threadutils_cleanup', "no threads in pool") else: for thread in _threadutils_threads_pool: devlog('threadutils_cleanup', "cleaning thread %s" % thread.getName()) threadutils_del(thread)
def SystemAPI(): try: maj, min, micro = PythonVersion() modulename = "PySystemAPI_%s_%s_python%s%s" % ( OSVersion(), MachineVersion(), maj, min) debug.devlog('SystemAPI', "trying to import module <%s>" % modulename) mod = __import__(modulename, globals(), locals(), ["internal"]) return mod except: class void: pass return void()
def deprecate(message=None, file=sys.stderr): """ warn when a deprecated function is called """ if not debug.debug_enabled: pass tb = traceback.extract_stack() # stack is [?=main][...][caller][here=deprecated][extract_stack] if (len(tb) <= 2): # called by main? return if not message: message = "" (file, line, calling_func, deprecated_code) = tb[-3] #debug.devlog("!!! use of deprecated function %s in %s() [%s:%d] %s" % (f_deprecated, f_calling, file, line, message)) debug.devlog("DEPRECATED File %s, line %d, in %s()" % (file, line, calling_func)) debug.devlog("DEPRECATED -> %s" % message) debug.devlog("DEPRECATED code: %s" % deprecated_code)
#! /usr/bin/env python import sys, os import colors import debug logging_initialized = False def logging_init(): logging_initialized = True if debug.debug_enabled: display("debug activated", color=colors.GREEN) def display(msg, color=None, prefix="[DEV] ", postfix="\n"): if os.name != "posix" or not color: colorprefix = "" colorpostfix = "" else: colorprefix = color colorpostfix = colors.EOC sys.stdout.flush() #flush this first sys.stderr.write("%s%s%s%s%s" % (colorprefix, prefix, msg, postfix, colorpostfix)) sys.stderr.flush() if __name__ == "__main__": debug.devlog("devlog msg")
def threadutils_exiting(thread): global _threadutils_threads_pool devlog('threadutils_exiting', "thread %s exiting" % thread.getName()) if thread in _threadutils_threads_pool: _threadutils_threads_pool.remove(thread)
def threadutils_del(thread, timeout=10): global _threadutils_threads_pool name = None if hasattr(thread, 'name'): name = thread.name else: name = thread.getName() if thread.isAlive(): devlog('threadutils_del', "thread is alive") if hasattr(thread, 'shutdown'): devlog('threadutils_del', "calling thread.shutdown()") thread.shutdown() else: devlog('threadutils_del', "thread doesn't have shutdown() function...") # we are mad and pissed of, no time to play if hasattr(thread, '_Thread__stop'): devlog('threadutils_del', "calling _Thread__stop()!") thread._Thread__stop() if thread.isDaemon(): devlog('threadutils_del', "can not join Daemon thread") else: devlog('threadutils_del', "trying to join thread") thread.join(timeout) print "[T] %s exited" % name if thread in _threadutils_threads_pool: _threadutils_threads_pool.remove(thread)
def add_python_paths(): if os.name != "posix" or sys.platform in ["cygwin", "nt", "win32"]: return for path in get_python_paths(): debug.devlog("adding python path %s" % path) sys.path.append(pathstr(pathjoin(path, "site-packages")))