From 8e816640b515ce0cd12d2caee3cf1ea7b39834e4 Mon Sep 17 00:00:00 2001 From: Thea Flowers Date: Thu, 17 May 2018 19:06:36 -0700 Subject: [PATCH] Add ability to override pip's user agent with PIP_USER_AGENT_INSTALLER_OVERRIDE --- news/66F69322-5F5D-442A-B2FE-6ABD8D3B8A1F.trivial | 0 src/pip/_internal/download.py | 10 ++++++++-- tests/unit/test_download.py | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 news/66F69322-5F5D-442A-B2FE-6ABD8D3B8A1F.trivial diff --git a/news/66F69322-5F5D-442A-B2FE-6ABD8D3B8A1F.trivial b/news/66F69322-5F5D-442A-B2FE-6ABD8D3B8A1F.trivial new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_internal/download.py b/src/pip/_internal/download.py index dfc558ef403..3c908cda19d 100644 --- a/src/pip/_internal/download.py +++ b/src/pip/_internal/download.py @@ -167,8 +167,14 @@ 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 - return "{data[installer][name]}/{data[installer][version]} {json}".format( - data=data, + installer = os.environ.get("PIP_USER_AGENT_INSTALLER_OVERRIDE") + if not installer: + installer = ( + "{data[installer][name]}/{data[installer][version]}".format( + data=data)) + + return "{installer} {json}".format( + installer=installer, json=json.dumps(data, separators=(",", ":"), sort_keys=True), ) diff --git a/tests/unit/test_download.py b/tests/unit/test_download.py index 4272d7f8d2f..dc1817bb4ad 100644 --- a/tests/unit/test_download.py +++ b/tests/unit/test_download.py @@ -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_override(monkeypatch): + monkeypatch.setenv("PIP_USER_AGENT_INSTALLER_OVERRIDE", "pipenv/1.0.0") + PipSession().headers["User-Agent"].startswith("pipenv/1.0.0") + + class FakeStream(object): def __init__(self, contents):