sapling/eden/scm/contrib/pick_python.py
Jun Wu 2a99866d7b codemod: update license headers
Summary:
The "Portions" license cannot be updated automatically. So this is a manual
update using:

  sd -s 'Portions Copyright (c) Facebook, Inc. and its affiliates.' 'Portions Copyright (c) Meta Platforms, Inc. and affiliates.' `rg -l Facebook`
  sd -s 'Copyright (c) Facebook, Inc. and its affiliates.' 'Copyright (c) Meta Platforms, Inc. and affiliates.' `rg -l Facebook`

Differential Revision: D33420114

fbshipit-source-id: 49ae00a7b62e3b8cc6c5dd839b3c104a75e72a56
2022-01-05 14:43:32 -08:00

76 lines
2.2 KiB
Python

# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2.
"""
Pick a Python that is more likely to be able to build with setup.py.
Arguments: Binary name (ex. python2, or python3).
Stdout: Full path to a system python, or the first argument of the input.
This script does not need to be executed using a system Python.
"""
import ast
import os
import subprocess
import sys
def main(args):
dirs = os.environ.get("PATH").split(os.pathsep)
names = args or ["python3"]
if names == ["python3"]:
# Try different pythons
names = ["python3.8", "python3.7", "python3.6"] + names
for name in names:
for dir in dirs:
path = os.path.join(dir, name)
if does_python_look_good(path):
print(path)
return
# Fallback
sys.stderr.write("warning: cannot find a proper Python\n")
sys.stdout.write(names[0])
def does_python_look_good(path):
if not os.path.isfile(path):
return False
try:
cfg = ast.literal_eval(
subprocess.check_output(
[path, "-c", "import sysconfig;print(sysconfig.get_config_vars())"]
).decode("utf-8")
)
cflags = cfg["CFLAGS"]
if "-nostdinc" in cflags.split():
sys.stderr.write("%s: ignored, lack of C stdlib\n" % path)
return False
includepy = cfg["INCLUDEPY"]
if not os.path.exists(os.path.join(includepy, "Python.h")):
sys.stderr.write(
"%s: ignored, missing Python.h in %s\n" % (path, includepy)
)
return False
realpath = subprocess.check_output(
[path, "-c", "import sys;print(sys.executable)"]
)
if b"fbprojects" in realpath:
sys.stderr.write(
"%s: ignored, avoid using the fb python for non-fb builds\n" % path
)
return False
return True
except Exception:
return False
if __name__ == "__main__":
code = main(sys.argv[1:]) or 0
sys.stderr.flush()
sys.stdout.flush()
sys.exit(code)