Shebang lines on Jython

View: New views
3 Messages — Rating Filter:   Alert me  

Shebang lines on Jython

by Philip Jenvey-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Speaking of shebang lines, there's problems with setuptools shebang  
lines on Jython. Unfortunately it's going to require another patch or  
two to setuptools.

The problem being that Jython's executable is a .sh or .bat file  
(that'll invoke java), and interpreters in shebang lines can't be  
interpreter files, i.e. another script. Running a .py script that  
shebangs the Jython .sh executable directly will result in something  
along the lines of a "test.py: line 2 import command not found" sh  
error.

For posix, we'll just need to special case Jython on posix to make a  
shebang along the lines of:

#!/usr/bin/env /opt/local/bin/jython

Windows isn't as easy. setuptools' launcher.c similarly can't use  
a .bat file as the interpreter. A workaround would be to use cmd.exe /
c in the same way we'll use /usr/bin/env on posix. launcher.c  
currently can't handle this, though, the problem being it can't  
correctly quote the cmd.exe /c line correctly.

With:

#!c:\windows\system32\cmd.exe /c c:\jython2.2\jython.bat

launcher.c ends up calling:

"c:\windows\system32\cmd.exe" "/c" "c:\jython2.2\jython.bat" "c:
\scripts\myscript-script.py"

when we need:

"c:\windows\system32\cmd.exe" /c ""c:\jython2.2\jython.bat" "c:
\scripts\myscript-script.py""

which is another set of quotes around the entire /c arg, and yes no  
quotes around /c (I have no idea why "/c" doesn't work).

I guess we would have to patch launcher.c to deal with this special  
situation or make a custom version for Jython. That's pretty awful --  
might anyone have a simpler way of handling this?

--
Philip Jenvey


_______________________________________________
Distutils-SIG maillist  -  Distutils-SIG@...
http://mail.python.org/mailman/listinfo/distutils-sig

Re: Shebang lines on Jython

by P.J. Eby :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

At 11:00 AM 4/16/2008 -0700, Philip Jenvey wrote:

>Speaking of shebang lines, there's problems with setuptools shebang
>lines on Jython. Unfortunately it's going to require another patch or
>two to setuptools.
>
>The problem being that Jython's executable is a .sh or .bat file
>(that'll invoke java), and interpreters in shebang lines can't be
>interpreter files, i.e. another script. Running a .py script that
>shebangs the Jython .sh executable directly will result in something
>along the lines of a "test.py: line 2 import command not found" sh
>error.
>
>For posix, we'll just need to special case Jython on posix to make a
>shebang along the lines of:
>
>#!/usr/bin/env /opt/local/bin/jython
>
>Windows isn't as easy. setuptools' launcher.c similarly can't use
>a .bat file as the interpreter. A workaround would be to use cmd.exe /
>c in the same way we'll use /usr/bin/env on posix. launcher.c
>currently can't handle this, though, the problem being it can't
>correctly quote the cmd.exe /c line correctly.
>
>With:
>
>#!c:\windows\system32\cmd.exe /c c:\jython2.2\jython.bat
>
>launcher.c ends up calling:
>
>"c:\windows\system32\cmd.exe" "/c" "c:\jython2.2\jython.bat" "c:
>\scripts\myscript-script.py"
>
>when we need:
>
>"c:\windows\system32\cmd.exe" /c ""c:\jython2.2\jython.bat" "c:
>\scripts\myscript-script.py""
>
>which is another set of quotes around the entire /c arg, and yes no
>quotes around /c (I have no idea why "/c" doesn't work).
>
>I guess we would have to patch launcher.c to deal with this special
>situation or make a custom version for Jython. That's pretty awful --
>might anyone have a simpler way of handling this?

Make a jython.exe that either runs the batch file, or does the same
things itself.  As you can see from launcher.c, it's not that hard to
make one, and trivial to build it with the cygwin/MinGW tools.  And
then make jython's sys.executable on Windows be that Jython.exe.

You can pretty much do the same thing for other platforms.  Yes, I
know it means you'll have to compile something, but it beats the
living tar out of trying to handle parameters and options in shebang
lines.  Have a look at the chart on this page:

   http://www.in-ulm.de/~mascheck/various/shebang/

for some horror stories.

_______________________________________________
Distutils-SIG maillist  -  Distutils-SIG@...
http://mail.python.org/mailman/listinfo/distutils-sig

Re: Shebang lines on Jython

by Philip Jenvey-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Apr 16, 2008, at 11:39 AM, Phillip J. Eby wrote:

> Make a jython.exe that either runs the batch file, or does the same
> things itself.  As you can see from launcher.c, it's not that hard to
> make one, and trivial to build it with the cygwin/MinGW tools.  And
> then make jython's sys.executable on Windows be that Jython.exe.
>
> You can pretty much do the same thing for other platforms.  Yes, I
> know it means you'll have to compile something, but it beats the
> living tar out of trying to handle parameters and options in shebang
> lines.  Have a look at the chart on this page:
>
>    http://www.in-ulm.de/~mascheck/various/shebang/
>
> for some horror stories.

Yea I'm trying not to worry about the arg parsing evilness on posix.  
Since it's not too common to pass an argument to python in the  
shebang line anyway, without going out of our way to support that we  
could get away with #!/usr/bin/env /opt/local/bin/jython

Your point definitely applies to our Windows situation, though,  
thanks. Maybe we could ship a windows .exe and just keep posix the  
way it is for now. In the future maybe we could have our installer  
attempt to compile a runner on posix if a C compiler is installed,  
otherwise fallback to the .sh script, but I'm not going to deal with  
that at the moment.

With that I'll have to create a patch to setuptools to use #!/usr/bin/
env if on jython and open(sys.executable).read(2) == '#!' and not  
sys.executable.endswith('.bat') (or some kind of windows check).

--
Philip Jenvey


_______________________________________________
Distutils-SIG maillist  -  Distutils-SIG@...
http://mail.python.org/mailman/listinfo/distutils-sig