Skip to content

Commit

Permalink
Append "cwltool" to HTTP User-Agent string (#1977)
Browse files Browse the repository at this point in the history
Also appends the program name, if not "cwltool"

Should work for calrissian, reana-cwl-runner, and arvados-cwl-runner, but not
toil-cwl-runner which doesn't call the cwltool.main.main() function.

Probably works for streamflow, but I haven't tested that.

---------

Co-authored-by: Michael R. Crusoe <1330696+mr-c@users.noreply.github.com>
  • Loading branch information
svonworl and mr-c committed Feb 28, 2024
1 parent 814d6d1 commit ca16314
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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()

0 comments on commit ca16314

Please sign in to comment.