diff options
author | nags <nags@nags-desktop.(none)> | 2009-07-23 20:52:05 -0700 |
---|---|---|
committer | nags <nags@nags-desktop.(none)> | 2009-07-23 20:52:05 -0700 |
commit | 35c4e3dbe13f92e15fc1ed215d63c1a93565d437 (patch) | |
tree | 45b51c2425b9495d147856c191dfbcbf40aecd41 | |
parent | 5026bdee32538a88db2e1f4a61232efd4b8e5cf2 (diff) |
2009-07-23 Nagappan Alagappan <nagappan@gmail.com>
* ldtputils.py (imagecapture): Fixes bug 588819 – 'import ldtp'
freezes interpreter
-rw-r--r-- | python/ChangeLog | 5 | ||||
-rw-r--r-- | python/ldtputils.py | 215 |
2 files changed, 96 insertions, 124 deletions
diff --git a/python/ChangeLog b/python/ChangeLog index fc46652..ee3e4ed 100644 --- a/python/ChangeLog +++ b/python/ChangeLog @@ -1,3 +1,8 @@ +2009-07-23 Nagappan Alagappan <nagappan@gmail.com> + + * ldtputils.py (imagecapture): Fixes bug 588819 – 'import ldtp' + freezes interpreter + 2009-07-12 Nagappan Alagappan <nagappan@gmail.com> * ooldtp.py (component.hasstate): Fixes bug # 586655, typo diff --git a/python/ldtputils.py b/python/ldtputils.py index 59d4b15..6c5b6be 100644 --- a/python/ldtputils.py +++ b/python/ldtputils.py @@ -32,7 +32,6 @@ import os import re import sys import time -import atexit import string import thread import commands @@ -48,74 +47,6 @@ import ldtp _ldtpDebug = os.getenv ('LDTP_DEBUG') -gtkImport = False -try: - import gtk.gdk - import gobject - gobject.threads_init() - gtkImport = True -except ImportError: - if _ldtpDebug: - print 'pygtk package not installed' - -wnckImport = False -try: - import wnck - wnckImport = True -except ImportError: - if _ldtpDebug: - print 'wnck python package not installed' - -## Process pending gtk+ events so that wnck can find out about -## existing windows. This has to be called as the generatemouseevent -## is holding all the calls till we exit LDTP script or python prompt. -_importThread = None -if gtkImport: - class GtkImportThread(threading.Thread): - def __init__(self): - threading.Thread.__init__(self) - self._loop = gobject.MainLoop() - self._context = self._loop.get_context() - self._suspend = False - gobject.idle_add(self._idle_add) - - def _start(self): - self._loop.run() - - def _idle_add(self): - try: - try: - gtk.gdk.threads_enter() - while self._loop.is_running(): - time.sleep(1.0/10) - while not self._suspend and gtk.events_pending(): - gtk.main_iteration() - except: - pass - finally: - try: - gtk.gdk.threads_leave() - except: - pass - - def __del__(self): - try: - self._stop() - except: - pass - - def _stop(self): - try: - if self._loop and self._loop.is_running(): - self._loop.quit() - self._loop = None - except: - pass - - _importThread = GtkImportThread() - thread.start_new_thread(_importThread._start, ()) - atexit.register(_importThread._loop.quit) - statGrabMsg = None try: import statgrab @@ -174,68 +105,104 @@ def imagecapture (winName = None, outFile = None, width = None, # winname == None, let us capture the entire screen # if output file name is not passed, then a random file name will be generated in # /tmp and it will be returned - winid = None - if winName: + global tmpFile + gtkImport = False + tmpFile = None + try: + import gtk.gdk + import gobject + gobject.threads_init() + gtkImport = True + except ImportError: + if _ldtpDebug: + print 'pygtk package not installed' + + wnckImport = False + try: + import wnck + wnckImport = True + except ImportError: + if _ldtpDebug: + print 'wnck python package not installed' + + def getWinId(winName): if not wnckImport: ldtp.log ('wnck package not installed, screen capture disabled ' \ 'based on window name', 'warning') return None - global _importThread - if _importThread: - _importThread._suspend = True - try: - screen = wnck.screen_get_default() - # Process pending gtk+ events so that wnck can find out about - # existing windows. - while gtk.events_pending(): - gtk.main_iteration() - for window in screen.get_windows(): - if re.search(winName, window.get_name()): - winid = window.get_xid() - # Activate the window - # http://faq.pygtk.org/index.py?req=show&file=faq10.027.htp - window.activate(gtk.get_current_event_time()) + screen = wnck.screen_get_default() + winid = None + for window in screen.get_windows(): + if re.search(winName, window.get_name()): + winid = window.get_xid() + # Activate the window + # http://faq.pygtk.org/index.py?req=show&file=faq10.027.htp + window.activate(gtk.get_current_event_time()) + break + return winid + + def setTmpFile(fileName): + global tmpFile + tmpFile = fileName + + def generateTmpFile(): + global tmpFile + if outFile == None: + fp = tempfile.NamedTemporaryFile () + tmpFile = fp.name + '.png' + fp.close () + else: + tmpFile = outFile + return tmpFile + + def capturescreenshot(): + winid = None + if winName: + count = 0 + while count < 3: + winid = getWinId(winName) + count += 1 + time.sleep(1) + if winid: break - finally: - if _importThread: - _importThread._suspend = False - if outFile == None: - fp = tempfile.NamedTemporaryFile () - tmpFile = fp.name + '.png' - fp.close () - else: - tmpFile = outFile - pb = None - if gtkImport and winid is None: - # Based on http://ubuntuforums.org/showthread.php?t=448160 - window = gtk.gdk.get_default_root_window () - size = window.get_size () - pb = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, False, 8, - width or size [0], - height or size [1]) - pb = pb.get_from_drawable (window, window.get_colormap (), - x, y, 0, 0, - width or size [0], - height or size [1]) - if pb: - pb.save (tmpFile, "png") - # based on http://faq.pygtk.org/index.py?req=show&file=faq08.004.htp - del pb - gc.collect () - else: - if winid == None: - winid = 'root' - cmd = "import -window %s" % winid - if width and height: - cmd = "%s -crop %dx%d+%d+%d" % (cmd, width, - height, x, y) - cmd = "%s %s" % (cmd, tmpFile) - status = commands.getstatusoutput (cmd) - if status [0] != 0: - ldtp.log ('Unable to capture screenshot: %s' % status [1], 'error') - return None + pb = None + if gtkImport: + if winid is None: + # Based on http://ubuntuforums.org/showthread.php?t=448160 + window = gtk.gdk.get_default_root_window () + size = window.get_size () + pb = gtk.gdk.Pixbuf (gtk.gdk.COLORSPACE_RGB, False, 8, + width or size [0], + height or size [1]) + pb = pb.get_from_drawable (window, window.get_colormap (), + x, y, 0, 0, + width or size [0], + height or size [1]) + tmpFile = generateTmpFile() + setTmpFile(tmpFile) + if pb: + pb.save(tmpFile, "png") + # based on http://faq.pygtk.org/index.py?req=show&file=faq08.004.htp + del pb + gc.collect() + else: + if winid == None: + winid = 'root' + cmd = "import -window %s" % winid + if width and height: + cmd = "%s -crop %dx%d+%d+%d" % (cmd, width, + height, x, y) + cmd = "%s %s" % (cmd, tmpFile) + status = commands.getstatusoutput (cmd) + if status [0] != 0: + ldtp.log ('Unable to capture screenshot: %s' % status [1], 'error') + setTmpFile(None) + gtk.main_quit() + gobject.idle_add(capturescreenshot) + gobject.idle_add(gtk.main_quit) + gtk.main() return tmpFile def blackoutregion (infile, outfile, topx, topy, botx, boty): |