forked from libyal/libewf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·370 lines (292 loc) · 11.2 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
#
# Script to build and install Python-bindings.
# Version: 20221217
from __future__ import print_function
import copy
import datetime
import glob
import gzip
import platform
import os
import shlex
import shutil
import subprocess
import sys
import tarfile
import zipfile
from distutils.ccompiler import new_compiler
from setuptools import Extension
from setuptools import setup
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist
try:
from distutils.command.bdist_msi import bdist_msi
except ImportError:
bdist_msi = None
try:
from setuptools.command.bdist_rpm import bdist_rpm
except ImportError:
from distutils.command.bdist import bdist as bdist_rpm
if not bdist_msi:
custom_bdist_msi = None
else:
class custom_bdist_msi(bdist_msi):
"""Custom handler for the bdist_msi command."""
def run(self):
"""Builds an MSI."""
# Make a deepcopy of distribution so the following version changes
# only apply to bdist_msi.
self.distribution = copy.deepcopy(self.distribution)
# bdist_msi does not support the library version so we add ".1"
# as a work around.
self.distribution.metadata.version = "{0:s}.1".format(
self.distribution.metadata.version)
bdist_msi.run(self)
class custom_bdist_rpm(bdist_rpm):
"""Custom handler for the bdist_rpm command."""
def run(self):
"""Builds a RPM."""
print("'setup.py bdist_rpm' command not supported use 'rpmbuild' instead.")
sys.exit(1)
class custom_build_ext(build_ext):
"""Custom handler for the build_ext command."""
def _RunCommand(self, command):
"""Runs the command."""
arguments = shlex.split(command)
process = subprocess.Popen(
arguments, stderr=subprocess.PIPE, stdout=subprocess.PIPE,
universal_newlines=True)
if not process:
raise RuntimeError("Running: {0:s} failed.".format(command))
output, error = process.communicate()
if process.returncode != 0:
error = "\n".join(error.split("\n")[-5:])
raise RuntimeError("Running: {0:s} failed with error:\n{1:s}.".format(
command, error))
return output
def build_extensions(self):
"""Set up the build extensions."""
# TODO: move build customization here?
build_ext.build_extensions(self)
def run(self):
"""Runs the build extension."""
compiler = new_compiler(compiler=self.compiler)
if compiler.compiler_type == "msvc":
self.define = [
("UNICODE", ""),
]
else:
command = "sh configure --disable-nls --disable-shared-libs"
output = self._RunCommand(command)
print_line = False
for line in output.split("\n"):
line = line.rstrip()
if line == "configure:":
print_line = True
if print_line:
print(line)
self.define = [
("HAVE_CONFIG_H", ""),
]
build_ext.run(self)
class custom_sdist(sdist):
"""Custom handler for the sdist command."""
def run(self):
"""Builds a source distribution (sdist) package."""
if self.formats != ["gztar"] and self.formats != ["zip"]:
print("'setup.py sdist' unsupported format.")
sys.exit(1)
if glob.glob("*.tar.gz"):
print("'setup.py sdist' remove existing *.tar.gz files from "
"source directory.")
sys.exit(1)
command = "make dist"
exit_code = subprocess.call(command, shell=True)
if exit_code != 0:
raise RuntimeError("Running: {0:s} failed.".format(command))
if not os.path.exists(self.dist_dir):
os.mkdir(self.dist_dir)
source_package_file = glob.glob("*.tar.gz")[0]
source_package_prefix, _, source_package_suffix = (
source_package_file.partition("-"))
sdist_package_file = "{0:s}-python-{1:s}".format(
source_package_prefix, source_package_suffix)
sdist_package_file = os.path.join(self.dist_dir, sdist_package_file)
os.rename(source_package_file, sdist_package_file)
# Create and add the PKG-INFO file to the source package.
with gzip.open(sdist_package_file, "rb") as input_file:
with open(sdist_package_file[:-3], "wb") as output_file:
shutil.copyfileobj(input_file, output_file)
os.remove(sdist_package_file)
self.distribution.metadata.write_pkg_info(".")
pkg_info_path = "{0:s}-{1:s}/PKG-INFO".format(
source_package_prefix, source_package_suffix[:-7])
with tarfile.open(sdist_package_file[:-3], "a:") as tar_file:
tar_file.add("PKG-INFO", arcname=pkg_info_path)
os.remove("PKG-INFO")
with open(sdist_package_file[:-3], "rb") as input_file:
with gzip.open(sdist_package_file, "wb") as output_file:
shutil.copyfileobj(input_file, output_file)
os.remove(sdist_package_file[:-3])
# Convert the .tar.gz into a .zip
if self.formats == ["zip"]:
zip_sdist_package_file = "{0:s}.zip".format(sdist_package_file[:-7])
with tarfile.open(sdist_package_file, "r|gz") as tar_file:
with zipfile.ZipFile(
zip_sdist_package_file, "w", zipfile.ZIP_DEFLATED) as zip_file:
for tar_file_entry in tar_file:
file_entry = tar_file.extractfile(tar_file_entry)
if tar_file_entry.isfile():
modification_time = datetime.datetime.fromtimestamp(
tar_file_entry.mtime)
zip_modification_time = (
modification_time.year, modification_time.month,
modification_time.day, modification_time.hour,
modification_time.minute, modification_time.second)
zip_info = zipfile.ZipInfo(
date_time=zip_modification_time,
filename=tar_file_entry.name)
zip_info.external_attr = (tar_file_entry.mode & 0xff) << 16
file_data = file_entry.read()
zip_file.writestr(zip_info, file_data)
os.remove(sdist_package_file)
sdist_package_file = zip_sdist_package_file
# Inform distutils what files were created.
dist_files = getattr(self.distribution, "dist_files", [])
dist_files.append(("sdist", "", sdist_package_file))
class ProjectInformation(object):
"""Project information."""
def __init__(self):
"""Initializes project information."""
super(ProjectInformation, self).__init__()
self.include_directories = []
self.library_name = None
self.library_names = []
self.library_version = None
self._ReadConfigureAc()
self._ReadMakefileAm()
@property
def module_name(self):
"""The Python module name."""
return "py{0:s}".format(self.library_name[3:])
@property
def package_name(self):
"""The package name."""
return "{0:s}-python".format(self.library_name)
@property
def package_description(self):
"""The package description."""
return "Python bindings module for {0:s}".format(self.library_name)
@property
def project_url(self):
"""The project URL."""
return "https://github.com/libyal/{0:s}/".format(self.library_name)
def _ReadConfigureAc(self):
"""Reads configure.ac to initialize the project information."""
file_object = open("configure.ac", "rb")
if not file_object:
raise IOError("Unable to open: configure.ac")
found_ac_init = False
found_library_name = False
for line in file_object.readlines():
line = line.strip()
if found_library_name:
library_version = line[1:-2]
if sys.version_info[0] >= 3:
library_version = library_version.decode("ascii")
self.library_version = library_version
break
elif found_ac_init:
library_name = line[1:-2]
if sys.version_info[0] >= 3:
library_name = library_name.decode("ascii")
self.library_name = library_name
found_library_name = True
elif line.startswith(b"AC_INIT"):
found_ac_init = True
file_object.close()
if not self.library_name or not self.library_version:
raise RuntimeError(
"Unable to find library name and version in: configure.ac")
def _ReadMakefileAm(self):
"""Reads Makefile.am to initialize the project information."""
if not self.library_name:
raise RuntimeError("Missing library name")
file_object = open("Makefile.am", "rb")
if not file_object:
raise IOError("Unable to open: Makefile.am")
found_subdirs = False
for line in file_object.readlines():
line = line.strip()
if found_subdirs:
library_name, _, _ = line.partition(b" ")
if sys.version_info[0] >= 3:
library_name = library_name.decode("ascii")
self.include_directories.append(library_name)
if library_name.startswith("lib"):
self.library_names.append(library_name)
if library_name == self.library_name:
break
elif line.startswith(b"SUBDIRS"):
found_subdirs = True
file_object.close()
if not self.include_directories or not self.library_names:
raise RuntimeError(
"Unable to find include directories and library names in: "
"Makefile.am")
project_information = ProjectInformation()
CMDCLASS = {
"build_ext": custom_build_ext,
"bdist_rpm": custom_bdist_rpm,
"sdist": custom_sdist}
if custom_bdist_msi:
CMDCLASS["bdist_msi"] = custom_bdist_msi
SOURCES = []
# TODO: replace by detection of MSC
DEFINE_MACROS = []
if platform.system() == "Windows":
DEFINE_MACROS.append(("WINVER", "0x0501"))
# TODO: determine how to handle third party DLLs.
for library_name in project_information.library_names:
if library_name != project_information.library_name:
definition = "HAVE_LOCAL_{0:s}".format(library_name.upper())
DEFINE_MACROS.append((definition, ""))
# Put everything inside the Python module to prevent issues with finding
# shared libaries since pip does not integrate well with the system package
# management.
for library_name in project_information.library_names:
for source_file in glob.glob(os.path.join(library_name, "*.[ly]")):
generated_source_file = "{0:s}.c".format(source_file[:-2])
if not os.path.exists(generated_source_file):
raise RuntimeError("Missing generated source file: {0:s}".format(
generated_source_file))
source_files = glob.glob(os.path.join(library_name, "*.c"))
SOURCES.extend(source_files)
source_files = glob.glob(os.path.join(project_information.module_name, "*.c"))
SOURCES.extend(source_files)
# TODO: find a way to detect missing python.h
# e.g. on Ubuntu python-dev is not installed by python-pip
# TODO: what about description and platform in egg file
setup(
name=project_information.package_name,
url=project_information.project_url,
version=project_information.library_version,
description=project_information.package_description,
long_description=project_information.package_description,
long_description_content_type="text/plain",
author="Joachim Metz",
author_email="joachim.metz@gmail.com",
license="GNU Lesser General Public License v3 or later (LGPLv3+)",
cmdclass=CMDCLASS,
ext_modules=[
Extension(
project_information.module_name,
define_macros=DEFINE_MACROS,
include_dirs=project_information.include_directories,
libraries=[],
library_dirs=[],
sources=SOURCES,
),
],
)