-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcompile.py
More file actions
executable file
·277 lines (234 loc) · 8.39 KB
/
compile.py
File metadata and controls
executable file
·277 lines (234 loc) · 8.39 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
import os
import shutil
import tempfile
from typing import Dict, Optional
from urllib.parse import urljoin, urlparse
import requests
from typing_extensions import Literal
from .config import (
BUSTOOLS_RELEASES_URL,
BUSTOOLS_TARBALL_URL,
COMPILED_DIR,
KALLISTO_RELEASES_URL,
KALLISTO_TARBALL_URL,
PLATFORM,
)
from .logging import logger
from .utils import download_file, restore_cwd, run_executable
class CompileError(Exception):
pass
def get_latest_github_release_tag(releases_url: str) -> str:
"""Get the tag name of the latest GitHub release, given a url to the
releases API.
Args:
releases_url: Url to the releases API
Returns:
The tag name
"""
response = requests.get(releases_url)
response.raise_for_status()
return response.json()[0]['tag_name']
def get_filename_from_url(url: str) -> str:
"""Fetch the filename from a URL.
Args:
url: The url
Returns:
The filename
"""
response = requests.get(url)
response.raise_for_status()
disposition = response.headers.get('content-disposition')
if disposition:
for split in disposition.split(';'):
split = split.strip()
if split.startswith('filename'):
return split[split.index('=') + 1:].strip('\"\'')
else:
return os.path.basename(urlparse(url).path)
def get_kallisto_url(ref: Optional[str] = None) -> str:
"""Get the tarball url of the specified or latest kallisto release.
Args:
ref: Commit or release tag, defaults to `None`. By default, the most
recent release is used.
Returns:
Tarball url
"""
tag = ref or get_latest_github_release_tag(KALLISTO_RELEASES_URL)
return urljoin(KALLISTO_TARBALL_URL, tag)
def get_bustools_url(ref: Optional[str] = None) -> str:
"""Get the tarball url of the specified or latest bustools release.
Args:
ref: Commit or release tag, defaults to `None`. By default, the most
recent release is used.
Returns:
Tarball url
"""
tag = ref or get_latest_github_release_tag(BUSTOOLS_RELEASES_URL)
return urljoin(BUSTOOLS_TARBALL_URL, tag)
def find_git_root(path: str) -> str:
"""Find the root directory of a git repo by walking.
Args:
path: Path to start the search
Returns:
Path to root of git repo
Raises:
CompileError: If the git root could not be found
"""
for root, dirs, files in os.walk(path):
if '.gitignore' in files:
return root
raise CompileError('Unable to find git root.')
@restore_cwd
def compile_kallisto(
source_dir: str,
binary_path: str,
cmake_arguments: Optional[str] = None
) -> str:
"""Compile `kallisto` from source.
Args:
source_dir: Path to directory containing root of kallisto git repo
binary_path: Path to place compiled binary
cmake_arguments: Additional arguments to pass to the cmake command
Returns:
Path to compiled binary
"""
source_dir = os.path.abspath(source_dir)
binary_path = os.path.abspath(binary_path)
os.makedirs(os.path.dirname(binary_path), exist_ok=True)
logger.info(
f'Compiling `kallisto` binary from source at {source_dir} to {binary_path}. '
'This requires `autoheader`, `autoconf`, `cmake` and `make` to be executable '
'from the command-line, as well as zlib development headers. '
'See https://pachterlab.github.io/kallisto/source for more information.'
)
os.chdir(source_dir)
shutil.copyfile(
'license.txt',
os.path.join(os.path.dirname(binary_path), 'license.txt')
)
os.chdir(os.path.join('ext', 'htslib'))
run_executable(['autoheader'])
run_executable(['autoconf'])
os.chdir(os.path.join('..', '..'))
os.makedirs('build', exist_ok=True)
os.chdir('build')
cmake_command = ['cmake', '..']
if cmake_arguments:
cmake_command.append(cmake_arguments)
run_executable(cmake_command)
run_executable(['make'])
os.makedirs(os.path.dirname(binary_path), exist_ok=True)
shutil.copy2(
os.path.join(
'src', 'kallisto.exe' if PLATFORM == 'windows' else 'kallisto'
), binary_path
)
return binary_path
@restore_cwd
def compile_bustools(
source_dir: str,
binary_path: str,
cmake_arguments: Optional[str] = None
) -> str:
"""Compile `bustools` from source.
Args:
source_dir: Path to directory containing root of bustools git repo
binary_path: Path to place compiled binary
cmake_arguments: Additional arguments to pass to the cmake command
Returns:
Path to compiled binary
"""
source_dir = os.path.abspath(source_dir)
binary_path = os.path.abspath(binary_path)
os.makedirs(os.path.dirname(binary_path), exist_ok=True)
logger.info(
f'Compiling `bustools` binary from source {source_dir} to {binary_path}. '
'This requires `cmake` and `make` to be executable from the command-line. '
'See https://bustools.github.io/source for more information.'
)
os.chdir(source_dir)
shutil.copyfile(
'LICENSE', os.path.join(os.path.dirname(binary_path), 'LICENSE')
)
os.makedirs('build', exist_ok=True)
os.chdir('build')
cmake_command = ['cmake', '..']
if cmake_arguments:
cmake_command.append(cmake_arguments)
run_executable(cmake_command)
run_executable(['make'])
shutil.copy2(
os.path.join(
'src', 'bustools.exe' if PLATFORM == 'windows' else 'bustools'
), binary_path
)
return binary_path
@logger.namespaced('compile')
def compile(
target: Literal['kallisto', 'bustools', 'all'],
out_dir: Optional[str] = None,
cmake_arguments: Optional[str] = None,
url: Optional[str] = None,
ref: Optional[str] = None,
overwrite: bool = False,
temp_dir: str = 'tmp',
) -> Dict[str, str]:
"""Compile `kallisto` and/or `bustools` binaries by downloading and compiling
a source archive.
Args:
target: Which binary to compile. May be one of `kallisto`, `bustools`
or `all`
out_dir: Path to output directory, defaults to `None`
cmake_arguments: Additional arguments to pass to the cmake command
url: Download the source archive from this url instead, defaults to
`None`
ref: Commit hash or tag to use, defaults to `None`
overwrite: Overwrite any existing results, defaults to `False`
temp_dir: Path to temporary directory, defaults to `tmp`
Returns:
Dictionary of results
"""
results = {}
if target in ('kallisto', 'all'):
binary_path = os.path.join(
out_dir or os.path.join(COMPILED_DIR, 'kallisto'), 'kallisto'
)
if os.path.exists(binary_path) and not overwrite:
raise Exception(
f'Compiled binary already exists at {binary_path}. '
'Use `--overwrite` to overwrite.'
)
_url = url or get_kallisto_url(ref)
logger.info(f'Downloading kallisto source from {_url}')
archive_path = download_file(
_url, os.path.join(temp_dir, get_filename_from_url(_url))
)
source_dir = tempfile.mkdtemp(dir=temp_dir)
shutil.unpack_archive(archive_path, source_dir)
source_dir = find_git_root(source_dir)
binary_path = compile_kallisto(
source_dir, binary_path, cmake_arguments=cmake_arguments
)
results['kallisto'] = binary_path
if target in ('bustools', 'all'):
binary_path = os.path.join(
out_dir or os.path.join(COMPILED_DIR, 'bustools'), 'bustools'
)
if os.path.exists(binary_path) and not overwrite:
raise Exception(
f'Compiled binary already exists at {binary_path}. '
'Use `--overwrite` to overwrite.'
)
_url = url or get_bustools_url(ref)
logger.info(f'Downloading bustools source from {_url}')
archive_path = download_file(
_url, os.path.join(temp_dir, get_filename_from_url(_url))
)
source_dir = tempfile.mkdtemp(dir=temp_dir)
shutil.unpack_archive(archive_path, source_dir)
source_dir = find_git_root(source_dir)
binary_path = compile_bustools(
source_dir, binary_path, cmake_arguments=cmake_arguments
)
results['bustools'] = binary_path
return results