Example #1
0
    def check_special_opts(self, argv):
        """process known '-optlist_* options' and other global_opts,
           nuking them from argv

           some options are terminal
        """

        # global options (some take a parameter)
        global_opts = [ '-optlist_verbose', '-optlist_no_show_count',
                        '-all_opts', '-h_find', '-h_view' ]

        alen = len(argv)

        if '-optlist_verbose' in argv:
            ind = argv.index('-optlist_verbose')
            self.verb = 4
            argv[ind:ind+1] = []
            print '++ optlist: setting verb to %d' % self.verb
        if '-optlist_no_show_count' in argv:
            ind = argv.index('-optlist_no_show_count')
            self.show_count = 0
            argv[ind:ind+1] = []
            if self.verb>1: print '++ optlist: clearing show_count'

        # terminal options (all end in exit)
        if '-all_opts' in argv:
            oname = '-all_opts'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            self.show(verb=1)
            sys.exit(0)

        if '-h_find' in argv:
            oname = '-h_find'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            if ind == alen-1:
               print '** global opt %s needs %s option as parameter' \
                     % (oname, prog)
               sys.exit(1)
            cmd = 'apsearch -phelp %s -word %s' % (prog, argv[ind+1])
            if self.verb>1: print '++ optlist: applying %s via: %s'%(oname,cmd)
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        if '-h_view' in argv:
            oname = '-h_view'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            cmd = 'apsearch -view_prog_help %s' % prog
            if self.verb>1: print '++ optlist: applying %s via: %s'%(oname,cmd)
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        if self.verb > 1:
            print '-- argv: orig len %d, new len %d' % (alen,len(argv))
Example #2
0
    def check_for_progs(self, plist, repos=0, show_missing=0):
        """see whether the programs seem to be found, and show version

         repos:        if set, append any found to self.repo_prog list
         show_missing: if set, show empty prog line to indicate failed test
      """
        # check for programs
        nfound = 0
        for prog in plist:
            # the version file is treated specially here
            if prog == 'AFNI_version.txt':
                vinfo = UTIL.read_AFNI_version_file()
                if vinfo != '':
                    nfound += 1
                else:
                    self.comments.append('missing %s, maybe package is old' %
                                         prog)
                print('%-20s : %s' % (prog, vinfo))
                continue

            # as is afni
            elif prog == 'afni label':
                nfound += 1  # do not call this an error yet
                s, v = self.get_prog_version('afni')
                print('%-20s : %s' % ('', self.afni_label))
                continue

            # and python - add a comment if they are using version 3 (no continue)
            #            - do not 'continue'
            elif prog == 'python':
                s, vstr = self.get_prog_version(prog)
                vf = self.get_python_ver_float()
                mesg = ''
                if vf >= 3.0:
                    mesg = 'have python version %s, some programs need 2.7.x' % vstr
                elif vf < 2.7:
                    mesg = 'have python version %s, consider using 2.7.x' % vstr
                if mesg != '':
                    self.comments.append(mesg)

            # now run the normal test

            cmd = 'which %s' % prog
            s, so, se = BASE.simple_shell_exec(cmd, capture=1)
            if not s:  # found one
                # if we do not yet know of a repo program, mark as this one
                if repos and self.repo_prog == '': self.repo_prog = prog

                print('%-20s : %s' % (cmd, so.strip()))
                s, v = self.get_prog_version(prog)
                if s: print('%-20s : %s' % ('%s version' % prog, v))

                if prog == 'afni': self.afni_ver = v  # save result
                nfound += 1
            elif show_missing:
                print('%-20s : %s' % (cmd, se))

        print('')

        return nfound
Example #3
0
 def show_general_afni_info(self, header=1):
    print UTIL.section_divider('AFNI and related program tests', hchar='-')
    for prog in ['afni', 'python', 'R', 'tcsh']:
       cmd = 'which %s' % prog
       s, so, se = BASE.simple_shell_exec(cmd, capture=1)
       if s: print '%-20s : %s' % (cmd, se)
       else:
          print '%-20s : %s' % (cmd, so.strip())
          s, v = self.get_prog_version(prog)
          if s:
              print '%-20s : %s' % ('%s version'%prog, v)
              if prog == 'afni': self.afni_ver = v # save result
Example #4
0
 def show_general_afni_info(self, header=1):
     print UTIL.section_divider('AFNI and related program tests', hchar='-')
     for prog in ['afni', 'python', 'R', 'tcsh']:
         cmd = 'which %s' % prog
         s, so, se = BASE.simple_shell_exec(cmd, capture=1)
         if s: print '%-20s : %s' % (cmd, se)
         else:
             print '%-20s : %s' % (cmd, so.strip())
             s, v = self.get_prog_version(prog)
             if s:
                 print '%-20s : %s' % ('%s version' % prog, v)
                 if prog == 'afni': self.afni_ver = v  # save result
Example #5
0
def copy_images_to_subdir(iopts):

    count_fail = 0
    list_fail = []

    for ii in range(iopts.nmedia):
        iname = iopts.list_media[ii]
        oname = iopts.subdir + '/' + os.path.basename(iopts.list_media[ii])
        a, b, c = ab.simple_shell_exec("\cp {} {}".format(iname, oname))
        if a:
            count_fail += 1
            list_fail.append(iname)

    return count_fail, list_fail
Example #6
0
    def check_for_progs(self, plist, repos=0, show_missing=0):
        """see whether the programs seem to be found, and show version

         repos:        if set, append any found to self.repo_prog list
         show_missing: if set, show empty prog line to indicate failed test
      """
        # check for programs
        nfound = 0
        for prog in plist:
            # the version file is treated specially here
            if prog == 'AFNI_version.txt':
                vinfo = UTIL.read_AFNI_version_file()
                if vinfo != '':
                    nfound += 1
                else:
                    self.comments.append('missing %s, maybe package is old' %
                                         prog)
                print '%-20s : %s' % (prog, vinfo)
                continue

            elif prog == 'afni label':
                nfound += 1  # do not call this an error yet
                s, v = self.get_prog_version('afni')
                print '%-20s : %s' % ('', self.afni_label)
                continue

            cmd = 'which %s' % prog
            s, so, se = BASE.simple_shell_exec(cmd, capture=1)
            if not s:  # found one
                # if we do not yet know of a repo program, mark as this one
                if repos and self.repo_prog == '': self.repo_prog = prog

                print '%-20s : %s' % (cmd, so.strip())
                s, v = self.get_prog_version(prog)
                if s: print '%-20s : %s' % ('%s version' % prog, v)

                if prog == 'afni': self.afni_ver = v  # save result
                nfound += 1
            elif show_missing:
                print '%-20s : %s' % (cmd, se)

        print

        return nfound
Example #7
0
def exec_or_error(cmd, error_msg="See above!!!", showerr=1, showcmd=1):
    """return text output from the given command
       if showcmd, print the cmd
       if showerr and there is a command error, show it
       This will exit on status other than 0 so BE CAREFULL!!!
    """

    status, cmd_output, error_out = afni_base.simple_shell_exec(cmd, capture=1)
    if showcmd:
        print(cmd)
    if showerr:
        print(error_out)
    if status != 0:
        if error_msg != "See above!!!":
            error_msg = error_msg + "!!!"
        printNcolor("** ERROR:", " " + error_msg, "Red", "White", "Bold")
        sys.exit(1)

    return status, cmd_output
Example #8
0
   def check_for_progs(self, plist, repos=0, show_missing=0):
      """see whether the programs seem to be found, and show version

         repos:        if set, append any found to self.repo_prog list
         show_missing: if set, show empty prog line to indicate failed test
      """
      # check for programs
      nfound = 0
      for prog in plist:
         # the version file is treated specially here
         if prog == 'AFNI_version.txt':
            vinfo = UTIL.read_AFNI_version_file()
            if vinfo != '':
               nfound += 1
            else:
               self.comments.append('missing %s, maybe package is old'%prog)
            print '%-20s : %s' % (prog, vinfo)
            continue

         cmd = 'which %s' % prog
         s, so, se = BASE.simple_shell_exec(cmd, capture=1)
         if not s: # found one
            # if we do not yet know of a repo program, mark as this one
            if repos and self.repo_prog == '': self.repo_prog = prog

            print '%-20s : %s' % (cmd, so.strip())
            s, v = self.get_prog_version(prog)
            if s: print '%-20s : %s' % ('%s version'%prog, v)

            if prog == 'afni': self.afni_ver = v # save result
            nfound += 1
         elif show_missing:
            print '%-20s : %s' % (cmd, se)

      print

      return nfound
Example #9
0
    def check_special_opts(self, argv):
        """process known '-optlist_* options' and other global_opts,
           nuking them from argv

           some options are terminal
        """

        # global options (some take a parameter)
        global_opts = [
            '-optlist_verbose', '-optlist_no_show_count',
            '-optlist_show_global_opts', '-optlist_show_valid_opts',
            '-optlist_show_argv_array', '-h_find', '-h_view', '-hview'
        ]

        alen = len(argv)

        if '-optlist_verbose' in argv:
            ind = argv.index('-optlist_verbose')
            self.verb = 4
            argv[ind:ind + 1] = []
            print('++ optlist: setting verb to %d' % self.verb)
        if '-optlist_no_show_count' in argv:
            ind = argv.index('-optlist_no_show_count')
            self.show_count = 0
            argv[ind:ind + 1] = []
            if self.verb > 1: print('++ optlist: clearing show_count')

        # terminal options (all end in exit)

        # terminal opts specific to this library
        if '-optlist_show_global_opts' in argv:
            global_opts.sort()
            print("-- global OptionList options (%d):" % len(global_opts))
            print("     %s\n" % '\n     '.join(global_opts))
            sys.exit(0)

        if '-optlist_show_valid_opts' in argv:
            oname = '-optlist_show_valid_opts'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            self.show(verb=1)
            sys.exit(0)

        if '-optlist_show_argv_array' in argv:
            oname = '-optlist_show_argv_array'
            ind = argv.index(oname)
            # this takes one parameter, which must be in list
            atype = ''
            if alen >= ind + 2:
                atype = argv[ind + 1]
            if atype not in self.argv_array_types:
                print("** %s: requires a parameter in %s" \
                      % (oname, self.argv_array_types))
                sys.exit(1)
            argv[ind:ind + 2] = []
            self.show_argv_array = atype

        # terminal general options
        if '-h_find' in argv:
            oname = '-h_find'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            if ind == alen - 1:
                print('** global opt %s needs %s option as parameter' \
                      % (oname, prog))
                sys.exit(1)
            cmd = 'apsearch -phelp %s -word %s' % (prog, argv[ind + 1])
            if self.verb > 1:
                print('++ optlist: applying %s via: %s' % (oname, cmd))
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        if '-h_view' in argv:
            oname = '-h_view'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            cmd = 'apsearch -view_prog_help %s' % prog
            if self.verb > 1:
                print('++ optlist: applying %s via: %s' % (oname, cmd))
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        if '-hview' in argv:
            oname = '-hview'
            ind = argv.index(oname)
            prog = os.path.basename(argv[0])
            cmd = 'apsearch -view_prog_help %s' % prog
            if self.verb > 1:
                print('++ optlist: applying %s via: %s' % (oname, cmd))
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        if self.verb > 1:
            print('-- argv: orig len %d, new len %d' % (alen, len(argv)))
Example #10
0
    return data


# --------------------------------------------------------------------------

if __name__ == "__main__":

    for n in ncolors:

        print("Ncolors = {}".format(n))

        nn = n + 1
        sn = str(n)
        snn = str(nn)
        ncolor = snn + ' '
        ofile = "ROI_glasbey_" + snn
        oname = "ROI_glasbey_" + sn

        cmd = "python glasbey.py "
        cmd += "--no-black "
        cmd += "--format=byte "  # so we can convert to hex
        cmd += ncolor
        cmd += ofile

        ab.simple_shell_exec(cmd)

        ddd = ReadIn_CodeOut(ofile, oname)

    print("DONE!")
Example #11
0
def parse_args_this_prog(full_argv):

    argv = full_argv[1:]
    Narg = len(argv)

    if not (Narg):
        print(help_string_this_prog)
        sys.exit(0)

    # initialize objs
    iopts = iopts_this_prog()

    iopts.set_full_cmd(full_argv)

    i = 0
    while i < Narg:
        if argv[i] == "{ver}".format(**all_opts):
            print(ver)
            sys.exit(0)

        elif argv[i] == "{date}".format(**all_opts):
            print(date)
            sys.exit(0)

        elif argv[i] == "{help}".format(**all_opts) or \
             argv[i] == "{h}".format(**all_opts) :
            print(help_string_this_prog)
            sys.exit(0)

        elif argv[i] == "{hview}".format(**all_opts):
            prog = os.path.basename(full_argv[0])
            cmd = 'apsearch -view_prog_help {}'.format(prog)
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        # ---------- req ---------------

        elif argv[i] == "{input}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            iopts.set_input(argv[i])

        elif argv[i] == "{eps_iso}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            iopts.set_eps_iso(argv[i])

        elif argv[i] == "{eps_size}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            iopts.set_eps_size(argv[i])

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_iso}".format(**all_opts):
            if iopts.check_all != 1:
                iopts.set_rep_vox_iso(True)
                iopts.set_check_all_val(0)

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_mat_even}".format(**all_opts):
            if iopts.check_all != 1:
                iopts.set_rep_mat_even(True)
                iopts.set_check_all_val(0)

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_1mm_max}".format(**all_opts):
            if iopts.check_all != 1:
                iopts.set_rep_vox_1mm_max(True)
                iopts.set_check_all_val(0)

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_05mm_min}".format(**all_opts):
            if iopts.check_all != 1:
                iopts.set_rep_vox_05mm_min(True)
                iopts.set_check_all_val(0)

        # default behavior; and/or will supercede indiv opts
        elif argv[i] == "{check_all}".format(**all_opts):
            iopts.set_check_all_val(1)

        elif argv[i] == "{fix_all}".format(**all_opts):
            iopts.set_fix_all_val(True)

        # if not entered, will use min vox dim, floored at min allowed val
        elif argv[i] == "{fix_out_vox_dim}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            fovd = float(argv[i])
            if fovd < 0.5 or 1.0 < fovd:
                print("** ERROR: value for output voxel size ({}) is"
                      "   outside the allowed range [0.5, 1.0]"
                      "".format(fovd))
                sys.exit(3)
            else:
                iopts.set_fix_out_vox_dim(fovd)

        elif argv[i] == "{fix_out_prefix}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            iopts.set_fix_out_prefix(argv[i])

        elif argv[i] == "{overwrite}".format(**all_opts):
            iopts.set_fix_overwrite('-overwrite')

        elif argv[i] == "{verbose}".format(**all_opts) or \
             argv[i] == "{verb}".format(**all_opts) :
            iopts.set_verbose(True)

        # --------- finish -------------

        else:
            print("** ERROR: unknown opt: '{}'".format(argv[i]))
            sys.exit(2)
        i += 1

    # --------------------------------------------------------------------

    if iopts.check_req():
        print("   -------------------------------")
        print("** ERROR with input arguments (see detailed whining above).")
        sys.exit(1)

    iopts.finish_defs()  # make some paths we need later

    return iopts
Example #12
0
 def get_afni_dir(self):
    s, so, se = BASE.simple_shell_exec('which afni', capture=1)
    if s: return ''
    return so.strip()
Example #13
0
    # might have several scripts to parse
    for ii in range(iopts.ninfile):
        ainfo.add_text(lmsar.read_text_to_list(iopts.infile_list[ii]))

        # only include TOC for the first file
        oscript_txt, orst_txt = \
            lmsar.interpret_MSAR_list( ainfo.text_list[ii], iopts, ii,
                                       DO_TOC=not(ii) )
        ainfo.add_oscript_txt(oscript_txt)
        ainfo.add_orst_txt(orst_txt)

    # ------------------ write output files ------------------

    # Make dir to hold script/any images
    a, b, c = ab.simple_shell_exec("\mkdir -p {}".format(iopts.subdir))
    if not (a):
        print("++ Wrote directory to hold scripts")
    else:
        print("** Badness writing dir to hold scripts {}".format(iopts.subdir))
        sys.exit(12)

    # write script to local dir
    for ii in range(iopts.nscript):
        fff = open(iopts.oname_script_list[ii], 'w')
        fff.write(ainfo.oscript_txt_list[ii])
        fff.close()

    if iopts.tarball_name:
        all_script = ' '.join(iopts.oname_script_list)
        # \\ needed before the t, because \t is a TAB!
Example #14
0
def parse_args_this_prog(full_argv):

    argv = full_argv[1:]
    Narg = len(argv)

    if not (Narg):
        print(help_string_this_prog)
        sys.exit(0)

    # initialize objs
    iopts = iopts_this_prog()

    iopts.set_full_cmd(full_argv)

    i = 0
    while i < Narg:
        if argv[i] == "{ver}".format(**all_opts):
            print(ver)
            sys.exit(0)

        elif argv[i] == "{date}".format(**all_opts):
            print(date)
            sys.exit(0)

        elif argv[i] == "{help}".format(**all_opts) or \
             argv[i] == "{h}".format(**all_opts) :
            print(help_string_b0_corr)
            sys.exit(0)

        elif argv[i] == "{hview}".format(**all_opts):
            prog = os.path.basename(full_argv[0])
            cmd = 'apsearch -view_prog_help {}'.format(prog)
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        # ---------- req ---------------

        elif argv[i] == "{prefix}".format(**all_opts):
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i += 1
            iopts.set_prefix(argv[i])

        elif argv[i] == "{overwrite}".format(**all_opts):
            iopts.set_overwrite(True)

        # --------- finish -------------

        else:
            print("** ERROR: unknown opt: '{}'".format(argv[i]))
            sys.exit(2)
        i += 1

    if iopts.check_req():
        print("   -------------------------------")
        print("** ERROR with input arguments (see detailed whining above).")
        sys.exit(1)

    iopts.finish_defs()  # make some paths we need later

    return iopts
Example #15
0
 def get_afni_dir(self):
     s, so, se = BASE.simple_shell_exec('which afni', capture=1)
     if s: return ''
     adir = so.strip()
     if adir[-5:] == '/afni': return adir[0:-5]
     else: return ''
Example #16
0
 def get_afni_dir(self):
     s, so, se = BASE.simple_shell_exec('which afni', capture=1)
     if s: return ''
     return so.strip()
Example #17
0
# =============================================================================

if __name__ == "__main__" : 

    iopts     = lmsar.parse_MSAR_args(sys.argv[1:])

    text_list = lmsar.read_text_to_list( iopts.infile )

    oscript_txt, orst_txt = \
                lmsar.interpret_MSAR_list( text_list, iopts )
    
    # --------- write output files ---------

    # Make dir to hold script/any images
    a, b, c = ab.simple_shell_exec("\mkdir -p {}".format(iopts.subdir))
    if not(a) :
        print("++ Wrote directory to hold scripts")
    else:
        print("** Badness writing dir to hold scripts {}".format(iopts.subdir))

    # write script to local dir
    fff = open(iopts.oname_script, 'w')
    fff.write(oscript_txt)
    fff.close()

    # execute script, if desired
    if iopts.do_execute :
        a, b, c = ab.simple_shell_exec("tcsh -ef {}".format(iopts.oname_script))
        if not(a) :
            print("++ Success running script {}".format(iopts.oname_script))
Example #18
0
def parse_args_this_prog(full_argv):

    argv = full_argv[1:]
    Narg = len(argv)

    if not(Narg):
        print(help_string_this_prog)
        sys.exit(0)
    
    # initialize objs
    iopts  = iopts_this_prog()

    iopts.set_full_cmd(full_argv)

    i = 0
    while i < Narg:
        if argv[i] == "{ver}".format(**all_opts) :
            print(ver)
            sys.exit(0)

        elif argv[i] == "{date}".format(**all_opts) :
            print(date)
            sys.exit(0)

        elif argv[i] == "{help}".format(**all_opts) or \
             argv[i] == "{h}".format(**all_opts) :
            print(help_string_this_prog)
            sys.exit(0)

        elif argv[i] == "{hview}".format(**all_opts) :
            prog = os.path.basename(full_argv[0])
            cmd = 'apsearch -view_prog_help {}'.format( prog )
            BASE.simple_shell_exec(cmd)
            sys.exit(0)

        # ---------- req ---------------

        elif argv[i] == "{input}".format(**all_opts) :
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i+= 1
            iopts.set_input(argv[i])

        elif argv[i] == "{eps_iso}".format(**all_opts) :
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i+= 1
            iopts.set_eps_iso(argv[i])

        elif argv[i] == "{eps_size}".format(**all_opts) :
            if i >= Narg:
                ARG_missing_arg(argv[i])
            i+= 1
            iopts.set_eps_size(argv[i])

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_iso}".format(**all_opts) :
            if iopts.check_all != 1:
                iopts.set_rep_vox_iso(True)
                iopts.set_check_all_val( 0 )

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_mat_even}".format(**all_opts) :
            if iopts.check_all != 1:
                iopts.set_rep_mat_even(True)
                iopts.set_check_all_val( 0 )

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_1mm_max}".format(**all_opts) :
            if iopts.check_all != 1:
                iopts.set_rep_vox_1mm_max(True)
                iopts.set_check_all_val( 0 )

        # can be outweighed by "-check_all"
        elif argv[i] == "{is_vox_05mm_min}".format(**all_opts) :
            if iopts.check_all != 1:
                iopts.set_rep_vox_05mm_min(True)
                iopts.set_check_all_val( 0 )

        # default behavior; and/or will supercede indiv opts
        elif argv[i] == "{check_all}".format(**all_opts) :
            iopts.set_check_all_val( 1 )

        elif argv[i] == "{verbose}".format(**all_opts) or \
             argv[i] == "{verb}".format(**all_opts) :
            iopts.set_verbose(True)

        # --------- finish -------------

        else:
            print("** ERROR: unknown opt: '{}'".format(argv[i]))
            sys.exit(2)
        i+=1

    # --------------------------------------------------------------------

    if iopts.check_req():
        print("   -------------------------------")
        print("** ERROR with input arguments (see detailed whining above).")
        sys.exit(1)

    iopts.finish_defs()  # make some paths we need later

    return iopts
Example #19
0
 def get_afni_dir(self):
    s, so, se = BASE.simple_shell_exec('which afni', capture=1)
    if s: return ''
    adir = so.strip()
    if adir[-5:] == '/afni': return adir[0:-5]
    else:                    return ''