def extract_app_paths(values, app_should_exist=True):
    """ Extracts application paths from the values provided.

        `values`
            List of strings to extract paths from.
        `app_should_exist`
            Set to ``True`` to check that application file exists.

        Returns list of strings.

        * Raises ``ValueError`` exception if value extraction fails.
        """

    def _osx_app_path(name):
        """ Attempts to find the full application path for the name specified.

            `name`
                Application name.

            Returns string or ``None``.
            """

        # we use find because it is faster to traverse the
        # hierachy for app dir.
        cmd = ('find /Applications -type d '
               '-iname "{0}.app" -maxdepth 4'.format(name))

        data = shell_process(cmd)
        if not data is None:
            lines = str(data).split('\n')

            if lines:
                bundle_dir = lines[0]
                path = os.path.join(bundle_dir, 'Contents', 'MacOS', name)

                if os.path.isfile(path) and os.access(path, os.X_OK):
                    return path

        return None

    paths = set()

    for value in values:
        # split path into relevant tokens
        parts = list(shlex.split(value))

        # program name
        name = parts[0]

        # just the name, search bin paths
        if os.path.dirname(name) == '':
            path = which(name)

            if not path:
                # MacOS X, maybe it's an application name; let's try to build
                # default application binary path
                errmsg = u'"{0}" command does not exist'.format(name)

                if IS_MACOSX:
                    path = _osx_app_path(name)
                    if not path:
                        raise ValueError(errmsg)
                else:
                    raise ValueError(errmsg)  # no luck

        else:
            # relative to current working dir or full path
            path = os.path.realpath(name)

            if app_should_exist:
                # should be a file or link and be executable
                if os.path.isdir(path) or not os.access(path, os.X_OK):
                    errmsg = u'"{0}" is not an executable file'.format(name)
                    raise ValueError(errmsg)

        # update program path
        parts[0] = path

        # quote params with spaces in value
        parts[:] = ['"{0}"'.format(p.replace('"', '\\"'))
                    if ' ' in p else p for p in parts]

        # add flattened path
        paths.add(' '.join(parts))

    return list(paths)
def extract_app_paths ( values , app_should_exist = True ) : def _osx_app_path ( name ) : cmd = ( 'find /Applications -type d ' '-iname "{0}.app" -maxdepth 4' . format ( name ) ) data = shell_process ( cmd ) if not data is None : lines = str ( data ) . split ( '\n' ) if lines : bundle_dir = lines [ 0 ] path = os . path . join ( bundle_dir , 'Contents' , 'MacOS' , name ) if os . path . isfile ( path ) and os . access ( path , os . X_OK ) : return path return None paths = set ( ) for value in values : parts = list ( shlex . split ( value ) ) name = parts [ 0 ] if os . path . dirname ( name ) == '' : path = which ( name ) if not path : errmsg = u'"{0}" command does not exist' . format ( name ) if IS_MACOSX : path = _osx_app_path ( name ) if not path : raise ValueError ( errmsg ) else : raise ValueError ( errmsg ) else : path = os . path . realpath ( name ) if app_should_exist : if os . path . isdir ( path ) or not os . access ( path , os . X_OK ) : errmsg = u'"{0}" is not an executable file' . format ( name ) raise ValueError ( errmsg ) parts [ 0 ] = path parts [ : ] = [ '"{0}"' . format ( p . replace ( '"' , '\\"' ) ) if ' ' in p else p for p in parts ] paths . add ( ' ' . join ( parts ) ) return list ( paths )
Extracts application paths from the values provided.

        `values`
            List of strings to extract paths from.
        `app_should_exist`
            Set to ``True`` to check that application file exists.

        Returns list of strings.

        * Raises ``ValueError`` exception if value extraction fails.