| 1 |
from glob import glob |
| 2 |
import os |
| 3 |
import os.path |
| 4 |
import sys |
| 5 |
|
| 6 |
from distutils.command.install_data import install_data |
| 7 |
from distutils.errors import CCompilerError, DistutilsExecError, \ |
| 8 |
DistutilsPlatformError |
| 9 |
from setuptools import setup |
| 10 |
from setuptools.command.build_ext import build_ext |
| 11 |
|
| 12 |
|
| 13 |
from Cheetah.FileUtils import findFiles |
| 14 |
from Cheetah.compat import string_type |
| 15 |
|
| 16 |
if sys.platform == 'win32': |
| 17 |
|
| 18 |
|
| 19 |
ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError, |
| 20 |
IOError, TypeError) |
| 21 |
else: |
| 22 |
ext_errors = (CCompilerError, DistutilsExecError, DistutilsPlatformError) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class BuildFailed(Exception): |
| 29 |
pass |
| 30 |
|
| 31 |
|
| 32 |
class mod_build_ext(build_ext): |
| 33 |
"""A modified version of build_ext command that raises an |
| 34 |
exception when building of the extension fails. |
| 35 |
""" |
| 36 |
|
| 37 |
def run(self): |
| 38 |
try: |
| 39 |
build_ext.run(self) |
| 40 |
except DistutilsPlatformError as x: |
| 41 |
raise BuildFailed(x) |
| 42 |
|
| 43 |
def build_extension(self, ext): |
| 44 |
try: |
| 45 |
build_ext.build_extension(self, ext) |
| 46 |
except ext_errors as x: |
| 47 |
raise BuildFailed(x) |
| 48 |
|
| 49 |
|
| 50 |
class mod_install_data(install_data): |
| 51 |
"""A modified version of the disutils install_data command that allows data |
| 52 |
files to be included directly in the installed Python package tree. |
| 53 |
""" |
| 54 |
|
| 55 |
def finalize_options(self): |
| 56 |
|
| 57 |
if self.install_dir is None: |
| 58 |
installobj = self.distribution.get_command_obj('install') |
| 59 |
|
| 60 |
self.install_dir = installobj.install_lib |
| 61 |
install_data.finalize_options(self) |
| 62 |
|
| 63 |
def run(self): |
| 64 |
|
| 65 |
if not self.dry_run: |
| 66 |
self.mkpath(self.install_dir) |
| 67 |
data_files = self.get_inputs() |
| 68 |
|
| 69 |
for entry in data_files: |
| 70 |
if not isinstance(entry, string_type): |
| 71 |
raise ValueError('The entries in "data_files" must be strings') |
| 72 |
|
| 73 |
entry = os.sep.join(entry.split('/')) |
| 74 |
|
| 75 |
if entry.startswith('recursive:'): |
| 76 |
entry = entry[len('recursive:'):] |
| 77 |
dir = entry.split()[0] |
| 78 |
globPatterns = entry.split()[1:] |
| 79 |
filenames = findFiles(dir, globPatterns) |
| 80 |
else: |
| 81 |
filenames = glob(entry) |
| 82 |
|
| 83 |
for filename in filenames: |
| 84 |
|
| 85 |
|
| 86 |
topDir, subPath = (filename.split(os.sep)[0], |
| 87 |
os.sep.join(filename.split(os.sep)[1:]) |
| 88 |
) |
| 89 |
|
| 90 |
package_dirDict = self.distribution.package_dir |
| 91 |
if package_dirDict: |
| 92 |
packageDir = topDir |
| 93 |
for key, val in package_dirDict.items(): |
| 94 |
if val == topDir: |
| 95 |
packageDir = key |
| 96 |
break |
| 97 |
else: |
| 98 |
packageDir = topDir |
| 99 |
dstPath = os.path.join(self.install_dir, packageDir, subPath) |
| 100 |
|
| 101 |
|
| 102 |
dstdir = os.path.split(dstPath)[0] |
| 103 |
if not self.dry_run: |
| 104 |
self.mkpath(dstdir) |
| 105 |
outfile = self.copy_file(filename, dstPath)[0] |
| 106 |
else: |
| 107 |
outfile = dstPath |
| 108 |
self.outfiles.append(outfile) |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
def run_setup(configurations): |
| 115 |
"""Run distutils/setuptools setup. |
| 116 |
|
| 117 |
The parameters passed to setup() are extracted from the list of modules, |
| 118 |
classes or instances given in configurations. |
| 119 |
|
| 120 |
Names with leading underscore are removed from the parameters. |
| 121 |
Parameters which are not strings, lists, tuples, or dicts are removed as |
| 122 |
well. Configurations which occur later in the configurations list |
| 123 |
override settings of configurations earlier in the list. |
| 124 |
""" |
| 125 |
|
| 126 |
kws = {} |
| 127 |
newkws = {} |
| 128 |
for configuration in configurations: |
| 129 |
kws.update(vars(configuration)) |
| 130 |
for name, value in kws.items(): |
| 131 |
if name[:1] == '_': |
| 132 |
continue |
| 133 |
if not isinstance(value, (string_type, list, tuple, dict, int)): |
| 134 |
continue |
| 135 |
newkws[name] = value |
| 136 |
kws = newkws |
| 137 |
|
| 138 |
|
| 139 |
cmdclasses = { |
| 140 |
'build_ext': mod_build_ext, |
| 141 |
'install_data': mod_install_data, |
| 142 |
} |
| 143 |
|
| 144 |
kws['cmdclass'] = cmdclasses |
| 145 |
|
| 146 |
try: |
| 147 |
setup(**kws) |
| 148 |
except BuildFailed as x: |
| 149 |
print("One or more C extensions failed to build.") |
| 150 |
print("Details: %s" % x) |
| 151 |
if os.environ.get('CHEETAH_C_EXTENSIONS_REQUIRED'): |
| 152 |
raise x |
| 153 |
print("Retrying without C extensions enabled.") |
| 154 |
|
| 155 |
del kws['ext_modules'] |
| 156 |
setup(**kws) |
| 157 |
|
| 158 |
print("One or more C extensions failed to build.") |
| 159 |
print("Performance enhancements will not be available.") |
| 160 |
print("Pure Python installation succeeded.") |