From 5a45d7dd8f008dd91dc9648f1ece90bac4534af2 Mon Sep 17 00:00:00 2001 From: mailchuck Date: Sun, 13 Dec 2015 21:36:25 +0100 Subject: [PATCH] Portable mode fixes Fixes Bitmessage#379, Bitmessage#341 --- src/bitmessageqt/__init__.py | 14 +++++++------- src/class_sqlThread.py | 8 ++++---- src/helper_startup.py | 4 ++-- src/shared.py | 9 +++++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 194ba8b1..e0a594d6 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2708,18 +2708,18 @@ class MyForm(settingsmixin.SMainWindow): if shared.appdata != '' and self.settingsDialogInstance.ui.checkBoxPortableMode.isChecked(): # If we are NOT using portable mode now but the user selected that we should... # Write the keys.dat file to disk in the new location sqlStoredProcedure('movemessagstoprog') - with open('keys.dat', 'wb') as configfile: + with open(shared.lookupExeFolder() + 'keys.dat', 'wb') as configfile: shared.config.write(configfile) # Write the knownnodes.dat file to disk in the new location shared.knownNodesLock.acquire() - output = open('knownnodes.dat', 'wb') + output = open(shared.lookupExeFolder() + 'knownnodes.dat', 'wb') pickle.dump(shared.knownNodes, output) output.close() shared.knownNodesLock.release() os.remove(shared.appdata + 'keys.dat') os.remove(shared.appdata + 'knownnodes.dat') previousAppdataLocation = shared.appdata - shared.appdata = '' + shared.appdata = shared.lookupExeFolder() debug.restartLoggingInUpdatedAppdataLocation() try: os.remove(previousAppdataLocation + 'debug.log') @@ -2740,12 +2740,12 @@ class MyForm(settingsmixin.SMainWindow): pickle.dump(shared.knownNodes, output) output.close() shared.knownNodesLock.release() - os.remove('keys.dat') - os.remove('knownnodes.dat') + os.remove(shared.lookupExeFolder() + 'keys.dat') + os.remove(shared.lookupExeFolder() + 'knownnodes.dat') debug.restartLoggingInUpdatedAppdataLocation() try: - os.remove('debug.log') - os.remove('debug.log.1') + os.remove(shared.lookupExeFolder() + 'debug.log') + os.remove(shared.lookupExeFolder() + 'debug.log.1') except: pass diff --git a/src/class_sqlThread.py b/src/class_sqlThread.py index 63621c89..682cffee 100644 --- a/src/class_sqlThread.py +++ b/src/class_sqlThread.py @@ -501,8 +501,8 @@ class sqlThread(threading.Thread): return self.conn.close() shutil.move( - shared.lookupAppdataFolder() + 'messages.dat', 'messages.dat') - self.conn = sqlite3.connect('messages.dat') + shared.lookupAppdataFolder() + 'messages.dat', shared.lookupExeFolder() + 'messages.dat') + self.conn = sqlite3.connect(shared.lookupExeFolder() + 'messages.dat') self.conn.text_factory = str self.cur = self.conn.cursor() elif item == 'movemessagstoappdata': @@ -520,8 +520,8 @@ class sqlThread(threading.Thread): return self.conn.close() shutil.move( - 'messages.dat', shared.lookupAppdataFolder() + 'messages.dat') - self.conn = sqlite3.connect(shared.appdata + 'messages.dat') + shared.lookupExeFolder() + 'messages.dat', shared.lookupAppdataFolder() + 'messages.dat') + self.conn = sqlite3.connect(shared.lookupAppdataFolder() + 'messages.dat') self.conn.text_factory = str self.cur = self.conn.cursor() elif item == 'deleteandvacuume': diff --git a/src/helper_startup.py b/src/helper_startup.py index 0d4e0cb0..11671481 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -35,12 +35,12 @@ def loadConfig(): needToCreateKeysFile = True else: - shared.config.read('keys.dat') + shared.config.read(shared.lookupExeFolder() + 'keys.dat')) try: shared.config.get('bitmessagesettings', 'settingsversion') print 'Loading config files from same directory as program.' needToCreateKeysFile = False - shared.appdata = '' + shared.appdata = shared.lookupExeFolder() except: # Could not load the keys.dat file in the program directory. Perhaps it # is in the appdata directory. diff --git a/src/shared.py b/src/shared.py index edc5d575..77b81322 100644 --- a/src/shared.py +++ b/src/shared.py @@ -198,6 +198,15 @@ def assembleErrorMessage(fatal=0, banTime=0, inventoryVector='', errorText=''): payload += errorText return CreatePacket('error', payload) +def lookupExeFolder(): + if frozen: + exeFolder = os.path.join(os.path.dirname(sys.executable), '/') + elif __file__: + exeFolder = os.path.join(os.path.dirname(__file__), '/') + else: + exeFolder = '' + return exeFolder + def lookupAppdataFolder(): APPNAME = "PyBitmessage" if "BITMESSAGE_HOME" in environ: