def test_returns_unicode_string_not_bytes(self):
        """This test is to ensure python 3 compatibility (i.e. check_output
        returns bytes unless decoded) """

        output = utilities.check_output("echo")

        # decode('utf-8') == str in py3 and == unicode in py2
        assert type(output) == str or type(output) == unicode
Exemplo n.º 2
0
def find_freesurfer():
    """
    Returns the path of the freesurfer bin/ folder, or None if unavailable.
    """
    try:
        dir_freesurfer = util.check_output('which recon-all')
        dir_freesurfer = '/'.join(dir_freesurfer.split('/')[:-1])
    except:
        dir_freesurfer = None

    return dir_freesurfer
Exemplo n.º 3
0
def find_workbench():
    """
    Returns path of the workbench bin/ folder, or None if unavailable.
    """
    try:
        workbench = util.check_output('which wb_command')
        workbench = workbench.strip()
    except:
        workbench = None

    return workbench
Exemplo n.º 4
0
def msm_version():
    '''
    Returns version info for msm
    '''
    msm_path = find_msm()
    if not msm_path:
        return "MSM not found."
    try:
        version = util.check_output('msm --version').replace(os.linesep, '')
    except:
        version = ''
    info = "MSM:{0}Path: {1}{0}Version: {2}".format('{}    '.format(os.linesep),
            msm_path, version)
    return info
Exemplo n.º 5
0
def ciftify_version(file_name=None):
    '''
    Returns the path and the latest git commit number and date if working from
    a git repo, or the version number if working with an installed copy.
    '''
    logger = logging.getLogger(__name__)

    try:
        version = pkg_resources.get_distribution('ciftify').version
    except pkg_resources.DistributionNotFound:
        # Ciftify not installed, but a git repo, so return commit info
        pass
    else:
        return "Ciftify version {}".format(version)

    try:
        dir_ciftify = util.check_output('which {}'.format(file_name))
    except subprocess.CalledProcessError:
        file_name = None
        dir_ciftify = __file__

    ciftify_path = os.path.dirname(dir_ciftify)
    git_log = get_git_log(ciftify_path)

    if not git_log:
        logger.error("Something went wrong while retrieving git log. Returning "
                "ciftify path only.")
        return "Ciftify:{0}Path: {1}".format(os.linesep, ciftify_path)

    commit_num, commit_date = read_commit(git_log)
    info = "Ciftify:{0}Path: {1}{0}{2}{0}{3}".format('{}    '.format(os.linesep),
            ciftify_path, commit_num, commit_date)

    if not file_name:
        return info

    ## Try to return the file_name's git commit too, if a file was given
    file_log = get_git_log(ciftify_path, file_name)

    if not file_log:
        # File commit info not found
        return info

    commit_num, commit_date = read_commit(file_log)
    info = "{1}{5}Last commit for {2}:{0}{3}{0}{4}".format('{}    '.format(
            os.linesep), info, file_name, commit_num,
            commit_date, os.linesep)

    return info
Exemplo n.º 6
0
def wb_command_version():
    '''
    Returns version info about wb_command.

    Will raise an error if wb_command is not found, since the scripts that use
    this depend heavily on wb_command and should crash anyway in such
    an unexpected situation.
    '''
    wb_path = find_workbench()
    if wb_path is None:
        raise EnvironmentError("wb_command not found. Please check that it is "
                "installed.")
    wb_help = util.check_output('wb_command')
    wb_version = wb_help.split(os.linesep)[0:3]
    sep = '{}    '.format(os.linesep)
    wb_v = sep.join(wb_version)
    all_info = 'wb_command: {}Path: {}    {}'.format(sep,wb_path,wb_v)
    return(all_info)
Exemplo n.º 7
0
def get_git_log(git_dir, file_name=None):
    git_cmd = ["cd {}; git log".format(git_dir)]
    if file_name:
        git_cmd.append("--follow {}".format(file_name))
    git_cmd.append("| head")
    git_cmd = " ".join(git_cmd)

    # Silence stderr
    try:
        with open(os.devnull, 'w') as DEVNULL:
            file_log = util.check_output(git_cmd, stderr=DEVNULL)
    except subprocess.CalledProcessError:
        # Fail safe in git command returns non-zero value
        logger = logging.getLogger(__name__)
        logger.error("Unrecognized command: {} "
                "\nReturning empty git log.".format(git_cmd))
        file_log = ""

    return file_log
Exemplo n.º 8
0
def find_fsl():
    """
    Returns the path of the fsl bin/ folder, or None if unavailable.
    """
    # Check the FSLDIR environment variable first
    shell_val = os.getenv('FSLDIR')
    dir_fsl = os.path.join(shell_val, 'bin') if shell_val else ''

    if os.path.exists(dir_fsl):
        return dir_fsl

    # If the env var method fails, fall back to using which. This method is
    # not used first because sometimes the executable is installed separately
    # from the rest of the fsl package, making it hard (or impossible) to locate
    # fsl data files based on the returned path
    try:
        dir_fsl = util.check_output('which fsl')
        dir_fsl = '/'.join(dir_fsl.split('/')[:-1])
    except:
        dir_fsl = None

    return dir_fsl
Exemplo n.º 9
0
def find_msm():
    try:
        msm = util.check_output("which msm")
    except:
        msm = None
    return msm.replace(os.linesep, '')