From mboxrd@z Thu Jan 1 00:00:00 1970 From: Hartmut Goebel Subject: Re: avoid wrapper scripts when possible Date: Sat, 4 Nov 2017 11:12:30 +0100 Message-ID: <9ccb6232-ded3-9399-9314-91644db7d89e@crazy-compilers.com> References: <87mv44gyfv.fsf@elephly.net> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:60810) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eAvRN-0005ZV-4I for guix-devel@gnu.org; Sat, 04 Nov 2017 06:12:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eAvRJ-0006jy-V1 for guix-devel@gnu.org; Sat, 04 Nov 2017 06:12:37 -0400 Received: from mail-out.m-online.net ([2001:a60:0:28:0:1:25:1]:58617) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eAvRJ-0006ia-OB for guix-devel@gnu.org; Sat, 04 Nov 2017 06:12:33 -0400 Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 3yTZPq5s40z1qsWD for ; Sat, 4 Nov 2017 11:12:31 +0100 (CET) Received: from localhost (dynscan1.mnet-online.de [192.168.6.70]) by mail.m-online.net (Postfix) with ESMTP id 3yTZPq4pDLz1qql0 for ; Sat, 4 Nov 2017 11:12:31 +0100 (CET) Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.70]) (amavisd-new, port 10024) with ESMTP id DIIl1nH_BX8I for ; Sat, 4 Nov 2017 11:12:30 +0100 (CET) Received: from hermia.goebel-consult.de (ppp-46-244-146-124.dynamic.mnet-online.de [46.244.146.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPS for ; Sat, 4 Nov 2017 11:12:30 +0100 (CET) Received: from [192.168.110.2] (lenashee.goebel-consult.de [192.168.110.2]) by hermia.goebel-consult.de (Postfix) with ESMTP id E8BD360512 for ; Sat, 4 Nov 2017 11:12:30 +0100 (CET) In-Reply-To: <87mv44gyfv.fsf@elephly.net> Content-Language: en-US List-Id: "Development of GNU Guix and the GNU System distribution." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-devel-bounces+gcggd-guix-devel=m.gmane.org@gnu.org Sender: "Guix-devel" To: guix-devel@gnu.org Hallo Ricardo, Am 02.11.2017 um 22:31 schrieb Ricardo Wurmus: > This made me wonder if we could avoid shell wrappers in more cases. This is an interesting challenge :-) > If > the target script is a Python script we could set the environment > variables right after the Python shebang with Python code. If the > target script is a shell script we can set environment variables right > there after the shebang. Basically thsi would be a good solution IMO. But I'm afraid it might be complicated to insert the code "right after the [=E2=80=A6] shebang". For= example Python has "doc-strings" [1], this is: if the first statement of the script is a string (expression statement), this string is put into the variable __doc__. So for at least for Python we might need a language-aware scanner to insert the code at the correct position. We might need similar tor other languages. But taking your challenge :-) I had another idea which should work for all script languages: The real script's she-bang delegates to the wrapper (instead of /bin/sh), which execs "interpreter $0". Here is a proof on concept: =2E...8<------- real.py #!/tmp/wrapper.sh print("This is real") import sys, os print("sys.path[:2]", sys.path[:2]) print("argumentes:", sys.argv) print("ppid", os.getppid()) =2E...8<------- =2E...8<-------=C2=A0 wrapper.sh #!/bin/sh echo "This is the wrapper" echo "arguments in wrapper:" "$@" echo "ppid:" $PPID export PYTHONPATH=3D/foo/bar exec python3 /tmp/real.py "${@:2}" # <- replace first argument by full pa= th =2E...8<------- I have to admit that this still adds another script and is less elegant, but should be easier to implement. []1] https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring --=20 Regards Hartmut Goebel | Hartmut Goebel | h.goebel@crazy-compilers.com | | www.crazy-compilers.com | compilers which you thought are impossible |