From mboxrd@z Thu Jan 1 00:00:00 1970 From: Hartmut Goebel Subject: Re: avoid wrapper scripts when possible Date: Sun, 5 Nov 2017 12:00:17 +0100 Message-ID: <65968230-bd78-dfe7-2767-c1b7b89ad748@crazy-compilers.com> References: <87mv44gyfv.fsf@elephly.net> <9ccb6232-ded3-9399-9314-91644db7d89e@crazy-compilers.com> <87tvy9ptge.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]:47787) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eBIfE-00074x-KR for guix-devel@gnu.org; Sun, 05 Nov 2017 06:00:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eBIfA-0000hg-JH for guix-devel@gnu.org; Sun, 05 Nov 2017 06:00:28 -0500 Received: from mail-out.m-online.net ([212.18.0.10]:45243) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eBIfA-0000fS-7p for guix-devel@gnu.org; Sun, 05 Nov 2017 06:00:24 -0500 In-Reply-To: <87tvy9ptge.fsf@elephly.net> Content-Language: de-DE 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 Hi, answering on a mail which Ricardo CCed to me and which did not make it to the list yet. Thus I full-quote. Am 04.11.2017 um 23:30 schrieb Ricardo Wurmus: > 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:= " (getenv "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? I testes this and (beside some necessary adjustments) this works. Indeed this a very elegant hack :-). My respect! Compared to my idea this is more elegant since it does not require a separate file.=C2=A0 Some fine-tuning needs to be done, though: 1) The setenv-line should only append the existing env-var, if this was set. (At least in Python, an empty PYTHONPATH part is interpreted as "current directory", thus adding an empty part would change the desired behavior.) 2) Arguments need to be passed to the script, too. Since I was eager to learn how this can be done, here is my draft fixing these two issues: --8<---------------cut here---------------start------------->8--- #!/bin/guile --no-auto-compile #!#; Guix wrapping header #\- (define (append-env name path) (let ((var (getenv name))) (setenv name (if var (string-append path ":" var) path)))) #\- (append-env "PYTHONPATH" "/gnu/store/foo:/gnu/store/bar") #\- (apply execl "/bin/python3" (car (command-line)) (command-line)) #!/home/rekado/.guix-profile/bin/python3 print("This is the real program") import sys, os print("sys.path[:4]", sys.path[:4]) print("argumentes:", sys.argv) print("ppid", os.getppid()) --8<---------------cut here---------------end--------------->8--- --=20 Regards Hartmut Goebel | Hartmut Goebel | h.goebel@crazy-compilers.com | | www.crazy-compilers.com | compilers which you thought are impossible |