def Run(self): HydrusData.RecordRunningStart(self.db_dir, 'server') HydrusData.Print(u'Initialising db\u2026') self.InitModel() HydrusData.Print(u'Initialising daemons and services\u2026') self.InitView() HydrusData.Print('Server is running. Press Ctrl+C to quit.') try: while not self._model_shutdown and not self._shutdown: time.sleep(1) except KeyboardInterrupt: HydrusData.Print(u'Received a keyboard interrupt\u2026') HydrusData.Print(u'Shutting down controller\u2026') self.Exit()
def GetMime(path): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') with open(path, 'rb') as f: f.seek(0) bit_to_check = f.read(256) for (offset, header, mime) in header_and_mime: offset_bit_to_check = bit_to_check[offset:] if offset_bit_to_check.startswith(header): if mime == HC.UNDETERMINED_WM: if HydrusVideoHandling.HasVideoStream(path): return HC.VIDEO_WMV # we'll catch and verify wma later elif mime == HC.UNDETERMINED_PNG: if HydrusVideoHandling.HasVideoStream(path): return HC.IMAGE_APNG else: return HC.IMAGE_PNG else: return mime try: mime = HydrusVideoHandling.GetMime(path) if mime != HC.APPLICATION_UNKNOWN: return mime except HydrusExceptions.MimeException: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) except Exception as e: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) HydrusData.PrintException(e, do_wait=False) return HC.APPLICATION_UNKNOWN
def Exit( self ): HydrusData.Print( 'Shutting down daemons and services...' ) self.ShutdownView() HydrusData.Print( 'Shutting down db...' ) self.ShutdownModel()
def Exit(self): HydrusData.Print(u'Shutting down daemons and services\u2026') self.ShutdownView() HydrusData.Print(u'Shutting down db\u2026') self.ShutdownModel() HydrusData.CleanRunningFile(self.db_dir, 'server')
def Exit(self): HydrusData.Print('Shutting down daemons and services...') self.ShutdownView() HydrusData.Print('Shutting down db...') self.ShutdownModel() HydrusData.CleanRunningFile(HC.DB_DIR, 'server')
def CreateSplash( self ): try: self._splash = ClientGUI.FrameSplash( self ) except: HydrusData.Print( 'There was an error trying to start the splash screen!' ) HydrusData.Print( traceback.format_exc() ) raise
def finish(self): Request.finish(self) host = self.getHost() if self.hydrus_response_context is not None: status_text = HydrusData.ToUnicode( self.hydrus_response_context.GetStatusCode()) elif hasattr(self, 'code'): status_text = str(self.code) else: status_text = '200' message = str(host.port) + ' ' + HydrusData.ToUnicode( self.method ) + ' ' + HydrusData.ToUnicode( self.path ) + ' ' + status_text + ' in ' + HydrusData.ConvertTimeDeltaToPrettyString( time.clock() - self.start_time) HydrusData.Print(message)
def ShowExceptionClient(e, do_wait=True): (etype, value, tb) = sys.exc_info() if etype is None: etype = type(e) value = HydrusData.ToUnicode(e) trace = 'No error trace--here is the stack:' + os.linesep + ''.join( traceback.format_stack()) else: trace = ''.join(traceback.format_exception(etype, value, tb)) trace = HydrusData.ToUnicode(trace) pretty_value = HydrusData.ToUnicode(value) if os.linesep in pretty_value: (first_line, anything_else) = HydrusData.ToUnicode(value).split(os.linesep, 1) trace = trace + os.linesep + anything_else else: first_line = pretty_value job_key = ClientThreading.JobKey() if isinstance(e, HydrusExceptions.ShutdownException): return else: if hasattr(etype, '__name__'): title = HydrusData.ToUnicode(etype.__name__) else: title = HydrusData.ToUnicode(etype) job_key.SetVariable('popup_title', title) job_key.SetVariable('popup_text_1', first_line) job_key.SetVariable('popup_traceback', trace) text = job_key.ToString() HydrusData.Print('Exception:') HydrusData.DebugPrint(text) HG.client_controller.pub('message', job_key) if do_wait: time.sleep(1)
def _FetchData(self, url, referral_url=None, temp_path=None): self._EnsureLoggedIn() network_job = self._network_job_factory('GET', url, referral_url=referral_url, temp_path=temp_path) HG.client_controller.network_engine.AddJob(network_job) try: network_job.WaitUntilDone() except Exception as e: HydrusData.Print('The url ' + url + ' gave the following problem:') HydrusData.PrintException(e) raise if temp_path is None: return network_job.GetContent()
def Start(*args, **kwargs): try: port = service.GetPort() try: connection = HydrusNetworking.GetLocalConnection(port) connection.close() raise Exception( 'Something was already bound to port ' + str(port)) except: if service_type == HC.SERVER_ADMIN: http_factory = ServerServer.HydrusServiceAdmin( service) elif service_type == HC.FILE_REPOSITORY: http_factory = ServerServer.HydrusServiceRepositoryFile( service) elif service_type == HC.TAG_REPOSITORY: http_factory = ServerServer.HydrusServiceRepositoryTag( service) else: return (ssl_cert_path, ssl_key_path) = self.db.GetSSLPaths() sslmethod = twisted.internet.ssl.SSL.TLSv1_2_METHOD context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory( ssl_key_path, ssl_cert_path, sslmethod) self._service_keys_to_connected_ports[ service_key] = reactor.listenSSL( port, http_factory, context_factory) try: connection = HydrusNetworking.GetLocalConnection( port) connection.close() except: raise Exception('Tried to bind port ' + str(port) + ' but it failed.') except Exception as e: HydrusData.Print(traceback.format_exc())
def CleanUpTempPath( os_file_handle, temp_path ): try: os.close( os_file_handle ) except OSError: gc.collect() try: os.close( os_file_handle ) except OSError: HydrusData.Print( 'Could not close the temporary file ' + temp_path ) return try: os.remove( temp_path ) except OSError: with TEMP_PATH_LOCK: IN_USE_TEMP_PATHS.add( ( HydrusData.GetNow(), temp_path ) )
def EnsureLoggedIn( self, name ): with self._lock: if name in self._error_names: raise Exception( name + ' could not establish a session! This ugly error is temporary due to the network engine rewrite. Please restart the client to reattempt this network context.' ) if name == 'hentai foundry': network_context = ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOMAIN, 'hentai-foundry.com' ) required_cookies = [ 'PHPSESSID', 'YII_CSRF_TOKEN' ] elif name == 'pixiv': network_context = ClientNetworking.NetworkContext( CC.NETWORK_CONTEXT_DOMAIN, 'pixiv.net' ) required_cookies = [ 'PHPSESSID' ] if self._IsLoggedIn( network_context, required_cookies ): return try: if name == 'hentai foundry': self.LoginHF( network_context ) elif name == 'pixiv': result = self.engine.controller.Read( 'serialisable_simple', 'pixiv_account' ) if result is None: raise HydrusExceptions.DataMissing( 'You need to set up your pixiv credentials in services->manage pixiv account.' ) ( pixiv_id, password ) = result self.LoginPixiv( network_context, pixiv_id, password ) if not self._IsLoggedIn( network_context, required_cookies ): raise Exception( name + ' login did not work correctly!' ) HydrusData.Print( 'Successfully logged into ' + name + '.' ) except: self._error_names.add( name ) raise
def SetStatus(self, status, note='', exception=None): if exception is not None: first_line = HydrusData.ToUnicode(exception).split(os.linesep)[0] note = first_line + u'\u2026 (Copy note to see full error)' note += os.linesep note += HydrusData.ToUnicode(traceback.format_exc()) HydrusData.Print('Error when processing ' + self.url + ' !') HydrusData.Print(traceback.format_exc()) self.status = status self.note = note self._UpdateModified()
def Run(self): self._app = wx.App() self._app.SetAssertMode(wx.PYAPP_ASSERT_SUPPRESS) HydrusData.Print('booting controller...') self.CreateSplash() boot_thread = threading.Thread(target=self.THREADBootEverything, name='Application Boot Thread') boot_thread.start() self._app.MainLoop() HydrusData.Print('shutting down controller...')
def _Rollback( self ): if self._in_transaction: self._c.execute( 'ROLLBACK TO hydrus_savepoint;' ) else: HydrusData.Print( 'Received a call to rollback, but was not in a transaction!' )
def Parse4chanPostScreen(html): soup = ClientParsing.GetSoup(html) title_tag = soup.find('title') if title_tag.string == 'Post successful!': return ('success', None) elif title_tag.string == '4chan - Banned': HydrusData.Print(soup) text = 'You are banned from this board! html written to log.' HydrusData.ShowText(text) return ('big error', text) else: try: problem_tag = soup.find(id='errmsg') if problem_tag is None: HydrusData.Print(soup) text = 'Unknown problem; html written to log.' HydrusData.ShowText(text) return ('error', text) problem = HydrusData.ToUnicode(problem_tag) if 'CAPTCHA' in problem: return ('captcha', None) elif 'seconds' in problem: return ('too quick', None) elif 'Duplicate' in problem: return ('error', 'duplicate file detected') else: return ('error', problem) except: return ('error', 'unknown error')
def RecyclePath( path ): original_path = path if HC.PLATFORM_LINUX: # send2trash for Linux tries to do some Python3 str() stuff in prepping non-str paths for recycling if not isinstance( path, str ): try: path = path.encode( sys.getfilesystemencoding() ) except: HydrusData.Print( 'Trying to prepare ' + path + ' for recycling created this error:' ) HydrusData.DebugPrint( traceback.format_exc() ) return if os.path.exists( path ): MakeFileWritable( path ) try: send2trash.send2trash( path ) except: HydrusData.Print( 'Trying to recycle ' + path + ' created this error:' ) HydrusData.DebugPrint( traceback.format_exc() ) HydrusData.Print( 'It has been fully deleted instead.' ) DeletePath( original_path )
def Run( self ): self._app = wx.App() self._app.locale = wx.Locale( wx.LANGUAGE_DEFAULT ) # Very important to init this here and keep it non garbage collected # do not import locale here and try anything clever--assume that bad locale formatting is due to OS-level mess-up, not mine # wx locale is supposed to set it all up nice, so if someone's doesn't, explore that and find the external solution HydrusData.Print( u'booting controller\u2026' ) self.frame_icon = wx.Icon( os.path.join( HC.STATIC_DIR, 'hydrus_32_non-transparent.png' ), wx.BITMAP_TYPE_PNG ) self.CreateSplash() self.CallToThreadLongRunning( self.THREADBootEverything ) self._app.MainLoop() HydrusData.Print( u'shutting down controller\u2026' )
def Run( self ): self._app = wx.App() self._app.locale = wx.Locale( wx.LANGUAGE_DEFAULT ) # Very important to init this here and keep it non garbage collected # I have had this as 'suppress' before self._app.SetAssertMode( wx.PYAPP_ASSERT_EXCEPTION ) HydrusData.Print( u'booting controller\u2026' ) self.frame_icon = wx.Icon( os.path.join( HC.STATIC_DIR, 'hydrus_32_non-transparent.png' ), wx.BITMAP_TYPE_PNG ) self._CreateSplash() self.CallToThreadLongRunning( self.THREADBootEverything ) self._app.MainLoop() HydrusData.Print( u'shutting down controller\u2026' )
def _Commit( self ): if self._in_transaction: self._c.execute( 'COMMIT;' ) self._in_transaction = False else: HydrusData.Print( 'Received a call to commit, but was not in a transaction!' )
def ShutdownFromServer(self): HydrusData.Print(u'Received a server shut down request\u2026') def do_it(): time.sleep(1) self.Exit() self.CallToThread(do_it)
def _ProcessJob( self, job ): job_type = job.GetType() ( action, args, kwargs ) = job.GetCallableTuple() in_transaction = False try: if job_type in ( 'read_write', 'write' ): self._c.execute( 'BEGIN IMMEDIATE;' ) in_transaction = True if job_type in ( 'read', 'read_write' ): result = self._Read( action, *args, **kwargs ) elif job_type in ( 'write' ): result = self._Write( action, *args, **kwargs ) if in_transaction: self._c.execute( 'COMMIT;' ) in_transaction = False for ( topic, args, kwargs ) in self._pubsubs: self._controller.pub( topic, *args, **kwargs ) if job.IsSynchronous(): job.PutResult( result ) except Exception as e: if in_transaction: try: self._c.execute( 'ROLLBACK;' ) except Exception as rollback_e: HydrusData.Print( 'When the transaction failed, attempting to rollback the database failed.' ) HydrusData.PrintException( rollback_e ) self._ManageDBError( job, e )
def ShowTextClient(text): job_key = ClientThreading.JobKey() job_key.SetVariable('popup_text_1', HydrusData.ToUnicode(text)) text = job_key.ToString() HydrusData.Print(text) HG.client_controller.pub('message', job_key)
def Run( self ): HydrusData.RecordRunningStart( self._db_dir, 'server' ) HydrusData.Print( u'Initialising db\u2026' ) self.InitModel() HydrusData.Print( u'Initialising daemons and services\u2026' ) self.InitView() HydrusData.Print( 'Server is running. Press Ctrl+C to quit.' ) interrupt_received = False while not self._model_shutdown: try: time.sleep( 1 ) except KeyboardInterrupt: if not interrupt_received: interrupt_received = True HydrusData.Print( u'Received a keyboard interrupt\u2026' ) def do_it(): self.Exit() self.CallToThread( do_it ) HydrusData.Print( u'Shutting down controller\u2026' )
def _BeginImmediate(self): if self._in_transaction: HydrusData.Print( 'Received a call to begin, but was already in a transaction!') else: self._c.execute('BEGIN IMMEDIATE;') self._in_transaction = True
def ProcessStartingAction( db_dir, action ): already_running = HydrusData.IsAlreadyRunning( db_dir, 'server' ) if action == 'start': if already_running: HydrusData.Print( 'The server is already running. Would you like to [s]top it, [r]estart it, or e[x]it?' ) answer = raw_input() if len( answer ) > 0: answer = answer[0] if answer == 's': return 'stop' elif answer == 'r': return 'restart' raise HydrusExceptions.PermissionException( 'Exiting!' ) else: return action elif action == 'stop': if already_running: return action else: raise HydrusExceptions.PermissionException( 'The server is not running, so it cannot be stopped!' ) elif action == 'restart': if already_running: return action else: return 'start'
def PathIsFree(path): try: os.rename(path, path) # rename a path to itself return True except OSError as e: # 'already in use by another process' HydrusData.Print('Already in use: ' + path) return False
def AddMessage(self, job_key): try: self._pending_job_keys.append(job_key) if ClientGUITopLevelWindows.MouseIsOnMyDisplay(self.GetParent()): self._CheckPending() except: HydrusData.Print(traceback.format_exc())
def PathIsFree(path): try: os.rename(path, path) # rename a path to itself return True except OSError as e: # 'already in use by another process' or an odd filename too long error HydrusData.Print('Already in use/inaccessible: ' + path) return False
def Run(self): self._app = wx.App() self._app.locale = wx.Locale( wx.LANGUAGE_DEFAULT ) # Very important to init this here and keep it non garbage collected # I have had this as 'suppress' before self._app.SetAssertMode(wx.PYAPP_ASSERT_EXCEPTION) HydrusData.Print('booting controller...') self.CreateSplash() boot_thread = threading.Thread(target=self.THREADBootEverything, name='Application Boot Thread') boot_thread.start() self._app.MainLoop() HydrusData.Print('shutting down controller...')