Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: long-description on Windows #2168

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
revert changes to setup.py
  • Loading branch information
giampaolo authored Nov 10, 2022
commit 2b3a728f141e27a40f54e8814fbe336c1617f0a0
91 changes: 52 additions & 39 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def get_version():
VERSION = get_version()
macros.append(('PSUTIL_VERSION', int(VERSION.replace('.', ''))))

# Py_LIMITED_API lets us create a single wheel which works with multiple
# python versions, including unreleased ones.
if bdist_wheel and CP36_PLUS and (MACOS or LINUX or WINDOWS):
py_limited_api = {"py_limited_api": True}
macros.append(('Py_LIMITED_API', '0x03060000'))
Expand All @@ -119,11 +121,12 @@ def get_version():
def get_description():
script = os.path.join(HERE, "scripts", "internal", "convert_readme.py")
readme = os.path.join(HERE, 'README.rst')
temp = os.path.join(HERE, 'README.rst.tmp')
subprocess.check_call([sys.executable, script, readme, temp])
with open(temp) as f:
data = f.read()
os.unlink(temp)
p = subprocess.Popen([sys.executable, script, readme],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise RuntimeError(stderr)
data = stdout.decode('utf8')
if WINDOWS:
data = data.replace('\r\n', '\n')
return data
Expand All @@ -146,11 +149,41 @@ def write(self, s):
setattr(sys, stream_name, orig)


def missdeps(msg):
s = hilite("C compiler or Python headers are not installed ", color="red")
s += hilite("on this system. Try to run:\n", color="red")
s += hilite(msg, color="red", bold=True)
print(s, file=sys.stderr)
def missdeps(cmdline):
s = "psutil could not be installed from sources"
if not SUNOS and not which("gcc"):
s += " because gcc is not installed. "
else:
s += ". Perhaps Python header files are not installed. "
s += "Try running:\n"
s += " %s" % cmdline
print(hilite(s, color="red", bold=True), file=sys.stderr)


def unix_can_compile(c_code):
from distutils.errors import CompileError
from distutils.unixccompiler import UnixCCompiler

with tempfile.NamedTemporaryFile(
suffix='.c', delete=False, mode="wt") as f:
f.write(c_code)

tempdir = tempfile.mkdtemp()
try:
compiler = UnixCCompiler()
# https://github.com/giampaolo/psutil/pull/1568
if os.getenv('CC'):
compiler.set_executable('compiler_so', os.getenv('CC'))
with silenced_output('stderr'):
with silenced_output('stdout'):
compiler.compile([f.name], output_dir=tempdir)
except CompileError:
return False
else:
return True
finally:
os.remove(f.name)
shutil.rmtree(tempdir)


if WINDOWS:
Expand Down Expand Up @@ -262,36 +295,11 @@ def get_winver():
**py_limited_api)

elif LINUX:
def get_ethtool_macro():
# see: https://github.com/giampaolo/psutil/issues/659
from distutils.errors import CompileError
from distutils.unixccompiler import UnixCCompiler

with tempfile.NamedTemporaryFile(
suffix='.c', delete=False, mode="wt") as f:
f.write("#include <linux/ethtool.h>")

output_dir = tempfile.mkdtemp()
try:
compiler = UnixCCompiler()
# https://github.com/giampaolo/psutil/pull/1568
if os.getenv('CC'):
compiler.set_executable('compiler_so', os.getenv('CC'))
with silenced_output('stderr'):
with silenced_output('stdout'):
compiler.compile([f.name], output_dir=output_dir)
except CompileError:
return ("PSUTIL_ETHTOOL_MISSING_TYPES", 1)
else:
return None
finally:
os.remove(f.name)
shutil.rmtree(output_dir)
# see: https://github.com/giampaolo/psutil/issues/659
if not unix_can_compile("#include <linux/ethtool.h>"):
macros.append(("PSUTIL_ETHTOOL_MISSING_TYPES", 1))

macros.append(("PSUTIL_LINUX", 1))
ETHTOOL_MACRO = get_ethtool_macro()
if ETHTOOL_MACRO is not None:
macros.append(ETHTOOL_MACRO)
ext = Extension(
'psutil._psutil_linux',
sources=sources + ['psutil/_psutil_linux.c'],
Expand Down Expand Up @@ -454,13 +462,18 @@ def main():
setup(**kwargs)
success = True
finally:
if not success and POSIX and not which('gcc'):
cmd = sys.argv[1] if len(sys.argv) >= 2 else ''
if not success and POSIX and \
cmd.startswith(("build", "install", "sdist", "bdist",
"develop")):
py3 = "3" if PY3 else ""
if LINUX:
if which('dpkg'):
missdeps("sudo apt-get install gcc python%s-dev" % py3)
elif which('rpm'):
missdeps("sudo yum install gcc python%s-devel" % py3)
elif which('apk'):
missdeps("sudo apk add gcc python%s-dev" % py3)
elif MACOS:
print(hilite("XCode (https://developer.apple.com/xcode/) "
"is not installed"), color="red", file=sys.stderr)
Expand Down