Skip to content
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

Allow adding a custom string to pip's User-Agent via an environment variable #5550

Merged
merged 12 commits into from
Mar 31, 2019
Merged
2 changes: 2 additions & 0 deletions docs/html/user_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pip can be configured to connect through a proxy server in various ways:
* using ``proxy`` in a :ref:`config-file`
* by setting the standard environment-variables ``http_proxy``, ``https_proxy``
and ``no_proxy``.
* using the environment variable ``PIP_USER_AGENT_USER_DATA`` to include
a JSON-encoded string in the user-agent variable used in pip's requests.


.. _`Requirements Files`:
Expand Down
2 changes: 2 additions & 0 deletions news/5549.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A custom (JSON-encoded) string can now be added to pip's User-Agent
using the ``PIP_USER_AGENT_USER_DATA`` environment variable.
4 changes: 4 additions & 0 deletions src/pip/_internal/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def user_agent():
# value to make it easier to know that the check has been run.
data["ci"] = True if looks_like_ci() else None

user_data = os.environ.get("PIP_USER_AGENT_USER_DATA")
if user_data is not None:
data["user_data"] = user_data

return "{data[installer][name]}/{data[installer][version]} {json}".format(
data=data,
json=json.dumps(data, separators=(",", ":"), sort_keys=True),
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ def test_user_agent__ci(monkeypatch, name, expected_like_ci):
assert ('"ci":null' in user_agent) == (not expected_like_ci)


def test_user_agent_user_data(monkeypatch):
monkeypatch.setenv("PIP_USER_AGENT_USER_DATA", "some_string")
assert "some_string" in PipSession().headers["User-Agent"]


class FakeStream(object):

def __init__(self, contents):
Expand Down