Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dh-virtualenv does not fix shebangs by the latest pip/distlib #225

Closed
ghost opened this issue Apr 11, 2018 · 8 comments
Closed

dh-virtualenv does not fix shebangs by the latest pip/distlib #225

ghost opened this issue Apr 11, 2018 · 8 comments

Comments

@ghost
Copy link

ghost commented Apr 11, 2018

With the latest pip, on "posix" systems, with shebang paths longer than 127 characters, pip generates a multiline shebang. dh-virtualenv is not aware of such paths and they keep their build-time value.

New shebang style example:

#!/bin/sh
'''exec' /build/ourpackage/debian/ourpackage-venv3/opt/venvs/ourpackage-venv3/bin/python "$0" "$@"
' '''

@jhermann
Copy link
Contributor

Should be fixed before 30 Apr – the pypi.org final switch date which implies using a recent pip.

@nailor
Copy link
Collaborator

nailor commented Apr 13, 2018

Thanks for flagging this! Do you happen to have a pointer to relevant parts of distlib/pip where this happens? I'd like to dig a bit and understand more

@ghost
Copy link
Author

ghost commented Apr 13, 2018

Right here: https://github.com/pypa/pip/blob/0007825733aa862e76a66e5d1cfee121c29192ff/src/pip/_vendor/distlib/scripts.py#L139

It took me a while to find out what was causing this in the first place.

@nailor
Copy link
Collaborator

nailor commented Apr 13, 2018

Thanks!

By the way, have to say this is a pretty damn neat hack on pip-side! Took a moment to grasp for me, but basically the flow is:

  1. Count how long shebang is.
  2. If less than limit (Linux limit 127), use the old style. If longer, cook up new one
  3. Use /bin/sh as shebang. Make first 'executable' line the '''exec' ....

Now when executing the script with the long shebang, it'll be executed by your regular interpreter. Quotes don't matter (this took while for me to figure out), as the two first single quotes are just empty string (you can try this by executing ''ls in your terminal). The third single quote just quotes the exec (as you can see there's quote after that too). Also doesn't matter: try '''ls'. Then it just calls Python with first argument being $0, i.e. the script itself, followed by positional arguments passed in. Next line is just first single quotes, i.e. does not matter at all for sh.

This leads it to executing the same script again with the virtualenv's python interpreter, except this time the exec statement is actually just a random string in triple quotes and thus skipped! Mind. Blown.

clapping

@nailor
Copy link
Collaborator

nailor commented Apr 13, 2018

Side note: This trick would be neat to replicate in dh-virtualenv if we detect too long shebangs!

nailor pushed a commit that referenced this issue Apr 18, 2018
Note: this is mainly working tests & code. No build tests yet.

Related to issue #225
@nailor
Copy link
Collaborator

nailor commented Apr 18, 2018

I added an initial PR, which still needs some test builds, to fix these shebangs

@ghost
Copy link
Author

ghost commented Apr 18, 2018

@nailor Your pull request works correctly and produces corrected shebangs in my setup.

Thanks !

@nailor
Copy link
Collaborator

nailor commented Jun 12, 2018

This is now fixed in the master, closing the ticket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants