def update_winapp2(url, hash_expected, append_text, cb_success): """Download latest winapp2.ini file. Hash is sha512 or None to disable checks""" # first, determine whether an update is necessary from bleachbit import personal_cleaners_dir fn = os.path.join(personal_cleaners_dir, 'winapp2.ini') delete_current = False if os.path.exists(fn): f = open(fn, 'r') hash_current = hashlib.sha512(f.read()).hexdigest() if not hash_expected or hash_current == hash_expected: # update is same as current return f.close() delete_current = True # download update opener = build_opener() opener.addheaders = [('User-Agent', user_agent())] doc = opener.open(fullurl=url, timeout=20).read() # verify hash hash_actual = hashlib.sha512(doc).hexdigest() if hash_expected and not hash_actual == hash_expected: raise RuntimeError("hash for %s actually %s instead of %s" % (url, hash_actual, hash_expected)) # delete current if delete_current: from bleachbit.FileUtilities import delete delete(fn, True) # write file if not os.path.exists(personal_cleaners_dir): os.mkdir(personal_cleaners_dir) f = open(fn, 'w') f.write(doc) append_text(_('New winapp2.ini was downloaded.')) cb_success()
def test_download_url_to_fn(self): """Unit test for function download_url_to_fn()""" from bleachbit.Chaff import download_url_to_fn # 200: no error # 404: not retryable error # 503: retryable error tests = (('http://httpstat.us/200', True), ('http://httpstat.us/404', False), ('http://httpstat.us/503', False)) fn = os.path.join(self.tempdir, 'download') on_error_called = [False] def on_error(msg1, msg2): print('test on_error(%s, %s)' % (msg1, msg2)) on_error_called[0] = True for test in tests: self.assertNotExists(fn) url = test[0] expected_rc = test[1] on_error_called = [False] rc = download_url_to_fn(url, fn, on_error=on_error, max_retries=2, backoff_factor=1) self.assertEqual(rc, expected_rc) if expected_rc: self.assertExists(fn) self.assertNotEqual(rc, on_error_called[0]) from bleachbit.FileUtilities import delete delete(fn, ignore_missing=True)
def update_winapp2(url, hash_expected, append_text, cb_success): """Download latest winapp2.ini file. Hash is sha512 or None to disable checks""" # first, determine whether an update is necessary #먼저 업데이트가 필요한지 여부를 확인합니다. #bleachbit모듈에서 personal_cleaners_dir을 가져온다. from bleachbit import personal_cleaners_dir #fn은 personal_cleaners_dir의 'winapp2.ini'이다. fn = os.path.join(personal_cleaners_dir, 'winapp2.ini') delete_current = False if os.path.exists(fn): #파일을 읽기모드로 연다. f = open(fn, 'r') hash_current = hashlib.sha512(f.read()).hexdigest() if not hash_expected or hash_current == hash_expected: # update is same as current #업데이트가 현재 업데이트와 동일하면 return한다. return #파일을 닫는다. f.close() delete_current = True # download update # 업데이트를 다운로드한다. opener = build_opener() opener.addheaders = [('User-Agent', user_agent())] doc = opener.open(fullurl=url, timeout=20).read() # verify hash #해시를 확인한다. hash_actual = hashlib.sha512(doc).hexdigest() #hash_expected이고 hash_actual이 아니면 hash_expected이다. if hash_expected and not hash_actual == hash_expected: raise RuntimeError("hash for %s actually %s instead of %s" % (url, hash_actual, hash_expected)) # delete current # current를 삭제한다. if delete_current: from bleachbit.FileUtilities import delete delete(fn, True) # write file # 파일을 쓰기모드로 열고 'New winapp2.ini was downloaded.'에 추가한다. if not os.path.exists(personal_cleaners_dir): os.mkdir(personal_cleaners_dir) f = open(fn, 'w') f.write(doc) append_text(_('New winapp2.ini was downloaded.')) cb_success()
def test_whitelist(self): tests = [ ('/tmp/.truecrypt_aux_mnt1/control', True), ('/tmp/.truecrypt_aux_mnt1/volume', True), ('/tmp/.vbox-foo-ipc/lock', True), ('/tmp/.wine-500/server-806-102400f/lock', True), ('/tmp/gconfd-foo/lock/ior', True), ('/tmp/ksocket-foo/Arts_SoundServerV2', True), ('/tmp/ksocket-foo/secret-cookie', True), ('/tmp/orbit-foo/bonobo-activation-server-ior', True), ('/tmp/orbit-foo/bonobo-activation-register.lock', True), ('/tmp/orbit-foo/bonobo-activation-server-a9cd6cc4973af098918b154c4957a93f-ior', True), ('/tmp/orbit-foo/bonobo-activation-register-a9cd6cc4973af098918b154c4957a93f.lock', True), ('/tmp/pulse-foo/pid', True), ('/tmp/tmpsDOBFd', False), (os.path.expanduser('~/.cache/obexd'), True), (os.path.expanduser('~/.cache/obexd/'), True), (os.path.expanduser('~/.cache/obexd/foo'), True), (os.path.expanduser('~/.cache/obex'), False), (os.path.expanduser('~/.cache/obexd-foo'), False) ] list(register_cleaners()) for test in tests: self.assertEqual( backends['system'].whitelisted(test[0]), test[1], test[0]) # Make sure directory ~/.cache/obexd is ignored # https://github.com/bleachbit/bleachbit/issues/572 obexd_dir = os.path.expanduser('~/.cache/obexd') if not os.path.exists(obexd_dir): os.makedirs(obexd_dir) obexd_fn = os.path.join(obexd_dir, 'bleachbit-test') common.touch_file(obexd_fn) found_canary = False for cmd in backends['system'].get_commands('cache'): for _result in cmd.execute(really_delete=False): self.assertNotEqual(cmd.path, obexd_fn) self.assertFalse('/.cache/obexd/' in cmd.path) from bleachbit.FileUtilities import delete delete(obexd_fn, ignore_missing=True)
def download_models(content_model_path=DEFAULT_CONTENT_MODEL_PATH, subject_model_path=DEFAULT_SUBJECT_MODEL_PATH, twentysixhundred_model_path=DEFAULT_2600_MODEL_PATH, on_error=None): """Download models Calls on_error(primary_message, secondary_message) in case of error Returns success as boolean value """ from urllib2 import urlopen, URLError, HTTPError from httplib import HTTPException import socket if HAVE_CERTIFI: cafile = certifi.where() else: cafile = None for (url, fn) in ((URL_CLINTON_SUBJECT, subject_model_path), (URL_CLINTON_CONTENT, content_model_path), (URL_2600, twentysixhundred_model_path)): if os.path.exists(fn): logger.debug('File %s already exists', fn) continue logger.info('Downloading %s to %s', url, fn) try: resp = urlopen(url, cafile=cafile) with open(fn, 'wb') as f: f.write(resp.read()) except (URLError, HTTPError, HTTPException, socket.error) as exc: msg = _('Downloading url failed: %s') % url msg2 = '{}: {}'.format(type(exc).__name__, exc) logger.exception(msg) if on_error: on_error(msg, msg2) from bleachbit.FileUtilities import delete delete(fn, ignore_missing=True) # delete any partial download return False return True
def test_wipe_sub(n_bytes, mkfs_cmd): """Test FileUtilities.wipe_path""" if 'nt' == os.name: print 'WARNING: test_wipe() not supported on Windows' return filename = create_disk_image(n_bytes) print 'created disk image %s' % filename # format filesystem format_filesystem(filename, mkfs_cmd) # mount mountpoint = tempfile.mkdtemp(prefix='bleachbit-wipe-mountpoint') mount_filesystem(filename, mountpoint) # baseline free disk space print 'df for clean filesystem' print run_external(['df', mountpoint])[1] # make dirty make_dirty(mountpoint) # verify dirtiness unmount_filesystem(mountpoint) assert(verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) # standard delete print 'info: standard delete' delete_counter = 0 for secretfile in listdir(mountpoint): if not 'secret' in secretfile: # skip lost+found continue delete(secretfile, shred=False) delete_counter += 1 print 'debug: deleted %d files' % delete_counter # check print 'df for empty, dirty filesystem' print run_external(['df', mountpoint])[1] # verify dirtiness unmount_filesystem(mountpoint) assert(verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) expected_free_space = free_space(mountpoint) # measure effectiveness of multiple wipes for i in range(1, 10): print '*' * 30 print '* pass %d *' % i print '*' * 30 # remount if i > 1: mount_filesystem(filename, mountpoint)\ # really wipe print 'wiping %s' % mountpoint for w in wipe_path(mountpoint): pass # verify cleaning process freed all space it allocated actual_free_space = free_space(mountpoint) if not expected_free_space == actual_free_space: print 'expecting %d free space but got %d' % \ (expected_free_space, actual_free_space) import pdb pdb.set_trace() # unmount unmount_filesystem(mountpoint) # verify cleanliness cleanliness = verify_cleanliness(filename) assert(cleanliness < 2) # remove temporary delete(filename) delete(mountpoint)
def test_wipe_sub(n_bytes, mkfs_cmd): """Test FileUtilities.wipe_path""" filename = create_disk_image(n_bytes) print('created disk image %s' % filename) # format filesystem format_filesystem(filename, mkfs_cmd) # mount mountpoint = tempfile.mkdtemp(prefix='bleachbit-wipe-mountpoint') mount_filesystem(filename, mountpoint) # baseline free disk space print('df for clean filesystem') print(run_external(['df', mountpoint])[1]) # make dirty make_dirty(mountpoint) # verify dirtiness unmount_filesystem(mountpoint) assert (verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) # standard delete logger.info('standard delete') delete_counter = 0 for secretfile in listdir(mountpoint): if 'secret' not in secretfile: # skip lost+found continue delete(secretfile, shred=False) delete_counter += 1 logger.debug('deleted %d files', delete_counter) # check print('df for empty, dirty filesystem') print(run_external(['df', mountpoint])[1]) # verify dirtiness unmount_filesystem(mountpoint) assert (verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) expected_free_space = free_space(mountpoint) # measure effectiveness of multiple wipes for i in range(1, 10): print('*' * 30) print('* pass %d *' % i) print('*' * 30) # remount if i > 1: mount_filesystem(filename, mountpoint)\ # really wipe print('wiping %s' % mountpoint) for w in wipe_path(mountpoint): pass # verify cleaning process freed all space it allocated actual_free_space = free_space(mountpoint) if not expected_free_space == actual_free_space: print('expecting %d free space but got %d' % (expected_free_space, actual_free_space)) import pdb pdb.set_trace() # unmount unmount_filesystem(mountpoint) # verify cleanliness cleanliness = verify_cleanliness(filename) assert (cleanliness < 2) # remove temporary delete(filename) delete(mountpoint)
def do_error(msg2): if on_error: on_error(msg, msg2) from bleachbit.FileUtilities import delete delete(fn, ignore_missing=True) # delete any partial download
def test_wipe_sub(n_bytes, mkfs_cmd): """Test FileUtilities.wipe_path""" if 'nt' == os.name: print 'WARNING: test_wipe() not supported on Windows' return filename = create_disk_image(n_bytes) print 'created disk image %s' % filename # format filesystem format_filesystem(filename, mkfs_cmd) # mount mountpoint = tempfile.mkdtemp('bleachbit-wipe-mountpoint') mount_filesystem(filename, mountpoint) # baseline free disk space print 'df for clean filesystem' print run_external(['df', mountpoint])[1] # make dirty make_dirty(mountpoint) # verify dirtiness unmount_filesystem(mountpoint) assert (verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) # standard delete print 'info: standard delete' delete_counter = 0 for secretfile in listdir(mountpoint): if not 'secret' in secretfile: # skip lost+found continue delete(secretfile, shred=False) delete_counter += 1 print 'debug: deleted %d files' % delete_counter # check print 'df for empty, dirty filesystem' print run_external(['df', mountpoint])[1] # verify dirtiness unmount_filesystem(mountpoint) assert (verify_cleanliness(filename) == 11) mount_filesystem(filename, mountpoint) expected_free_space = free_space(mountpoint) # measure effectiveness of multiple wipes for i in range(1, 10): print '*' * 30 print '* pass %d *' % i print '*' * 30 # remount if i > 1: mount_filesystem(filename, mountpoint)\ # really wipe print 'wiping %s' % mountpoint for w in wipe_path(mountpoint): pass # verify cleaning process freed all space it allocated actual_free_space = free_space(mountpoint) if not expected_free_space == actual_free_space: print 'expecting %d free space but got %d' % \ (expected_free_space, actual_free_space) import pdb pdb.set_trace() # unmount unmount_filesystem(mountpoint) # verify cleanliness cleanliness = verify_cleanliness(filename) assert (cleanliness < 2) # remove temporary delete(filename) delete(mountpoint)