From 6d0fed0b1330af9ae51b4022ab5312f89d7bfe98 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 b6da74f9..7c3211ad 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2709,18 +2709,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') @@ -2741,12 +2741,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 8f6b4294..ce3ecd7b 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: