def find_msm(): try: msm = util.check_output("which msm") msm = msm.replace(os.linesep, '') except: msm = None return msm
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 = utils.check_output("echo") # decode('utf-8') == str in py3 and == unicode in py2 assert type(output) == str or type(output) == unicode
def ciftify_meants(image, seed, csv, mask=None): meants = "ciftify_meants {} {} --outputcsv {}".format(image, seed, csv) if mask: meants = meants + ' --mask {}'.format(mask) rtn = check_output(meants, shell=True) if rtn or not os.path.exists(csv): sys.exit("Error experienced while generating {}".format(csv))
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
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
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__) sep = '{} '.format(os.linesep) 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:{0}Version: {1}".format(sep, 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(sep, ciftify_path) commit_num, commit_date = read_commit(git_log) info = "ciftify:{0}Path: {1}{0}{2}{0}{3}".format('{} '.format(sep), 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
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
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:{0}Path: {1}{0}{2}'.format(sep, wb_path, wb_v) return (all_info)
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
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.abspath(shell_val) 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('/')[:-2]) except: dir_fsl = None return dir_fsl
def get_fslinfo_fields(nii_path): fsl_info = "fslinfo {}".format(nii_path) nii_info = check_output(fsl_info, shell=True) fields = nii_info.strip().split('\n') return fields