-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from artichoke/lopopolo/packaging
Improve Python packaging, enable some code reuses
- Loading branch information
Showing
8 changed files
with
107 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import os | ||
from collections.abc import Iterator | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
|
||
|
||
def set_output(*, name: str, value: str) -> None: | ||
""" | ||
Set an output for a GitHub Actions job. | ||
https://docs.github.com/en/actions/using-jobs/defining-outputs-for-jobs | ||
https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ | ||
""" | ||
|
||
if github_output := os.getenv("GITHUB_OUTPUT"): | ||
with Path(github_output).open("a") as out: | ||
print(f"{name}={value}", file=out) | ||
|
||
|
||
@contextmanager | ||
def log_group(group: str) -> Iterator[None]: | ||
""" | ||
Create an expandable log group in GitHub Actions job logs. | ||
https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines | ||
""" | ||
|
||
print(f"::group::{group}") | ||
try: | ||
yield | ||
finally: | ||
print("::endgroup::") | ||
|
||
|
||
def emit_metadata() -> None: | ||
if os.getenv("CI") != "true": | ||
return | ||
with log_group("Workflow metadata"): | ||
if repository := os.getenv("GITHUB_REPOSITORY"): | ||
print(f"GitHub Repository: {repository}") | ||
if actor := os.getenv("GITHUB_ACTOR"): | ||
print(f"GitHub Actor: {actor}") | ||
if workflow := os.getenv("GITHUB_WORKFLOW"): | ||
print(f"GitHub Workflow: {workflow}") | ||
if job := os.getenv("GITHUB_JOB"): | ||
print(f"GitHub Job: {job}") | ||
if run_id := os.getenv("GITHUB_RUN_ID"): | ||
print(f"GitHub Run ID: {run_id}") | ||
if ref := os.getenv("GITHUB_REF"): | ||
print(f"GitHub Ref: {ref}") | ||
if ref_name := os.getenv("GITHUB_REF_NAME"): | ||
print(f"GitHub Ref Name: {ref_name}") | ||
if sha := os.getenv("GITHUB_SHA"): | ||
print(f"GitHub SHA: {sha}") | ||
|
||
|
||
def runner_tempdir() -> Path | None: | ||
if temp := os.getenv("RUNNER_TEMP"): | ||
return Path(temp) | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import subprocess | ||
|
||
import stamina | ||
|
||
|
||
@stamina.retry(on=subprocess.CalledProcessError, attempts=3) | ||
def run_command_with_merged_output(command: list[str]) -> None: | ||
""" | ||
Run the given command as a subprocess and merge its stdout and stderr | ||
streams. This function will retry the given command on any error, up to 3 | ||
times. | ||
This is useful for funnelling all output of a command into a GitHub Actions | ||
log group. | ||
This command uses `check=True` when delegating to `subprocess`. | ||
""" | ||
|
||
proc = subprocess.run( | ||
command, | ||
check=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT, | ||
text=True, | ||
) | ||
|
||
for line in proc.stdout.splitlines(): | ||
if line: | ||
print(line) |