2015-11-25 22:00:57 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
|
2014-07-30 21:18:17 +00:00
|
|
|
import locale
|
|
|
|
|
|
|
|
try: locale.setlocale( locale.LC_ALL, '' )
|
|
|
|
except: pass
|
|
|
|
|
2013-07-10 20:25:57 +00:00
|
|
|
from include import HydrusConstants as HC
|
2014-04-23 20:56:12 +00:00
|
|
|
from include import ClientConstants as CC
|
2015-03-25 22:04:19 +00:00
|
|
|
from include import HydrusGlobals
|
2015-03-18 21:46:29 +00:00
|
|
|
from include import ClientDefaults
|
2015-10-21 21:53:10 +00:00
|
|
|
from include import ClientNetworking
|
2015-08-26 21:18:39 +00:00
|
|
|
from include import HydrusPubSub
|
2013-10-23 21:36:47 +00:00
|
|
|
from include import HydrusSessions
|
2013-07-24 20:26:00 +00:00
|
|
|
from include import HydrusTags
|
2015-08-26 21:18:39 +00:00
|
|
|
from include import HydrusThreading
|
2013-08-07 22:25:18 +00:00
|
|
|
from include import TestClientConstants
|
2013-07-31 21:26:38 +00:00
|
|
|
from include import TestClientDaemons
|
2015-04-01 20:44:54 +00:00
|
|
|
from include import TestClientDownloading
|
2013-07-10 20:25:57 +00:00
|
|
|
from include import TestConstants
|
2013-07-24 20:26:00 +00:00
|
|
|
from include import TestDialogs
|
2013-07-10 20:25:57 +00:00
|
|
|
from include import TestDB
|
|
|
|
from include import TestFunctions
|
2013-11-13 21:30:38 +00:00
|
|
|
from include import TestHydrusEncryption
|
2015-11-18 22:44:07 +00:00
|
|
|
from include import TestClientImageHandling
|
2014-04-16 20:31:59 +00:00
|
|
|
from include import TestHydrusNATPunch
|
2013-11-20 21:12:21 +00:00
|
|
|
from include import TestHydrusServer
|
2013-11-06 18:22:07 +00:00
|
|
|
from include import TestHydrusSessions
|
2013-07-17 20:56:13 +00:00
|
|
|
from include import TestHydrusTags
|
2013-07-31 21:26:38 +00:00
|
|
|
import collections
|
2013-07-24 20:26:00 +00:00
|
|
|
import os
|
2015-08-26 21:18:39 +00:00
|
|
|
import random
|
2015-12-02 22:32:18 +00:00
|
|
|
import shutil
|
2013-11-06 18:22:07 +00:00
|
|
|
import sys
|
2015-12-02 22:32:18 +00:00
|
|
|
import tempfile
|
2013-11-27 18:27:11 +00:00
|
|
|
import threading
|
2014-04-16 20:31:59 +00:00
|
|
|
import time
|
2013-07-10 20:25:57 +00:00
|
|
|
import unittest
|
2013-07-24 20:26:00 +00:00
|
|
|
import wx
|
2013-11-27 18:27:11 +00:00
|
|
|
from twisted.internet import reactor
|
2015-03-18 21:46:29 +00:00
|
|
|
from include import ClientCaches
|
|
|
|
from include import ClientData
|
2015-03-25 22:04:19 +00:00
|
|
|
from include import HydrusData
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2013-11-06 18:22:07 +00:00
|
|
|
only_run = None
|
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
class Controller( object ):
|
2013-07-24 20:26:00 +00:00
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
def __init__( self ):
|
2013-07-24 20:26:00 +00:00
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
HydrusGlobals.controller = self
|
2015-09-16 18:11:00 +00:00
|
|
|
HydrusGlobals.client_controller = self
|
|
|
|
HydrusGlobals.server_controller = self
|
|
|
|
HydrusGlobals.test_controller = self
|
2015-08-26 21:18:39 +00:00
|
|
|
self._pubsub = HydrusPubSub.HydrusPubSub( self )
|
|
|
|
|
2015-12-02 22:32:18 +00:00
|
|
|
self._old_db_dir = HC.DB_DIR
|
|
|
|
self._old_client_files_dir = HC.CLIENT_FILES_DIR
|
|
|
|
self._old_client_thumbnails_dir = HC.CLIENT_THUMBNAILS_DIR
|
|
|
|
|
|
|
|
HC.DB_DIR = tempfile.mkdtemp()
|
|
|
|
|
|
|
|
HC.CLIENT_FILES_DIR = os.path.join( HC.DB_DIR, 'client_files' )
|
|
|
|
HC.CLIENT_THUMBNAILS_DIR = os.path.join( HC.DB_DIR, 'client_thumbnails' )
|
|
|
|
|
2015-10-14 21:02:25 +00:00
|
|
|
self._new_options = ClientData.ClientOptions()
|
|
|
|
|
2014-03-26 21:23:10 +00:00
|
|
|
def show_text( text ): pass
|
|
|
|
|
2015-03-25 22:04:19 +00:00
|
|
|
HydrusData.ShowText = show_text
|
2014-03-26 21:23:10 +00:00
|
|
|
|
2015-12-23 22:51:04 +00:00
|
|
|
self._call_to_threads = [ HydrusThreading.THREADCallToThread( self ) for i in range( 10 ) ]
|
|
|
|
|
|
|
|
for thread in self._call_to_threads:
|
|
|
|
|
|
|
|
thread.start()
|
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
|
2015-10-21 21:53:10 +00:00
|
|
|
self._http = ClientNetworking.HTTPConnectionManager()
|
2015-04-01 20:44:54 +00:00
|
|
|
|
2013-07-24 20:26:00 +00:00
|
|
|
self._reads = {}
|
|
|
|
|
2013-11-27 18:27:11 +00:00
|
|
|
self._reads[ 'hydrus_sessions' ] = []
|
2014-07-16 20:50:18 +00:00
|
|
|
self._reads[ 'local_booru_share_keys' ] = []
|
2013-11-27 18:27:11 +00:00
|
|
|
self._reads[ 'messaging_sessions' ] = []
|
2014-03-12 22:08:23 +00:00
|
|
|
self._reads[ 'tag_censorship' ] = []
|
2015-03-18 21:46:29 +00:00
|
|
|
self._reads[ 'options' ] = ClientDefaults.GetClientDefaultOptions()
|
2014-08-13 22:18:12 +00:00
|
|
|
|
|
|
|
services = []
|
2015-03-25 22:04:19 +00:00
|
|
|
services.append( ClientData.Service( CC.LOCAL_BOORU_SERVICE_KEY, HC.LOCAL_BOORU, CC.LOCAL_BOORU_SERVICE_KEY, { 'max_monthly_data' : None, 'used_monthly_data' : 0 } ) )
|
|
|
|
services.append( ClientData.Service( CC.LOCAL_FILE_SERVICE_KEY, HC.LOCAL_FILE, CC.LOCAL_FILE_SERVICE_KEY, {} ) )
|
|
|
|
services.append( ClientData.Service( CC.LOCAL_TAG_SERVICE_KEY, HC.LOCAL_TAG, CC.LOCAL_TAG_SERVICE_KEY, {} ) )
|
2014-08-13 22:18:12 +00:00
|
|
|
self._reads[ 'services' ] = services
|
|
|
|
|
2015-12-02 22:32:18 +00:00
|
|
|
client_files_locations = { prefix : HC.CLIENT_FILES_DIR for prefix in HydrusData.IterateHexPrefixes() }
|
|
|
|
|
|
|
|
self._reads[ 'client_files_locations' ] = client_files_locations
|
|
|
|
|
2013-11-27 18:27:11 +00:00
|
|
|
self._reads[ 'sessions' ] = []
|
2013-07-24 20:26:00 +00:00
|
|
|
self._reads[ 'tag_parents' ] = {}
|
|
|
|
self._reads[ 'tag_siblings' ] = {}
|
2013-11-27 18:27:11 +00:00
|
|
|
self._reads[ 'web_sessions' ] = {}
|
2013-07-24 20:26:00 +00:00
|
|
|
|
2015-03-18 21:46:29 +00:00
|
|
|
HC.options = ClientDefaults.GetClientDefaultOptions()
|
2013-09-11 21:28:19 +00:00
|
|
|
|
2013-07-31 21:26:38 +00:00
|
|
|
self._writes = collections.defaultdict( list )
|
|
|
|
|
2013-11-27 18:27:11 +00:00
|
|
|
self._managers = {}
|
2013-07-24 20:26:00 +00:00
|
|
|
|
2015-11-25 22:00:57 +00:00
|
|
|
self._services_manager = ClientCaches.ServicesManager( self )
|
2015-12-02 22:32:18 +00:00
|
|
|
self._client_files_manager = ClientCaches.ClientFilesManager( self )
|
2015-12-09 23:16:41 +00:00
|
|
|
self._client_session_manager = ClientCaches.HydrusSessionManager( self )
|
2014-08-13 22:18:12 +00:00
|
|
|
|
2015-11-25 22:00:57 +00:00
|
|
|
self._managers[ 'tag_censorship' ] = ClientCaches.TagCensorshipManager( self )
|
|
|
|
self._managers[ 'tag_siblings' ] = ClientCaches.TagSiblingsManager( self )
|
|
|
|
self._managers[ 'tag_parents' ] = ClientCaches.TagParentsManager( self )
|
|
|
|
self._managers[ 'undo' ] = ClientCaches.UndoManager( self )
|
2013-12-04 22:44:16 +00:00
|
|
|
self._managers[ 'web_sessions' ] = TestConstants.FakeWebSessionManager()
|
2015-12-09 23:16:41 +00:00
|
|
|
self._server_session_manager = HydrusSessions.HydrusSessionManagerServer()
|
2015-11-25 22:00:57 +00:00
|
|
|
self._managers[ 'local_booru' ] = ClientCaches.LocalBooruCache( self )
|
2014-07-16 20:50:18 +00:00
|
|
|
|
2013-09-04 16:48:44 +00:00
|
|
|
self._cookies = {}
|
|
|
|
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
def pub( self, topic, *args, **kwargs ):
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def pubimmediate( self, topic, *args, **kwargs ):
|
|
|
|
|
|
|
|
self._pubsub.pubimmediate( topic, *args, **kwargs )
|
|
|
|
|
|
|
|
|
|
|
|
def sub( self, object, method_name, topic ):
|
|
|
|
|
|
|
|
self._pubsub.sub( object, method_name, topic )
|
|
|
|
|
|
|
|
|
|
|
|
def CallToThread( self, callable, *args, **kwargs ):
|
|
|
|
|
|
|
|
call_to_thread = random.choice( self._call_to_threads )
|
|
|
|
|
|
|
|
while call_to_thread == threading.current_thread: call_to_thread = random.choice( self._call_to_threads )
|
|
|
|
|
|
|
|
call_to_thread.put( callable, *args, **kwargs )
|
|
|
|
|
|
|
|
|
2015-04-01 20:44:54 +00:00
|
|
|
def DoHTTP( self, *args, **kwargs ): return self._http.Request( *args, **kwargs )
|
|
|
|
|
2015-12-02 22:32:18 +00:00
|
|
|
def GetClientFilesManager( self ):
|
|
|
|
|
|
|
|
return self._client_files_manager
|
|
|
|
|
|
|
|
|
2015-12-09 23:16:41 +00:00
|
|
|
def GetClientSessionManager( self ):
|
|
|
|
|
|
|
|
return self._client_session_manager
|
|
|
|
|
|
|
|
|
2015-04-01 20:44:54 +00:00
|
|
|
def GetHTTP( self ): return self._http
|
|
|
|
|
2015-10-14 21:02:25 +00:00
|
|
|
def GetNewOptions( self ):
|
|
|
|
|
|
|
|
return self._new_options
|
|
|
|
|
|
|
|
|
2015-05-13 20:22:39 +00:00
|
|
|
def GetOptions( self ):
|
|
|
|
|
|
|
|
return HC.options
|
|
|
|
|
|
|
|
|
2014-09-10 22:37:38 +00:00
|
|
|
def GetManager( self, manager_type ): return self._managers[ manager_type ]
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2015-07-01 22:02:07 +00:00
|
|
|
def GetServicesManager( self ):
|
|
|
|
|
|
|
|
return self._services_manager
|
|
|
|
|
|
|
|
|
2015-12-09 23:16:41 +00:00
|
|
|
def GetServerSessionManager( self ):
|
|
|
|
|
|
|
|
return self._server_session_manager
|
|
|
|
|
|
|
|
|
2013-07-31 21:26:38 +00:00
|
|
|
def GetWrite( self, name ):
|
|
|
|
|
|
|
|
write = self._writes[ name ]
|
|
|
|
|
|
|
|
del self._writes[ name ]
|
|
|
|
|
|
|
|
return write
|
|
|
|
|
|
|
|
|
2015-11-04 22:30:28 +00:00
|
|
|
def ModelIsShutdown( self ):
|
|
|
|
|
|
|
|
return HydrusGlobals.model_shutdown
|
|
|
|
|
|
|
|
|
2013-10-23 21:36:47 +00:00
|
|
|
def Read( self, name, *args, **kwargs ): return self._reads[ name ]
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2014-12-03 22:56:40 +00:00
|
|
|
def ResetIdleTimer( self ): pass
|
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
def Run( self ):
|
|
|
|
|
|
|
|
suites = []
|
|
|
|
|
|
|
|
if only_run is None: run_all = True
|
|
|
|
else: run_all = False
|
|
|
|
|
|
|
|
if run_all or only_run == 'cc': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientConstants ) )
|
|
|
|
if run_all or only_run == 'daemons': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientDaemons ) )
|
|
|
|
if run_all or only_run == 'dialogs': suites.append( unittest.TestLoader().loadTestsFromModule( TestDialogs ) )
|
|
|
|
if run_all or only_run == 'db': suites.append( unittest.TestLoader().loadTestsFromModule( TestDB ) )
|
|
|
|
if run_all or only_run == 'downloading': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientDownloading ) )
|
|
|
|
if run_all or only_run == 'encryption': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusEncryption ) )
|
|
|
|
if run_all or only_run == 'functions': suites.append( unittest.TestLoader().loadTestsFromModule( TestFunctions ) )
|
2015-11-18 22:44:07 +00:00
|
|
|
if run_all or only_run == 'image': suites.append( unittest.TestLoader().loadTestsFromModule( TestClientImageHandling ) )
|
2015-09-02 23:16:09 +00:00
|
|
|
if run_all or only_run == 'nat': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusNATPunch ) )
|
|
|
|
if run_all or only_run == 'server': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusServer ) )
|
|
|
|
if run_all or only_run == 'sessions': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusSessions ) )
|
|
|
|
if run_all or only_run == 'tags': suites.append( unittest.TestLoader().loadTestsFromModule( TestHydrusTags ) )
|
|
|
|
|
|
|
|
suite = unittest.TestSuite( suites )
|
|
|
|
|
|
|
|
runner = unittest.TextTestRunner( verbosity = 1 )
|
|
|
|
|
|
|
|
runner.run( suite )
|
|
|
|
|
|
|
|
|
2015-04-01 20:44:54 +00:00
|
|
|
def SetHTTP( self, http ): self._http = http
|
|
|
|
|
2013-07-24 20:26:00 +00:00
|
|
|
def SetRead( self, name, value ): self._reads[ name ] = value
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2013-09-04 16:48:44 +00:00
|
|
|
def SetWebCookies( self, name, value ): self._cookies[ name ] = value
|
|
|
|
|
2015-12-02 22:32:18 +00:00
|
|
|
def TidyUp( self ):
|
|
|
|
|
|
|
|
shutil.rmtree( HC.DB_DIR )
|
|
|
|
|
|
|
|
HC.DB_DIR = self._old_db_dir
|
|
|
|
HC.CLIENT_FILES_DIR = self._old_client_files_dir
|
|
|
|
HC.CLIENT_THUMBNAILS_DIR = self._old_client_thumbnails_dir
|
|
|
|
|
|
|
|
|
2015-11-04 22:30:28 +00:00
|
|
|
def ViewIsShutdown( self ):
|
|
|
|
|
|
|
|
return HydrusGlobals.view_shutdown
|
|
|
|
|
|
|
|
|
2013-07-31 21:26:38 +00:00
|
|
|
def Write( self, name, *args, **kwargs ):
|
|
|
|
|
|
|
|
self._writes[ name ].append( ( args, kwargs ) )
|
|
|
|
|
|
|
|
|
2013-08-14 20:21:49 +00:00
|
|
|
def WriteSynchronous( self, name, *args, **kwargs ):
|
2013-07-31 21:26:38 +00:00
|
|
|
|
|
|
|
self._writes[ name ].append( ( args, kwargs ) )
|
|
|
|
|
|
|
|
if name == 'import_file':
|
2013-08-07 22:25:18 +00:00
|
|
|
|
|
|
|
( path, ) = args
|
|
|
|
|
|
|
|
with open( path, 'rb' ) as f: file = f.read()
|
|
|
|
|
|
|
|
if file == 'blarg': raise Exception( 'File failed to import for some reason!' )
|
2015-09-09 22:04:39 +00:00
|
|
|
else: return ( CC.STATUS_SUCCESSFUL, '0123456789abcdef'.decode( 'hex' ) )
|
|
|
|
|
2013-07-31 21:26:38 +00:00
|
|
|
|
|
|
|
|
2013-07-24 20:26:00 +00:00
|
|
|
if __name__ == '__main__':
|
2013-07-10 20:25:57 +00:00
|
|
|
|
2013-11-06 18:22:07 +00:00
|
|
|
args = sys.argv[1:]
|
|
|
|
|
|
|
|
if len( args ) > 0:
|
|
|
|
|
|
|
|
only_run = args[0]
|
|
|
|
|
|
|
|
else: only_run = None
|
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
try:
|
|
|
|
|
|
|
|
threading.Thread( target = reactor.run, kwargs = { 'installSignalHandlers' : 0 } ).start()
|
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
controller = Controller()
|
|
|
|
|
|
|
|
app = wx.App()
|
|
|
|
|
|
|
|
win = wx.Frame( None )
|
|
|
|
|
|
|
|
wx.CallAfter( controller.Run )
|
|
|
|
#threading.Thread( target = controller.Run ).start()
|
|
|
|
|
|
|
|
wx.CallAfter( win.Destroy )
|
|
|
|
|
|
|
|
app.MainLoop()
|
2015-08-26 21:18:39 +00:00
|
|
|
|
2015-09-16 18:11:00 +00:00
|
|
|
except:
|
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
finally:
|
|
|
|
|
|
|
|
HydrusGlobals.view_shutdown = True
|
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
controller.pubimmediate( 'wake_daemons' )
|
2015-08-26 21:18:39 +00:00
|
|
|
|
|
|
|
HydrusGlobals.model_shutdown = True
|
|
|
|
|
2015-09-02 23:16:09 +00:00
|
|
|
controller.pubimmediate( 'wake_daemons' )
|
2015-08-26 21:18:39 +00:00
|
|
|
|
2015-12-02 22:32:18 +00:00
|
|
|
controller.TidyUp()
|
|
|
|
|
2015-08-26 21:18:39 +00:00
|
|
|
reactor.callFromThread( reactor.stop )
|
|
|
|
|
|
|
|
raw_input()
|
|
|
|
|
|
|
|
|