/
__init__.py
267 lines (204 loc) · 6.73 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
#!/usr/bin/env python
"""
Copyright (C) 2017-2024 Vanessa Sochat.
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
Modified from https://github.com/Visual-mov/Colorful-Julia (MIT License)
"""
import scif
import argparse
import sys
import os
def get_parser():
parser = argparse.ArgumentParser(description="scientific filesystem tools")
# Global
parser.add_argument(
"--debug",
dest="debug",
help="use verbose logging to debug.",
default=False,
action="store_true",
)
parser.add_argument(
"--quiet",
dest="quiet",
help="suppress print output",
default=False,
action="store_true",
)
parser.add_argument(
"--writable",
"-w",
dest="writable",
help="for relevant commands, if writable SCIF is needed",
default=False,
action="store_true",
)
description = "actions for Scientific Filesystem"
subparsers = parser.add_subparsers(
help="scif actions", title="actions", description=description, dest="command"
)
version = subparsers.add_parser("version", help="show software version")
# Shell and Interactive Terminal (it)
pyshell = subparsers.add_parser(
"pyshell", help="Interactive python shell to scientific filesystem"
)
pyshell.add_argument(
"recipe", nargs="*", help="recipe file or scientific filesystem base", type=str
)
shell = subparsers.add_parser(
"shell", help="shell to interact with scientific filesystem"
)
shell.add_argument(
"app",
nargs="?",
help="app shell to, defaults to SCIF base if not set.",
type=str,
)
preview = subparsers.add_parser("preview", help="preview changes to a filesytem")
# if the user provides more than one argument here, they are apps
preview.add_argument(
"recipe", nargs="*", help="recipe file for the filesystem", type=str
)
# Help
help = subparsers.add_parser("help", help="look at help for an app, if it exists.")
help.add_argument("app", nargs="*", help="app(s) to print help for", type=str)
# Install
install = subparsers.add_parser(
"install", help="install a recipe on the filesystem"
)
install.add_argument(
"recipe", nargs="*", help="recipe file for the filesystem", type=str
)
# Inspect
inspect = subparsers.add_parser(
"inspect", help="inspect an attribute for a scif installation"
)
inspect.add_argument(
"attributes",
nargs="*",
help="""attribute to inspect (runscript|r),
(environment|e),
(labels|l), or (all|a) (default)""",
)
# Run
run = subparsers.add_parser("run", help="entrypoint to run a scientific filesystem")
run.add_argument(
"cmd",
nargs="*",
help="app and optional arguments to target for the entry",
type=str,
)
# Test
test = subparsers.add_parser(
"test", help="entrypoint to test an app in a scientific filesystem"
)
test.add_argument(
"cmd",
nargs="*",
help="app and optional arguments to target for the entry",
type=str,
)
# List and dump
ls = subparsers.add_parser("apps", help="list apps installed")
ls.add_argument(
"-l",
dest="longlist",
help="show long listing, including paths",
default=False,
action="store_true",
)
dump = subparsers.add_parser("dump", help="dump recipe")
# Execute
execute = subparsers.add_parser(
"exec", help="execute a command to a scientific filesystem"
)
execute.add_argument(
"cmd",
nargs="*",
help="app and command to execute. Eg, exec appname echo $SCIF_APPNAME",
type=str,
)
return parser
def get_subparsers(parser):
"""get_subparser will get a dictionary of subparsers, to help with printing help"""
actions = [
action
for action in parser._actions
if isinstance(action, argparse._SubParsersAction)
]
subparsers = dict()
for action in actions:
# get all subparsers and print help
for choice, subparser in action.choices.items():
subparsers[choice] = subparser
return subparsers
def main():
"""main is the entrypoint to managing or interacting with an scif
organized filesystem.
"""
parser = get_parser()
subparsers = get_subparsers(parser)
def help(return_code=0):
"""print help, including the software version and active client
and exit with return code.
"""
version = scif.__version__
print("\nScientific Filesystem [v%s]" % (version))
parser.print_help()
sys.exit(return_code)
# If the user didn't provide any arguments, show the full help
if len(sys.argv) == 1:
help()
try:
args, unknown = parser.parse_known_args()
except:
sys.exit(0)
# If unknown arguments were provided, pass on to cmd to run
if args.command in ["run", "exec"] and len(unknown) > 0:
args.cmd += unknown
# if environment logging variable not set, make silent
if args.debug is False:
os.environ["SCIF_MESSAGELEVEL"] = "INFO"
os.putenv("SCIF_MESSAGELEVEL", "INFO")
if args.quiet is True:
os.environ["SCIF_MESSAGELEVEL"] = "QUIET"
os.putenv("SCIF_MESSAGELEVEL", "QUIET")
# Show the version and exit
if args.command == "version":
print(scif.__version__)
sys.exit(0)
# Does the user want a shell?
if args.command == "apps":
from .list import main
if args.command == "dump":
from .dump import main
if args.command == "exec":
from .execute import main
if args.command == "help":
from .help import main
if args.command == "inspect":
from .inspect import main
if args.command == "install":
from .install import main
if args.command == "preview":
from .preview import main
if args.command == "pyshell":
from .pyshell import main
if args.command == "run":
from .run import main
if args.command == "shell":
from .shell import main
if args.command == "test":
from .test import main
# Pass on to the correct parser
return_code = 0
try:
main(args=args, parser=parser, subparser=subparsers[args.command])
sys.exit(return_code)
except UnboundLocalError:
return_code = 1
help(return_code)
if __name__ == "__main__":
main()