20
20
import subprocess
21
21
22
22
__version__ = '99.99.99'
23
- __copyright__ = (
24
- 'Copyright 2021, The NiPreps Developers'
25
- )
23
+ __copyright__ = 'Copyright 2022, The NiPreps Developers'
26
24
__bugreports__ = 'https://github.com/nipreps/nibabies/issues'
27
25
28
26
MISSING = """
@@ -120,9 +118,7 @@ def add_mount(self, src, dst, read_only=True):
120
118
dst : absolute container path
121
119
read_only : disable writing to bound path
122
120
"""
123
- self .mounts .append (
124
- "{0}:{1}{2}" .format (src , dst , ":ro" if read_only else "" )
125
- )
121
+ self .mounts .append ("{0}:{1}{2}" .format (src , dst , ":ro" if read_only else "" ))
126
122
127
123
def check_install (self ):
128
124
"""Verify that the service is installed and the user has permission to
@@ -153,9 +149,7 @@ def check_install(self):
153
149
def check_image (self , image ):
154
150
"""Check whether image is present on local system"""
155
151
if self .service == "docker" :
156
- ret = subprocess .run (
157
- ["docker" , "images" , "-q" , image ], stdout = subprocess .PIPE
158
- )
152
+ ret = subprocess .run (["docker" , "images" , "-q" , image ], stdout = subprocess .PIPE )
159
153
return bool (ret .stdout )
160
154
elif self .service == "singularity" :
161
155
# check if the image file exists
@@ -349,9 +343,7 @@ def _is_file(path, parser):
349
343
"""Ensure a given path exists and it is a file."""
350
344
path = os .path .abspath (path )
351
345
if not os .path .isfile (path ):
352
- raise parser .error (
353
- "Path should point to a file (or symlink of file): <%s>." % path
354
- )
346
+ raise parser .error ("Path should point to a file (or symlink of file): <%s>." % path )
355
347
return path
356
348
357
349
parser = argparse .ArgumentParser (
@@ -467,14 +459,14 @@ def _is_file(path, parser):
467
459
"--segmentation-atlases-dir" ,
468
460
metavar = "PATH" ,
469
461
type = os .path .abspath ,
470
- help = "Directory containing prelabeled segmentations to use for JointLabelFusion."
462
+ help = "Directory containing prelabeled segmentations to use for JointLabelFusion." ,
471
463
)
472
464
g_wrap .add_argument (
473
465
"--derivatives" ,
474
466
nargs = "+" ,
475
467
metavar = "PATH" ,
476
468
type = os .path .abspath ,
477
- help = "One or more directory containing pre-computed derivatives"
469
+ help = "One or more directory containing pre-computed derivatives" ,
478
470
)
479
471
g_wrap .add_argument (
480
472
"--bids-filter-file" ,
@@ -533,9 +525,7 @@ def _is_file(path, parser):
533
525
help = "Run container with a different network driver "
534
526
'("none" to simulate no internet connection)' ,
535
527
)
536
- g_dev .add_argument (
537
- "--no-tty" , action = "store_true" , help = "Run docker without TTY flag -it"
538
- )
528
+ g_dev .add_argument ("--no-tty" , action = "store_true" , help = "Run docker without TTY flag -it" )
539
529
540
530
return parser
541
531
@@ -552,44 +542,35 @@ def main():
552
542
return
553
543
554
544
# Set help if no directories set
555
- if not all ((opts .service , opts .bids_dir , opts .output_dir )):
545
+ if opts . help or not all ((opts .service , opts .bids_dir , opts .output_dir )):
556
546
parser .print_help ()
557
- return 1
547
+ return
558
548
559
549
container = ContainerManager (opts .service , image = opts .image )
560
550
check = container .check_install ()
561
551
if check < 1 :
562
- if opts .version :
563
- print ("nibabies wrapper {!s}" .format (__version__ ))
564
- if opts .help :
565
- parser .print_help ()
566
552
if check == - 1 :
567
553
print (
568
- "nibabies: Could not find %s command... Is it installed?"
569
- % opts . service
554
+ "nibabies: Could not find %s command... Is it installed?" % opts . service ,
555
+ file = sys . stderr ,
570
556
)
571
557
else :
572
558
print (
573
- "nibabies: Make sure you have permission to run '%s'" % opts .service
559
+ "nibabies: Make sure you have permission to run '%s'" % opts .service ,
560
+ file = sys .stderr ,
574
561
)
575
562
return 1
576
563
577
- # For --help or --version, ask before downloading an image
578
564
if not container .check_image (opts .image ):
579
565
resp = "Y"
580
- if opts .version :
581
- print ("nibabies wrapper {!s}" .format (__version__ ))
582
- if opts .help :
583
- parser .print_help ()
584
- if opts .version == "singularity" :
585
- print ("Singularity images must already exist locally." )
566
+ if opts .service == "singularity" :
567
+ print ("Singularity image must already exist locally." , file = sys .stderr )
568
+ return 1
569
+ try :
570
+ resp = input (MISSING .format (opts .image ))
571
+ except KeyboardInterrupt :
572
+ print ()
586
573
return 1
587
- if opts .version or opts .help :
588
- try :
589
- resp = input (MISSING .format (opts .image ))
590
- except KeyboardInterrupt :
591
- print ()
592
- return 1
593
574
if resp not in ("y" , "Y" , "" ):
594
575
return 0
595
576
print ("Downloading. This may take a while..." )
@@ -602,10 +583,7 @@ def main():
602
583
"Do you have permission to run docker?"
603
584
)
604
585
return 1
605
- if (
606
- not (opts .help or opts .version or "--reports-only" in unknown_args )
607
- and mem_total < 8000
608
- ):
586
+ if "--reports-only" not in unknown_args and mem_total < 8000 :
609
587
print (
610
588
"Warning: <8GB of RAM is available within your environment.\n "
611
589
"Some parts of nibabies may fail to complete."
@@ -624,11 +602,7 @@ def main():
624
602
625
603
if opts .service == "docker" :
626
604
if not opts .no_tty :
627
- if opts .help :
628
- # TTY can corrupt stdout
629
- container .add_cmd ("-i" )
630
- else :
631
- container .add_cmd ("-it" )
605
+ container .add_cmd ("-it" )
632
606
if opts .user :
633
607
container .add_cmd (("-u" , opts .user ))
634
608
if opts .network :
@@ -718,9 +692,7 @@ def main():
718
692
if space .split (":" )[0 ] not in (TF_TEMPLATES + NONSTANDARD_REFERENCES ):
719
693
tpl = os .path .basename (space )
720
694
if not tpl .startswith ("tpl-" ):
721
- raise RuntimeError (
722
- "Custom template %s requires a `tpl-` prefix" % tpl
723
- )
695
+ raise RuntimeError ("Custom template %s requires a `tpl-` prefix" % tpl )
724
696
target = "/home/fmriprep/.cache/templateflow/" + tpl
725
697
container .add_mount (os .path .abspath (space ), target )
726
698
spaces .append (tpl [4 :])
@@ -742,16 +714,16 @@ def main():
742
714
743
715
# Override help and version to describe underlying program
744
716
# Respects '-i' flag, so will retrieve information from any image
745
- if opts .help :
746
- container .add_cmd ("-h" )
747
- targethelp = subprocess .check_output (container .command ).decode ()
748
- print (merge_help (parser .format_help (), targethelp ))
749
- return 0
750
- elif opts .version :
751
- # Get version to be run and exit
752
- container .add_cmd ("--version" )
753
- ret = subprocess .run (container .command )
754
- return ret .returncode
717
+ # if opts.help:
718
+ # container.add_cmd("-h")
719
+ # targethelp = subprocess.check_output(container.command).decode()
720
+ # print(merge_help(parser.format_help(), targethelp))
721
+ # return 0
722
+ # elif opts.version:
723
+ # # Get version to be run and exit
724
+ # container.add_cmd("--version")
725
+ # ret = subprocess.run(container.command)
726
+ # return ret.returncode
755
727
756
728
if not opts .shell :
757
729
container .add_cmd (main_args )
0 commit comments