Merge pull request #353 from cjmayo/setup

Tidy setup.py for C extensions and Python 2
This commit is contained in:
anarcat 2020-04-02 10:10:38 -04:00 committed by GitHub
commit 25d517521c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,7 +20,6 @@ Setup file for the distuils module.
It includes the following features:
- creation and installation of configuration files with installation data
- automatic detection and usage of GNU99 standard for C compiler
- automatic MANIFEST.in check
- automatic generation of .mo locale files
- automatic permission setting on POSIX systems for installed files
@ -28,28 +27,17 @@ It includes the following features:
Because of all the features, this script is nasty and big.
Change it very carefully.
"""
from __future__ import print_function
import sys
if not (hasattr(sys, 'version_info') or
sys.version_info < (3, 5, 0, 'final', 0)):
if sys.version_info < (3, 5, 0, 'final', 0):
raise SystemExit("This program requires Python 3.5 or later.")
import os
import re
import codecs
import subprocess
import stat
import glob
import shutil
try:
unicode
except NameError:
unicode = lambda x: x
# import Distutils stuff
from setuptools import setup
from distutils.core import Extension
from distutils.command.install_lib import install_lib
from distutils.command.build_ext import build_ext
from distutils.command.sdist import sdist
from distutils.command.clean import clean
from distutils.command.install_data import install_data
@ -108,7 +96,7 @@ def get_portable():
return os.environ.get('LINKCHECKER_PORTABLE', '0')
class MyInstallLib (install_lib, object):
class MyInstallLib (install_lib):
"""Custom library installation."""
def install (self):
@ -168,7 +156,7 @@ class MyInstallLib (install_lib, object):
return outs
class MyInstallData (install_data, object):
class MyInstallData (install_data):
"""Fix file permissions."""
def run (self):
@ -218,7 +206,7 @@ class MyInstallData (install_data, object):
os.chmod(path, mode)
class MyDistribution (Distribution, object):
class MyDistribution (Distribution):
"""Custom distribution class generating config file."""
def __init__ (self, attrs):
@ -257,8 +245,6 @@ class MyDistribution (Distribution, object):
for name in metanames:
method = "get_" + name
val = getattr(self.metadata, method)()
if isinstance(val, str):
val = unicode(val)
cmd = "%s = %r" % (name, val)
data.append(cmd)
data.append('release_date = "%s"' % get_release_date())
@ -268,50 +254,6 @@ class MyDistribution (Distribution, object):
"creating %s" % filename, self.verbose >= 1, self.dry_run)
def cc_run (args):
"""Run the C compiler with a simple main program.
@return: successful exit flag
@rtype: bool
"""
prog = b"int main(){}\n"
pipe = subprocess.Popen(args,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
pipe.communicate(input=prog)
if os.WIFEXITED(pipe.returncode):
return os.WEXITSTATUS(pipe.returncode) == 0
return False
def cc_supports_option (cc, option):
"""Check if the given C compiler supports the given option.
@return: True if the compiler supports the option, else False
@rtype: bool
"""
return cc_run([cc[0], "-E", option, "-"])
class MyBuildExt (build_ext, object):
"""Custom build extension command."""
def build_extensions (self):
"""Add -std=gnu99 to build options if supported."""
# For gcc >= 3 we can add -std=gnu99 to get rid of warnings.
extra = []
if self.compiler.compiler_type == 'unix':
option = "-std=gnu99"
if cc_supports_option(self.compiler.compiler, option):
extra.append(option)
# First, sanity-check the 'extensions' list
self.check_extensions_list(self.extensions)
for ext in self.extensions:
for opt in extra:
if opt not in ext.extra_compile_args:
ext.extra_compile_args.append(opt)
self.build_extension(ext)
def list_message_files (package, suffix=".mo"):
"""Return list of all found message files and their installation paths."""
for fname in glob.glob("po/*" + suffix):
@ -343,7 +285,7 @@ def check_manifest ():
print('\nMissing: '.join(err))
class MyBuild (build, object):
class MyBuild (build):
"""Custom build command."""
def run (self):
@ -352,7 +294,7 @@ class MyBuild (build, object):
build.run(self)
class MyClean (clean, object):
class MyClean (clean):
"""Custom clean command."""
def run (self):
@ -367,7 +309,7 @@ class MyClean (clean, object):
clean.run(self)
class MySdist (sdist, object):
class MySdist (sdist):
"""Custom sdist command."""
def get_file_list (self):
@ -376,28 +318,9 @@ class MySdist (sdist, object):
self.filelist.append("MANIFEST")
# global include dirs
include_dirs = []
# global macros
define_macros = []
# compiler args
extra_compile_args = []
# library directories
library_dirs = []
# libraries
libraries = []
# scripts
scripts = ['linkchecker']
if os.name == 'nt':
# windows does not have unistd.h
define_macros.append(('YY_NO_UNISTD_H', None))
else:
extra_compile_args.append("-pedantic")
if sys.platform == 'darwin':
define_macros.extend([('HAVE_STRLCPY', None), ('HAVE_STRLCAT', None)])
myname = "Bastian Kleineidam"
myemail = "bastian.kleineidam@web.de"
@ -447,7 +370,6 @@ setup(
cmdclass = {
'install_lib': MyInstallLib,
'install_data': MyInstallData,
'build_ext': MyBuildExt,
'build': MyBuild,
'clean': MyClean,
'sdist': MySdist,