Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/cwltool/argparser.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 (2021-03-22) |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 """Command line argument parsing for cwltool.""" | |
2 | |
3 import argparse | |
4 import os | |
5 from typing import ( | |
6 Any, | |
7 AnyStr, | |
8 Dict, | |
9 List, | |
10 MutableMapping, | |
11 MutableSequence, | |
12 Optional, | |
13 Sequence, | |
14 Union, | |
15 cast, | |
16 ) | |
17 | |
18 from schema_salad.ref_resolver import file_uri | |
19 | |
20 from .loghandler import _logger | |
21 from .process import Process, shortname | |
22 from .resolver import ga4gh_tool_registries | |
23 from .software_requirements import SOFTWARE_REQUIREMENTS_ENABLED | |
24 from .utils import DEFAULT_TMP_PREFIX, onWindows | |
25 | |
26 | |
27 def arg_parser() -> argparse.ArgumentParser: | |
28 parser = argparse.ArgumentParser( | |
29 description="Reference executor for Common Workflow Language standards. " | |
30 "Not for production use." | |
31 ) | |
32 parser.add_argument("--basedir", type=str) | |
33 parser.add_argument( | |
34 "--outdir", | |
35 type=str, | |
36 default=os.path.abspath("."), | |
37 help="Output directory. The default is the current directory.", | |
38 ) | |
39 | |
40 parser.add_argument( | |
41 "--parallel", | |
42 action="store_true", | |
43 default=False, | |
44 help="[experimental] Run jobs in parallel. ", | |
45 ) | |
46 envgroup = parser.add_mutually_exclusive_group() | |
47 envgroup.add_argument( | |
48 "--preserve-environment", | |
49 type=str, | |
50 action="append", | |
51 help="Preserve specific environment variable when running " | |
52 "CommandLineTools without a software container. May be provided " | |
53 "multiple times. The default is to preserve only the PATH.", | |
54 metavar="ENVVAR", | |
55 default=["PATH"], | |
56 dest="preserve_environment", | |
57 ) | |
58 envgroup.add_argument( | |
59 "--preserve-entire-environment", | |
60 action="store_true", | |
61 help="Preserve all environment variables when running CommandLineTools " | |
62 "without a software container.", | |
63 default=False, | |
64 dest="preserve_entire_environment", | |
65 ) | |
66 | |
67 containergroup = parser.add_mutually_exclusive_group() | |
68 containergroup.add_argument( | |
69 "--rm-container", | |
70 action="store_true", | |
71 default=True, | |
72 help="Delete Docker container used by jobs after they exit (default)", | |
73 dest="rm_container", | |
74 ) | |
75 | |
76 containergroup.add_argument( | |
77 "--leave-container", | |
78 action="store_false", | |
79 default=True, | |
80 help="Do not delete Docker container used by jobs after they exit", | |
81 dest="rm_container", | |
82 ) | |
83 | |
84 cidgroup = parser.add_argument_group( | |
85 "Options for recording the Docker container identifier into a file." | |
86 ) | |
87 cidgroup.add_argument( | |
88 # Disabled as containerid is now saved by default | |
89 "--record-container-id", | |
90 action="store_true", | |
91 default=False, | |
92 help=argparse.SUPPRESS, | |
93 dest="record_container_id", | |
94 ) | |
95 | |
96 cidgroup.add_argument( | |
97 "--cidfile-dir", | |
98 type=str, | |
99 help="Store the Docker container ID into a file in the specified directory.", | |
100 default=None, | |
101 dest="cidfile_dir", | |
102 ) | |
103 | |
104 cidgroup.add_argument( | |
105 "--cidfile-prefix", | |
106 type=str, | |
107 help="Specify a prefix to the container ID filename. " | |
108 "Final file name will be followed by a timestamp. " | |
109 "The default is no prefix.", | |
110 default=None, | |
111 dest="cidfile_prefix", | |
112 ) | |
113 | |
114 parser.add_argument( | |
115 "--tmpdir-prefix", | |
116 type=str, | |
117 help="Path prefix for temporary directories. If --tmpdir-prefix is not " | |
118 "provided, then the prefix for temporary directories is influenced by " | |
119 "the value of the TMPDIR, TEMP, or TMP environment variables. Taking " | |
120 "those into consideration, the current default is {}.".format( | |
121 DEFAULT_TMP_PREFIX | |
122 ), | |
123 default=DEFAULT_TMP_PREFIX, | |
124 ) | |
125 | |
126 intgroup = parser.add_mutually_exclusive_group() | |
127 intgroup.add_argument( | |
128 "--tmp-outdir-prefix", | |
129 type=str, | |
130 help="Path prefix for intermediate output directories. Defaults to the " | |
131 "value of --tmpdir-prefix.", | |
132 default="", | |
133 ) | |
134 | |
135 intgroup.add_argument( | |
136 "--cachedir", | |
137 type=str, | |
138 default="", | |
139 help="Directory to cache intermediate workflow outputs to avoid " | |
140 "recomputing steps. Can be very helpful in the development and " | |
141 "troubleshooting of CWL documents.", | |
142 ) | |
143 | |
144 tmpgroup = parser.add_mutually_exclusive_group() | |
145 tmpgroup.add_argument( | |
146 "--rm-tmpdir", | |
147 action="store_true", | |
148 default=True, | |
149 help="Delete intermediate temporary directories (default)", | |
150 dest="rm_tmpdir", | |
151 ) | |
152 | |
153 tmpgroup.add_argument( | |
154 "--leave-tmpdir", | |
155 action="store_false", | |
156 default=True, | |
157 help="Do not delete intermediate temporary directories", | |
158 dest="rm_tmpdir", | |
159 ) | |
160 | |
161 outgroup = parser.add_mutually_exclusive_group() | |
162 outgroup.add_argument( | |
163 "--move-outputs", | |
164 action="store_const", | |
165 const="move", | |
166 default="move", | |
167 help="Move output files to the workflow output directory and delete " | |
168 "intermediate output directories (default).", | |
169 dest="move_outputs", | |
170 ) | |
171 | |
172 outgroup.add_argument( | |
173 "--leave-outputs", | |
174 action="store_const", | |
175 const="leave", | |
176 default="move", | |
177 help="Leave output files in intermediate output directories.", | |
178 dest="move_outputs", | |
179 ) | |
180 | |
181 outgroup.add_argument( | |
182 "--copy-outputs", | |
183 action="store_const", | |
184 const="copy", | |
185 default="move", | |
186 help="Copy output files to the workflow output directory and don't " | |
187 "delete intermediate output directories.", | |
188 dest="move_outputs", | |
189 ) | |
190 | |
191 pullgroup = parser.add_mutually_exclusive_group() | |
192 pullgroup.add_argument( | |
193 "--enable-pull", | |
194 default=True, | |
195 action="store_true", | |
196 help="Try to pull Docker images", | |
197 dest="pull_image", | |
198 ) | |
199 | |
200 pullgroup.add_argument( | |
201 "--disable-pull", | |
202 default=True, | |
203 action="store_false", | |
204 help="Do not try to pull Docker images", | |
205 dest="pull_image", | |
206 ) | |
207 | |
208 parser.add_argument( | |
209 "--rdf-serializer", | |
210 help="Output RDF serialization format used by --print-rdf (one of " | |
211 "turtle (default), n3, nt, xml)", | |
212 default="turtle", | |
213 ) | |
214 | |
215 parser.add_argument( | |
216 "--eval-timeout", | |
217 help="Time to wait for a Javascript expression to evaluate before giving " | |
218 "an error, default 20s.", | |
219 type=float, | |
220 default=20, | |
221 ) | |
222 | |
223 provgroup = parser.add_argument_group( | |
224 "Options for recording provenance information of the execution" | |
225 ) | |
226 provgroup.add_argument( | |
227 "--provenance", | |
228 help="Save provenance to specified folder as a " | |
229 "Research Object that captures and aggregates " | |
230 "workflow execution and data products.", | |
231 type=str, | |
232 ) | |
233 | |
234 provgroup.add_argument( | |
235 "--enable-user-provenance", | |
236 default=False, | |
237 action="store_true", | |
238 help="Record user account info as part of provenance.", | |
239 dest="user_provenance", | |
240 ) | |
241 provgroup.add_argument( | |
242 "--disable-user-provenance", | |
243 default=False, | |
244 action="store_false", | |
245 help="Do not record user account info in provenance.", | |
246 dest="user_provenance", | |
247 ) | |
248 provgroup.add_argument( | |
249 "--enable-host-provenance", | |
250 default=False, | |
251 action="store_true", | |
252 help="Record host info as part of provenance.", | |
253 dest="host_provenance", | |
254 ) | |
255 provgroup.add_argument( | |
256 "--disable-host-provenance", | |
257 default=False, | |
258 action="store_false", | |
259 help="Do not record host info in provenance.", | |
260 dest="host_provenance", | |
261 ) | |
262 provgroup.add_argument( | |
263 "--orcid", | |
264 help="Record user ORCID identifier as part of " | |
265 "provenance, e.g. https://orcid.org/0000-0002-1825-0097 " | |
266 "or 0000-0002-1825-0097. Alternatively the environment variable " | |
267 "ORCID may be set.", | |
268 dest="orcid", | |
269 default=os.environ.get("ORCID", ""), | |
270 type=str, | |
271 ) | |
272 provgroup.add_argument( | |
273 "--full-name", | |
274 help="Record full name of user as part of provenance, " | |
275 "e.g. Josiah Carberry. You may need to use shell quotes to preserve " | |
276 "spaces. Alternatively the environment variable CWL_FULL_NAME may " | |
277 "be set.", | |
278 dest="cwl_full_name", | |
279 default=os.environ.get("CWL_FULL_NAME", ""), | |
280 type=str, | |
281 ) | |
282 | |
283 printgroup = parser.add_mutually_exclusive_group() | |
284 printgroup.add_argument( | |
285 "--print-rdf", | |
286 action="store_true", | |
287 help="Print corresponding RDF graph for workflow and exit", | |
288 ) | |
289 printgroup.add_argument( | |
290 "--print-dot", | |
291 action="store_true", | |
292 help="Print workflow visualization in graphviz format and exit", | |
293 ) | |
294 printgroup.add_argument( | |
295 "--print-pre", | |
296 action="store_true", | |
297 help="Print CWL document after preprocessing.", | |
298 ) | |
299 printgroup.add_argument( | |
300 "--print-deps", action="store_true", help="Print CWL document dependencies." | |
301 ) | |
302 printgroup.add_argument( | |
303 "--print-input-deps", | |
304 action="store_true", | |
305 help="Print input object document dependencies.", | |
306 ) | |
307 printgroup.add_argument( | |
308 "--pack", | |
309 action="store_true", | |
310 help="Combine components into single document and print.", | |
311 ) | |
312 printgroup.add_argument( | |
313 "--version", action="store_true", help="Print version and exit" | |
314 ) | |
315 printgroup.add_argument( | |
316 "--validate", action="store_true", help="Validate CWL document only." | |
317 ) | |
318 printgroup.add_argument( | |
319 "--print-supported-versions", | |
320 action="store_true", | |
321 help="Print supported CWL specs.", | |
322 ) | |
323 printgroup.add_argument( | |
324 "--print-subgraph", | |
325 action="store_true", | |
326 help="Print workflow subgraph that will execute. Can combined with " | |
327 "--target or --single-step", | |
328 ) | |
329 printgroup.add_argument( | |
330 "--print-targets", action="store_true", help="Print targets (output parameters)" | |
331 ) | |
332 printgroup.add_argument( | |
333 "--make-template", action="store_true", help="Generate a template input object" | |
334 ) | |
335 | |
336 strictgroup = parser.add_mutually_exclusive_group() | |
337 strictgroup.add_argument( | |
338 "--strict", | |
339 action="store_true", | |
340 help="Strict validation (unrecognized or out of place fields are error)", | |
341 default=True, | |
342 dest="strict", | |
343 ) | |
344 strictgroup.add_argument( | |
345 "--non-strict", | |
346 action="store_false", | |
347 help="Lenient validation (ignore unrecognized fields)", | |
348 default=True, | |
349 dest="strict", | |
350 ) | |
351 | |
352 parser.add_argument( | |
353 "--skip-schemas", | |
354 action="store_true", | |
355 help="Skip loading of schemas", | |
356 default=False, | |
357 dest="skip_schemas", | |
358 ) | |
359 | |
360 doccachegroup = parser.add_mutually_exclusive_group() | |
361 doccachegroup.add_argument( | |
362 "--no-doc-cache", | |
363 action="store_false", | |
364 help="Disable disk cache for documents loaded over HTTP", | |
365 default=True, | |
366 dest="doc_cache", | |
367 ) | |
368 doccachegroup.add_argument( | |
369 "--doc-cache", | |
370 action="store_true", | |
371 help="Enable disk cache for documents loaded over HTTP", | |
372 default=True, | |
373 dest="doc_cache", | |
374 ) | |
375 | |
376 volumegroup = parser.add_mutually_exclusive_group() | |
377 volumegroup.add_argument("--verbose", action="store_true", help="Default logging") | |
378 volumegroup.add_argument( | |
379 "--quiet", action="store_true", help="Only print warnings and errors." | |
380 ) | |
381 volumegroup.add_argument( | |
382 "--debug", action="store_true", help="Print even more logging" | |
383 ) | |
384 | |
385 parser.add_argument( | |
386 "--strict-memory-limit", | |
387 action="store_true", | |
388 help="When running with " | |
389 "software containers and the Docker engine, pass either the " | |
390 "calculated memory allocation from ResourceRequirements or the " | |
391 "default of 1 gigabyte to Docker's --memory option.", | |
392 ) | |
393 | |
394 parser.add_argument( | |
395 "--timestamps", | |
396 action="store_true", | |
397 help="Add timestamps to the errors, warnings, and notifications.", | |
398 ) | |
399 parser.add_argument( | |
400 "--js-console", action="store_true", help="Enable javascript console output" | |
401 ) | |
402 parser.add_argument( | |
403 "--disable-js-validation", | |
404 action="store_true", | |
405 help="Disable javascript validation.", | |
406 ) | |
407 parser.add_argument( | |
408 "--js-hint-options-file", | |
409 type=str, | |
410 help="File of options to pass to jshint. " | |
411 'This includes the added option "includewarnings". ', | |
412 ) | |
413 dockergroup = parser.add_mutually_exclusive_group() | |
414 dockergroup.add_argument( | |
415 "--user-space-docker-cmd", | |
416 metavar="CMD", | |
417 help="(Linux/OS X only) Specify the path to udocker. Implies --udocker", | |
418 ) | |
419 dockergroup.add_argument( | |
420 "--udocker", | |
421 help="(Linux/OS X only) Use the udocker runtime for running containers " | |
422 "(equivalent to --user-space-docker-cmd=udocker).", | |
423 action="store_const", | |
424 const="udocker", | |
425 dest="user_space_docker_cmd", | |
426 ) | |
427 | |
428 dockergroup.add_argument( | |
429 "--singularity", | |
430 action="store_true", | |
431 default=False, | |
432 help="[experimental] Use " | |
433 "Singularity runtime for running containers. " | |
434 "Requires Singularity v2.6.1+ and Linux with kernel " | |
435 "version v3.18+ or with overlayfs support " | |
436 "backported.", | |
437 ) | |
438 dockergroup.add_argument( | |
439 "--no-container", | |
440 action="store_false", | |
441 default=True, | |
442 help="Do not execute jobs in a " | |
443 "Docker container, even when `DockerRequirement` " | |
444 "is specified under `hints`.", | |
445 dest="use_container", | |
446 ) | |
447 | |
448 dependency_resolvers_configuration_help = argparse.SUPPRESS | |
449 dependencies_directory_help = argparse.SUPPRESS | |
450 use_biocontainers_help = argparse.SUPPRESS | |
451 conda_dependencies = argparse.SUPPRESS | |
452 | |
453 if SOFTWARE_REQUIREMENTS_ENABLED: | |
454 dependency_resolvers_configuration_help = "Dependency resolver " | |
455 "configuration file describing how to adapt 'SoftwareRequirement' " | |
456 "packages to current system." | |
457 dependencies_directory_help = ( | |
458 "Defaut root directory used by dependency resolvers configuration." | |
459 ) | |
460 use_biocontainers_help = "Use biocontainers for tools without an " | |
461 "explicitly annotated Docker container." | |
462 conda_dependencies = ( | |
463 "Short cut to use Conda to resolve 'SoftwareRequirement' packages." | |
464 ) | |
465 | |
466 parser.add_argument( | |
467 "--beta-dependency-resolvers-configuration", | |
468 default=None, | |
469 help=dependency_resolvers_configuration_help, | |
470 ) | |
471 parser.add_argument( | |
472 "--beta-dependencies-directory", default=None, help=dependencies_directory_help | |
473 ) | |
474 parser.add_argument( | |
475 "--beta-use-biocontainers", | |
476 default=None, | |
477 help=use_biocontainers_help, | |
478 action="store_true", | |
479 ) | |
480 parser.add_argument( | |
481 "--beta-conda-dependencies", | |
482 default=None, | |
483 help=conda_dependencies, | |
484 action="store_true", | |
485 ) | |
486 | |
487 parser.add_argument( | |
488 "--tool-help", action="store_true", help="Print command line help for tool" | |
489 ) | |
490 | |
491 parser.add_argument( | |
492 "--relative-deps", | |
493 choices=["primary", "cwd"], | |
494 default="primary", | |
495 help="When using --print-deps, print paths " | |
496 "relative to primary file or current working directory.", | |
497 ) | |
498 | |
499 parser.add_argument( | |
500 "--enable-dev", | |
501 action="store_true", | |
502 help="Enable loading and running unofficial development versions of " | |
503 "the CWL standards.", | |
504 default=False, | |
505 ) | |
506 | |
507 parser.add_argument( | |
508 "--enable-ext", | |
509 action="store_true", | |
510 help="Enable loading and running 'cwltool:' extensions to the CWL standards.", | |
511 default=False, | |
512 ) | |
513 | |
514 colorgroup = parser.add_mutually_exclusive_group() | |
515 colorgroup.add_argument( | |
516 "--enable-color", | |
517 action="store_true", | |
518 help="Enable logging color (default enabled)", | |
519 default=not onWindows(), | |
520 ) | |
521 colorgroup.add_argument( | |
522 "--disable-color", | |
523 action="store_false", | |
524 dest="enable_color", | |
525 help="Disable colored logging (default false)", | |
526 default=onWindows(), | |
527 ) | |
528 | |
529 parser.add_argument( | |
530 "--default-container", | |
531 help="Specify a default software container to use for any " | |
532 "CommandLineTool without a DockerRequirement.", | |
533 ) | |
534 parser.add_argument( | |
535 "--no-match-user", | |
536 action="store_true", | |
537 help="Disable passing the current uid to `docker run --user`", | |
538 ) | |
539 parser.add_argument( | |
540 "--custom-net", | |
541 type=str, | |
542 help="Passed to `docker run` as the '--net' parameter when " | |
543 "NetworkAccess is true, which is its default setting.", | |
544 ) | |
545 parser.add_argument( | |
546 "--disable-validate", | |
547 dest="do_validate", | |
548 action="store_false", | |
549 default=True, | |
550 help=argparse.SUPPRESS, | |
551 ) | |
552 | |
553 reggroup = parser.add_mutually_exclusive_group() | |
554 reggroup.add_argument( | |
555 "--enable-ga4gh-tool-registry", | |
556 action="store_true", | |
557 help="Enable tool resolution using GA4GH tool registry API", | |
558 dest="enable_ga4gh_tool_registry", | |
559 default=True, | |
560 ) | |
561 reggroup.add_argument( | |
562 "--disable-ga4gh-tool-registry", | |
563 action="store_false", | |
564 help="Disable tool resolution using GA4GH tool registry API", | |
565 dest="enable_ga4gh_tool_registry", | |
566 default=True, | |
567 ) | |
568 | |
569 parser.add_argument( | |
570 "--add-ga4gh-tool-registry", | |
571 action="append", | |
572 help="Add a GA4GH tool registry endpoint to use for resolution, default %s" | |
573 % ga4gh_tool_registries, | |
574 dest="ga4gh_tool_registries", | |
575 default=[], | |
576 ) | |
577 | |
578 parser.add_argument( | |
579 "--on-error", | |
580 help="Desired workflow behavior when a step fails. One of 'stop' (do " | |
581 "not submit any more steps) or 'continue' (may submit other steps that " | |
582 "are not downstream from the error). Default is 'stop'.", | |
583 default="stop", | |
584 choices=("stop", "continue"), | |
585 ) | |
586 | |
587 checkgroup = parser.add_mutually_exclusive_group() | |
588 checkgroup.add_argument( | |
589 "--compute-checksum", | |
590 action="store_true", | |
591 default=True, | |
592 help="Compute checksum of contents while collecting outputs", | |
593 dest="compute_checksum", | |
594 ) | |
595 checkgroup.add_argument( | |
596 "--no-compute-checksum", | |
597 action="store_false", | |
598 help="Do not compute checksum of contents while collecting outputs", | |
599 dest="compute_checksum", | |
600 ) | |
601 | |
602 parser.add_argument( | |
603 "--relax-path-checks", | |
604 action="store_true", | |
605 default=False, | |
606 help="Relax requirements on path names to permit " | |
607 "spaces and hash characters.", | |
608 dest="relax_path_checks", | |
609 ) | |
610 | |
611 parser.add_argument( | |
612 "--force-docker-pull", | |
613 action="store_true", | |
614 default=False, | |
615 help="Pull latest software container image even if it is locally present", | |
616 dest="force_docker_pull", | |
617 ) | |
618 parser.add_argument( | |
619 "--no-read-only", | |
620 action="store_true", | |
621 default=False, | |
622 help="Do not set root directory in the container as read-only", | |
623 dest="no_read_only", | |
624 ) | |
625 | |
626 parser.add_argument( | |
627 "--overrides", | |
628 type=str, | |
629 default=None, | |
630 help="Read process requirement overrides from file.", | |
631 ) | |
632 | |
633 subgroup = parser.add_mutually_exclusive_group() | |
634 subgroup.add_argument( | |
635 "--target", | |
636 "-t", | |
637 action="append", | |
638 help="Only execute steps that contribute to listed targets (can be " | |
639 "provided more than once).", | |
640 ) | |
641 subgroup.add_argument( | |
642 "--single-step", | |
643 type=str, | |
644 default=None, | |
645 help="Only executes a single step in a workflow. The input object must " | |
646 "match that step's inputs. Can be combined with --print-subgraph.", | |
647 ) | |
648 | |
649 parser.add_argument( | |
650 "--mpi-config-file", | |
651 type=str, | |
652 default=None, | |
653 help="Platform specific configuration for MPI (parallel launcher, its " | |
654 "flag etc). See README section 'Running MPI-based tools' for details " | |
655 "of the format.", | |
656 ) | |
657 | |
658 parser.add_argument( | |
659 "workflow", | |
660 type=str, | |
661 nargs="?", | |
662 default=None, | |
663 metavar="cwl_document", | |
664 help="path or URL to a CWL Workflow, " | |
665 "CommandLineTool, or ExpressionTool. If the `inputs_object` has a " | |
666 "`cwl:tool` field indicating the path or URL to the cwl_document, " | |
667 " then the `cwl_document` argument is optional.", | |
668 ) | |
669 parser.add_argument( | |
670 "job_order", | |
671 nargs=argparse.REMAINDER, | |
672 metavar="inputs_object", | |
673 help="path or URL to a YAML or JSON " | |
674 "formatted description of the required input values for the given " | |
675 "`cwl_document`.", | |
676 ) | |
677 | |
678 return parser | |
679 | |
680 | |
681 def get_default_args() -> Dict[str, Any]: | |
682 """Get default values of cwltool's command line options.""" | |
683 ap = arg_parser() | |
684 args = ap.parse_args([]) | |
685 return vars(args) | |
686 | |
687 | |
688 class FSAction(argparse.Action): | |
689 objclass = None # type: str | |
690 | |
691 def __init__( | |
692 self, option_strings: List[str], dest: str, nargs: Any = None, **kwargs: Any | |
693 ) -> None: | |
694 """Fail if nargs is used.""" | |
695 if nargs is not None: | |
696 raise ValueError("nargs not allowed") | |
697 super().__init__(option_strings, dest, **kwargs) | |
698 | |
699 def __call__( | |
700 self, | |
701 parser: argparse.ArgumentParser, | |
702 namespace: argparse.Namespace, | |
703 values: Union[AnyStr, Sequence[Any], None], | |
704 option_string: Optional[str] = None, | |
705 ) -> None: | |
706 setattr( | |
707 namespace, | |
708 self.dest, | |
709 { | |
710 "class": self.objclass, | |
711 "location": file_uri(str(os.path.abspath(cast(AnyStr, values)))), | |
712 }, | |
713 ) | |
714 | |
715 | |
716 class FSAppendAction(argparse.Action): | |
717 objclass = None # type: str | |
718 | |
719 def __init__( | |
720 self, option_strings: List[str], dest: str, nargs: Any = None, **kwargs: Any | |
721 ) -> None: | |
722 """Initialize.""" | |
723 if nargs is not None: | |
724 raise ValueError("nargs not allowed") | |
725 super().__init__(option_strings, dest, **kwargs) | |
726 | |
727 def __call__( | |
728 self, | |
729 parser: argparse.ArgumentParser, | |
730 namespace: argparse.Namespace, | |
731 values: Union[AnyStr, Sequence[Any], None], | |
732 option_string: Optional[str] = None, | |
733 ) -> None: | |
734 g = getattr(namespace, self.dest) | |
735 if not g: | |
736 g = [] | |
737 setattr(namespace, self.dest, g) | |
738 g.append( | |
739 { | |
740 "class": self.objclass, | |
741 "location": file_uri(str(os.path.abspath(cast(AnyStr, values)))), | |
742 } | |
743 ) | |
744 | |
745 | |
746 class FileAction(FSAction): | |
747 objclass = "File" | |
748 | |
749 | |
750 class DirectoryAction(FSAction): | |
751 objclass = "Directory" | |
752 | |
753 | |
754 class FileAppendAction(FSAppendAction): | |
755 objclass = "File" | |
756 | |
757 | |
758 class DirectoryAppendAction(FSAppendAction): | |
759 objclass = "Directory" | |
760 | |
761 | |
762 def add_argument( | |
763 toolparser: argparse.ArgumentParser, | |
764 name: str, | |
765 inptype: Any, | |
766 records: List[str], | |
767 description: str = "", | |
768 default: Any = None, | |
769 input_required: bool = True, | |
770 ) -> None: | |
771 if len(name) == 1: | |
772 flag = "-" | |
773 else: | |
774 flag = "--" | |
775 | |
776 # if input_required is false, don't make the command line | |
777 # parameter required. | |
778 required = default is None and input_required | |
779 if isinstance(inptype, MutableSequence): | |
780 if inptype[0] == "null": | |
781 required = False | |
782 if len(inptype) == 2: | |
783 inptype = inptype[1] | |
784 else: | |
785 _logger.debug("Can't make command line argument from %s", inptype) | |
786 return None | |
787 | |
788 ahelp = description.replace("%", "%%") | |
789 action = None # type: Optional[Union[argparse.Action, str]] | |
790 atype = None # type: Any | |
791 | |
792 if inptype == "File": | |
793 action = cast(argparse.Action, FileAction) | |
794 elif inptype == "Directory": | |
795 action = cast(argparse.Action, DirectoryAction) | |
796 elif isinstance(inptype, MutableMapping) and inptype["type"] == "array": | |
797 if inptype["items"] == "File": | |
798 action = cast(argparse.Action, FileAppendAction) | |
799 elif inptype["items"] == "Directory": | |
800 action = cast(argparse.Action, DirectoryAppendAction) | |
801 else: | |
802 action = "append" | |
803 elif isinstance(inptype, MutableMapping) and inptype["type"] == "enum": | |
804 atype = str | |
805 elif isinstance(inptype, MutableMapping) and inptype["type"] == "record": | |
806 records.append(name) | |
807 for field in inptype["fields"]: | |
808 fieldname = name + "." + shortname(field["name"]) | |
809 fieldtype = field["type"] | |
810 fielddescription = field.get("doc", "") | |
811 add_argument(toolparser, fieldname, fieldtype, records, fielddescription) | |
812 return | |
813 elif inptype == "string": | |
814 atype = str | |
815 elif inptype == "int": | |
816 atype = int | |
817 elif inptype == "double": | |
818 atype = float | |
819 elif inptype == "float": | |
820 atype = float | |
821 elif inptype == "boolean": | |
822 action = "store_true" | |
823 else: | |
824 _logger.debug("Can't make command line argument from %s", inptype) | |
825 return None | |
826 | |
827 if inptype != "boolean": | |
828 typekw = {"type": atype} | |
829 else: | |
830 typekw = {} | |
831 | |
832 toolparser.add_argument( | |
833 flag + name, | |
834 required=required, | |
835 help=ahelp, | |
836 action=action, # type: ignore | |
837 default=default, | |
838 **typekw | |
839 ) | |
840 | |
841 | |
842 def generate_parser( | |
843 toolparser: argparse.ArgumentParser, | |
844 tool: Process, | |
845 namemap: Dict[str, str], | |
846 records: List[str], | |
847 input_required: bool = True, | |
848 ) -> argparse.ArgumentParser: | |
849 toolparser.description = tool.tool.get("doc", None) | |
850 toolparser.add_argument("job_order", nargs="?", help="Job input json file") | |
851 namemap["job_order"] = "job_order" | |
852 | |
853 for inp in tool.tool["inputs"]: | |
854 name = shortname(inp["id"]) | |
855 namemap[name.replace("-", "_")] = name | |
856 inptype = inp["type"] | |
857 description = inp.get("doc", "") | |
858 default = inp.get("default", None) | |
859 add_argument( | |
860 toolparser, name, inptype, records, description, default, input_required | |
861 ) | |
862 | |
863 return toolparser |