| 
									
										
										
										
											2024-09-25 19:38:57 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-27 08:18:12 +00:00
										 |  |  | require "utils" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-20 11:13:13 -08:00
										 |  |  | module OS | 
					
						
							| 
									
										
										
										
											2020-08-26 03:06:19 +02:00
										 |  |  |   # Helper module for querying system information on Linux. | 
					
						
							| 
									
										
										
										
											2019-09-13 16:48:12 +01:00
										 |  |  |   module Linux | 
					
						
							| 
									
										
										
										
											2024-09-24 10:15:34 +01:00
										 |  |  |     raise "Loaded OS::Linux on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # This check is the only acceptable or necessary one in this file. | 
					
						
							|  |  |  |     # rubocop:disable Homebrew/MoveToExtendOS | 
					
						
							|  |  |  |     raise "Loaded OS::Linux on macOS!" if OS.mac? | 
					
						
							|  |  |  |     # rubocop:enable Homebrew/MoveToExtendOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  |     @languages = T.let([], T::Array[String]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-22 21:05:48 +02:00
										 |  |  |     # Get the OS version. | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # @api internal | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |     sig { returns(String) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.os_version | 
					
						
							| 
									
										
										
										
											2019-09-13 16:48:12 +01:00
										 |  |  |       if which("lsb_release") | 
					
						
							| 
									
										
										
										
											2021-03-17 15:34:20 +00:00
										 |  |  |         lsb_info = Utils.popen_read("lsb_release", "-a") | 
					
						
							| 
									
										
										
										
											2022-01-09 21:12:52 -05:00
										 |  |  |         description = lsb_info[/^Description:\s*(.*)$/, 1].force_encoding("UTF-8") | 
					
						
							| 
									
										
										
										
											2020-11-30 14:41:02 -06:00
										 |  |  |         codename = lsb_info[/^Codename:\s*(.*)$/, 1] | 
					
						
							| 
									
										
										
										
											2020-12-02 11:54:59 +00:00
										 |  |  |         if codename.blank? || (codename == "n/a") | 
					
						
							| 
									
										
										
										
											2020-11-30 14:41:02 -06:00
										 |  |  |           description | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           "#{description} (#{codename})" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2023-07-29 00:58:36 -04:00
										 |  |  |       elsif ::OS_VERSION.present? | 
					
						
							| 
									
										
										
										
											2023-07-29 00:41:13 -04:00
										 |  |  |         ::OS_VERSION | 
					
						
							| 
									
										
										
										
											2019-09-13 16:48:12 +01:00
										 |  |  |       else | 
					
						
							|  |  |  |         "Unknown" | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2023-02-27 08:18:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.wsl? | 
					
						
							| 
									
										
										
										
											2023-02-27 08:18:12 +00:00
										 |  |  |       /-microsoft/i.match?(OS.kernel_version.to_s) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     sig { returns(Version) } | 
					
						
							| 
									
										
										
										
											2023-04-17 10:37:59 -07:00
										 |  |  |     def self.wsl_version | 
					
						
							| 
									
										
										
										
											2023-02-27 10:17:50 -06:00
										 |  |  |       return Version::NULL unless wsl? | 
					
						
							| 
									
										
										
										
											2023-02-27 08:18:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-27 10:17:50 -06:00
										 |  |  |       kernel = OS.kernel_version.to_s | 
					
						
							|  |  |  |       if Version.new(T.must(kernel[/^([0-9.]*)-.*/, 1])) > Version.new("5.15") | 
					
						
							|  |  |  |         Version.new("2 (Microsoft Store)") | 
					
						
							|  |  |  |       elsif kernel.include?("-microsoft") | 
					
						
							|  |  |  |         Version.new("2") | 
					
						
							|  |  |  |       elsif kernel.include?("-Microsoft") | 
					
						
							|  |  |  |         Version.new("1") | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         Version::NULL | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-02-27 08:18:12 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     sig { returns(T::Array[String]) } | 
					
						
							|  |  |  |     def self.languages | 
					
						
							|  |  |  |       return @languages if @languages.present? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:19:31 +00:00
										 |  |  |       locale_variables = ENV.keys.grep(/^(?:LC_\S+|LANG|LANGUAGE)\Z/).sort | 
					
						
							|  |  |  |       ctl_ret = Utils.popen_read("localectl", "list-locales") | 
					
						
							|  |  |  |       if ctl_ret.present? | 
					
						
							|  |  |  |         list = ctl_ret.scan(/[^ \n"(),]+/) | 
					
						
							|  |  |  |       elsif locale_variables.present? | 
					
						
							|  |  |  |         keys = locale_variables.select { |var| ENV.fetch(var) } | 
					
						
							|  |  |  |         list = keys.map { |key| ENV.fetch(key) } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         list = ["en_US.utf8"] | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       @languages = list.map { |item| item.split(".").first.tr("_", "-") } | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-19 11:19:31 +00:00
										 |  |  |     sig { returns(T.nilable(String)) } | 
					
						
							|  |  |  |     def self.language | 
					
						
							|  |  |  |       languages.first | 
					
						
							| 
									
										
										
										
											2024-12-04 22:49:14 +01:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-09-13 16:48:12 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-02-20 11:13:13 -08:00
										 |  |  | end |