Skip to content

Commit

Permalink
feat: --json to produce json session listing
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Mar 3, 2023
1 parent befe771 commit f79ce9c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 7 additions & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ def _session_completer(
action="store_true",
help="List all available sessions and exit.",
),
_option_set.Option(
"json",
"--json",
group=options.groups["sessions"],
action="store_true",
help="JSON output formatting. Requires list-sessions currently.",
),
_option_set.Option(
"sessions",
"-s",
Expand Down
29 changes: 27 additions & 2 deletions nox/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ def filter_manifest(manifest: Manifest, global_config: Namespace) -> Manifest |
_produce_listing(manifest, global_config)
return 0

# JSON output requires list sessions also be specified
if global_config.json and not global_config.list_sessions:
logger.error("Must specify --list-sessions with --json")
return 3

# Filter by python interpreter versions.
if global_config.pythons:
manifest.filter_by_python_interpreter(global_config.pythons)
Expand Down Expand Up @@ -264,6 +269,23 @@ def _produce_listing(manifest: Manifest, global_config: Namespace) -> None:
)


def _produce_json_listing(manifest: Manifest, global_config: Namespace) -> None:
report = []
for session, selected in manifest.list_all_sessions():
if selected:
report.append(
{
"session": session.friendly_name,
"name": session.name,
"description": session.description or "",
"python": session.func.python,
"tags": session.tags,
"call_spec": getattr(session.func, "call_spec", {}),
}
)
print(json.dumps(report))


def honor_list_request(manifest: Manifest, global_config: Namespace) -> Manifest | int:
"""If --list was passed, simply list the manifest and exit cleanly.
Expand All @@ -275,10 +297,13 @@ def honor_list_request(manifest: Manifest, global_config: Namespace) -> Manifest
Union[~.Manifest,int]: ``0`` if a listing is all that is requested,
the manifest otherwise (to be sent to the next task).
"""
if not global_config.list_sessions:
if not (global_config.list_sessions or global_config.json):
return manifest

_produce_listing(manifest, global_config)
if global_config.json:
_produce_json_listing(manifest, global_config)
else:
_produce_listing(manifest, global_config)

return 0

Expand Down

0 comments on commit f79ce9c

Please sign in to comment.