brew/Library/Homebrew/mktemp.rb
Issy Long 45978435e7
rubocop: Use Sorbet/StrictSigil as it's better than comments
- Previously I thought that comments were fine to discourage people from
  wasting their time trying to bump things that used `undef` that Sorbet
  didn't support. But RuboCop is better at this since it'll complain if
  the comments are unnecessary.

- Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501.

- I've gone for a mixture of `rubocop:disable` for the files that can't
  be `typed: strict` (use of undef, required before everything else, etc)
  and `rubocop:todo` for everything else that should be tried to make
  strictly typed. There's no functional difference between the two as
  `rubocop:todo` is `rubocop:disable` with a different name.

- And I entirely disabled the cop for the docs/ directory since
  `typed: strict` isn't going to gain us anything for some Markdown
  linting config files.

- This means that now it's easier to track what needs to be done rather
  than relying on checklists of files in our big Sorbet issue:

```shell
$ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l
    268
```

- And this is confirmed working for new files:

```shell
$ git status
On branch use-rubocop-for-sorbet-strict-sigils
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Library/Homebrew/bad.rb
        Library/Homebrew/good.rb

nothing added to commit but untracked files present (use "git add" to track)

$ brew style
Offenses:

bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true.
^^^^^^^^^^^^^

1340 files inspected, 1 offense detected
```
2024-08-12 15:24:27 +01:00

100 lines
2.5 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
# Performs {Formula#mktemp}'s functionality and tracks the results.
# Each instance is only intended to be used once.
class Mktemp
include FileUtils
# Path to the tmpdir used in this run, as a {Pathname}.
attr_reader :tmpdir
def initialize(prefix, opts = {})
@prefix = prefix
@retain_in_cache = opts[:retain_in_cache]
@retain = opts[:retain] || @retain_in_cache
@quiet = false
end
# Instructs this {Mktemp} to retain the staged files.
sig { void }
def retain!
@retain = true
end
# True if the staged temporary files should be retained.
def retain?
@retain
end
# True if the source files should be retained.
def retain_in_cache?
@retain_in_cache
end
# Instructs this Mktemp to not emit messages when retention is triggered.
sig { void }
def quiet!
@quiet = true
end
sig { returns(String) }
def to_s
"[Mktemp: #{tmpdir} retain=#{@retain} quiet=#{@quiet}]"
end
def run
prefix_name = @prefix.tr "@", "AT"
@tmpdir = if retain_in_cache?
tmp_dir = HOMEBREW_CACHE/"Sources/#{prefix_name}"
chmod_rm_rf(tmp_dir) # clear out previous staging directory
tmp_dir.mkpath
tmp_dir
else
Pathname.new(Dir.mktmpdir("#{prefix_name}-", HOMEBREW_TEMP))
end
# Make sure files inside the temporary directory have the same group as the
# brew instance.
#
# Reference from `man 2 open`
# > When a new file is created, it is given the group of the directory which
# contains it.
group_id = if HOMEBREW_BREW_FILE.grpowned?
HOMEBREW_BREW_FILE.stat.gid
else
Process.gid
end
begin
chown(nil, group_id, @tmpdir)
rescue Errno::EPERM
opoo "Failed setting group \"#{T.must(Etc.getgrgid(group_id)).name}\" on #{@tmpdir}"
end
begin
Dir.chdir(tmpdir) { yield self }
ensure
ignore_interrupts { chmod_rm_rf(@tmpdir) } unless retain?
end
ensure
if retain? && @tmpdir.present? && !@quiet
message = retain_in_cache? ? "Source files for debugging available at:" : "Temporary files retained at:"
ohai message, @tmpdir.to_s
end
end
private
def chmod_rm_rf(path)
if path.directory? && !path.symlink?
chmod("u+rw", path) if path.owned? # Need permissions in order to see the contents
path.children.each { |child| chmod_rm_rf(child) }
rmdir(path)
else
rm_f(path)
end
rescue
nil # Just skip this directory.
end
end