def execute(self, really_delete): """Make changes and return results""" if FileUtilities.whitelisted(self.path): yield whitelist(self.path) return ret = { # TRANSLATORS: This is the label in the log indicating will be # deleted (for previews) or was actually deleted 'label': _('Delete'), 'n_deleted': 1, 'n_special': 0, 'path': self.path, 'size': FileUtilities.getsize(self.path)} if really_delete: try: FileUtilities.delete(self.path, self.shred) except WindowsError as e: # WindowsError: [Error 32] The process cannot access the file because it is being # used by another process: u'C:\\Documents and # Settings\\username\\Cookies\\index.dat' if 32 != e.winerror and 5 != e.winerror: raise try: bleachbit.Windows.delete_locked_file(self.path) except: raise else: if self.shred: import warnings warnings.warn( _('At least one file was locked by another process, so its contents could not be overwritten. It will be marked for deletion upon system reboot.')) # TRANSLATORS: The file will be deleted when the # system reboots ret['label'] = _('Mark for deletion') yield ret
def test_delete(self): """Unit test for --delete option""" prefixes = ['bleachbit-test-cli-delete', '\x8b\x8b-bad-encoding'] for i in range(len(prefixes)): filename = self.mkstemp(prefix=prefixes[i]) if 'nt' == os.name: filename = os.path.normcase(filename) # replace delete function for testing save_delete = FileUtilities.delete deleted_paths = [] crash = [False] def dummy_delete(path, shred=False): try: self.assertExists(path) except: crash[0] = True deleted_paths.append(os.path.normcase(path)) FileUtilities.delete = dummy_delete FileUtilities.delete(filename) self.assertExists(filename) operations = args_to_operations(['system.tmp'], False, False) preview_or_clean(operations, True) FileUtilities.delete = save_delete self.assertIn(filename, deleted_paths, "%s not found deleted" % filename) os.remove(filename) self.assertNotExists(filename) self.assertFalse(crash[0])
def test_delete(self): """Unit test for --delete option""" (fd, filename) = tempfile.mkstemp(prefix='bleachbit-test-cli-delete') os.close(fd) if 'nt' == os.name: import win32api filename = os.path.normcase(win32api.GetLongPathName(filename)) # replace delete function for testing save_delete = FileUtilities.delete deleted_paths = [] def dummy_delete(path, shred=False): self.assert_(os.path.exists(path)) deleted_paths.append(os.path.normcase(path)) FileUtilities.delete = dummy_delete FileUtilities.delete(filename) self.assert_(os.path.exists(filename)) operations = args_to_operations(['system.tmp'], False) preview_or_clean(operations, True) FileUtilities.delete = save_delete self.assert_(filename in deleted_paths, "%s not found deleted" % filename) os.remove(filename) self.assert_(not os.path.exists(filename))
def start_with_computer(enabled): """If enabled, create shortcut to start application with computer. If disabled, then delete the shortcut.""" autostart_path = get_autostart_path() if not enabled: if os.path.lexists(autostart_path): FileUtilities.delete(autostart_path) return if os.path.lexists(autostart_path): return import winshell winshell.CreateShortcut(Path=autostart_path, Target=os.path.join(bleachbit.bleachbit_exe_path, 'bleachbit.exe'))
def sqlite_clean_helper(self, sql, fn, clean_func, check_func=None, setup_func=None): """Helper for cleaning special SQLite cleaning""" self.assertFalse( sql and fn, "sql and fn are mutually exclusive ways to create the data") if fn: filename = os.path.normpath(os.path.join(self.dir_base, fn)) self.assertExists(filename) # create sqlite file elif sql: # create test file filename = self.mkstemp(prefix='bleachbit-test-sqlite') # additional setup if setup_func: setup_func(filename) # before SQL creation executed, cleaning should fail self.assertRaises(sqlite3.DatabaseError, clean_func, filename) # create FileUtilities.execute_sqlite3(filename, sql) self.assertExists(filename) else: raise RuntimeError('neither fn nor sql supplied') # clean the file old_shred = options.get('shred') options.set('shred', False, commit=False) self.assertFalse(options.get('shred')) clean_func(filename) options.set('shred', True, commit=False) self.assertTrue(options.get('shred')) options.set('shred', old_shred, commit=False) clean_func(filename) self.assertExists(filename) # check if check_func: check_func(self, filename) # tear down FileUtilities.delete(filename) self.assertNotExists(filename)
def sqlite_clean_helper(self, sql, fn, clean_func, check_func=None, setup_func=None): """Helper for cleaning special SQLite cleaning""" self.assertFalse(sql and fn, "sql and fn are mutually exclusive ways to create the data") if fn: filename = os.path.normpath(os.path.join(self.dir_base, fn)) self.assertExists(filename) # create sqlite file elif sql: # create test file filename = self.mkstemp(prefix='bleachbit-test-sqlite') # additional setup if setup_func: setup_func(filename) # before SQL creation executed, cleaning should fail self.assertRaises(sqlite3.DatabaseError, clean_func, filename) # create FileUtilities.execute_sqlite3(filename, sql) self.assertExists(filename) else: raise RuntimeError('neither fn nor sql supplied') # clean the file old_shred = options.get('shred') options.set('shred', False, commit=False) self.assertFalse(options.get('shred')) clean_func(filename) options.set('shred', True, commit=False) self.assertTrue(options.get('shred')) options.set('shred', old_shred, commit=False) clean_func(filename) self.assertExists(filename) # check if check_func: check_func(self, filename) # tear down FileUtilities.delete(filename) self.assertNotExists(filename)
def test_delete(self): """Unit test for --delete option""" filename = self.mkstemp(prefix='bleachbit-test-cli-delete') if 'nt' == os.name: filename = os.path.normcase(filename) # replace delete function for testing save_delete = FileUtilities.delete deleted_paths = [] def dummy_delete(path, shred=False): self.assertExists(path) deleted_paths.append(os.path.normcase(path)) FileUtilities.delete = dummy_delete FileUtilities.delete(filename) self.assertExists(filename) operations = args_to_operations(['system.tmp'], False) preview_or_clean(operations, True) FileUtilities.delete = save_delete self.assertIn(filename, deleted_paths, "%s not found deleted" % filename) os.remove(filename) self.assertNotExists(filename)
def start_with_computer(enabled): """If enabled, create shortcut to start application with computer. If disabled, then delete the shortcut.""" if not enabled: # User requests to not automatically start BleachBit if os.path.lexists(bleachbit.autostart_path): # Delete the shortcut FileUtilities.delete(bleachbit.autostart_path) return # User requests to automatically start BleachBit if os.path.lexists(bleachbit.autostart_path): # Already automatic, so exit return if not os.path.exists(bleachbit.launcher_path): logger.error('%s does not exist: ', bleachbit.launcher_path) return import shutil General.makedirs(os.path.dirname(bleachbit.autostart_path)) shutil.copy(bleachbit.launcher_path, bleachbit.autostart_path) os.chmod(bleachbit.autostart_path, 0o755) if General.sudo_mode(): General.chownself(bleachbit.autostart_path)
def test_delete(self): """Unit test for --delete option""" (fd, filename) = tempfile.mkstemp('bleachbit-test') os.close(fd) if 'nt' == os.name: import win32api filename = os.path.normcase(win32api.GetLongPathName(filename)) # replace delete function for testing save_delete = FileUtilities.delete deleted_paths = [] def dummy_delete(path, shred = False): self.assert_(os.path.exists(path)) deleted_paths.append(os.path.normcase(path)) FileUtilities.delete = dummy_delete FileUtilities.delete(filename) self.assert_(os.path.exists(filename)) operations = args_to_operations(['system.tmp'], False) preview_or_clean(operations, True) FileUtilities.delete = save_delete self.assert_(filename in deleted_paths, \ "%s not found deleted" % filename) os.remove(filename) self.assert_(not os.path.exists(filename))
def cleanup_nonce(): """On exit, clean up GTK junk files""" for fn in glob.glob(os.path.expandvars('%TEMP%\gdbus-nonce-file-*')): logger.debug('cleaning GTK nonce file: %s', fn) FileUtilities.delete(fn)
'HKCU': _winreg.HKEY_CURRENT_USER, 'HKLM': _winreg.HKEY_LOCAL_MACHINE, 'HKU': _winreg.HKEY_USERS} if k1 not in hive_map: raise RuntimeError("Invalid Windows registry hive '%s'" % k1) return hive_map[k1], k2 #사용 가능한 경우 바로가기를 만들어 컴퓨터로 응용 프로그램을 시작하는 함수 def start_with_computer(enabled): """If enabled, create shortcut to start application with computer. If disabled, then delete the shortcut.""" #함수 사용하지 않도록 설정된 경우 바로가기를 삭제 autostart_path = get_autostart_path() if not enabled: if os.path.lexists(autostart_path): FileUtilities.delete(autostart_path) return if os.path.lexists(autostart_path): return import winshell winshell.CreateShortcut(Path=autostart_path, Target=os.path.join(bleachbit.bleachbit_exe_path, 'bleachbit.exe')) # import win32com.client # wscript_shell = win32com.client.Dispatch('WScript.Shell') # shortcut = wscript_shell.CreateShortCut(autostart_path) # shortcut.TargetPath = os.path.join( # Common.bleachbit_exe_path, 'bleachbit.exe') # shortcut.save() #BleachBit이 컴퓨터로 시작할지 여부를 boolean으로 반환하는 함수