-
-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy path_python.py
More file actions
41 lines (33 loc) · 1.24 KB
/
_python.py
File metadata and controls
41 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Generic helpers for working with a Python executable.
import hashlib
from pyperformance import _pythoninfo
def get_id(python=None, prefix=None, *, short=True):
"""Return a string that uniquely identifies the given Python executable."""
if isinstance(python, str):
python = _pythoninfo.get_info(python)
data = [
# "executable" represents the install location
# (and build, to an extent).
python.sys.executable,
# sys.version encodes version, git info, build_date, and build_tool.
python.sys.version,
python.sys.implementation.name.lower(),
".".join(str(v) for v in python.sys.implementation.version),
str(python.sys.api_version),
python.pyc_magic_number.hex(),
]
# XXX Add git info if a dev build.
h = hashlib.sha256()
for value in data:
h.update(value.encode("utf-8"))
# XXX Also include the sorted output of "python -m pip freeze"?
py_id = h.hexdigest()
if short:
py_id = py_id[:12]
if prefix:
if prefix is True:
major, minor = python.sys.version_info[:2]
py_id = f"{python.sys.implementation.name}{major}.{minor}-{py_id}"
else:
py_id = prefix + py_id
return py_id