Cleanup periodically if HOMEBREW_INSTALL_CLEANUP is set.

This will become the default in a later version of Homebrew but has an
opt-out through HOMEBREW_NO_INSTALL_CLEANUP.

Also, always cleanup files older than 120 days and set the general
default value for "old" logs, casks etc. to 30 days.
This commit is contained in:
Mike McQuaid 2019-01-03 16:23:44 +00:00
parent 62448ebbc2
commit 8144667a71
No known key found for this signature in database
GPG Key ID: 48A898132FD8EE70
8 changed files with 109 additions and 51 deletions

View File

@ -3,9 +3,10 @@ require "formula"
require "cask/cask_loader" require "cask/cask_loader"
require "set" require "set"
module CleanupRefinement CLEANUP_DEFAULT_DAYS = 30
LATEST_CASK_OUTDATED = 7.days.ago CLEANUP_MAX_AGE_DAYS = 120
module CleanupRefinement
refine Enumerator do refine Enumerator do
def parallel def parallel
queue = Queue.new queue = Queue.new
@ -123,7 +124,7 @@ module CleanupRefinement
return true if scrub && !cask.versions.include?(cask.version) return true if scrub && !cask.versions.include?(cask.version)
return mtime < LATEST_CASK_OUTDATED if cask.version.latest? return mtime < CLEANUP_DEFAULT_DAYS.days.ago if cask.version.latest?
false false
end end
@ -136,6 +137,8 @@ module Homebrew
class Cleanup class Cleanup
extend Predicable extend Predicable
PERIODIC_CLEAN_FILE = HOMEBREW_CACHE/".cleaned"
attr_predicate :dry_run?, :scrub? attr_predicate :dry_run?, :scrub?
attr_reader :args, :days, :cache attr_reader :args, :days, :cache
attr_reader :disk_cleanup_size attr_reader :disk_cleanup_size
@ -145,11 +148,38 @@ module Homebrew
@args = args @args = args
@dry_run = dry_run @dry_run = dry_run
@scrub = scrub @scrub = scrub
@days = days @days = days || CLEANUP_MAX_AGE_DAYS
@cache = cache @cache = cache
@cleaned_up_paths = Set.new @cleaned_up_paths = Set.new
end end
def self.install_formula_clean!(f)
return if ENV["HOMEBREW_NO_INSTALL_CLEANUP"]
return unless ENV["HOMEBREW_INSTALL_CLEANUP"]
cleanup = Cleanup.new
if cleanup.periodic_clean_due?
cleanup.periodic_clean!
elsif f.installed?
cleanup.cleanup_formula(f)
end
end
def periodic_clean_due?
return false if ENV["HOMEBREW_NO_INSTALL_CLEANUP"]
return unless ENV["HOMEBREW_INSTALL_CLEANUP"]
return true unless PERIODIC_CLEAN_FILE.exist?
PERIODIC_CLEAN_FILE.mtime < CLEANUP_DEFAULT_DAYS.days.ago
end
def periodic_clean!
return false unless periodic_clean_due?
ohai "`brew cleanup` has not been run in #{CLEANUP_DEFAULT_DAYS} days, running now..."
clean!
end
def clean! def clean!
if args.empty? if args.empty?
Formula.installed.sort_by(&:name).each do |formula| Formula.installed.sort_by(&:name).each do |formula|
@ -164,6 +194,7 @@ module Homebrew
cleanup_old_cache_db cleanup_old_cache_db
rm_ds_store rm_ds_store
prune_prefix_symlinks_and_directories prune_prefix_symlinks_and_directories
FileUtils.touch PERIODIC_CLEAN_FILE
else else
args.each do |arg| args.each do |arg|
formula = begin formula = begin
@ -208,13 +239,16 @@ module Homebrew
unremovable_kegs << keg unremovable_kegs << keg
end end
DEFAULT_LOG_DAYS = 14
def cleanup_logs def cleanup_logs
return unless HOMEBREW_LOGS.directory? return unless HOMEBREW_LOGS.directory?
logs_days = if days > CLEANUP_DEFAULT_DAYS
CLEANUP_DEFAULT_DAYS
else
days
end
HOMEBREW_LOGS.subdirs.each do |dir| HOMEBREW_LOGS.subdirs.each do |dir|
cleanup_path(dir) { dir.rmtree } if dir.prune?(days || DEFAULT_LOG_DAYS) cleanup_path(dir) { dir.rmtree } if dir.prune?(logs_days)
end end
end end

View File

@ -73,15 +73,13 @@
#: #:
#: If `--git` (or `-g`) is passed, Homebrew will create a Git repository, useful for #: If `--git` (or `-g`) is passed, Homebrew will create a Git repository, useful for
#: creating patches to the software. #: creating patches to the software.
#:
#: If `HOMEBREW_INSTALL_CLEANUP` is set then remove previously installed versions
#: of upgraded <formulae> as well as the HOMEBREW_CACHE for that formula.
require "missing_formula" require "missing_formula"
require "formula_installer" require "formula_installer"
require "development_tools" require "development_tools"
require "install" require "install"
require "search" require "search"
require "cleanup"
module Homebrew module Homebrew
module_function module_function
@ -257,7 +255,7 @@ module Homebrew
formulae.each do |f| formulae.each do |f|
Migrator.migrate_if_needed(f) Migrator.migrate_if_needed(f)
install_formula(f) install_formula(f)
Cleanup.new.cleanup_formula(f) if ENV["HOMEBREW_INSTALL_CLEANUP"] Cleanup.install_formula_clean!(f)
end end
Homebrew.messages.display_messages Homebrew.messages.display_messages
rescue FormulaUnreadableError, FormulaClassUnavailableError, rescue FormulaUnreadableError, FormulaClassUnavailableError,

View File

@ -12,6 +12,7 @@ require "development_tools"
require "messages" require "messages"
require "reinstall" require "reinstall"
require "cli_parser" require "cli_parser"
require "cleanup"
module Homebrew module Homebrew
module_function module_function
@ -49,7 +50,7 @@ module Homebrew
end end
Migrator.migrate_if_needed(f) Migrator.migrate_if_needed(f)
reinstall_formula(f) reinstall_formula(f)
Cleanup.new.cleanup_formula(f) if ENV["HOMEBREW_INSTALL_CLEANUP"] Cleanup.install_formula_clean!(f)
end end
Homebrew.messages.display_messages Homebrew.messages.display_messages
end end

View File

@ -1,11 +1,8 @@
#: * `upgrade` [<install-options>] [`--cleanup`] [`--fetch-HEAD`] [`--ignore-pinned`] [`--display-times`] [<formulae>]: #: * `upgrade` [<install-options>] [`--fetch-HEAD`] [`--ignore-pinned`] [`--display-times`] [<formulae>]:
#: Upgrade outdated, unpinned brews (with existing install options). #: Upgrade outdated, unpinned brews (with existing install options).
#: #:
#: Options for the `install` command are also valid here. #: Options for the `install` command are also valid here.
#: #:
#: If `--cleanup` is specified or `HOMEBREW_INSTALL_CLEANUP` is set then remove
#: previously installed version(s) of upgraded <formulae>.
#:
#: If `--fetch-HEAD` is passed, fetch the upstream repository to detect if #: If `--fetch-HEAD` is passed, fetch the upstream repository to detect if
#: the HEAD installation of the formula is outdated. Otherwise, the #: the HEAD installation of the formula is outdated. Otherwise, the
#: repository's HEAD will be checked for updates when a new stable or devel #: repository's HEAD will be checked for updates when a new stable or devel
@ -23,14 +20,23 @@
require "install" require "install"
require "reinstall" require "reinstall"
require "formula_installer" require "formula_installer"
require "cleanup"
require "development_tools" require "development_tools"
require "messages" require "messages"
require "cleanup"
module Homebrew module Homebrew
module_function module_function
def upgrade def upgrade
# TODO: deprecate for next minor release.
if ARGV.include?("--cleanup")
ENV["HOMEBREW_INSTALL_CLEANUP"] = "1"
# odeprecated("'brew upgrade --cleanup'", "'HOMEBREW_INSTALL_CLEANUP'")
elsif ENV["HOMEBREW_UPGRADE_CLEANUP"]
ENV["HOMEBREW_INSTALL_CLEANUP"] = "1"
# odeprecated("'HOMEBREW_UPGRADE_CLEANUP'", "'HOMEBREW_INSTALL_CLEANUP'")
end
FormulaInstaller.prevent_build_flags unless DevelopmentTools.installed? FormulaInstaller.prevent_build_flags unless DevelopmentTools.installed?
Install.perform_preinstall_checks Install.perform_preinstall_checks
@ -107,10 +113,7 @@ module Homebrew
Migrator.migrate_if_needed(f) Migrator.migrate_if_needed(f)
begin begin
upgrade_formula(f) upgrade_formula(f)
next if !ARGV.include?("--cleanup") && !ENV["HOMEBREW_UPGRADE_CLEANUP"] && !ENV["HOMEBREW_INSTALL_CLEANUP"] Cleanup.install_formula_clean!(f)
next unless f.installed?
Cleanup.new.cleanup_formula(f)
rescue UnsatisfiedRequirements => e rescue UnsatisfiedRequirements => e
Homebrew.failed = true Homebrew.failed = true
onoe "#{f}: #{e}" onoe "#{f}: #{e}"

View File

@ -198,6 +198,16 @@ Note that environment variables must have a value set to be detected. For exampl
*Default:* the beer emoji. *Default:* the beer emoji.
* `HOMEBREW_INSTALL_CLEANUP`:
If set, `brew install`, `brew upgrade` and `brew reinstall` will remove
previously installed version(s) of the installed/upgraded formulae.
If `brew cleanup` has not been run in 30 days then it will be run at this
time.
This will become the default in a later version of Homebrew. To opt-out see
`HOMEBREW_NO_INSTALL_CLEANUP`.
* `HOMEBREW_LOGS`: * `HOMEBREW_LOGS`:
If set, Homebrew will use the specified directory to store log files. If set, Homebrew will use the specified directory to store log files.
@ -235,6 +245,11 @@ Note that environment variables must have a value set to be detected. For exampl
If set, Homebrew will not use the GitHub API, e.g. for searches or If set, Homebrew will not use the GitHub API, e.g. for searches or
fetching relevant issues on a failed install. fetching relevant issues on a failed install.
* `HOMEBREW_NO_INSTALL_CLEANUP`:
If set, `brew install`, `brew upgrade` and `brew reinstall` will never
automatically remove the previously installed version(s) of the
installed/upgraded formulae.
* `HOMEBREW_PRY`: * `HOMEBREW_PRY`:
If set, Homebrew will use Pry for the `brew irb` command. If set, Homebrew will use Pry for the `brew irb` command.
@ -256,9 +271,6 @@ Note that environment variables must have a value set to be detected. For exampl
If set, instructs Homebrew to always use the latest stable tag (even if If set, instructs Homebrew to always use the latest stable tag (even if
developer commands have been run). developer commands have been run).
* `HOMEBREW_UPGRADE_CLEANUP`:
If set, `brew upgrade` always assumes `--cleanup` has been passed.
* `HOMEBREW_VERBOSE`: * `HOMEBREW_VERBOSE`:
If set, Homebrew always assumes `--verbose` when running commands. If set, Homebrew always assumes `--verbose` when running commands.

View File

@ -178,10 +178,10 @@ describe Homebrew::Cleanup do
expect(download).to exist expect(download).to exist
end end
it "removes the download for the latest version after a week" do it "removes the download for the latest version after 30 days" do
download = Cask::Cache.path/"#{cask.token}--#{cask.version}" download = Cask::Cache.path/"#{cask.token}--#{cask.version}"
FileUtils.touch download, mtime: 7.days.ago - 1.hour FileUtils.touch download, mtime: 30.days.ago - 1.hour
subject.cleanup_cask(cask) subject.cleanup_cask(cask)
@ -202,14 +202,14 @@ describe Homebrew::Cleanup do
expect(path).not_to exist expect(path).not_to exist
end end
it "cleans up logs if older than 14 days" do it "cleans up logs if older than 30 days" do
allow_any_instance_of(Pathname).to receive(:mtime).and_return(15.days.ago) allow_any_instance_of(Pathname).to receive(:mtime).and_return(31.days.ago)
subject.cleanup_logs subject.cleanup_logs
expect(path).not_to exist expect(path).not_to exist
end end
it "does not clean up logs less than 14 days old" do it "does not clean up logs less than 30 days old" do
allow_any_instance_of(Pathname).to receive(:mtime).and_return(2.days.ago) allow_any_instance_of(Pathname).to receive(:mtime).and_return(15.days.ago)
subject.cleanup_logs subject.cleanup_logs
expect(path).to exist expect(path).to exist
end end

View File

@ -312,9 +312,6 @@ these flags should only appear after a command.
If `--git` (or `-g`) is passed, Homebrew will create a Git repository, useful for If `--git` (or `-g`) is passed, Homebrew will create a Git repository, useful for
creating patches to the software. creating patches to the software.
If `HOMEBREW_INSTALL_CLEANUP` is set then remove previously installed versions
of upgraded *`formulae`* as well as the HOMEBREW_CACHE for that formula.
* `leaves`: * `leaves`:
Show installed formulae that are not dependencies of another installed formula. Show installed formulae that are not dependencies of another installed formula.
@ -599,14 +596,11 @@ these flags should only appear after a command.
`repositories`) using `git`(1) to their latest `origin/master`. Note this `repositories`) using `git`(1) to their latest `origin/master`. Note this
will destroy all your uncommitted or committed changes. will destroy all your uncommitted or committed changes.
* `upgrade` [*`install-options`*] [`--cleanup`] [`--fetch-HEAD`] [`--ignore-pinned`] [`--display-times`] [*`formulae`*]: * `upgrade` [*`install-options`*] [`--fetch-HEAD`] [`--ignore-pinned`] [`--display-times`] [*`formulae`*]:
Upgrade outdated, unpinned brews (with existing install options). Upgrade outdated, unpinned brews (with existing install options).
Options for the `install` command are also valid here. Options for the `install` command are also valid here.
If `--cleanup` is specified or `HOMEBREW_INSTALL_CLEANUP` is set then remove
previously installed version(s) of upgraded *`formulae`*.
If `--fetch-HEAD` is passed, fetch the upstream repository to detect if If `--fetch-HEAD` is passed, fetch the upstream repository to detect if
the HEAD installation of the formula is outdated. Otherwise, the the HEAD installation of the formula is outdated. Otherwise, the
repository's HEAD will be checked for updates when a new stable or devel repository's HEAD will be checked for updates when a new stable or devel
@ -1198,6 +1192,16 @@ Note that environment variables must have a value set to be detected. For exampl
*Default:* the beer emoji. *Default:* the beer emoji.
* `HOMEBREW_INSTALL_CLEANUP`:
If set, `brew install`, `brew upgrade` and `brew reinstall` will remove
previously installed version(s) of the installed/upgraded formulae.
If `brew cleanup` has not been run in 30 days then it will be run at this
time.
This will become the default in a later version of Homebrew. To opt-out see
`HOMEBREW_NO_INSTALL_CLEANUP`.
* `HOMEBREW_LOGS`: * `HOMEBREW_LOGS`:
If set, Homebrew will use the specified directory to store log files. If set, Homebrew will use the specified directory to store log files.
@ -1235,6 +1239,11 @@ Note that environment variables must have a value set to be detected. For exampl
If set, Homebrew will not use the GitHub API, e.g. for searches or If set, Homebrew will not use the GitHub API, e.g. for searches or
fetching relevant issues on a failed install. fetching relevant issues on a failed install.
* `HOMEBREW_NO_INSTALL_CLEANUP`:
If set, `brew install`, `brew upgrade` and `brew reinstall` will never
automatically remove the previously installed version(s) of the
installed/upgraded formulae.
* `HOMEBREW_PRY`: * `HOMEBREW_PRY`:
If set, Homebrew will use Pry for the `brew irb` command. If set, Homebrew will use Pry for the `brew irb` command.
@ -1256,9 +1265,6 @@ Note that environment variables must have a value set to be detected. For exampl
If set, instructs Homebrew to always use the latest stable tag (even if If set, instructs Homebrew to always use the latest stable tag (even if
developer commands have been run). developer commands have been run).
* `HOMEBREW_UPGRADE_CLEANUP`:
If set, `brew upgrade` always assumes `--cleanup` has been passed.
* `HOMEBREW_VERBOSE`: * `HOMEBREW_VERBOSE`:
If set, Homebrew always assumes `--verbose` when running commands. If set, Homebrew always assumes `--verbose` when running commands.

View File

@ -315,9 +315,6 @@ If \fB\-\-interactive\fR (or \fB\-i\fR) is passed, download and patch \fIformula
.IP .IP
If \fB\-\-git\fR (or \fB\-g\fR) is passed, Homebrew will create a Git repository, useful for creating patches to the software\. If \fB\-\-git\fR (or \fB\-g\fR) is passed, Homebrew will create a Git repository, useful for creating patches to the software\.
. .
.IP
If \fBHOMEBREW_INSTALL_CLEANUP\fR is set then remove previously installed versions of upgraded \fIformulae\fR as well as the HOMEBREW_CACHE for that formula\.
.
.TP .TP
\fBleaves\fR \fBleaves\fR
Show installed formulae that are not dependencies of another installed formula\. Show installed formulae that are not dependencies of another installed formula\.
@ -610,16 +607,13 @@ If \fB\-\-force\fR (or \fB\-f\fR) is specified then always do a slower, full upd
Fetches and resets Homebrew and all tap repositories (or the specified \fBrepositories\fR) using \fBgit\fR(1) to their latest \fBorigin/master\fR\. Note this will destroy all your uncommitted or committed changes\. Fetches and resets Homebrew and all tap repositories (or the specified \fBrepositories\fR) using \fBgit\fR(1) to their latest \fBorigin/master\fR\. Note this will destroy all your uncommitted or committed changes\.
. .
.TP .TP
\fBupgrade\fR [\fIinstall\-options\fR] [\fB\-\-cleanup\fR] [\fB\-\-fetch\-HEAD\fR] [\fB\-\-ignore\-pinned\fR] [\fB\-\-display\-times\fR] [\fIformulae\fR] \fBupgrade\fR [\fIinstall\-options\fR] [\fB\-\-fetch\-HEAD\fR] [\fB\-\-ignore\-pinned\fR] [\fB\-\-display\-times\fR] [\fIformulae\fR]
Upgrade outdated, unpinned brews (with existing install options)\. Upgrade outdated, unpinned brews (with existing install options)\.
. .
.IP .IP
Options for the \fBinstall\fR command are also valid here\. Options for the \fBinstall\fR command are also valid here\.
. .
.IP .IP
If \fB\-\-cleanup\fR is specified or \fBHOMEBREW_INSTALL_CLEANUP\fR is set then remove previously installed version(s) of upgraded \fIformulae\fR\.
.
.IP
If \fB\-\-fetch\-HEAD\fR is passed, fetch the upstream repository to detect if the HEAD installation of the formula is outdated\. Otherwise, the repository\'s HEAD will be checked for updates when a new stable or devel version has been released\. If \fB\-\-fetch\-HEAD\fR is passed, fetch the upstream repository to detect if the HEAD installation of the formula is outdated\. Otherwise, the repository\'s HEAD will be checked for updates when a new stable or devel version has been released\.
. .
.IP .IP
@ -1322,6 +1316,16 @@ Text printed before the installation summary of each successful build\.
\fIDefault:\fR the beer emoji\. \fIDefault:\fR the beer emoji\.
. .
.TP .TP
\fBHOMEBREW_INSTALL_CLEANUP\fR
If set, \fBbrew install\fR, \fBbrew upgrade\fR and \fBbrew reinstall\fR will remove previously installed version(s) of the installed/upgraded formulae\.
.
.IP
If \fBbrew cleanup\fR has not been run in 30 days then it will be run at this time\.
.
.IP
This will become the default in a later version of Homebrew\. To opt\-out see \fBHOMEBREW_NO_INSTALL_CLEANUP\fR\.
.
.TP
\fBHOMEBREW_LOGS\fR \fBHOMEBREW_LOGS\fR
If set, Homebrew will use the specified directory to store log files\. If set, Homebrew will use the specified directory to store log files\.
. .
@ -1363,6 +1367,10 @@ While ensuring your downloads are fully secure, this is likely to cause from\-so
If set, Homebrew will not use the GitHub API, e\.g\. for searches or fetching relevant issues on a failed install\. If set, Homebrew will not use the GitHub API, e\.g\. for searches or fetching relevant issues on a failed install\.
. .
.TP .TP
\fBHOMEBREW_NO_INSTALL_CLEANUP\fR
If set, \fBbrew install\fR, \fBbrew upgrade\fR and \fBbrew reinstall\fR will never automatically remove the previously installed version(s) of the installed/upgraded formulae\.
.
.TP
\fBHOMEBREW_PRY\fR \fBHOMEBREW_PRY\fR
If set, Homebrew will use Pry for the \fBbrew irb\fR command\. If set, Homebrew will use Pry for the \fBbrew irb\fR command\.
. .
@ -1385,10 +1393,6 @@ This issue typically occurs when using FileVault or custom SSD configurations\.
If set, instructs Homebrew to always use the latest stable tag (even if developer commands have been run)\. If set, instructs Homebrew to always use the latest stable tag (even if developer commands have been run)\.
. .
.TP .TP
\fBHOMEBREW_UPGRADE_CLEANUP\fR
If set, \fBbrew upgrade\fR always assumes \fB\-\-cleanup\fR has been passed\.
.
.TP
\fBHOMEBREW_VERBOSE\fR \fBHOMEBREW_VERBOSE\fR
If set, Homebrew always assumes \fB\-\-verbose\fR when running commands\. If set, Homebrew always assumes \fB\-\-verbose\fR when running commands\.
. .