| 
									
										
										
										
											2024-08-12 10:30:59 +01:00
										 |  |  | # typed: true # rubocop:todo Sorbet/StrictSigil | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  | module Utils | 
					
						
							| 
									
										
										
										
											2020-09-02 18:14:56 +02:00
										 |  |  |   IO_DEFAULT_BUFFER_SIZE = 4096
 | 
					
						
							|  |  |  |   private_constant :IO_DEFAULT_BUFFER_SIZE | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 10:22:40 -08:00
										 |  |  |   def self.popen_read(*args, safe: false, **options, &block) | 
					
						
							|  |  |  |     output = popen(args, "rb", options, &block) | 
					
						
							|  |  |  |     return output if !safe || $CHILD_STATUS.success? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   def self.safe_popen_read(*args, **options, &block) | 
					
						
							| 
									
										
										
										
											2021-01-12 10:22:40 -08:00
										 |  |  |     popen_read(*args, safe: true, **options, &block) | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-12 10:22:40 -08:00
										 |  |  |   def self.popen_write(*args, safe: false, **options) | 
					
						
							|  |  |  |     output = "" | 
					
						
							| 
									
										
										
										
											2020-08-30 22:36:28 +02:00
										 |  |  |     popen(args, "w+b", options) do |pipe| | 
					
						
							|  |  |  |       # Before we yield to the block, capture as much output as we can | 
					
						
							|  |  |  |       loop do | 
					
						
							| 
									
										
										
										
											2020-09-02 18:14:56 +02:00
										 |  |  |         output += pipe.read_nonblock(IO_DEFAULT_BUFFER_SIZE) | 
					
						
							| 
									
										
										
										
											2020-08-30 22:36:28 +02:00
										 |  |  |       rescue IO::WaitReadable, EOFError | 
					
						
							|  |  |  |         break | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       yield pipe | 
					
						
							|  |  |  |       pipe.close_write | 
					
						
							| 
									
										
										
										
											2022-01-18 14:10:23 -05:00
										 |  |  |       pipe.wait_readable | 
					
						
							| 
									
										
										
										
											2020-08-30 22:36:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # Capture the rest of the output | 
					
						
							|  |  |  |       output += pipe.read | 
					
						
							|  |  |  |       output.freeze | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2021-01-12 10:22:40 -08:00
										 |  |  |     return output if !safe || $CHILD_STATUS.success? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     raise ErrorDuringExecution.new(args, status: $CHILD_STATUS, output: [[:stdout, output]]) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   def self.safe_popen_write(*args, **options, &block) | 
					
						
							| 
									
										
										
										
											2021-01-12 10:22:40 -08:00
										 |  |  |     popen_write(*args, safe: true, **options, &block) | 
					
						
							| 
									
										
										
										
											2018-06-22 16:56:37 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-19 10:18:04 -07:00
										 |  |  |   def self.popen(args, mode, options = {}) | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |     IO.popen("-", mode) do |pipe| | 
					
						
							|  |  |  |       if pipe | 
					
						
							| 
									
										
										
										
											2016-09-23 22:02:23 +02:00
										 |  |  |         return pipe.read unless block_given? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 22:02:23 +02:00
										 |  |  |         yield pipe | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |       else | 
					
						
							| 
									
										
										
										
											2021-03-19 17:14:59 +00:00
										 |  |  |         options[:err] ||= "/dev/null" unless ENV["HOMEBREW_STDERR"] | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  |         begin | 
					
						
							|  |  |  |           exec(*args, options) | 
					
						
							|  |  |  |         rescue Errno::ENOENT | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |           $stderr.puts "brew: command not found: #{args[0]}" if options[:err] != :close | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  |           exit! 127
 | 
					
						
							|  |  |  |         rescue SystemCallError | 
					
						
							| 
									
										
										
										
											2023-04-18 15:06:50 -07:00
										 |  |  |           $stderr.puts "brew: exec failed: #{args[0]}" if options[:err] != :close | 
					
						
							| 
									
										
										
										
											2017-12-01 15:00:27 -08:00
										 |  |  |           exit! 1
 | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-07-05 13:50:54 -05:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |