From 0bc778437d1d4fd6d7e14d744402fc3c08e6484f Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Tue, 26 Aug 2014 15:44:44 -0700 Subject: [PATCH] refactored cleanup handlers to use a queue, so they will work with the GUI --- onionshare/onionshare.py | 31 ++++++++++++++++++++----------- onionshare_gui/onionshare_gui.py | 2 +- setup/onionshare-launcher.py | 2 +- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/onionshare/onionshare.py b/onionshare/onionshare.py index d370fd30..5e6be874 100644 --- a/onionshare/onionshare.py +++ b/onionshare/onionshare.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -import os, sys, subprocess, time, hashlib, platform, json, locale, socket, argparse, Queue, inspect, base64, mimetypes, hmac +import os, sys, subprocess, time, hashlib, platform, json, locale, socket, argparse, Queue, inspect, base64, mimetypes, hmac, shutil from itertools import izip from stem.control import Controller @@ -92,6 +92,22 @@ def add_request(type, path, data=None): 'data': data }) +cleanup_q = Queue.Queue() +def register_cleanup_handler(directory): + global cleanup_q + def handler(signum = None, frame = None): + shutil.rmtree(directory) + cleanup_q.put(handler) + +def execute_cleanup_handlers(): + global cleanup_q + try: + while True: + handler = cleanup_q.get(False) + handler() + except Queue.Empty: + pass + def human_readable_filesize(b): thresh = 1024.0 if b < thresh: @@ -305,16 +321,6 @@ def tails_root(): while True: time.sleep(1) -def register_cleanup_handler(directory): - import signal - import shutil - def handler(signum = None, frame = None): - shutil.rmtree(directory) - sys.exit() - for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]: - signal.signal(sig, handler) - - def main(): load_strings() tails_root() @@ -383,5 +389,8 @@ def main(): app.run(port=port) print '\n' + # shutdown + execute_cleanup_handlers() + if __name__ == '__main__': main() diff --git a/onionshare_gui/onionshare_gui.py b/onionshare_gui/onionshare_gui.py index 8f7f2806..36081b84 100644 --- a/onionshare_gui/onionshare_gui.py +++ b/onionshare_gui/onionshare_gui.py @@ -373,7 +373,7 @@ def main(): # clean up when app quits def shutdown(): - pass + onionshare.execute_cleanup_handlers() app.connect(app, QtCore.SIGNAL("aboutToQuit()"), shutdown) # launch the gui diff --git a/setup/onionshare-launcher.py b/setup/onionshare-launcher.py index 83bf628b..14803cb1 100644 --- a/setup/onionshare-launcher.py +++ b/setup/onionshare-launcher.py @@ -1,5 +1,5 @@ from __future__ import division -import os, sys, subprocess, time, hashlib, platform, json, locale, socket, argparse, Queue, inspect, base64, random, functools, logging, ctypes, hmac +import os, sys, subprocess, time, hashlib, platform, json, locale, socket, argparse, Queue, inspect, base64, random, functools, logging, ctypes, hmac, shutil from itertools import izip import stem, stem.control, flask from PyQt4 import QtCore, QtGui