-
-
Notifications
You must be signed in to change notification settings - Fork 610
feat(rules): add main_module attribute to run a module name (python -m) #2671
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,6 +126,24 @@ Optional; the name of the source file that is the main entry point of the | |
application. This file must also be listed in `srcs`. If left unspecified, | ||
`name`, with `.py` appended, is used instead. If `name` does not match any | ||
filename in `srcs`, `main` must be specified. | ||
|
||
This is mutually exclusive with {obj}`main_module`. | ||
""", | ||
), | ||
"main_module": lambda: attrb.String( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: what about That said, then we would have worse error messages, so this solution is also good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not because it's not clear how to transform the file name into a module name. |
||
doc = """ | ||
Module name to execute as the main program. | ||
|
||
When set, `srcs` is not required, and it is assumed the module is | ||
provided by a dependency. | ||
|
||
See https://docs.python.org/3/using/cmdline.html#cmdoption-m for more | ||
information about running modules as the main program. | ||
|
||
This is mutually exclusive with {obj}`main`. | ||
|
||
:::{versionadded} VERSION_NEXT_FEATURE | ||
::: | ||
""", | ||
), | ||
"pyc_collection": lambda: attrb.String( | ||
|
@@ -638,14 +656,19 @@ def _create_stage2_bootstrap( | |
|
||
template = runtime.stage2_bootstrap_template | ||
|
||
if main_py: | ||
main_py_path = "{}/{}".format(ctx.workspace_name, main_py.short_path) | ||
else: | ||
main_py_path = "" | ||
ctx.actions.expand_template( | ||
template = template, | ||
output = output, | ||
substitutions = { | ||
"%coverage_tool%": _get_coverage_tool_runfiles_path(ctx, runtime), | ||
"%import_all%": "True" if ctx.fragments.bazel_py.python_import_all_repositories else "False", | ||
"%imports%": ":".join(imports.to_list()), | ||
"%main%": "{}/{}".format(ctx.workspace_name, main_py.short_path), | ||
"%main%": main_py_path, | ||
"%main_module%": ctx.attr.main_module, | ||
"%target%": str(ctx.label), | ||
"%workspace_name%": ctx.workspace_name, | ||
}, | ||
|
@@ -929,7 +952,10 @@ def py_executable_base_impl(ctx, *, semantics, is_test, inherited_environment = | |
""" | ||
_validate_executable(ctx) | ||
|
||
main_py = determine_main(ctx) | ||
if not ctx.attr.main_module: | ||
main_py = determine_main(ctx) | ||
else: | ||
main_py = None | ||
direct_sources = filter_to_py_srcs(ctx.files.srcs) | ||
precompile_result = semantics.maybe_precompile(ctx, direct_sources) | ||
|
||
|
@@ -1049,6 +1075,12 @@ def _validate_executable(ctx): | |
if ctx.attr.python_version == "PY2": | ||
fail("It is not allowed to use Python 2") | ||
|
||
if ctx.attr.main and ctx.attr.main_module: | ||
fail(( | ||
"Only one of main and main_module can be set, got: " + | ||
"main={}, main_module={}" | ||
).format(ctx.attr.main, ctx.attr.main_module)) | ||
|
||
def _declare_executable_file(ctx): | ||
if target_platform_has_any_constraint(ctx, ctx.attr._windows_constraints): | ||
executable = ctx.actions.declare_file(ctx.label.name + ".exe") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
import unittest | ||
|
||
|
||
class MainModuleTest(unittest.TestCase): | ||
def test_run_as_module(self): | ||
self.assertIsNotNone(__spec__, "__spec__ was none") | ||
# If not run as a module, __spec__ is None | ||
self.assertNotEqual(__name__, __spec__.name) | ||
self.assertEqual(__spec__.name, "tests.bootstrap_impls.main_module") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
else: | ||
# Guard against running it as a module in a non-main way. | ||
sys.exit(f"__name__ should be __main__, got {__name__}") |
Uh oh!
There was an error while loading. Please reload this page.