Skip to content

Commit

Permalink
Merge be9d2f6 into b2d6bbd
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Lagi committed Oct 12, 2016
2 parents b2d6bbd + be9d2f6 commit 1058e37
Show file tree
Hide file tree
Showing 23 changed files with 297 additions and 162 deletions.
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ install:
- '%PYTHON_HOME%\python.exe -V'
- '%PYTHON_HOME%\Scripts\pip.exe --version'
- '%PYTHON_HOME%\Scripts\pip.exe install pypiwin32'
- '%PYTHON_HOME%\Scripts\pip.exe install winshell'
- '%PYTHON_HOME%\Scripts\pip.exe install psutil'
- ps: Start-FileDownload "http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.24/pygtk-all-in-one-2.24.2.win32-py2.7.msi"
- msiexec /i pygtk-all-in-one-2.24.2.win32-py2.7.msi /qn /norestart /log pygtk-install.log TARGETDIR=C:\Python27 ALLUSERS=1
# download Winapp.ini for TestWinapp.py
Expand Down
10 changes: 4 additions & 6 deletions bleachbit/Action.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import General
import Special

from Common import _
from Common import _, FSE, expanduser, expandvars

if 'posix' == os.name:
re_flags = 0
Expand Down Expand Up @@ -96,8 +96,7 @@ def __init__(self, action_element):
assert(isinstance(self.nwholeregex, (str, unicode, types.NoneType)))
self.search = action_element.getAttribute('search')
self.object_type = action_element.getAttribute('type')
self.path = os.path.expanduser(FileUtilities.expandvars(
action_element.getAttribute('path')))
self.path = expanduser(expandvars(action_element.getAttribute('path')))
if 'nt' == os.name and self.path:
# convert forward slash to backslash for compatibility with getsize()
# and for display. Do not convert an empty path, or it will become
Expand Down Expand Up @@ -171,8 +170,7 @@ def get_walk_all(top):

def get_walk_files(top):
for expanded in glob.iglob(top):
for path in FileUtilities.children_in_directory(
expanded, False):
for path in FileUtilities.children_in_directory(expanded, False):
yield path

if 'deep' == self.search:
Expand Down Expand Up @@ -379,7 +377,7 @@ class Process(ActionProvider):
action_key = 'process'

def __init__(self, action_element):
self.cmd = FileUtilities.expandvars(action_element.getAttribute('cmd'))
self.cmd = expandvars(action_element.getAttribute('cmd'))
# by default, wait
self.wait = True
wait = action_element.getAttribute('wait')
Expand Down
35 changes: 17 additions & 18 deletions bleachbit/Cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
elif 'nt' == os.name:
import Windows

from Common import _
from FileUtilities import children_in_directory, expandvars
from Common import _, expanduser, expandvars
from FileUtilities import children_in_directory
from Options import options

# Suppress GTK warning messages while running in CLI #34
Expand Down Expand Up @@ -164,7 +164,7 @@ def is_running(self):
print "debug: process '%s' is running" % pathname
return True
elif 'pathname' == test:
expanded = os.path.expanduser(expandvars(pathname))
expanded = expanduser(expandvars(pathname))
for globbed in glob.iglob(expanded):
if os.path.exists(globbed):
print "debug: file '%s' exists indicating '%s' is running" % (globbed, self.name)
Expand Down Expand Up @@ -252,7 +252,7 @@ def get_commands(self, option_id):
if 'posix' == os.name:
# This path is whitelisted under the System - Cache cleaner,
# so it can be cleaned here.
dirs += [os.path.expanduser('~/.cache/mozilla')]
dirs += [expanduser('~/.cache/mozilla')]
for dirname in dirs:
for filename in children_in_directory(dirname, False):
yield Command.Delete(filename)
Expand All @@ -268,7 +268,7 @@ def get_commands(self, option_id):
self.profile_dir, "cookies.sqlite")
# crash reports
if 'posix' == os.name:
crashdir = os.path.expanduser("~/.mozilla/firefox/Crash Reports")
crashdir = expanduser("~/.mozilla/firefox/Crash Reports")
if 'nt' == os.name:
crashdir = expandvars(
"$USERPROFILE\\Application Data\\Mozilla\\Firefox\\Crash Reports")
Expand Down Expand Up @@ -533,7 +533,7 @@ def get_commands(self, option_id):

# cache
if 'posix' == os.name and 'cache' == option_id:
dirname = os.path.expanduser("~/.cache/")
dirname = expanduser("~/.cache/")
for filename in children_in_directory(dirname, True):
if self.whitelisted(filename):
continue
Expand Down Expand Up @@ -640,7 +640,7 @@ def get_commands(self, option_id):

# most recently used documents list
if 'posix' == os.name and 'recent_documents' == option_id:
files += [os.path.expanduser("~/.recently-used")]
files += [expanduser("~/.recently-used")]
# GNOME 2.26 (as seen on Ubuntu 9.04) will retain the list
# in memory if it is simply deleted, so it must be shredded
# (or at least truncated).
Expand All @@ -651,7 +651,7 @@ def get_commands(self, option_id):
#
# https://bugzilla.gnome.org/show_bug.cgi?id=591404
for pathname in ["~/.recently-used.xbel", "~/.local/share/recently-used.xbel"]:
pathname = os.path.expanduser(pathname)
pathname = expanduser(pathname)
if os.path.lexists(pathname):
yield Command.Shred(pathname)
if HAVE_GTK:
Expand Down Expand Up @@ -690,21 +690,21 @@ def get_commands(self, option_id):

# trash
if 'posix' == os.name and 'trash' == option_id:
dirname = os.path.expanduser("~/.Trash")
dirname = expanduser("~/.Trash")
for filename in children_in_directory(dirname, False):
yield Command.Delete(filename)
# fixme http://www.ramendik.ru/docs/trashspec.html
# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
# ~/.local/share/Trash
# * GNOME 2.22, Fedora 9
# * KDE 4.1.3, Ubuntu 8.10
dirname = os.path.expanduser("~/.local/share/Trash/files")
dirname = expanduser("~/.local/share/Trash/files")
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
dirname = os.path.expanduser("~/.local/share/Trash/info")
dirname = expanduser("~/.local/share/Trash/info")
for filename in children_in_directory(dirname, True):
yield Command.Delete(filename)
dirname = os.path.expanduser("~/.local/share/Trash/expunged")
dirname = expanduser("~/.local/share/Trash/expunged")
# desrt@irc.gimpnet.org tells me that the trash
# backend puts files in here temporary, but in some situations
# the files are stuck.
Expand Down Expand Up @@ -800,16 +800,15 @@ def whitelisted(self, pathname):
'^/tmp/orbit-[^/]+/bonobo-activation-server-[a-z0-9-]*ior$',
'^/tmp/pulse-[^/]+/pid$',
'^/var/tmp/kdecache-']
regexes.append('^' + os.path.expanduser('~/.cache/wallpaper/'))
regexes.append('^' + expanduser('~/.cache/wallpaper/'))
# Clean Firefox cache from Firefox cleaner (LP#1295826)
regexes.append('^' + os.path.expanduser('~/.cache/mozilla'))
regexes.append('^' + expanduser('~/.cache/mozilla'))
# Clean Google Chrome cache from Google Chrome cleaner (LP#656104)
regexes.append('^' + os.path.expanduser('~/.cache/google-chrome'))
regexes.append(
'^' + os.path.expanduser('~/.cache/gnome-control-center/'))
regexes.append('^' + expanduser('~/.cache/google-chrome'))
regexes.append('^' + expanduser('~/.cache/gnome-control-center/'))
# iBus Pinyin
# https://bugs.launchpad.net/bleachbit/+bug/1538919
regexes.append('^' + os.path.expanduser('~/.cache/ibus/'))
regexes.append('^' + expanduser('~/.cache/ibus/'))
for regex in regexes:
if None != re.match(regex, pathname):
return True
Expand Down
81 changes: 75 additions & 6 deletions bleachbit/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import locale
import logging
import os
import re
import sys

APP_VERSION = "1.13"
Expand Down Expand Up @@ -86,19 +87,88 @@
license_filename = lf
break

# os.path.expandvars does not work well with non-ascii Windows paths.
# This is a unicode-compatible reimplementation of that function.
def expandvars(var):
"""Expand environment variables.
Return the argument with environment variables expanded. Substrings of the
form $name or ${name} or %name% are replaced by the value of environment
variable name."""
if isinstance(var, str):
final = var.decode('utf-8')
else:
final = var

if 'posix' == os.name:
final = os.path.expandvars(final)
elif 'nt' == os.name:
if (2, 5) == sys.version_info[0:2]:
import backport
final = backport.expandvars(final)
else:
import _winreg
if final.startswith('${'):
final = re.sub(r'\$\{(.*?)\}(?=$|\\)',
lambda x: '%%%s%%' % x.group(1),
final)
elif final.startswith('$'):
final = re.sub(r'\$(.*?)(?=$|\\)',
lambda x: '%%%s%%' % x.group(1),
final)
final = _winreg.ExpandEnvironmentStrings(final)
return final

# Windows paths have to be unicode, but os.path.expanduser does not support it.
# This is a unicode-compatible reimplementation of that function.
def expanduser(path):
"""Expand the path with the home directory.
Return the argument with an initial component of "~" or "~user" replaced by
that user's home directory.
"""
if isinstance(path, str):
final = path.decode('utf-8')
else:
final = path

if 'posix' == os.name:
final = os.path.expanduser(final)
elif 'nt' == os.name:
if (2, 5) == sys.version_info[0:2]:
import backport
final = backport.expandvars(final)
elif final == '~' or final.startswith('~/') or final.startswith('~user/'):
found = False
for env in [u'%USERPROFILE%', u'%HOME%']:
if env in os.environ:
home = expandvars(env)
found = True
break
if not found:
h_drive = expandvars(u'%HOMEDRIVE%')
h_path = expandvars(u'%HOMEPATH%')
home = os.path.join(h_drive, h_path)
final = final.replace('~user/', '')
final = final.replace('~/', '')
final = final.replace('~', '')
final = os.path.join(home, final)
return final


# configuration
portable_mode = False
options_dir = None
if 'posix' == os.name:
options_dir = os.path.expanduser("~/.config/bleachbit")
options_dir = expanduser("~/.config/bleachbit")
elif 'nt' == os.name:
if os.path.exists(os.path.join(bleachbit_exe_path, 'bleachbit.ini')):
# portable mode
portable_mode = True
options_dir = bleachbit_exe_path
else:
# installed mode
options_dir = os.path.expandvars("${APPDATA}\\BleachBit")
options_dir = expandvars("${APPDATA}")
options_file = os.path.join(options_dir, "bleachbit.ini")

# personal cleaners
Expand Down Expand Up @@ -148,13 +218,13 @@
# launcher
launcher_path = '/usr/share/applications/bleachbit.desktop'
if 'posix' == os.name:
autostart_path = os.path.expanduser(
'~/.config/autostart/bleachbit.desktop')
autostart_path = expanduser('~/.config/autostart/bleachbit.desktop')


#
# gettext
#
FSE = sys.getfilesystemencoding()
try:
(user_locale, encoding) = locale.getdefaultlocale()
except:
Expand Down Expand Up @@ -188,15 +258,14 @@ def ungettext(singular, plural, n):
return singular
return plural


#
# string decoding
#
# In Python 2, some strings such as Python exceptions may be localized
# and byte encoded. This decodes them into Unicode.
# See <https://bugs.launchpad.net/bleachbit/+bug/1416640>.
#


def decode_str(s):
"""Decode a string into Unicode using the default encoding"""
if isinstance(s, Exception):
Expand Down
11 changes: 7 additions & 4 deletions bleachbit/Diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def diagnostic_info():
pass
s += "\nlocal_cleaners_dir = %s" % Common.local_cleaners_dir
s += "\nlocale_dir = %s" % Common.locale_dir
s += "\noptions_dir = %s" % Common.options_dir
s += "\npersonal_cleaners_dir = %s" % Common.personal_cleaners_dir
s += "\noptions_dir = %s" % Common.options_dir.decode(Common.FSE)
s += "\npersonal_cleaners_dir = %s" % Common.personal_cleaners_dir.decode(Common.FSE)
s += "\nsystem_cleaners_dir = %s" % Common.system_cleaners_dir
s += "\nlocale.getdefaultlocale = %s" % str(locale.getdefaultlocale())
if 'posix' == os.name:
Expand All @@ -54,8 +54,11 @@ def diagnostic_info():
envs = ('APPDATA', 'LocalAppData', 'LocalAppDataLow', 'Music',
'USERPROFILE', 'ProgramFiles', 'ProgramW6432', 'TMP')
for env in envs:
s += "\nos.getenv('%s') = %s" % (env, os.getenv(env))
s += "\nos.path.expanduser('~') = %s" % os.path.expanduser('~')
if os.getenv(env):
s += "\nos.getenv('%s') = %s" % (env, os.getenv(env).decode(Common.FSE))
else:
s += "\nos.getenv('%s') = %s" % (env, os.getenv(env))
s += "\nos.path.expanduser('~') = %s" % Common.expanduser('~').decode(Common.FSE)
if sys.platform.startswith('linux'):
if hasattr(platform, 'linux_distribution'):
s += "\nplatform.linux_distribution() = %s" % str(
Expand Down
29 changes: 9 additions & 20 deletions bleachbit/FileUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import time
import ConfigParser
import Common
from Common import expanduser

if 'nt' == os.name:
import pywintypes
Expand Down Expand Up @@ -354,23 +355,14 @@ def execute_sqlite3(path, cmds):
def expand_glob_join(pathname1, pathname2):
"""Join pathname1 and pathname1, expand pathname, glob, and return as list"""
ret = []
pathname3 = os.path.expanduser(
expandvars(os.path.join(pathname1, pathname2)))
pathname3 = expanduser(Common.expandvars(os.path.join(pathname1, pathname2)))
for pathname4 in glob.iglob(pathname3):
ret.append(pathname4)
return ret


def expandvars(path):
if (2, 5) == sys.version_info[0:2] and 'nt' == os.name:
# Python 2.5 should not change $foo when foo is unknown, but
# it actually strips it out.
import backport
expandvars = backport.expandvars
return backport.expandvars(path)
else:
expandvars = os.path.expandvars
return os.path.expandvars(path)
return Common.expandvars(path)


def extended_path(path):
Expand All @@ -387,8 +379,8 @@ def extended_path(path):
def free_space(pathname):
"""Return free space in bytes"""
if 'nt' == os.name:
_, _, free_bytes = win32file.GetDiskFreeSpaceEx(pathname)
return free_bytes
import psutil
return psutil.disk_usage(pathname).free
mystat = os.statvfs(pathname)
return mystat.f_bfree * mystat.f_bsize

Expand Down Expand Up @@ -449,21 +441,18 @@ def guess_overwrite_paths():
# ~/.config makes it easy to find them and clean them.
ret = []
if 'posix' == os.name:
home = os.path.expanduser('~/.cache')
home = expanduser('~/.cache')
if not os.path.exists(home):
home = os.path.expanduser("~")
home = expanduser("~")
ret.append(home)
if not same_partition(home, '/tmp/'):
ret.append('/tmp')
elif 'nt' == os.name:
localtmp = expandvars('$TMP')
localtmp = Common.expandvars('$TMP')
logger = logging.getLogger(__name__)
if not os.path.exists(localtmp):
logger.warning('%TMP% does not exist: %s', localtmp)
localtmp = None
else:
from win32file import GetLongPathName
localtmp = GetLongPathName(localtmp)
from Windows import get_fixed_drives
for drive in get_fixed_drives():
if localtmp and same_partition(localtmp, drive):
Expand Down Expand Up @@ -505,7 +494,7 @@ def listdir(directory):
for pathname in listdir(dirname):
yield pathname
return
dirname = os.path.expanduser(directory)
dirname = expanduser(directory)
if not os.path.lexists(dirname):
return
for filename in os.listdir(dirname):
Expand Down

0 comments on commit 1058e37

Please sign in to comment.