| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import print_function |
| 7 |
|
| 8 |
import copy |
| 9 |
import datetime |
| 10 |
import glob |
| 11 |
import gzip |
| 12 |
import platform |
| 13 |
import os |
| 14 |
import shlex |
| 15 |
import shutil |
| 16 |
import subprocess |
| 17 |
import sys |
| 18 |
import tarfile |
| 19 |
import zipfile |
| 20 |
|
| 21 |
from distutils.ccompiler import new_compiler |
| 22 |
|
| 23 |
from setuptools import Extension |
| 24 |
from setuptools import setup |
| 25 |
from setuptools.command.build_ext import build_ext |
| 26 |
from setuptools.command.sdist import sdist |
| 27 |
|
| 28 |
|
| 29 |
if (sys.version_info[0], sys.version_info[1]) < (3, 7): |
| 30 |
print(("Unsupported Python version: {0:s}, version 3.7 or higher " |
| 31 |
"required.").format(sys.version)) |
| 32 |
sys.exit(1) |
| 33 |
|
| 34 |
|
| 35 |
class custom_build_ext(build_ext): |
| 36 |
"""Custom handler for the build_ext command.""" |
| 37 |
|
| 38 |
def _RunCommand(self, command): |
| 39 |
"""Runs the command.""" |
| 40 |
arguments = shlex.split(command) |
| 41 |
process = subprocess.Popen( |
| 42 |
arguments, stderr=subprocess.PIPE, stdout=subprocess.PIPE, |
| 43 |
universal_newlines=True) |
| 44 |
if not process: |
| 45 |
raise RuntimeError("Running: {0:s} failed.".format(command)) |
| 46 |
|
| 47 |
output, error = process.communicate() |
| 48 |
if process.returncode != 0: |
| 49 |
error = "\n".join(error.split("\n")[-5:]) |
| 50 |
raise RuntimeError("Running: {0:s} failed with error:\n{1:s}.".format( |
| 51 |
command, error)) |
| 52 |
|
| 53 |
return output |
| 54 |
|
| 55 |
def build_extensions(self): |
| 56 |
"""Set up the build extensions.""" |
| 57 |
|
| 58 |
build_ext.build_extensions(self) |
| 59 |
|
| 60 |
def run(self): |
| 61 |
"""Runs the build extension.""" |
| 62 |
compiler = new_compiler(compiler=self.compiler) |
| 63 |
if compiler.compiler_type == "msvc": |
| 64 |
self.define = [ |
| 65 |
("_CRT_SECURE_NO_WARNINGS", ""), |
| 66 |
("UNICODE", ""), |
| 67 |
] |
| 68 |
|
| 69 |
else: |
| 70 |
command = "sh configure --disable-nls --disable-shared-libs" |
| 71 |
output = self._RunCommand(command) |
| 72 |
|
| 73 |
print_line = False |
| 74 |
for line in output.split("\n"): |
| 75 |
line = line.rstrip() |
| 76 |
if line == "configure:": |
| 77 |
print_line = True |
| 78 |
|
| 79 |
if print_line: |
| 80 |
print(line) |
| 81 |
|
| 82 |
self.define = [ |
| 83 |
("HAVE_CONFIG_H", ""), |
| 84 |
] |
| 85 |
|
| 86 |
build_ext.run(self) |
| 87 |
|
| 88 |
|
| 89 |
class custom_sdist(sdist): |
| 90 |
"""Custom handler for the sdist command.""" |
| 91 |
|
| 92 |
def run(self): |
| 93 |
"""Builds a source distribution (sdist) package.""" |
| 94 |
if self.formats != ["gztar"] and self.formats != ["zip"]: |
| 95 |
print("'setup.py sdist' unsupported format.") |
| 96 |
sys.exit(1) |
| 97 |
|
| 98 |
if glob.glob("*.tar.gz"): |
| 99 |
print("'setup.py sdist' remove existing *.tar.gz files from " |
| 100 |
"source directory.") |
| 101 |
sys.exit(1) |
| 102 |
|
| 103 |
command = "make dist" |
| 104 |
exit_code = subprocess.call(command, shell=True) |
| 105 |
if exit_code != 0: |
| 106 |
raise RuntimeError("Running: {0:s} failed.".format(command)) |
| 107 |
|
| 108 |
if not os.path.exists(self.dist_dir): |
| 109 |
os.mkdir(self.dist_dir) |
| 110 |
|
| 111 |
source_package_file = glob.glob("*.tar.gz")[0] |
| 112 |
source_package_prefix, _, source_package_suffix = ( |
| 113 |
source_package_file.partition("-")) |
| 114 |
sdist_package_file = "{0:s}_python-{1:s}".format( |
| 115 |
source_package_prefix, source_package_suffix) |
| 116 |
sdist_package_file = os.path.join(self.dist_dir, sdist_package_file) |
| 117 |
os.rename(source_package_file, sdist_package_file) |
| 118 |
|
| 119 |
|
| 120 |
with gzip.open(sdist_package_file, "rb") as input_file: |
| 121 |
with open(sdist_package_file[:-3], "wb") as output_file: |
| 122 |
shutil.copyfileobj(input_file, output_file) |
| 123 |
os.remove(sdist_package_file) |
| 124 |
|
| 125 |
self.distribution.metadata.write_pkg_info(".") |
| 126 |
pkg_info_path = "{0:s}-{1:s}/PKG-INFO".format( |
| 127 |
source_package_prefix, source_package_suffix[:-7]) |
| 128 |
with tarfile.open(sdist_package_file[:-3], "a:") as tar_file: |
| 129 |
tar_file.add("PKG-INFO", arcname=pkg_info_path) |
| 130 |
os.remove("PKG-INFO") |
| 131 |
|
| 132 |
with open(sdist_package_file[:-3], "rb") as input_file: |
| 133 |
with gzip.open(sdist_package_file, "wb") as output_file: |
| 134 |
shutil.copyfileobj(input_file, output_file) |
| 135 |
os.remove(sdist_package_file[:-3]) |
| 136 |
|
| 137 |
|
| 138 |
if self.formats == ["zip"]: |
| 139 |
zip_sdist_package_file = "{0:s}.zip".format(sdist_package_file[:-7]) |
| 140 |
|
| 141 |
with tarfile.open(sdist_package_file, "r|gz") as tar_file: |
| 142 |
with zipfile.ZipFile( |
| 143 |
zip_sdist_package_file, "w", zipfile.ZIP_DEFLATED) as zip_file: |
| 144 |
for tar_file_entry in tar_file: |
| 145 |
file_entry = tar_file.extractfile(tar_file_entry) |
| 146 |
if tar_file_entry.isfile(): |
| 147 |
modification_time = datetime.datetime.fromtimestamp( |
| 148 |
tar_file_entry.mtime) |
| 149 |
zip_modification_time = ( |
| 150 |
modification_time.year, modification_time.month, |
| 151 |
modification_time.day, modification_time.hour, |
| 152 |
modification_time.minute, modification_time.second) |
| 153 |
zip_info = zipfile.ZipInfo( |
| 154 |
date_time=zip_modification_time, |
| 155 |
filename=tar_file_entry.name) |
| 156 |
zip_info.external_attr = (tar_file_entry.mode & 0xff) << 16 |
| 157 |
|
| 158 |
file_data = file_entry.read() |
| 159 |
zip_file.writestr(zip_info, file_data) |
| 160 |
|
| 161 |
os.remove(sdist_package_file) |
| 162 |
sdist_package_file = zip_sdist_package_file |
| 163 |
|
| 164 |
|
| 165 |
dist_files = getattr(self.distribution, "dist_files", []) |
| 166 |
dist_files.append(("sdist", "", sdist_package_file)) |
| 167 |
|
| 168 |
|
| 169 |
class ProjectInformation(object): |
| 170 |
"""Project information.""" |
| 171 |
|
| 172 |
def __init__(self): |
| 173 |
"""Initializes project information.""" |
| 174 |
super(ProjectInformation, self).__init__() |
| 175 |
self.include_directories = [] |
| 176 |
self.library_name = None |
| 177 |
self.library_names = [] |
| 178 |
self.library_version = None |
| 179 |
|
| 180 |
self._ReadConfigureAc() |
| 181 |
self._ReadMakefileAm() |
| 182 |
|
| 183 |
@property |
| 184 |
def module_name(self): |
| 185 |
"""The Python module name.""" |
| 186 |
return "py{0:s}".format(self.library_name[3:]) |
| 187 |
|
| 188 |
def _ReadConfigureAc(self): |
| 189 |
"""Reads configure.ac to initialize the project information.""" |
| 190 |
with open("configure.ac", "r", encoding="utf-8") as file_object: |
| 191 |
found_ac_init = False |
| 192 |
found_library_name = False |
| 193 |
for line in file_object.readlines(): |
| 194 |
line = line.strip() |
| 195 |
if found_library_name: |
| 196 |
library_version = line[1:-2] |
| 197 |
self.library_version = library_version |
| 198 |
break |
| 199 |
|
| 200 |
elif found_ac_init: |
| 201 |
library_name = line[1:-2] |
| 202 |
self.library_name = library_name |
| 203 |
found_library_name = True |
| 204 |
|
| 205 |
elif line.startswith("AC_INIT"): |
| 206 |
found_ac_init = True |
| 207 |
|
| 208 |
if not self.library_name or not self.library_version: |
| 209 |
raise RuntimeError( |
| 210 |
"Unable to find library name and version in: configure.ac") |
| 211 |
|
| 212 |
def _ReadMakefileAm(self): |
| 213 |
"""Reads Makefile.am to initialize the project information.""" |
| 214 |
if not self.library_name: |
| 215 |
raise RuntimeError("Missing library name") |
| 216 |
|
| 217 |
with open("Makefile.am", "r", encoding="utf-8") as file_object: |
| 218 |
found_subdirs = False |
| 219 |
for line in file_object.readlines(): |
| 220 |
line = line.strip() |
| 221 |
if found_subdirs: |
| 222 |
library_name, _, _ = line.partition(" ") |
| 223 |
|
| 224 |
self.include_directories.append(library_name) |
| 225 |
|
| 226 |
if library_name.startswith("lib"): |
| 227 |
self.library_names.append(library_name) |
| 228 |
|
| 229 |
if library_name == self.library_name: |
| 230 |
break |
| 231 |
|
| 232 |
elif line.startswith("SUBDIRS"): |
| 233 |
found_subdirs = True |
| 234 |
|
| 235 |
if not self.include_directories or not self.library_names: |
| 236 |
raise RuntimeError( |
| 237 |
"Unable to find include directories and library names in: " |
| 238 |
"Makefile.am") |
| 239 |
|
| 240 |
|
| 241 |
project_information = ProjectInformation() |
| 242 |
|
| 243 |
CMDCLASS = { |
| 244 |
"build_ext": custom_build_ext, |
| 245 |
"sdist": custom_sdist} |
| 246 |
|
| 247 |
SOURCES = [] |
| 248 |
|
| 249 |
|
| 250 |
DEFINE_MACROS = [] |
| 251 |
if platform.system() == "Windows": |
| 252 |
DEFINE_MACROS.append(("WINVER", "0x0501")) |
| 253 |
|
| 254 |
for library_name in project_information.library_names: |
| 255 |
if library_name != project_information.library_name: |
| 256 |
definition = "HAVE_LOCAL_{0:s}".format(library_name.upper()) |
| 257 |
|
| 258 |
DEFINE_MACROS.append((definition, "")) |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
for library_name in project_information.library_names: |
| 264 |
for source_file in glob.glob(os.path.join(library_name, "*.[ly]")): |
| 265 |
generated_source_file = "{0:s}.c".format(source_file[:-2]) |
| 266 |
if not os.path.exists(generated_source_file): |
| 267 |
raise RuntimeError("Missing generated source file: {0:s}".format( |
| 268 |
generated_source_file)) |
| 269 |
|
| 270 |
source_files = glob.glob(os.path.join(library_name, "*.c")) |
| 271 |
SOURCES.extend(source_files) |
| 272 |
|
| 273 |
source_files = glob.glob(os.path.join(project_information.module_name, "*.c")) |
| 274 |
SOURCES.extend(source_files) |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
setup_args = dict( |
| 280 |
cmdclass=CMDCLASS, |
| 281 |
ext_modules=[ |
| 282 |
Extension( |
| 283 |
project_information.module_name, |
| 284 |
define_macros=DEFINE_MACROS, |
| 285 |
include_dirs=project_information.include_directories, |
| 286 |
libraries=[], |
| 287 |
library_dirs=[], |
| 288 |
sources=SOURCES |
| 289 |
) |
| 290 |
] |
| 291 |
) |
| 292 |
setup(**setup_args) |
| 293 |
|