def test_whitelisted(self): """Unit test for whitelisted()""" # setup old_whitelist = options.get_whitelist_paths() whitelist = [('file', '/home/foo'), ('folder', '/home/folder')] options.set_whitelist_paths(whitelist) self.assertEqual(set(whitelist), set(options.get_whitelist_paths())) # test self.assertFalse(whitelisted('')) self.assertFalse(whitelisted('/')) self.assertFalse(whitelisted('/home/foo2')) self.assertFalse(whitelisted('/home/fo')) self.assertTrue(whitelisted('/home/foo')) self.assertTrue(whitelisted('/home/folder')) if 'posix' == os.name: self.assertTrue(whitelisted('/home/folder/')) self.assertTrue(whitelisted('/home/folder/file')) self.assertFalse(whitelisted('/home/fold')) self.assertFalse(whitelisted('/home/folder2')) if 'nt' == os.name: whitelist = [('folder', 'D:\\'), ('file', 'c:\\windows\\foo.log'), ('folder', 'e:\\users')] options.set_whitelist_paths(whitelist) self.assertTrue(whitelisted('e:\\users')) self.assertTrue(whitelisted('e:\\users\\')) self.assertTrue(whitelisted('e:\\users\\foo.log')) self.assertFalse(whitelisted('e:\\users2')) # case insensitivity self.assertTrue(whitelisted('C:\\WINDOWS\\FOO.LOG')) self.assertTrue(whitelisted('D:\\USERS')) # drives letters have the seperator at the end while most paths # don't self.assertTrue(whitelisted('D:\\FOLDER\\FOO.LOG')) # test blank options.set_whitelist_paths([]) self.assertFalse(whitelisted('/home/foo')) self.assertFalse(whitelisted('/home/folder')) self.assertFalse(whitelisted('/home/folder/file')) options.config.remove_section('whitelist/paths') self.assertFalse(whitelisted('/home/foo')) self.assertFalse(whitelisted('/home/folder')) self.assertFalse(whitelisted('/home/folder/file')) # clean up options.set_whitelist_paths(old_whitelist) self.assertEqual(set(old_whitelist), set(options.get_whitelist_paths()))
def test_whitelisted(self): """Unit test for whitelisted()""" # setup old_whitelist = options.get_whitelist_paths() whitelist = [('file', '/home/foo'), ('folder', '/home/folder')] options.set_whitelist_paths(whitelist) self.assertEqual(set(whitelist), set(options.get_whitelist_paths())) # test self.assertFalse(whitelisted('')) self.assertFalse(whitelisted('/')) self.assertFalse(whitelisted('/home/foo2')) self.assertFalse(whitelisted('/home/fo')) self.assertTrue(whitelisted('/home/foo')) self.assertTrue(whitelisted('/home/folder')) if 'posix' == os.name: self.assertTrue(whitelisted('/home/folder/')) self.assertTrue(whitelisted('/home/folder/file')) self.assertFalse(whitelisted('/home/fold')) self.assertFalse(whitelisted('/home/folder2')) if 'nt' == os.name: whitelist = [('folder', 'D:\\'), ( 'file', 'c:\\windows\\foo.log'), ('folder', 'e:\\users')] options.set_whitelist_paths(whitelist) self.assertTrue(whitelisted('e:\\users')) self.assertTrue(whitelisted('e:\\users\\')) self.assertTrue(whitelisted('e:\\users\\foo.log')) self.assertFalse(whitelisted('e:\\users2')) # case insensitivity self.assertTrue(whitelisted('C:\\WINDOWS\\FOO.LOG')) self.assertTrue(whitelisted('D:\\USERS')) # drives letters have the separator at the end while most paths # don't self.assertTrue(whitelisted('D:\\FOLDER\\FOO.LOG')) # test blank options.set_whitelist_paths([]) self.assertFalse(whitelisted('/home/foo')) self.assertFalse(whitelisted('/home/folder')) self.assertFalse(whitelisted('/home/folder/file')) options.config.remove_section('whitelist/paths') self.assertFalse(whitelisted('/home/foo')) self.assertFalse(whitelisted('/home/folder')) self.assertFalse(whitelisted('/home/folder/file')) # clean up options.set_whitelist_paths(old_whitelist) self.assertEqual( set(old_whitelist), set(options.get_whitelist_paths()))
def test_whitelisted_speed(self): """Benchmark the speed of whitelisted() It is called frequently, so the speed is important.""" d = '/usr/bin' whitelist = [('file', '/home/foo'), ('folder', '/home/folder')] if 'nt' == os.name: d = expandvars('%windir%\system32') whitelist = [('file', r'c:\\filename'), ('folder', r'c:\\folder')] reps = 20 paths = [p for p in children_in_directory(d, True)] paths = paths[:1000] # truncate self.assertGreater(len(paths), 10) old_whitelist = options.get_whitelist_paths() options.set_whitelist_paths(whitelist) t0 = time.time() for i in xrange(0, reps): for p in paths: _ = whitelisted(p) t1 = time.time() logger.info('whitelisted() with {} repetitions and {} paths took {:.3g} seconds '.format( reps, len(paths), t1 - t0)) options.set_whitelist_paths(old_whitelist)
def test_whitelisted_posix_symlink(self): """Symlink test for whitelisted_posix()""" # setup old_whitelist = options.get_whitelist_paths() tmpdir = os.path.join(self.tempdir, 'bleachbit-whitelist') os.mkdir(tmpdir) realpath = self.write_file('real') linkpath = os.path.join(tmpdir, 'link') os.symlink(realpath, linkpath) self.assertExists(realpath) self.assertExists(linkpath) # test 1: the real path is whitelisted whitelist = [('file', realpath)] options.set_whitelist_paths(whitelist) self.assertFalse(whitelisted(tmpdir)) self.assertTrue(whitelisted(realpath)) self.assertTrue(whitelisted(linkpath)) # test 2: the link is whitelisted whitelist = [('file', linkpath)] options.set_whitelist_paths(whitelist) self.assertFalse(whitelisted(tmpdir)) self.assertFalse(whitelisted(realpath)) self.assertTrue(whitelisted(linkpath)) options.set_whitelist_paths(old_whitelist)
def whitelisted_windows(path): """Check whether this Windows path is whitelisted""" from bleachbit.Options import options for pathname in options.get_whitelist_paths(): # Windows is case insensitive if pathname[0] == 'file' and path.lower() == pathname[1].lower(): return True if pathname[0] == 'folder': if path.lower() == pathname[1].lower(): return True if path.lower().startswith(pathname[1].lower() + os.sep): return True # Simple drive letter like C:\ matches everything below if len(pathname[1]) == 3 and path.lower().startswith(pathname[1].lower()): return True return False
def whitelisted_posix(path, check_realpath=True): """Check whether this POSIX path is whitelisted""" from bleachbit.Options import options if check_realpath and os.path.islink(path): # also check the link name if whitelisted_posix(path, False): return True # resolve symlink path = os.path.realpath(path) for pathname in options.get_whitelist_paths(): if pathname[0] == 'file' and path == pathname[1]: return True if pathname[0] == 'folder': if path == pathname[1]: return True if path.startswith(pathname[1] + os.sep): return True return False
def __locations_page(self, page_type): """Return a widget containing a list of files and folders""" def add_whitelist_file_cb(button): """Callback for adding a file""" title = _("Choose a file") pathname = GuiBasic.browse_file(self.parent, title) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('File'), pathname]) pathnames.append(['file', pathname]) options.set_whitelist_paths(pathnames) def add_whitelist_folder_cb(button): """Callback for adding a folder""" title = _("Choose a folder") pathname = GuiBasic.browse_folder(self.parent, title, multiple=False, stock_button=gtk.STOCK_ADD) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('Folder'), pathname]) pathnames.append(['folder', pathname]) options.set_whitelist_paths(pathnames) def remove_whitelist_path_cb(button): """Callback for removing a path""" treeselection = treeview.get_selection() (model, _iter) = treeselection.get_selected() if None == _iter: # nothing selected return pathname = model[_iter][1] liststore.remove(_iter) for this_pathname in pathnames: if this_pathname[1] == pathname: pathnames.remove(this_pathname) options.set_whitelist_paths(pathnames) def add_custom_file_cb(button): """Callback for adding a file""" title = _("Choose a file") pathname = GuiBasic.browse_file(self.parent, title) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('File'), pathname]) pathnames.append(['file', pathname]) options.set_custom_paths(pathnames) def add_custom_folder_cb(button): """Callback for adding a folder""" title = _("Choose a folder") pathname = GuiBasic.browse_folder(self.parent, title, multiple=False, stock_button=gtk.STOCK_ADD) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('Folder'), pathname]) pathnames.append(['folder', pathname]) options.set_custom_paths(pathnames) def remove_custom_path_cb(button): """Callback for removing a path""" treeselection = treeview.get_selection() (model, _iter) = treeselection.get_selected() if None == _iter: # nothing selected return pathname = model[_iter][1] liststore.remove(_iter) for this_pathname in pathnames: if this_pathname[1] == pathname: pathnames.remove(this_pathname) options.set_custom_paths(pathnames) vbox = gtk.VBox() # load data if LOCATIONS_WHITELIST == page_type: pathnames = options.get_whitelist_paths() elif LOCATIONS_CUSTOM == page_type: pathnames = options.get_custom_paths() liststore = gtk.ListStore(str, str) for paths in pathnames: type_code = paths[0] type_str = None if type_code == 'file': type_str = _('File') elif type_code == 'folder': type_str = _('Folder') else: raise RuntimeError("Invalid type code: '%s'" % type_code) path = paths[1] liststore.append([type_str, path]) if LOCATIONS_WHITELIST == page_type: # TRANSLATORS: "Paths" is used generically to refer to both files # and folders notice = gtk.Label( _("Theses paths will not be deleted or modified.")) elif LOCATIONS_CUSTOM == page_type: notice = gtk.Label( _("These locations can be selected for deletion.")) vbox.pack_start(notice, False) # create treeview treeview = gtk.TreeView(liststore) # create column views self.renderer0 = gtk.CellRendererText() self.column0 = gtk.TreeViewColumn(_("Type"), self.renderer0, text=0) treeview.append_column(self.column0) self.renderer1 = gtk.CellRendererText() # TRANSLATORS: In the tree view "Path" is used generically to refer to a # file, a folder, or a pattern describing either self.column1 = gtk.TreeViewColumn(_("Path"), self.renderer1, text=1) treeview.append_column(self.column1) treeview.set_search_column(1) # finish tree view swindow = gtk.ScrolledWindow() swindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) swindow.set_size_request(300, 200) swindow.add(treeview) vbox.pack_start(swindow) # buttons that modify the list button_add_file = gtk.Button(_p('button', 'Add file')) if LOCATIONS_WHITELIST == page_type: button_add_file.connect("clicked", add_whitelist_file_cb) elif LOCATIONS_CUSTOM == page_type: button_add_file.connect("clicked", add_custom_file_cb) button_add_folder = gtk.Button(_p('button', 'Add folder')) if LOCATIONS_WHITELIST == page_type: button_add_folder.connect("clicked", add_whitelist_folder_cb) elif LOCATIONS_CUSTOM == page_type: button_add_folder.connect("clicked", add_custom_folder_cb) button_remove = gtk.Button(_p('button', 'Remove')) if LOCATIONS_WHITELIST == page_type: button_remove.connect("clicked", remove_whitelist_path_cb) elif LOCATIONS_CUSTOM == page_type: button_remove.connect("clicked", remove_custom_path_cb) button_box = gtk.HButtonBox() button_box.set_layout(gtk.BUTTONBOX_START) button_box.pack_start(button_add_file) button_box.pack_start(button_add_folder) button_box.pack_start(button_remove) vbox.pack_start(button_box, False) # return page return vbox
def __locations_page(self, page_type): // 파일 추가를 위한 콜백 함수 def add_whitelist_file_cb(button): title = _("Choose a file") pathname = GuiBasic.browse_file(self.parent, title) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('File'), pathname]) pathnames.append(['file', pathname]) options.set_whitelist_paths(pathnames) // 파일 추가를 위한 콜백 함수 def add_whitelist_folder_cb(button): title = _("Choose a folder") pathname = GuiBasic.browse_folder(self.parent, title, multiple=False, stock_button=gtk.STOCK_ADD) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('Folder'), pathname]) pathnames.append(['folder', pathname]) options.set_whitelist_paths(pathnames) // 경로를 삭제하기 위한 콜백 함수 def remove_whitelist_path_cb(button): """Callback for removing a path""" treeselection = treeview.get_selection() (model, _iter) = treeselection.get_selected() if None == _iter: // 아무것도 선택되지 않았다면 return pathname = model[_iter][1] liststore.remove(_iter) for this_pathname in pathnames: if this_pathname[1] == pathname: pathnames.remove(this_pathname) options.set_whitelist_paths(pathnames) // 파일 추가를 위한 콜백 함수 def add_custom_file_cb(button): title = _("Choose a file") pathname = GuiBasic.browse_file(self.parent, title) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('File'), pathname]) pathnames.append(['file', pathname]) options.set_custom_paths(pathnames) // 폴더 추가를 위한 콜백 함수 def add_custom_folder_cb(button): title = _("Choose a folder") pathname = GuiBasic.browse_folder(self.parent, title, multiple=False, stock_button=gtk.STOCK_ADD) if pathname: for this_pathname in pathnames: if pathname == this_pathname[1]: logger.warning("'%s' already exists in whitelist", pathname) return liststore.append([_('Folder'), pathname]) pathnames.append(['folder', pathname]) options.set_custom_paths(pathnames) // 경로 삭제를 위한 콜백 함수 def remove_custom_path_cb(button): """Callback for removing a path""" treeselection = treeview.get_selection() (model, _iter) = treeselection.get_selected() if None == _iter: // 아무것도 선택되지 return pathname = model[_iter][1] liststore.remove(_iter) for this_pathname in pathnames: if this_pathname[1] == pathname: pathnames.remove(this_pathname) options.set_custom_paths(pathnames) vbox = gtk.VBox() // 데이터 가져옴 if LOCATIONS_WHITELIST == page_type: pathnames = options.get_whitelist_paths() elif LOCATIONS_CUSTOM == page_type: pathnames = options.get_custom_paths() liststore = gtk.ListStore(str, str) for paths in pathnames: type_code = paths[0] type_str = None if type_code == 'file': type_str = _('File') elif type_code == 'folder': type_str = _('Folder') else: raise RuntimeError("Invalid type code: '%s'" % type_code) path = paths[1] liststore.append([type_str, path]) if LOCATIONS_WHITELIST == page_type: notice = gtk.Label( _("Theses paths will not be deleted or modified.")) elif LOCATIONS_CUSTOM == page_type: notice = gtk.Label( _("These locations can be selected for deletion.")) vbox.pack_start(notice, False) // 트리 뷰 생성 treeview = gtk.TreeView(liststore) // 칼럼 뷰 생성 self.renderer0 = gtk.CellRendererText() self.column0 = gtk.TreeViewColumn(_("Type"), self.renderer0, text=0) treeview.append_column(self.column0) self.renderer1 = gtk.CellRendererText() self.column1 = gtk.TreeViewColumn(_("Path"), self.renderer1, text=1) treeview.append_column(self.column1) treeview.set_search_column(1) // 트리 뷰 끝냄 swindow = gtk.ScrolledWindow() swindow.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) swindow.set_size_request(300, 200) swindow.add(treeview) vbox.pack_start(swindow)