Ejemplo n.º 1
0
    def InitDB(self):

        try:

            def make_temp_files_deletable(function_called, path,
                                          traceback_gumpf):

                os.chmod(path, stat.S_IWRITE)

                try:
                    function_called(path)  # try again
                except:
                    pass

            if os.path.exists(HC.TEMP_DIR):
                shutil.rmtree(HC.TEMP_DIR, onerror=make_temp_files_deletable)

        except:
            pass

        try:

            if not os.path.exists(HC.TEMP_DIR): os.mkdir(HC.TEMP_DIR)

        except:
            pass

        db_initialised = False

        while not db_initialised:

            try:

                self._db = ClientDB.DB()

                db_initialised = True

            except HydrusExceptions.DBAccessException as e:

                try:
                    print(HC.u(e))
                except:
                    print(repr(HC.u(e)))

                message = 'This instance of the client had a problem connecting to the database, which probably means an old instance is still closing.'
                message += os.linesep * 2
                message += 'If the old instance does not close for a _very_ long time, you can usually safely force-close it from task manager.'

                with ClientGUIDialogs.DialogYesNo(
                        None,
                        message,
                        'There was a problem connecting to the database.',
                        yes_label='wait a bit, then try again',
                        no_label='forget it') as dlg:

                    if dlg.ShowModal() == wx.ID_YES: time.sleep(3)
                    else: raise HydrusExceptions.PermissionException()

        threading.Thread(target=self._db.MainLoop,
                         name='Database Main Loop').start()
Ejemplo n.º 2
0
    def setUpClass(self):

        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 = HC.TEMP_DIR + os.path.sep + os.urandom(32).encode('hex')

        HC.CLIENT_FILES_DIR = HC.DB_DIR + os.path.sep + 'client_files'
        HC.CLIENT_THUMBNAILS_DIR = HC.DB_DIR + os.path.sep + 'client_thumbnails'

        if not os.path.exists(HC.TEMP_DIR): os.mkdir(HC.TEMP_DIR)
        if not os.path.exists(HC.DB_DIR): os.mkdir(HC.DB_DIR)

        self._db = ClientDB.DB()

        threading.Thread(target=self._db.MainLoop,
                         name='Database Main Loop').start()
Ejemplo n.º 3
0
 def _InitDB( self ):
     
     return ClientDB.DB( self, self.db_dir, 'client', no_wal = self._no_wal )
Ejemplo n.º 4
0
    def _InitDB(self):

        return ClientDB.DB(self, HC.DB_DIR, 'client', no_wal=self._no_wal)
Ejemplo n.º 5
0
 def test_import_folders_daemon( self ):
     
     test_dir = HC.TEMP_DIR + os.path.sep + 'test'
     
     if not os.path.exists( test_dir ): os.mkdir( test_dir )
     
     with open( test_dir + os.path.sep + '1', 'wb' ) as f: f.write( TestConstants.tinest_gif )
     with open( test_dir + os.path.sep + '2', 'wb' ) as f: f.write( TestConstants.tinest_gif )
     with open( test_dir + os.path.sep + '3', 'wb' ) as f: f.write( TestConstants.tinest_gif )
     with open( test_dir + os.path.sep + '4', 'wb' ) as f: f.write( 'blarg' ) # broken
     with open( test_dir + os.path.sep + '5', 'wb' ) as f: f.write( TestConstants.tinest_gif ) # previously failed for whatever reason
     
     #
     
     path = test_dir
     
     details = {}
     
     details[ 'type' ] = HC.IMPORT_FOLDER_TYPE_SYNCHRONISE
     details[ 'cached_imported_paths' ] = { test_dir + os.path.sep + '2' }
     details[ 'failed_imported_paths' ] = { test_dir + os.path.sep + '5' }
     details[ 'local_tag' ] = 'local tag'
     details[ 'last_checked' ] = HC.GetNow() - 1500
     details[ 'check_period' ] = 1000
     
     old_details = dict( details )
     
     HC.app.SetRead( 'import_folders', { path : details } )
     
     ClientDB.DAEMONCheckImportFolders()
     
     #(('C:\\code\\Hydrus\\temp\\7baa9a818a14b7a9cbefb04c16bdc45ac651eb7400c1996e66e2efeef9e3ee5d',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     #(('C:\\code\\Hydrus\\temp\\e0dbdcb1a13c0565ffb73f2f497528adbe1703ca1dfc69680202487187b9fcfa',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     #(('C:\\code\\Hydrus\\temp\\182c4eecf2a5b4dfc8b74813bcff5d967ed53d92a982d8ae18520e1504fa5902',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     
     import_file = HC.app.GetWrite( 'import_file' )
     
     self.assertEqual( len( import_file ), 3 )
     
     expected_tag_part = { 'service_keys_to_tags' : { HC.LOCAL_TAG_SERVICE_KEY : set( [ 'local tag' ] ) } }
     
     ( one, two, three ) = import_file
     
     ( temp_path, tag_part ) = one
     
     self.assertEqual( tag_part, expected_tag_part )
     
     ( temp_path, tag_part ) = two
     
     self.assertEqual( tag_part, expected_tag_part )
     
     ( temp_path, tag_part ) = three
     
     self.assertEqual( tag_part, expected_tag_part )
     
     # I need to expand tests here with the new file system
     
     [ ( ( updated_path, updated_details ), kwargs ) ] = HC.app.GetWrite( 'import_folder' )
     
     self.assertEqual( path, updated_path )
     
     self.assertEqual( updated_details[ 'type' ], old_details[ 'type' ] )
     self.assertEqual( updated_details[ 'cached_imported_paths' ], { test_dir + os.path.sep + '1', test_dir + os.path.sep + '2', test_dir + os.path.sep + '3' } )
     self.assertEqual( updated_details[ 'failed_imported_paths' ], { test_dir + os.path.sep + '4', test_dir + os.path.sep + '5' } )
     self.assertEqual( updated_details[ 'local_tag' ], old_details[ 'local_tag' ] )
     self.assertGreater( updated_details[ 'last_checked' ], old_details[ 'last_checked' ] )
     self.assertEqual( updated_details[ 'check_period' ], old_details[ 'check_period' ] )
     
     #
     
     path = test_dir
     
     details = {}
     
     details[ 'type' ] = HC.IMPORT_FOLDER_TYPE_DELETE
     details[ 'cached_imported_paths' ] = set()
     details[ 'failed_imported_paths' ] = { test_dir + os.path.sep + '5' }
     details[ 'local_tag' ] = 'local tag'
     details[ 'last_checked' ] = HC.GetNow() - 1500
     details[ 'check_period' ] = 1000
     
     old_details = dict( details )
     
     HC.app.SetRead( 'import_folders', { path : details } )
     
     ClientDB.DAEMONCheckImportFolders()
     
     # improve these tests as above
     
     # old entries
     #(('GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     #(('GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     #(('GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     #(('blarg',), {'service_keys_to_tags': {HC.LOCAL_TAG_SERVICE_KEY: set(['local tag'])}})
     
     import_file = HC.app.GetWrite( 'import_file' )
     
     [ ( ( updated_path, updated_details ), kwargs ) ] = HC.app.GetWrite( 'import_folder' )
     
     self.assertEqual( path, updated_path )
     
     self.assertEqual( updated_details[ 'type' ], old_details[ 'type' ] )
     self.assertEqual( updated_details[ 'cached_imported_paths' ], set() )
     self.assertEqual( updated_details[ 'failed_imported_paths' ], { test_dir + os.path.sep + '4', test_dir + os.path.sep + '5' } )
     self.assertEqual( updated_details[ 'local_tag' ], old_details[ 'local_tag' ] )
     self.assertGreater( updated_details[ 'last_checked' ], old_details[ 'last_checked' ] )
     self.assertEqual( updated_details[ 'check_period' ], old_details[ 'check_period' ] )
     
     self.assertTrue( not os.path.exists( test_dir + os.path.sep + '1' ) )
     self.assertTrue( not os.path.exists( test_dir + os.path.sep + '2' ) )
     self.assertTrue( not os.path.exists( test_dir + os.path.sep + '3' ) )
     self.assertTrue( os.path.exists( test_dir + os.path.sep + '4' ) )
     self.assertTrue( os.path.exists( test_dir + os.path.sep + '5' ) )
     
     os.remove( test_dir + os.path.sep + '4' )
     os.remove( test_dir + os.path.sep + '5' )
     
     os.rmdir( test_dir )