hydrus/client.pyw

191 lines
5.3 KiB
Python
Raw Normal View History

2019-01-09 22:59:03 +00:00
#!/usr/bin/env python3
2015-11-25 22:00:57 +00:00
2013-02-19 00:11:43 +00:00
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
2014-08-27 22:15:22 +00:00
try:
2013-02-19 00:11:43 +00:00
2019-01-09 22:59:03 +00:00
from include import HydrusPy2To3
2019-01-23 22:19:16 +00:00
import wx
HydrusPy2To3.do_2to3_test( wx_error_display_callable = wx.SafeShowMessage )
2019-01-09 22:59:03 +00:00
2016-10-12 21:52:50 +00:00
from include import HydrusExceptions
2014-08-27 22:15:22 +00:00
from include import HydrusConstants as HC
2015-11-18 22:44:07 +00:00
from include import HydrusData
2016-10-12 21:52:50 +00:00
from include import HydrusPaths
2014-08-27 22:15:22 +00:00
import os
import sys
import time
from include import ClientController
import threading
2017-05-10 21:33:58 +00:00
from include import HydrusGlobals as HG
2015-11-18 22:44:07 +00:00
from include import HydrusLogger
2015-09-02 23:16:09 +00:00
import traceback
2017-10-25 21:45:15 +00:00
try:
from twisted.internet import reactor
except:
HG.twisted_is_broke = True
2016-10-19 20:02:56 +00:00
#
2016-10-12 21:52:50 +00:00
import argparse
argparser = argparse.ArgumentParser( description = 'hydrus network client (windowed)' )
argparser.add_argument( '-d', '--db_dir', help = 'set an external db location' )
2016-10-19 20:02:56 +00:00
argparser.add_argument( '--no_daemons', action='store_true', help = 'run without background daemons' )
argparser.add_argument( '--no_wal', action='store_true', help = 'run without WAL db journalling' )
2019-03-06 23:06:22 +00:00
argparser.add_argument( '--no_db_temp_files', action='store_true', help = 'run the db entirely in memory' )
2019-03-20 21:22:10 +00:00
argparser.add_argument( '--temp_dir', help = 'override the program\'s temporary directory' )
2016-10-12 21:52:50 +00:00
result = argparser.parse_args()
if result.db_dir is None:
2017-01-18 22:52:39 +00:00
db_dir = HC.DEFAULT_DB_DIR
2016-10-12 21:52:50 +00:00
2019-01-09 22:59:03 +00:00
if not HydrusPaths.DirectoryIsWritable( db_dir ) or HC.RUNNING_FROM_OSX_APP:
2018-02-21 21:59:37 +00:00
2019-01-09 22:59:03 +00:00
db_dir = HC.USERPATH_DB_DIR
2018-02-21 21:59:37 +00:00
2016-10-12 21:52:50 +00:00
else:
db_dir = result.db_dir
2016-10-26 20:45:34 +00:00
db_dir = HydrusPaths.ConvertPortablePathToAbsPath( db_dir, HC.BASE_DIR )
2016-10-19 20:02:56 +00:00
2016-10-12 21:52:50 +00:00
try:
HydrusPaths.MakeSureDirectoryExists( db_dir )
except:
raise Exception( 'Could not ensure db path ' + db_dir + ' exists! Check the location is correct and that you have permission to write to it!' )
2019-03-20 21:22:10 +00:00
HG.no_daemons = result.no_daemons
HG.no_wal = result.no_wal
2019-03-06 23:06:22 +00:00
HG.no_db_temp_files = result.no_db_temp_files
2016-10-19 20:02:56 +00:00
2019-03-20 21:22:10 +00:00
if result.temp_dir is not None:
if not os.path.exists( result.temp_dir ):
raise Exception( 'The given temp directory, "{}", does not exist!'.format( result.temp_dir ) )
if HC.PLATFORM_WINDOWS:
os.environ[ 'TEMP' ] = result.temp_dir
os.environ[ 'TMP' ] = result.temp_dir
else:
os.environ[ 'TMPDIR' ] = result.temp_dir
2016-10-19 20:02:56 +00:00
#
2016-11-09 23:13:22 +00:00
with HydrusLogger.HydrusLogger( db_dir, 'client' ) as logger:
2014-06-18 21:53:48 +00:00
2014-08-27 22:15:22 +00:00
try:
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'hydrus client started' )
2014-08-27 22:15:22 +00:00
2017-10-25 21:45:15 +00:00
if not HG.twisted_is_broke:
2019-01-23 22:19:16 +00:00
threading.Thread( target = reactor.run, name = 'twisted', kwargs = { 'installSignalHandlers' : 0 } ).start()
2017-10-25 21:45:15 +00:00
2014-08-27 22:15:22 +00:00
2019-03-20 21:22:10 +00:00
controller = ClientController.Controller( db_dir )
2014-08-27 22:15:22 +00:00
2015-09-02 23:16:09 +00:00
controller.Run()
2014-08-27 22:15:22 +00:00
except:
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'hydrus client failed' )
2014-08-27 22:15:22 +00:00
2015-11-18 22:44:07 +00:00
HydrusData.Print( traceback.format_exc() )
2014-08-27 22:15:22 +00:00
2018-01-10 22:41:51 +00:00
try:
import wx
message = 'The client failed to start. The error follows (it has also been written to the log in the db directory). If it is not obvious, please inform hydrus dev.'
message += os.linesep * 2
message += traceback.format_exc()
wx.SafeShowMessage( 'hydrus client failed', message )
except:
pass
2015-08-26 21:18:39 +00:00
finally:
2017-05-10 21:33:58 +00:00
HG.view_shutdown = True
HG.model_shutdown = True
2015-08-26 21:18:39 +00:00
2016-01-13 22:08:19 +00:00
try:
controller.pubimmediate( 'wake_daemons' )
except:
HydrusData.Print( traceback.format_exc() )
2015-08-26 21:18:39 +00:00
2017-10-25 21:45:15 +00:00
if not HG.twisted_is_broke:
reactor.callFromThread( reactor.stop )
2015-08-26 21:18:39 +00:00
2015-11-18 22:44:07 +00:00
HydrusData.Print( 'hydrus client shut down' )
2015-08-26 21:18:39 +00:00
2013-07-24 20:26:00 +00:00
2014-11-12 23:33:13 +00:00
2017-05-10 21:33:58 +00:00
HG.shutdown_complete = True
2015-12-16 22:41:06 +00:00
2017-05-10 21:33:58 +00:00
if HG.restart:
2015-12-16 22:41:06 +00:00
HydrusData.RestartProcess()
2016-10-12 21:52:50 +00:00
except Exception as e:
2014-08-27 22:15:22 +00:00
import traceback
2016-08-10 19:04:08 +00:00
import os
2014-08-27 22:15:22 +00:00
2016-10-12 21:52:50 +00:00
print( traceback.format_exc() )
if 'db_dir' in locals() and os.path.exists( db_dir ):
2016-07-20 19:57:10 +00:00
2016-10-12 21:52:50 +00:00
dest_path = os.path.join( db_dir, 'crash.log' )
2016-07-20 19:57:10 +00:00
2019-01-16 22:40:53 +00:00
with open( dest_path, 'w', encoding = 'utf-8' ) as f:
2016-07-20 19:57:10 +00:00
f.write( traceback.format_exc() )
2018-07-18 21:07:15 +00:00
print( 'Critical error occurred! Details written to crash.log!' )
2016-07-20 19:57:10 +00:00
2017-01-18 22:52:39 +00:00