From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ricardo Wurmus Subject: Re: avoid wrapper scripts when possible Date: Fri, 03 Nov 2017 23:41:44 +0100 Message-ID: <87375vgf2v.fsf@elephly.net> References: <87mv44gyfv.fsf@elephly.net> <20171103185416.0ffc4065@cbaines.net> <8760argizk.fsf@elephly.net> <874lqbghlz.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]:37673) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBmG0-0004mB-U4 for guix-devel@gnu.org; Mon, 06 Nov 2017 13:36:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eBmFy-0002k3-Df for guix-devel@gnu.org; Mon, 06 Nov 2017 13:36:24 -0500 Received: from sender-of-o51.zoho.com ([135.84.80.216]:21021) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eBmFy-0002jt-80 for guix-devel@gnu.org; Mon, 06 Nov 2017 13:36:22 -0500 In-reply-to: <874lqbghlz.fsf@elephly.net> 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: Christopher Baines Cc: guix-devel@gnu.org Ricardo Wurmus writes: > Ricardo Wurmus writes: > >> I wonder if there=E2=80=99s a way to have a script header that starts ou= t as a >> shell script and then calls itself again but skipping over the header, >> thus calling the original script. >> >> Take this Python script as an example: >> >> --8<---------------cut here---------------start------------->8--- >> #!/home/rekado/.guix-profile/bin/python >> print("hello from python") >> --8<---------------cut here---------------end--------------->8--- >> >> We then prepend this header: >> >> --8<---------------cut here---------------start------------->8--- >> #!/run/current-system/profile/bin/bash >> echo "hello from bash" >> exec /home/rekado/.guix-profile/bin/python <(/run/current-system/profile= /bin/tail -n +4 "$0") >> --8<---------------cut here---------------end--------------->8--- >> >> When we execute the resulting thing it will be a shell script, and do >> shell things printing =E2=80=9Chello from bash=E2=80=9D. Then it scrubs= itself off the >> script and removes the original script=E2=80=99s Python shebang using ta= il (with >> =E2=80=9C-n +4=E2=80=9D where 4 is the number of lines of the shell scri= pt header + 1), >> and replaces itself with Python calling the original script. >> >> This seems to work just fine [=E2=80=A6] > > Erm, it does not. =E2=80=9C$0=E2=80=9D becomes a file descriptor like = =E2=80=9C/dev/fd/63=E2=80=9D. > =E2=80=9Cexec -a $0=E2=80=9D also doesn=E2=80=99t help here. How about this: --8<---------------cut here---------------start------------->8--- #!/home/rekado/.guix-profile/bin/guile --no-auto-compile #!# (let (($0 (car (command-line)))) (execl "/home/rekado/.guix-profile/bin= /python3" $0 $0)) #!/home/rekado/.guix-profile/bin/python3 import sys; print("hello from python: "+sys.argv[0]) --8<---------------cut here---------------end--------------->8--- The first two lines are Guile code, but they are also line comments in shell, Perl, Python, Ruby, and R. The Guile code in this example calls the script again as a Python script. Before doing that it can set environment variables, like so: --8<---------------cut here---------------start------------->8--- #!/home/rekado/.guix-profile/bin/guile --no-auto-compile #!# #\- (setenv "PYTHONPATH" (string-append "/gnu/store/foo:/gnu/store/bar:" (g= etenv "PYTHONPATH"))) #\- (let (($0 (car (command-line)))) (execl "/home/rekado/.guix-profile/bin= /python3" $0 $0)) #!/home/rekado/.guix-profile/bin/python3 import sys; print("hello from python: "+sys.argv[0]) print(sys.path) --8<---------------cut here---------------end--------------->8--- Did I overlook something? Or could we use this hack to remove separate wrapper scripts for Perl, Python, Ruby, and R? -- Ricardo GPG: BCA6 89B6 3655 3801 C3C6 2150 197A 5888 235F ACAC https://elephly.net