New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os: add kernel_version
#8381
os: add kernel_version
#8381
Conversation
Library/Homebrew/os.rb
Outdated
@@ -13,6 +13,10 @@ def self.linux? | |||
RbConfig::CONFIG["host_os"].include? "linux" | |||
end | |||
|
|||
def self.uname_version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call this kernel_version
?
Any new utility methods that are used by formulae should be used by at least 3 (ideally a lot more than that) before we add them. I'm not sure I see the advantage here; it's not even actually shorter in this case? |
About 17 formulae use
Hehe, you might have a point 😆 It's longer but I think it's more clear |
I'm not convinced the |
527fd35
to
1c3c382
Compare
Slightly off-topic, latest GNU |
You've changed my mind: I like these. |
Library/Homebrew/os.rb
Outdated
@@ -13,6 +13,10 @@ def self.linux? | |||
RbConfig::CONFIG["host_os"].include? "linux" | |||
end | |||
|
|||
def self.kernel_version | |||
@kernel_version ||= Version.new(`uname -r`.strip) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- What if
uname -r
fails? (should probablyraise
) Utils.popen_read
might be a nicer fit here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also: could this get a unit test.
I'm not certain, but after checking in |
Library/Homebrew/os.rb
Outdated
@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence | ||
Version.new output | ||
else | ||
Version::NULL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't Version.create
return Version::NULL
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be Version.parse
but that might be excessive in this case since uname -r
already returns a valid version string
Library/Homebrew/os.rb
Outdated
@@ -13,6 +13,14 @@ def self.linux? | |||
RbConfig::CONFIG["host_os"].include? "linux" | |||
end | |||
|
|||
def self.kernel_version | |||
@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kernel_version ||= if output = Utils.popen_read("uname", "-r").chomp.presence | |
@kernel_version ||= if output = Utils.safe_popen_read("uname", "-r").chomp.presence |
I don't think this should be able to fail, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, I forgot about that. I was just preserving the functionality of OS::Linux::Kernel.version
since that's where this came from
We probably won't need the if
since safe_popen_read
raises an error anyways
Library/Homebrew/os/linux/kernel.rb
Outdated
return @version if @version | ||
|
||
version = Utils.popen_read("uname", "-r").chomp | ||
return Version::NULL unless version | ||
|
||
@version = Version.new version | ||
OS.kernel_version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we want to preserve this or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so unless you can verify it's unused in e.g. linuxbrew-core in which case: 🔥 it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is only used in brew
in extend/os/linux/diagnostic
(including below_minimum_version?
):
% cd $(brew --repo)
% grep -r 'Linux::Kernel' *
Library/Homebrew/test/os/linux/diagnostic_spec.rb: allow(OS::Linux::Kernel).to receive(:below_minimum_version?).and_return(true)
Library/Homebrew/extend/os/linux/diagnostic.rb: return unless OS::Linux::Kernel.below_minimum_version?
Library/Homebrew/extend/os/linux/diagnostic.rb: Your Linux kernel #{OS::Linux::Kernel.version} is too old.
Library/Homebrew/extend/os/linux/diagnostic.rb: We only support kernel #{OS::Linux::Kernel.minimum_version} or later.
% cd $(brew --repo homebrew/core)
% git remote get-url origin
https://github.com/Homebrew/linuxbrew-core
% grep 'Linux::Kernel' Formula/*
% grep 'Linux::' Formula/*
Formula/clang-format.rb: if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/glibc.rb: Glibc.version >= OS::Linux::Glibc.system_version
Formula/glibc.rb: Your system's glibc version is #{OS::Linux::Glibc.system_version}, and Homebrew's glibc version is #{Glibc.version}.
Formula/llvm@7.rb: if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm@8.rb: if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm@9.rb: if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
Formula/llvm.rb: if Formula["glibc"].any_version_installed? || OS::Linux::Glibc.system_version < Formula["glibc"].version
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool! I think you can 🔥 then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So uh, what does 🔥 mean in this context? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry: burn it down (i.e. delete it)
@@ -13,6 +13,10 @@ def self.linux? | |||
RbConfig::CONFIG["host_os"].include? "linux" | |||
end | |||
|
|||
def self.kernel_version | |||
@kernel_version ||= Version.new(Utils.safe_popen_read("uname", "-r").chomp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect!
460f1d9
to
3379e3b
Compare
brew style
with your changes locally?brew tests
with your changes locally?This allows for e.g.
`uname -r`.split(".").first
inhomebrew/core/gcc
to be replaced withOS.kernel_version.major
Uses:
`uname -r`.split(".").first
→OS.kernel_version.major
`uname -r`.to_i
→OS.kernel_version.major
`uname -r`.chomp
→OS.kernel_version
shell_output("uname -r").strip
→OS.kernel_version
Utils.safe_popen_read("uname", "-r").chomp
→OS.kernel_version