Skip to content

Commit

Permalink
Allow adding a custom string to pip's User-Agent via an environment v…
Browse files Browse the repository at this point in the history
…ariable (#5550)
  • Loading branch information
conspicuousClockwork authored and cjerdonek committed Mar 31, 2019
1 parent 2cdca64 commit ac9010e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 0 deletions.
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

0 comments on commit ac9010e

Please sign in to comment.