-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathsetup.py
More file actions
385 lines (318 loc) · 10.9 KB
/
Copy pathsetup.py
File metadata and controls
385 lines (318 loc) · 10.9 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
=========================================================
Primer3-py: simple oligo analysis and primer design
=========================================================
**Primer3-py** is a collection of Python bindings for a derivative of the
popular Primer3 C library (version 2.6.1).
See README.md and http://libnano.github.io/primer3-py for more information
and primer3_test.py for usage examples.
Installation
------------
**Primer3-py** has no external runtime library dependencies and should compile
on easily most Linux and MacOS systems that are running Python 3.8 - 3.13.
Windows compilation is also generally easy.
To install from PyPI::
$ pip install primer3-py
To install from local source::
$ pip install .
For development installation (editable mode)::
$ pip install -e .
To build distribution packages::
$ python -m build
This will create both wheel and source distribution in the dist/ directory.
'''
import os
import shutil
import subprocess
import sys
from distutils import log as setup_log
from os.path import join as pjoin
from os.path import relpath as rpath
from setuptools import (
Extension,
setup,
)
from setuptools.command import (
build_clib,
install_lib,
sdist,
)
from setuptools.command.build_ext import build_ext as _build_ext
# Read long description from README.md
with open('README.md', encoding='utf-8') as fd:
LONG_DESCRIPTION = fd.read()
# Platform-dependent binary for `make` commands
WINDOWS_OS = sys.platform == 'win32'
MAKE_BIN = 'mingw32-make' if WINDOWS_OS else 'make'
# Indicates whether the Primer3 library has been built during install
P3_BUILT = False
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Package paths ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
PACKAGE_PATH = os.path.abspath(os.path.dirname(__file__))
MODULE_PATH = pjoin(PACKAGE_PATH, 'primer3')
SRC_PATH = pjoin(MODULE_PATH, 'src')
TESTS_PATH = pjoin(MODULE_PATH, 'tests')
LIBPRIMER3_PATH = pjoin(SRC_PATH, 'libprimer3')
THERMO_PARAMS_PATH = pjoin(LIBPRIMER3_PATH, 'primer3_config')
KLIB_PATH = pjoin(LIBPRIMER3_PATH, 'klib')
LIBPRIMER3_C_FPS = [
rpath(pjoin(LIBPRIMER3_PATH, 'dpal.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'libprimer3.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'oligotm.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'p3_seq_lib.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'read_boulder.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal_parameters.c')),
]
LIBPRIMER3_C_FPS_FLEX = [
rpath(pjoin(LIBPRIMER3_PATH, 'dpal.c')),
# rpath(pjoin(LIBPRIMER3_PATH, 'libprimer3.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'libprimer3flex.c')), # primer3-py new
rpath(pjoin(LIBPRIMER3_PATH, 'oligotm.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'p3_seq_lib.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'read_boulder.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thalflex.c')), # primer3-py new
rpath(pjoin(LIBPRIMER3_PATH, 'thal_parameters.c')),
]
LIBPRIMER3_H_FPS = [
rpath(pjoin(LIBPRIMER3_PATH, 'amplicontm.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'dpal.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'format_output.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'libprimer3.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'libprimer3flex.h')), # primer3-py new
rpath(pjoin(LIBPRIMER3_PATH, 'masker.h')), # masker.h is required
rpath(pjoin(LIBPRIMER3_PATH, 'oligotm.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'p3_seq_lib.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'print_boulder.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'read_boulder.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal.h')),
rpath(pjoin(LIBPRIMER3_PATH, 'thalflex.h')), # primer3-py new
rpath(pjoin(LIBPRIMER3_PATH, 'thalflexsignatures.h')), # primer3-py new
rpath(pjoin(LIBPRIMER3_PATH, 'thal_parameters.h')),
]
LIBPRIMER3_C_EXTRA_FPS = [
rpath(pjoin(LIBPRIMER3_PATH, 'amplicontm.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'format_output.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'print_boulder.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal_parameters.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'amplicontm_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'masker_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'long_seq_tm_test_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'oligotm_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'primer3_boulder_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'thal_main.c')),
rpath(pjoin(LIBPRIMER3_PATH, 'MAKEFILE')),
]
KLIB_H_FPS = [
rpath(pjoin(KLIB_PATH, 'khash.h')),
]
LIBPRIMER3_BINARIES = [
'amplicon3_core',
'oligotm',
'ntdpal',
'ntthal',
'primer3_core',
]
if WINDOWS_OS:
LIBPRIMER3_BINARIES = [bin_fn + '.exe' for bin_fn in LIBPRIMER3_BINARIES]
else:
LIBPRIMER3_C_FPS.append(rpath(pjoin(LIBPRIMER3_PATH, 'masker.c')))
LIBPRIMER3_C_FPS_FLEX.append(rpath(pjoin(LIBPRIMER3_PATH, 'masker.c')))
LIBPRIMER3_BINARIES.append('primer3_masker')
LIBPRIMER3_BINARY_FPS = [
pjoin(LIBPRIMER3_PATH, fn) for fn in LIBPRIMER3_BINARIES
]
LIBPRIMER3_THERMO_PARAMS_FPS = [
rpath(pjoin(root, fp), MODULE_PATH) for root, _, fps in
os.walk(THERMO_PARAMS_PATH) for fp in fps
]
LIBPRIMER3_TEST_FPS = [
rpath(pjoin(root, fp), MODULE_PATH) for root, _, fps in
os.walk(TESTS_PATH) for fp in fps
]
PACKAGE_FPS = (
LIBPRIMER3_THERMO_PARAMS_FPS +
LIBPRIMER3_TEST_FPS +
LIBPRIMER3_C_FPS_FLEX +
LIBPRIMER3_C_EXTRA_FPS +
LIBPRIMER3_H_FPS +
KLIB_H_FPS +
['p3helpers.pyx', 'p3helpers.h'] +
['thermoanalysis.pxd', 'thermoanalysis.pxi', 'thermoanalysis.pyx']
)
# ~~~~~~~~~~~~~~~~~~~~~ Primer3 C library build helpers ~~~~~~~~~~~~~~~~~~~~~ #
def p3Clean():
'''
Run `make clean` for libprimer3 in a platform-dependent manner
'''
proc = subprocess.Popen(
f'{MAKE_BIN} clean',
shell=True,
cwd=LIBPRIMER3_PATH,
)
proc.wait()
def p3Build():
'''
Run `make clean && make` for libprimer3 in a platform-dependent manner
'''
proc = subprocess.Popen(
f'{MAKE_BIN} all TESTOPTS=--windows' if WINDOWS_OS else f'{MAKE_BIN}',
shell=True,
cwd=LIBPRIMER3_PATH,
)
proc.wait()
# Insure that the copied binaries are executable
def makeExecutable(fp):
'''
Adds the executable bit to the file at filepath `fp`
'''
mode = ((os.stat(fp).st_mode) | 0o555) & 0o7777
setup_log.info('Adding executable bit to %s (mode is now %o)', fp, mode)
os.chmod(fp, mode)
class CustomInstallLib(install_lib.install_lib):
'''
Custom library installer to ensure that libprimer3 binaries are installed
and made executable.
Installed and invoked internally by `setuptools`/`distutils`
'''
def run(self):
global P3_BUILT
super().run()
if not P3_BUILT:
p3Clean()
p3Build()
P3_BUILT = True
binary_dest_fps = [
pjoin(
self.install_dir,
'primer3',
'src',
'libprimer3',
fn,
) for fn in LIBPRIMER3_BINARIES
]
for src_fp, dest_fp in zip(LIBPRIMER3_BINARY_FPS, binary_dest_fps):
shutil.copyfile(src_fp, dest_fp)
makeExecutable(dest_fp)
class CustomSdist(sdist.sdist):
'''
Custom sdist packager, ensures that libprimer3 build artifacts are removed
prior to packaging.
Installed and invoked internally by `setuptools`/`distutils`
'''
def run(self):
global P3_BUILT
# Clean up the primer3 build prior to sdist command to remove
# binaries and object/library files
p3Clean()
P3_BUILT = False
# Remove the build Cython artifact
try:
os.remove(os.path.join(MODULE_PATH, 'thermoanalysis.c'))
except FileNotFoundError:
# support possibly not existing in CI
pass
super().run()
class CustomBuildClib(build_clib.build_clib):
'''
Custom C library builder, ensures that libprimer3 is built prior to C
library builds.
Installed and invoked internally by `setuptools`/`distutils`
'''
def run(self):
global P3_BUILT
# Build primer3 prior to building the extension, if not already built
if not P3_BUILT:
p3Clean()
p3Build()
P3_BUILT = True
super().run()
class CustomBuildExt(_build_ext):
"""Custom build_ext command that ensures primer3 binaries are built."""
def run(self):
global P3_BUILT
if not P3_BUILT:
p3Clean()
p3Build()
P3_BUILT = True
super().run()
# ~~~~~~~~~~~~~~~~~~~~~~ Cython / C API extension setup ~~~~~~~~~~~~~~~~~~~~~ #
if WINDOWS_OS:
EXTRA_COMPILE_ARGS = ['']
else:
EXTRA_COMPILE_ARGS = [
'-Wno-error=declaration-after-statement',
'-Wno-unused-function',
]
cython_extensions = [
Extension(
'primer3.p3helpers',
sources=[pjoin('primer3', 'p3helpers.pyx')],
include_dirs=[MODULE_PATH],
extra_compile_args=EXTRA_COMPILE_ARGS,
),
Extension(
'primer3.thermoanalysis',
sources=(
[pjoin('primer3', 'thermoanalysis.pyx')] + LIBPRIMER3_C_FPS_FLEX
),
include_dirs=[SRC_PATH, LIBPRIMER3_PATH, KLIB_PATH],
extra_compile_args=EXTRA_COMPILE_ARGS,
),
]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# Ensure that libprimer3 is built prior to the `build_ext` or `install` cmds
if ('build_ext' in sys.argv or 'install' in sys.argv):
if not P3_BUILT:
p3Clean()
p3Build()
P3_BUILT = True
def try_cythonize(extension_list, *args, **kwargs):
'''
Light cythonize wrapper
'''
try:
from Cython.Build import cythonize
except (ImportError, ModuleNotFoundError):
def cythonize(x, **kwargs):
return x
cython_compiler_directives = dict(
# always_allow_keywords=False,
# boundscheck=False,
# embedsignature=False,
# initializedcheck=False,
# wraparound=False,
language_level='3',
c_string_encoding='utf-8',
)
return cythonize(
extension_list,
compiler_directives=cython_compiler_directives,
)
setup(
# Note: We intentionally ignore setuptools warnings about undeclared
# packages in primer3/src/libprimer3/ and its subdirectories. These
# directories contain C source code, headers, and configuration files -
# they are not meant to be Python packages despite being potentially
# importable by Python's rules. They are correctly handled as package
# data via package_data.
packages=['primer3'],
ext_modules=try_cythonize(cython_extensions),
package_data={'primer3': PACKAGE_FPS},
cmdclass={
'install_lib': CustomInstallLib,
'sdist': CustomSdist,
'build_clib': CustomBuildClib,
'build_ext': CustomBuildExt,
},
install_requires=[],
zip_safe=False,
# Build configuration
options={
'build': {
'build_base': 'build',
},
},
)