/
wrapper.py
136 lines (113 loc) · 3.23 KB
/
wrapper.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
__author__ = "Johannes Köster"
__copyright__ = "Copyright 2022, Johannes Köster"
__email__ = "johannes.koester@uni-due.de"
__license__ = "MIT"
import os
import posixpath
from urllib.error import URLError
from urllib.request import urlopen
from snakemake.exceptions import WorkflowError
from snakemake.script import script
from snakemake.sourcecache import LocalGitFile, SourceCache, infer_source_file
PREFIX = "https://github.com/snakemake/snakemake-wrappers/raw/"
EXTENSIONS = [".py", ".R", ".Rmd", ".jl"]
def is_script(source_file):
filename = source_file.get_filename()
return (
filename.endswith("wrapper.py")
or filename.endswith("wrapper.R")
or filename.endswith("wrapper.jl")
)
def get_path(path, prefix=None):
if not is_url(path):
if prefix is None:
prefix = PREFIX
elif prefix.startswith("git+file"):
parts = path.split("/")
path = "/" + "/".join(parts[1:]) + "@" + parts[0]
path = prefix + path
return infer_source_file(path)
def is_url(path):
return (
path.startswith("http")
or path.startswith("file:")
or path.startswith("git+file")
)
def find_extension(source_file, sourcecache: SourceCache):
for ext in EXTENSIONS:
if source_file.get_filename().endswith("wrapper{}".format(ext)):
return source_file
for ext in EXTENSIONS:
script = source_file.join("wrapper{}".format(ext))
if sourcecache.exists(script):
return script
def get_script(path, sourcecache: SourceCache, prefix=None):
path = get_path(path, prefix=prefix)
return find_extension(path, sourcecache)
def get_conda_env(path, prefix=None):
path = get_path(path, prefix=prefix)
if is_script(path):
# URLs and posixpaths share the same separator. Hence use posixpath here.
path = path.get_basedir()
return path.join("environment.yaml")
def wrapper(
path,
input,
output,
params,
wildcards,
threads,
resources,
log,
config,
rulename,
conda_env,
conda_base_path,
container_img,
singularity_args,
env_modules,
bench_record,
prefix,
jobid,
bench_iteration,
cleanup_scripts,
shadow_dir,
runtime_sourcecache_path,
):
"""
Load a wrapper from https://github.com/snakemake/snakemake-wrappers under
the given path + wrapper.(py|R|Rmd) and execute it.
"""
assert path is not None
script_source = get_script(
path, SourceCache(runtime_cache_path=runtime_sourcecache_path), prefix=prefix
)
if script_source is None:
raise WorkflowError(
f"Unable to locate wrapper script for wrapper {path}. "
"This can be a network issue or a mistake in the wrapper URL."
)
script(
script_source.get_path_or_uri(),
"",
input,
output,
params,
wildcards,
threads,
resources,
log,
config,
rulename,
conda_env,
conda_base_path,
container_img,
singularity_args,
env_modules,
bench_record,
jobid,
bench_iteration,
cleanup_scripts,
shadow_dir,
runtime_sourcecache_path,
)