-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.py
executable file
·336 lines (294 loc) · 10.3 KB
/
init.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
#!/usr/bin/env python3
import argparse as argp
import logging
import logging.config as logconf
import os
import pathlib as pl
import subprocess as sp
import sys
# parent of the script's parent is one level above
# the repository location
DEFAULT_ROOT = pl.Path(__file__).resolve().parent.parent
DEFAULT_CONST_MODULE = pl.Path(__file__).parent
DEFAULT_CONST_MODULE = DEFAULT_CONST_MODULE.joinpath(
"workflow", "rules", "commons", "10_constants.smk"
)
DEFAULT_CONST_MODULE.resolve(strict=True)
def create_execution_environment(repo_folder, project_folder, conda_env_name):
"""Create Conda environments if any Conda-like executable
is found on $PATH
Args:
repo_folder (pathlib.Path): This repository checkout location
project_folder (pathlib.Path): Project folder, assumed to be
one above the repo folder
conda_env_name: Name of Conda environment to create
Raises:
RuntimeError: In dev only mode, a Conda-like executable
must be available
subprocess.CalledProcessError: Propagated if Conda
environment cannot be created
Returns:
None: placeholder
"""
logger = logging.getLogger(__name__)
check_executables = ["mamba", "conda"]
use_executable = None
for executable in check_executables:
try:
_ = sp.check_call(
[executable, "--version"],
shell=False,
stdout=sp.DEVNULL,
stderr=sp.DEVNULL,
)
use_executable = executable
break
except sp.CalledProcessError as spe:
logger.warning(f"Executable {executable} not available: {spe}")
if use_executable is None:
logger.error(
"No executable available to create execution (conda) environment."
" If that is correct, please restart this script with the option"
" '--no-env' to skip this step."
)
raise RuntimeError("No Conda/Mamba executable in $PATH")
logger.debug(f"Found Conda executable {use_executable} - creating environment...")
# select Conda env yaml file for specified environment
std_path = repo_folder.joinpath("workflow", "envs", f"{conda_env_name}_env.yaml")
logger.debug(f"Searching for Conda env file at location: {std_path}")
yaml_file = std_path.resolve(strict=True)
env_prefix = yaml_file.stem
logger.debug(
f"Creating environment with prefix '{env_prefix}/' underneath path: {project_folder}"
)
env_path = project_folder.joinpath(env_prefix)
logger.debug("Setting up the Conda environment may take a while...")
call_args = [
use_executable,
"env",
"create",
"--quiet",
"--force",
"-f",
str(yaml_file),
"-p",
str(env_path),
]
try:
proc_out = sp.run(call_args, shell=False, capture_output=True, check=False)
proc_out.check_returncode() # check after to get stdout/stderr
except sp.CalledProcessError as spe:
logger.error(f"Could not create Snakemake execution environment: {spe}")
logger.error(f"\n=== STDOUT ===\n{proc_out.stdout.decode('utf-8')}")
logger.error(f"\n=== STDERR ===\n{proc_out.stderr.decode('utf-8')}")
raise
return None
def setup_logging(project_dir, debug_mode, dev_only):
"""Setup logging to stderr stream and file
Args:
project_dir (pathlib.Path): Project folder,
assumed to be one above repo location
debug_mode (boolean): log verbose / debug
dev_only (boolean): create only Conda dev
environment, do not create init.log file
Returns:
pathlib.Path or os.devnull: log file location
"""
base_level = "DEBUG" if debug_mode else "WARNING"
log_file_location = project_dir.joinpath("init.log")
if dev_only:
log_file_location = os.devnull
log_config = {
"version": 1,
"root": {"handlers": ["stream", "file"], "level": "DEBUG"},
"handlers": {
"stream": {
"formatter": "default",
"class": "logging.StreamHandler",
"level": base_level,
"stream": sys.stderr,
},
"file": {
"formatter": "default",
"class": "logging.FileHandler",
"level": "INFO",
"filename": log_file_location,
},
},
"formatters": {
"default": {
"format": (
"%(asctime)s : "
"%(levelname)s | "
"%(funcName)s | "
"ln:%(lineno)d >> "
"%(message)s"
),
"datefmt": "%Y-%m-%d %H:%M:%S",
},
},
}
logconf.dictConfig(log_config)
return log_file_location
def parse_command_line():
"""Create command line parser
Returns:
argparse.Namespace: command line options
"""
parser = argp.ArgumentParser()
parser.add_argument(
"--debug",
action="store_true",
default=False,
help="Print log messages to stderr.",
dest="debug",
)
parser.add_argument(
"--root-path",
"--root",
"-r",
type=lambda x: pl.Path(x).resolve(strict=False),
default=DEFAULT_ROOT,
dest="root_path",
help="Specify the top-level (root) path under which"
" the Conda environment and the working directory"
f" hierarchy shall be created. Default: {DEFAULT_ROOT}",
)
parser.add_argument(
"--constants",
"-c",
type=lambda x: pl.Path(x).resolve(strict=True),
default=DEFAULT_CONST_MODULE,
dest="constants",
help="Specify the path to the Snakemake 'constants' module"
" that contains the information which paths to create"
f" inside the working directory. Default: {DEFAULT_CONST_MODULE}",
)
parser.add_argument(
"--dev-no-wd",
"--no-wd",
action="store_true",
default=False,
help="Do not create the working directory hierarchy.",
dest="dev_no_wd",
)
parser.add_argument(
"--dev-no-env",
"--no-env",
action="store_true",
default=False,
help="Do not create the Conda environment.",
dest="dev_no_env",
)
parser.add_argument(
"--conda-env",
"--env",
"-e",
type=str,
choices=["exec", "dev"],
default="exec",
dest="conda_env",
help="Specify the Conda environment to create: 'exec' [default] or 'dev'",
)
args = parser.parse_args()
return args
def create_wd_folders(project_dir, std_paths):
"""Create folder hierarchy starting
at Snakemake's future working directory
Args:
project_dir (pathlib.Path): Project folder,
assumed to be one above repo location
std_paths (list of pathlib.Path): standard
paths to be created in the working directory
Returns:
None: placeholder
"""
logger = logging.getLogger(__name__)
logger.info("Creating Snakemake working directory structure")
wd_toplevel = project_dir.joinpath("wd")
wd_toplevel.mkdir(exist_ok=True, parents=True)
for sub_path in std_paths:
full_path = wd_toplevel.joinpath(sub_path)
logger.info(f"Creating path {full_path}")
full_path.mkdir(exist_ok=True, parents=True)
return None
def _extract_directory_paths(module_path):
"""
Args:
module_path (pathlib.Path): Path to Snakemake constants module
"""
# import needed for eval() of Paths
import pathlib
logger = logging.getLogger(__name__)
paths = dict()
ignore = None
extracting = False
logger.debug("Evaluating content of constants module")
with open(module_path, "r") as module:
for line in module:
if line.strip().startswith("#"):
continue
elif not line.strip() and extracting:
# reached end of class definition
break
elif line.strip().startswith("class ConstDirectories:"):
extracting = True
continue
elif line.strip().startswith("_no_init"):
ignore = eval(line.strip().split("=")[-1])
elif extracting:
path_name = line.strip().split(":")[0].strip()
path = eval(line.strip().split("=")[-1])
paths[path_name] = path
else:
pass
logger.debug(
f"Extracted a total of {len(paths)} paths, {len(ignore)}"
" of which to be ignored"
)
assert ignore is not None
return paths, ignore
def load_constant_paths(module_path):
"""Load all default paths from the
(default) constants module. The module
is considered the single source of truth
on standard paths required inside
the working directory.
Args:
module_path (pathlib.Path): Path to Snakemake commons constants module
"""
logger = logging.getLogger(__name__)
logger.debug(f"Loading constants from module {module_path}")
paths, ignore = _extract_directory_paths(module_path)
paths_to_create = []
for path_name, path in paths.items():
if path_name not in ignore:
paths_to_create.append(path)
logger.debug(f"{len(paths_to_create)} remaining paths after filtering")
return paths_to_create
def main():
"""Main function
Returns:
integer: explicit 0 on success
"""
args = parse_command_line()
dev_mode = args.conda_env == "dev"
args.root_path.mkdir(parents=True, exist_ok=True)
log_file_location = setup_logging(args.root_path, args.debug, dev_mode)
logger = logging.getLogger(__name__)
repo_location = pl.Path(__file__).parent
logger.info(f"Repository location: {repo_location}")
logger.info(f"Project directory: {args.root_path}")
logger.info(f"Log file location: {log_file_location}")
if not args.dev_no_env:
create_execution_environment(repo_location, args.root_path, args.conda_env)
else:
logger.info("Skipping Conda environment creation.")
if not args.dev_no_wd:
paths = load_constant_paths(args.constants)
create_wd_folders(args.root_path, paths)
else:
logger.info("Skipping creating working directory hierarchy.")
return 0
if __name__ == "__main__":
main()