Skip to content

Append "cwltool" to HTTP User-Agent string #1977

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

Merged
merged 15 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cwltool/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import argcomplete
import coloredlogs
import requests
import ruamel.yaml
from ruamel.yaml.comments import CommentedMap, CommentedSeq
from ruamel.yaml.main import YAML
Expand Down Expand Up @@ -173,6 +174,14 @@ def _signal_handler(signum: int, _: Any) -> None:
sys.exit(signum)


def append_word_to_default_user_agent(word: str) -> None:
"""Append the specified word to the requests http user agent string if it's not already there."""
original_function = requests.utils.default_user_agent
suffix = f" {word}"
if not original_function().endswith(suffix):
requests.utils.default_user_agent = lambda *args: original_function(*args) + suffix


def generate_example_input(
inptype: Optional[CWLOutputType],
default: Optional[CWLOutputType],
Expand Down Expand Up @@ -979,6 +988,12 @@ def main(
workflowobj = None
prov_log_handler: Optional[logging.StreamHandler[ProvOut]] = None
global docker_exe

user_agent = "cwltool"
if user_agent not in (progname := os.path.basename(sys.argv[0])):
user_agent += f" {progname}" # append the real program name as well
append_word_to_default_user_agent(user_agent)

try:
if args is None:
if argsl is None:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_user_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests

from cwltool.main import append_word_to_default_user_agent, main


def get_user_agent() -> str:
return requests.utils.default_headers()["User-Agent"]


def test_cwltool_in_user_agent() -> None:
"""python-requests HTTP User-Agent should include the string 'cwltool'."""
try:
assert main(["--version"]) == 0
except SystemExit as err:
assert err.code == 0
assert "cwltool" in get_user_agent()


def test_append_word_to_default_user_agent() -> None:
"""Confirm that append_word_to_default_user_agent works."""
word_to_append = "foobar123"
assert word_to_append not in get_user_agent()
append_word_to_default_user_agent(word_to_append)
assert word_to_append in get_user_agent()