guix-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: avoid wrapper scripts when possible


From: Hartmut Goebel
Subject: Re: avoid wrapper scripts when possible
Date: Sun, 5 Nov 2017 12:00:17 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0

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. 

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---

-- 
Regards
Hartmut Goebel

| Hartmut Goebel          | address@hidden               |
| www.crazy-compilers.com | compilers which you thought are impossible |





reply via email to

[Prev in Thread] Current Thread [Next in Thread]