Skip to content

pip: entry_point: Add support for exit codes - #550

Merged
hrfuller merged 2 commits into
bazel-contrib:mainfrom
gibfahn:entry-point-return
Nov 10, 2021
Merged

pip: entry_point: Add support for exit codes#550
hrfuller merged 2 commits into
bazel-contrib:mainfrom
gibfahn:entry-point-return

Conversation

@gibfahn

@gibfahn gibfahn commented Oct 13, 2021

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

  • Does not include precompiled binaries, eg. .par files. See CONTRIBUTING.md for info
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature (please, look at the "Scope of the project" section in the README.md file)
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Entry point used via the entry_point helper, if a function returning a int, the return int does not become the program return code.

What is the new behavior?

Entry points are expected to return an int and this int is the status code.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I tried looking for the code in setuptool that generates the shim but did not find it proper. The closest I found was setuptools/command/easy_install.py.

My use case is the sphinx-build entry point that maps to the following code:

def main(argv: List[str] = sys.argv[1:]) -> int:
    # ...

Using pipenv install the following shim gets generated:

#!SOMELOCATION/python
# -*- coding: utf-8 -*-
import re
import sys
from sphinx.cmd.build import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

This does forward the error to sys.exit. In the case of rules_python, I am not sure we need the regex bit, but I do know.


Commits (oldest to newest)

bc30a71 pip: entry_point: Add support for exit codes


4a044b5 Add test for entrypoint exit codes


cbe7c1a Update tests for entrypoint exit codes


@google-cla google-cla Bot added the cla: yes label Oct 13, 2021
@gibfahn
gibfahn force-pushed the entry-point-return branch 2 times, most recently from d818b19 to 45ce4d0 Compare October 14, 2021 11:27
@google-cla

google-cla Bot commented Oct 14, 2021

Copy link
Copy Markdown

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors. If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment @googlebot I fixed it.. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ Googlers: Go here for more info.

@google-cla google-cla Bot added cla: no and removed cla: yes labels Oct 14, 2021
@gibfahn
gibfahn force-pushed the entry-point-return branch from 45ce4d0 to cbe7c1a Compare October 14, 2021 11:29
@google-cla google-cla Bot added cla: yes and removed cla: no labels Oct 14, 2021
@UebelAndre

Copy link
Copy Markdown
Contributor

This seems exciting! Any updates here?

@hrfuller hrfuller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall seems like a reasonable change. There is some fit and finish to do before merging.

Comment thread examples/legacy_pip_import/WORKSPACE Outdated
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
local_repository(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't commit this. The integration test runner replaces the url here with a file:// url to a tar of the current version.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, thanks, reverted.

Comment thread examples/pip_install/BUILD Outdated

# For pip dependencies which have entry points, the `entry_point` macro can be
# used from the generated `pip_install` repository to access a runnable binary.
# used from the generated `pip_parse` repository to access a runnable binary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont think this should change here. This is pip_install/BUILD

Comment thread examples/pip_install/WORKSPACE Outdated
)

http_archive(
local_repository(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread examples/pip_parse/BUILD Outdated
)

# Test the use of all pip_parse utilities in a single py_test
# Test the use of all pip_install utilities in a single py_test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other comment changing the rule name seem wrong. Am I missing something?

Comment thread examples/pip_parse/WORKSPACE Outdated
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
local_repository(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment thread examples/py_import/WORKSPACE Outdated
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
local_repository(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

if __name__ == "__main__":
from {module} import {method}
{method}()
sys.exit({method}())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how often the convention of returning a numerical exit code holds for scripts in PyPI.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gibfahn What I mean is that we should actually check that this is an int / or at least coercible to an int before calling sys.exit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed replying to this one. Was going to say I considered scanning pypi for packages that return an int, but there didn't seem to be a straightforward way of checking that without some fancy mypy style magic 😁

What I mean is that we should actually check that this is an int / or at least coercible to an int before calling sys.exit.

Yeah okay, let me take a look.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looking at other similar things, existing Python tooling does not check. I am not sure we can check without a lot of code additions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it just is_integer as a runtime check?

>>> 1.5.is_integer()
False
>>> 1.0.is_integer()
True
>>> 1.4142135623730951.is_integer()
False

it's much better to exit 0 then to throw, if some module has a non-int main return value. And someone surely does

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, added that check, please take a look.

Pip doesn't seem to do this, so I'd say chances are probably low that it's in use, but better safe than sorry.

@gibfahn
gibfahn force-pushed the entry-point-return branch from cbe7c1a to 7284876 Compare October 20, 2021 19:52
Comment thread examples/pip_install/BUILD Outdated

alias(
name = "yamllint",
# If `pkg` and `script` are the same, passing a single string to

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is confusing, there's only one thing below.
do you mean "since pkg and script are the same"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this is wrong, fixed.

Comment thread examples/pip_parse/BUILD Outdated

alias(
name = "yamllint",
# If `pkg` and `script` are the same, passing a single string to

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, comment doesn't align with code

if __name__ == "__main__":
from {module} import {method}
{method}()
sys.exit({method}())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it just is_integer as a runtime check?

>>> 1.5.is_integer()
False
>>> 1.0.is_integer()
True
>>> 1.4142135623730951.is_integer()
False

it's much better to exit 0 then to throw, if some module has a non-int main return value. And someone surely does

@gibfahn
gibfahn force-pushed the entry-point-return branch from 7284876 to ed55f1e Compare November 5, 2021 21:52

@hrfuller hrfuller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes. LGTM.

@hrfuller
hrfuller merged commit 431caac into bazel-contrib:main Nov 10, 2021
@gibfahn
gibfahn deleted the entry-point-return branch November 11, 2021 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants