o
    *bŒ  ã                   @   s4   d dl Z d dlmZ d dlmZ G dd„ deƒZdS )é    N)ÚSoSCleanerParser)ÚSoSHostnameMapc                       sP   e Zd ZdZdZdgZd‡ fdd„	Zdd„ Zd	d
„ Zdd„ Z	‡ fdd„Z
‡  ZS )ÚSoSHostnameParserzHostname ParserZhostname_mapz5(((\b|_)[a-zA-Z0-9-\.]{1,200}\.[a-zA-Z]{1,63}(\b|_)))Nc                    sJ   t ƒ | _tt| ƒ |¡ | j ¡  | j |¡ g | _|  ¡  | j 	¡  d S ©N)
r   ÚmappingÚsuperr   Ú__init__Zload_domains_from_mapZload_domains_from_optionsÚshort_namesÚload_short_names_from_mappingZset_initial_counts)ÚselfZconfigZopt_domains©Ú	__class__© úE/usr/lib/python3/dist-packages/sos/cleaner/parsers/hostname_parser.pyr      s   
zSoSHostnameParser.__init__c                 C   s>   | j j ¡ D ]}t| d¡ƒdkr|| jvr| j |¡ qdS )a  When we load the mapping file into the hostname map, we have to do
        some dancing to get those loaded properly into the "intermediate" dicts
        that the map uses to hold hosts and domains. Similarly, we need to also
        extract shortnames known to the map here.
        Ú.é   N)r   ÚdatasetÚkeysÚlenÚsplitr	   Úappend)r   Zhnamer   r   r   r
   "   s   
€üz/SoSHostnameParser.load_short_names_from_mappingc                 C   sz   d|v rdS |  d¡}t|ƒdkr| j |d ¡ n| j |¡ t|ƒdkr5d |dd… ¡}| j |¡ | j |¡ dS )zöForce add the domainname found in /sos_commands/host/hostname into
        the map. We have to do this here since the normal map prep approach
        from the parser would be ignored since the system's hostname is not
        guaranteed
        Ú	localhostNr   r   r   é   éþÿÿÿ)r   r   r	   r   Újoinr   Úadd)r   Zhostname_stringZdomainsZhigh_domainr   r   r   Úload_hostname_into_map.   s   
z(SoSHostnameParser.load_hostname_into_mapc                 C   sp   |  ¡ }|D ]/}| d¡sd|v rq| ¡ dd… }|D ]}t| d¡ƒdkr.| j |¡ q| j |¡ qqdS )a$  Parse an archive's copy of /etc/hosts, which requires handling that
        is separate from the output of the `hostname` command. Just like
        load_hostname_into_map(), this has to be done explicitly and we
        cannot rely upon the more generic methods to do this reliably.
        ú#r   r   Nr   )Ú
splitlinesÚ
startswithr   r   r	   r   r   r   )r   ZcontentÚlinesÚlineZhostlnÚhostr   r   r   Úload_hostname_from_etc_hostsB   s   úüz.SoSHostnameParser.load_hostname_from_etc_hostsc           	         s¬   d‡ fdd„	}d}t tˆ ƒ |¡\}}dd„ ˆ jj ¡ D ƒ}t|ddd	„ d
D ]}|}dD ]}| |d¡}q-|||||ƒ\}}q'tˆ jddD ]
}||||ƒ\}}qG||fS )z€Override the default parse_line() method to also check for the
        shortname of the host derived from the hostname.
        Nc                    sL   |ˆ j jv r
| |fS t |tj¡}| | ¡r"| ˆ j  |p|¡| ¡S | |fS )zqPerform a second manual check for substrings that may have been
            missed by regex matching
            )r   Z	skip_keysÚreÚcompileÚIÚsearchÚsubnÚget)ZlnÚcountr'   ÚreplZ_reg©r   r   r   Ú_check_lineZ   s   
z1SoSHostnameParser.parse_line.<locals>._check_liner   c                 S   s   g | ]}d |v r|‘qS )r   r   )Ú.0Úhr   r   r   Ú
<listcomp>i   s    z0SoSHostnameParser.parse_line.<locals>.<listcomp>Tc                 S   s   t | ƒS r   )r   )Úxr   r   r   Ú<lambda>j   s    z.SoSHostnameParser.parse_line.<locals>.<lambda>)ÚreverseÚkeyz.-Ú_)r3   r   )	r   r   Ú
parse_liner   r   r   ÚsortedÚreplacer	   )	r   r!   r-   r*   Zhostsr"   ZfqdnÚcZ
short_namer   r,   r   r6   U   s   zSoSHostnameParser.parse_liner   )Ú__name__Ú
__module__Ú__qualname__ÚnameZmap_file_keyZregex_patternsr   r
   r   r#   r6   Ú__classcell__r   r   r   r   r      s    ÿ	r   )r$   Zsos.cleaner.parsersr   Z!sos.cleaner.mappings.hostname_mapr   r   r   r   r   r   Ú<module>   s   
