From 3432824c4653029fcc23c679bda9668353e5cbce Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 16 May 2021 21:19:15 -0400 Subject: [PATCH] Include rustc version in the user agent, if rustc is available Rust is becoming more popular for writing Python extension modules in, this information would be valuable for package maintainers to assess the ecosystem, in the same way glibc or openssl version is. --- src/pip/_internal/network/session.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/pip/_internal/network/session.py b/src/pip/_internal/network/session.py index 4af800f12fe..2e1d539d1a0 100644 --- a/src/pip/_internal/network/session.py +++ b/src/pip/_internal/network/session.py @@ -19,6 +19,8 @@ import mimetypes import os import platform +import shutil +import subprocess import sys import urllib.parse import warnings @@ -163,6 +165,18 @@ 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"]) + except Exception: + pass + else: + # 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