| 
									
										
										
										
											2014-03-07 18:03:54 +00:00
										 |  |  | require "utils.rb" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Language | 
					
						
							|  |  |  |   module Python | 
					
						
							|  |  |  |     def self.major_minor_version python | 
					
						
							|  |  |  |       version = /\d\.\d/.match `#{python} --version 2>&1` | 
					
						
							|  |  |  |       return unless version | 
					
						
							|  |  |  |       Version.new(version.to_s) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-19 13:47:55 -07:00
										 |  |  |     def self.homebrew_site_packages(version="2.7") | 
					
						
							|  |  |  |       HOMEBREW_PREFIX/"lib/python#{version}/site-packages" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-07 18:03:54 +00:00
										 |  |  |     def self.each_python build, &block | 
					
						
							|  |  |  |       original_pythonpath = ENV["PYTHONPATH"] | 
					
						
							|  |  |  |       ["python", "python3"].each do |python| | 
					
						
							|  |  |  |         next if build.without? python | 
					
						
							|  |  |  |         version = self.major_minor_version python | 
					
						
							| 
									
										
										
										
											2014-06-22 15:00:15 -05:00
										 |  |  |         ENV["PYTHONPATH"] = if Formulary.factory(python).installed? | 
					
						
							| 
									
										
										
										
											2014-03-07 18:03:54 +00:00
										 |  |  |           nil | 
					
						
							|  |  |  |         else | 
					
						
							| 
									
										
										
										
											2014-10-19 13:47:55 -07:00
										 |  |  |           homebrew_site_packages(version) | 
					
						
							| 
									
										
										
										
											2014-03-07 18:03:54 +00:00
										 |  |  |         end | 
					
						
							|  |  |  |         block.call python, version if block | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       ENV["PYTHONPATH"] = original_pythonpath | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-10-19 13:47:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def self.reads_brewed_pth_files? python | 
					
						
							|  |  |  |       version = major_minor_version python | 
					
						
							|  |  |  |       return unless homebrew_site_packages(version).directory? | 
					
						
							| 
									
										
										
										
											2014-11-05 19:37:24 -08:00
										 |  |  |       return unless homebrew_site_packages(version).writable_real? | 
					
						
							| 
									
										
										
										
											2014-10-19 13:47:55 -07:00
										 |  |  |       probe_file = homebrew_site_packages(version)/"homebrew-pth-probe.pth" | 
					
						
							| 
									
										
										
										
											2014-11-05 19:37:24 -08:00
										 |  |  |       begin | 
					
						
							|  |  |  |         probe_file.atomic_write("import site; site.homebrew_was_here = True") | 
					
						
							|  |  |  |         quiet_system python, "-c", "import site; assert(site.homebrew_was_here)" | 
					
						
							|  |  |  |       ensure | 
					
						
							|  |  |  |         probe_file.unlink if probe_file.exist? | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2014-10-19 13:47:55 -07:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.user_site_packages python | 
					
						
							|  |  |  |       Pathname.new(`#{python} -c "import site; print(site.getusersitepackages())"`.chomp) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def self.in_sys_path? python, path | 
					
						
							|  |  |  |       script = <<-EOS.undent
 | 
					
						
							|  |  |  |         import os, sys | 
					
						
							|  |  |  |         [os.path.realpath(p) for p in sys.path].index(os.path.realpath("#{path}")) | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       quiet_system python, "-c", script | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-11-06 22:25:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-09 23:17:11 -08:00
										 |  |  |     # deprecated; use system "python", *setup_install_args(prefix) instead | 
					
						
							| 
									
										
										
										
											2014-11-06 22:25:11 -08:00
										 |  |  |     def self.setup_install python, prefix, *args | 
					
						
							| 
									
										
										
										
											2014-12-10 09:10:44 -08:00
										 |  |  |       opoo <<-EOS.undent
 | 
					
						
							|  |  |  |         Language::Python.setup_install is deprecated. | 
					
						
							|  |  |  |         If you are a formula author, please use | 
					
						
							|  |  |  |           system "python", *Language::Python.setup_install_args(prefix) | 
					
						
							|  |  |  |         instead. | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 22:25:11 -08:00
										 |  |  |       # force-import setuptools, which monkey-patches distutils, to make | 
					
						
							|  |  |  |       # sure that we always call a setuptools setup.py. trick borrowed from pip: | 
					
						
							|  |  |  |       # https://github.com/pypa/pip/blob/043af83/pip/req/req_install.py#L743-L780 | 
					
						
							|  |  |  |       shim = <<-EOS.undent
 | 
					
						
							|  |  |  |         import setuptools, tokenize | 
					
						
							|  |  |  |         __file__ = 'setup.py' | 
					
						
							|  |  |  |         exec(compile(getattr(tokenize, 'open', open)(__file__).read() | 
					
						
							|  |  |  |           .replace('\\r\\n', '\\n'), __file__, 'exec')) | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       args += %w[--single-version-externally-managed --record=installed.txt] | 
					
						
							|  |  |  |       args << "--prefix=#{prefix}" | 
					
						
							|  |  |  |       system python, "-c", shim, "install", *args | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-12-09 23:17:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def self.setup_install_args prefix | 
					
						
							|  |  |  |       shim = <<-EOS.undent
 | 
					
						
							|  |  |  |         import setuptools, tokenize | 
					
						
							|  |  |  |         __file__ = 'setup.py' | 
					
						
							|  |  |  |         exec(compile(getattr(tokenize, 'open', open)(__file__).read() | 
					
						
							|  |  |  |           .replace('\\r\\n', '\\n'), __file__, 'exec')) | 
					
						
							|  |  |  |       EOS | 
					
						
							|  |  |  |       %W[
 | 
					
						
							|  |  |  |         -c | 
					
						
							|  |  |  |         #{shim} | 
					
						
							| 
									
										
										
										
											2015-01-08 16:43:40 -08:00
										 |  |  |         --no-user-cfg | 
					
						
							| 
									
										
										
										
											2014-12-09 23:17:11 -08:00
										 |  |  |         install | 
					
						
							|  |  |  |         --prefix=#{prefix} | 
					
						
							|  |  |  |         --single-version-externally-managed | 
					
						
							|  |  |  |         --record=installed.txt | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-03-02 21:44:35 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def self.package_available? python, module_name | 
					
						
							|  |  |  |       quiet_system python, "-c", "import #{module_name}" | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2014-03-07 18:03:54 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |