From 79a3d3568b5f5880947d7f857fbe9630b55cc492 Mon Sep 17 00:00:00 2001 From: Michka Popoff Date: Mon, 17 Feb 2020 21:29:38 +0100 Subject: [PATCH] python: add rewrite function for generic shebangs From PEP 394 https://www.python.org/dev/peps/pep-0394/#for-python-script-publishers In cases where the script is expected to be executed outside virtual environments, developers will need to be aware of the following discrepancies across platforms and installation methods: Older Linux distributions will provide a python command that refers to Python 2, and will likely not provide a python2 command. Some newer Linux distributions will provide a python command that refers to Python 3. Some Linux distributions will not provide a python command at all by default, but will provide a python3 command by default. When potentially targeting these environments, developers may either use a Python package installation tool that rewrites shebang lines for the installed environment, provide instructions on updating shebang lines interactively, or else use more specific shebang lines that are tailored to the target environment. --- Library/Homebrew/language/python.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Library/Homebrew/language/python.rb b/Library/Homebrew/language/python.rb index 4c60445d40..888969df2c 100644 --- a/Library/Homebrew/language/python.rb +++ b/Library/Homebrew/language/python.rb @@ -88,6 +88,17 @@ module Language ] end + def self.rewrite_python_shebang(python_path) + Pathname(".").find do |f| + regex = %r{^#! ?/usr/bin/(env )?python([23](\.\d{1,2})?)$} + maximum_regex_length = "#! /usr/bin/env pythonx.yyy$".length + next unless f.file? + next unless regex.match?(f.read(maximum_regex_length)) + + Utils::Inreplace.inreplace f.to_s, regex, "#!#{python_path}" + end + end + # Mixin module for {Formula} adding virtualenv support features. module Virtualenv def self.included(base)