/
sourcecache.py
431 lines (348 loc) · 12.1 KB
/
sourcecache.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
__authors__ = "Johannes Köster"
__copyright__ = "Copyright 2022, Johannes Köster"
__email__ = "johannes.koester@uni-due.de"
__license__ = "MIT"
import hashlib
from pathlib import Path
import posixpath
import re
import os
import shutil
from snakemake import utils
import tempfile
import io
from abc import ABC, abstractmethod
from datetime import datetime
from snakemake.common import (
ON_WINDOWS,
is_local_file,
get_appdirs,
parse_uri,
smart_join,
)
from snakemake.exceptions import WorkflowError, SourceFileError
from snakemake.io import git_content, split_git_path
from snakemake.logging import logger
def _check_git_args(tag: str = None, branch: str = None, commit: str = None):
n_refs = sum(1 for ref in (tag, branch, commit) if ref is not None)
if n_refs != 1:
raise SourceFileError(
"exactly one of tag, branch, or commit must be specified."
)
class SourceFile(ABC):
@abstractmethod
def get_path_or_uri(self):
...
@abstractmethod
def is_persistently_cacheable(self):
...
def get_uri_hash(self):
urihash = hashlib.sha256()
urihash.update(self.get_path_or_uri().encode())
return urihash.hexdigest()
def get_basedir(self):
path = os.path.dirname(self.get_path_or_uri())
return self.__class__(path)
@abstractmethod
def get_filename(self):
...
def join(self, path):
if isinstance(path, SourceFile):
path = path.get_path_or_uri()
return self.__class__(smart_join(self.get_path_or_uri(), path))
def mtime(self):
"""If possible, return mtime of the file. Otherwise, return None."""
return None
@property
@abstractmethod
def is_local(self):
...
def __hash__(self):
return self.get_path_or_uri().__hash__()
def __eq__(self, other):
if isinstance(other, SourceFile):
return self.get_path_or_uri() == other.get_path_or_uri()
return False
def __str__(self):
return self.get_path_or_uri()
def simplify_path(self):
return self
class GenericSourceFile(SourceFile):
def __init__(self, path_or_uri):
self.path_or_uri = path_or_uri
def get_path_or_uri(self):
return self.path_or_uri
def get_filename(self):
return os.path.basename(self.path_or_uri)
def is_persistently_cacheable(self):
return False
@property
def is_local(self):
return False
class LocalSourceFile(SourceFile):
def __init__(self, path):
self.path = path
def get_path_or_uri(self):
return self.path
def is_persistently_cacheable(self):
return False
def get_filename(self):
return os.path.basename(self.path)
def abspath(self):
return LocalSourceFile(os.path.abspath(self.path))
def isabs(self):
return os.path.isabs(self.path)
def simplify_path(self):
return utils.simplify_path(self.path)
def mtime(self):
return os.stat(self.path).st_mtime
def __fspath__(self):
return self.path
@property
def is_local(self):
return True
class LocalGitFile(SourceFile):
def __init__(
self, repo_path, path: str, tag: str = None, ref: str = None, commit: str = None
):
_check_git_args(tag, ref, commit)
self.tag = tag
self.commit = commit
self._ref = ref
self.repo_path = repo_path
self.path = path
def get_path_or_uri(self):
return "git+file://{}/{}@{}".format(self.repo_path, self.path, self.ref)
def join(self, path):
return LocalGitFile(
self.repo_path,
"/".join((self.path, path)),
tag=self.tag,
ref=self.ref,
commit=self.commit,
)
def get_basedir(self):
return self.__class__(
repo_path=self.repo_path,
path=os.path.dirname(self.path),
tag=self.tag,
commit=self.commit,
ref=self.ref,
)
def is_persistently_cacheable(self):
return False
def get_filename(self):
return posixpath.basename(self.path)
@property
def ref(self):
return self.tag or self.commit or self._ref
@property
def is_local(self):
return True
class HostingProviderFile(SourceFile):
"""Marker for denoting github source files from releases."""
valid_repo = re.compile("^.+/.+$")
def __init__(
self,
repo: str = None,
path: str = None,
tag: str = None,
branch: str = None,
commit: str = None,
):
if repo is None:
raise SourceFileError("repo must be given")
if not self.__class__.valid_repo.match(repo):
raise SourceFileError(
"repo {} is not a valid repo specification (must be given as owner/name)."
)
_check_git_args(tag, branch, commit)
if path is None:
raise SourceFileError("path must be given")
if not all(
isinstance(item, str)
for item in (repo, path, tag, branch, commit)
if item is not None
):
raise SourceFileError("arguments must be given as str.")
self.repo = repo
self.tag = tag
self.commit = commit
self.branch = branch
self.path = path.strip("/")
def is_persistently_cacheable(self):
return self.tag or self.commit
def get_filename(self):
return os.path.basename(self.path)
@property
def ref(self):
return self.tag or self.commit or self.branch
def get_basedir(self):
return self.__class__(
repo=self.repo,
path=os.path.dirname(self.path),
tag=self.tag,
commit=self.commit,
branch=self.branch,
)
def join(self, path):
path = os.path.normpath("{}/{}".format(self.path, path))
if ON_WINDOWS:
# convert back to URL separators
# (win specific separators are introduced by normpath above)
path = path.replace("\\", "/")
return self.__class__(
repo=self.repo,
path=path,
tag=self.tag,
commit=self.commit,
branch=self.branch,
)
@property
def is_local(self):
return False
class GithubFile(HostingProviderFile):
def get_path_or_uri(self):
return "https://github.com/{}/raw/{}/{}".format(self.repo, self.ref, self.path)
class GitlabFile(HostingProviderFile):
def __init__(
self,
repo: str,
path: str,
tag: str = None,
branch: str = None,
commit: str = None,
host: str = None,
):
super().__init__(repo, path, tag, branch, commit)
self.host = host
def get_path_or_uri(self):
return "https://{}/{}/-/raw/{}/{}".format(
self.host or "gitlab.com", self.repo, self.ref, self.path
)
def infer_source_file(path_or_uri, basedir: SourceFile = None):
if isinstance(path_or_uri, SourceFile):
if basedir is None or isinstance(path_or_uri, HostingProviderFile):
return path_or_uri
else:
path_or_uri = path_or_uri.get_path_or_uri()
if isinstance(path_or_uri, Path):
path_or_uri = str(path_or_uri)
if not isinstance(path_or_uri, str):
raise SourceFileError(
"must be given as Python string or one of the predefined source file marker types (see docs)"
)
if is_local_file(path_or_uri):
# either local file or relative to some remote basedir
for schema in ("file://", "file:"):
if path_or_uri.startswith(schema):
path_or_uri = path_or_uri[len(schema) :]
break
if not os.path.isabs(path_or_uri) and basedir is not None:
return basedir.join(path_or_uri)
return LocalSourceFile(path_or_uri)
if path_or_uri.startswith("git+file:"):
try:
root_path, file_path, ref = split_git_path(path_or_uri)
except Exception as e:
raise WorkflowError(
f"Failed to read source {path_or_uri} from git repo.", e
)
return LocalGitFile(root_path, file_path, ref=ref)
# something else
return GenericSourceFile(path_or_uri)
class SourceCache:
cache_whitelist = [
"https://raw.githubusercontent.com/snakemake/snakemake-wrappers/\d+\.\d+.\d+"
] # TODO add more prefixes for uris that are save to be cached
def __init__(self, runtime_cache_path=None):
self.cache = Path(
os.path.join(get_appdirs().user_cache_dir, "snakemake/source-cache")
)
os.makedirs(self.cache, exist_ok=True)
if runtime_cache_path is None:
runtime_cache_parent = self.cache / "runtime-cache"
os.makedirs(runtime_cache_parent, exist_ok=True)
self.runtime_cache = tempfile.TemporaryDirectory(
suffix="snakemake-runtime-source-cache",
dir=runtime_cache_parent,
)
self._runtime_cache_path = None
else:
self._runtime_cache_path = runtime_cache_path
self.runtime_cache = None
self.cacheable_prefixes = re.compile("|".join(self.cache_whitelist))
@property
def runtime_cache_path(self):
return self._runtime_cache_path or self.runtime_cache.name
def open(self, source_file, mode="r"):
cache_entry = self._cache(source_file)
return self._open_local_or_remote(LocalSourceFile(cache_entry), mode)
def exists(self, source_file):
try:
self._cache(source_file)
except Exception:
return False
return True
def get_path(self, source_file, mode="r"):
cache_entry = self._cache(source_file)
return cache_entry
def _cache_entry(self, source_file):
urihash = source_file.get_uri_hash()
# TODO add git support to smart_open!
if source_file.is_persistently_cacheable():
# check cache
return self.cache / urihash
else:
# check runtime cache
return Path(self.runtime_cache_path) / urihash
def _cache(self, source_file):
cache_entry = self._cache_entry(source_file)
if not cache_entry.exists():
self._do_cache(source_file, cache_entry)
return cache_entry
def _do_cache(self, source_file, cache_entry):
# open from origin
with self._open_local_or_remote(source_file, "rb") as source:
tmp_source = tempfile.NamedTemporaryFile(
prefix=str(cache_entry),
delete=False, # no need to delete since we move it below
)
tmp_source.write(source.read())
tmp_source.close()
# Atomic move to right name.
# This way we avoid the need to lock.
shutil.move(tmp_source.name, cache_entry)
mtime = source_file.mtime()
if mtime is not None:
# Set to mtime of original file
# In case we don't have that mtime, it is fine
# to just keep the time at the time of caching
# as mtime.
os.utime(cache_entry, times=(mtime, mtime))
def _open_local_or_remote(self, source_file, mode):
from retry.api import retry_call
if source_file.is_local:
return self._open(source_file, mode)
else:
return retry_call(
self._open,
[source_file, mode],
tries=3,
delay=3,
backoff=2,
)
def _open(self, source_file, mode):
from smart_open import open
if isinstance(source_file, LocalGitFile):
import git
return io.BytesIO(
git.Repo(source_file.repo_path)
.git.show("{}:{}".format(source_file.ref, source_file.path))
.encode()
)
path_or_uri = source_file.get_path_or_uri()
try:
return open(path_or_uri, mode)
except Exception as e:
raise WorkflowError("Failed to open source file {}".format(path_or_uri), e)