Skip to content

Commit

Permalink
Merge pull request #9987 from alex/patch-1
Browse files Browse the repository at this point in the history
Include rustc version in the user agent, if rustc is available
  • Loading branch information
dstufft authored May 21, 2021
2 parents e6414d6 + 9ab448d commit 31482d5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions news/9987.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Include ``rustc`` version in pip's ``User-Agent``, when the system has ``rustc``.
17 changes: 17 additions & 0 deletions src/pip/_internal/network/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import mimetypes
import os
import platform
import shutil
import subprocess
import sys
import urllib.parse
import warnings
Expand Down Expand Up @@ -163,6 +165,21 @@ def user_agent():
if setuptools_dist is not None:
data["setuptools_version"] = str(setuptools_dist.version)

if shutil.which("rustc") is not None:
# If for any reason `rustc --version` fails, silently ignore it
try:
rustc_output = subprocess.check_output(
["rustc", "--version"], stderr=subprocess.STDOUT, timeout=.5
)
except Exception:
pass
else:
if rustc_output.startswith(b"rustc "):
# The format of `rustc --version` is:
# `b'rustc 1.52.1 (9bc8c42bb 2021-05-09)\n'`
# We extract just the middle (1.52.1) part
data["rustc_version"] = rustc_output.split(b" ")[1].decode()

# Use None rather than False so as not to give the impression that
# pip knows it is not being run under CI. Rather, it is a null or
# inconclusive result. Also, we include some value rather than no
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/test_req_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,16 @@ def make_var(name):
with open(tmpdir.joinpath('req1.txt'), 'w') as fp:
fp.write(template.format(*map(make_var, env_vars)))

# Construct the session outside the monkey-patch, since it access the
# env
session = PipSession()
with patch('pip._internal.req.req_file.os.getenv') as getenv:
getenv.side_effect = lambda n: env_vars[n]

reqs = list(parse_reqfile(
tmpdir.joinpath('req1.txt'),
finder=finder,
session=PipSession()
session=session
))

assert len(reqs) == 1, \
Expand All @@ -623,13 +626,16 @@ def test_expand_missing_env_variables(self, tmpdir, finder):
with open(tmpdir.joinpath('req1.txt'), 'w') as fp:
fp.write(req_url)

# Construct the session outside the monkey-patch, since it access the
# env
session = PipSession()
with patch('pip._internal.req.req_file.os.getenv') as getenv:
getenv.return_value = ''

reqs = list(parse_reqfile(
tmpdir.joinpath('req1.txt'),
finder=finder,
session=PipSession()
session=session
))

assert len(reqs) == 1, \
Expand Down

0 comments on commit 31482d5

Please sign in to comment.