<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def search(name):
    '''
    Search for matches in the ports tree. Globs are supported, and the category
    is optional

    CLI Examples:

    .. code-block:: bash

        salt '*' ports.search 'security/*'
        salt '*' ports.search 'security/n*'
        salt '*' ports.search nmap

    .. warning::

        Takes a while to run
    '''
    name = six.text_type(name)
    all_ports = list_all()
    if '/' in name:
        if name.count('/') > 1:
            raise SaltInvocationError(
                'Invalid search string \'{0}\'. Port names cannot have more '
                'than one slash'
            )
        else:
            return fnmatch.filter(all_ports, name)
    else:
        ret = []
        for port in all_ports:
            if fnmatch.fnmatch(port.rsplit('/')[-1], name):
                ret.append(port)
        return ret