o
    b&                     @   s   d dl Z ddlmZ ddlmZmZmZ G dd deZG dd deZG d	d
 d
eZ	G dd deZ
G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N   )log)
XMLBuilderXMLChildPropertyXMLPropertyc                   @       e Zd ZdZedZedZdS )_CapsCPUcpu./arch./modelN)__name__
__module____qualname__XML_NAMEr   archmodel r   r   0/usr/share/virt-manager/virtinst/capabilities.pyr          r   c                   @   r   )_CapsSecmodelBaselabel	baselabel./@type.N)r   r   r   r   r   typecontentr   r   r   r   r      r   r   c                   @   s    e Zd ZdZedZeeZdS )_CapsSecmodelsecmodelr   N)	r   r   r   r   r   r   r   r   
baselabelsr   r   r   r   r   #   r   r   c                   @   s,   e Zd ZdZeeZeeddZdd Z	dS )	_CapsHosthostT	is_singlec              	   C   s   | j D ]I}|jdkrqd }|jD ]}|jdv r|j} nq|s qzt|dd dd}t	|d }||fW   S  t
yL   tjd|dd	 Y qw d
S )Ndac)qemukvm:r   + z'Exception parsing qemu dac baselabel=%sT)exc_info)NN)	secmodelsr   r   r   r   intsplitreplacepwdgetpwuid	Exceptionr   debug)selfr   labelr   uiduserr   r   r   get_qemu_baselabel.   s*   





z_CapsHost.get_qemu_baselabelN)
r   r   r   r   r   r   r)   r   r	   r5   r   r   r   r   r   )   s
    r   c                   @   r   )_CapsMachinemachiner   z./@canonicalN)r   r   r   r   r   name	canonicalr   r   r   r   r6   Q   r   r6   c                   @   s(   e Zd ZdZedZedZeeZ	dS )_CapsDomaindomainr   z
./emulatorN)
r   r   r   r   r   hypervisor_typeemulatorr   r6   machinesr   r   r   r   r:   W   s
    r:   c                   @   s4   e Zd ZdZedddZedddZedddZdS )	_CapsGuestFeaturesfeaturesz./paeT)is_boolz./acpi/@default)is_onoffz./apic/@defaultN)r   r   r   r   r   paeacpiapicr   r   r   r   r?   ^   s
    r?   c                   @   s|   e Zd ZdZedZedZedZedZe	e
ddZe	edd	Ze	eddZd
d Zdd Zdd Zdd Zdd ZdS )
_CapsGuestguestz	./os_typez./arch/@namez./arch/loaderz./arch/emulatorr
   )relative_xpathTr    c                 C   sJ   |r|j p| j }g }|D ]}||j |jr"|j|vr"||j q|S )z
        Return all machine string names, including canonical aliases for
        the guest+domain combo but avoiding duplicates
        )r>   appendr8   r9   )r1   r;   mobjsretmr   r   r   all_machine_namesw   s   z_CapsGuest.all_machine_namesc                 C   s    | j D ]
}|jdkr dS qdS )z<
        Return True if kvm guests can be installed
        r$   TF)domainsr<   )r1   dr   r   r   is_kvm_available   s
   

z_CapsGuest.is_kvm_availablec                 C      t | jjS )zD
        Return True if capabilities report support for PAE
        )boolr@   rC   r1   r   r   r   supports_pae      z_CapsGuest.supports_paec                 C   rQ   )zE
        Return Tree if capabilities report support for ACPI
        )rR   r@   rD   rS   r   r   r   supports_acpi   rU   z_CapsGuest.supports_acpic                 C   rQ   )zE
        Return Tree if capabilities report support for APIC
        )rR   r@   rE   rS   r   r   r   supports_apic   rU   z_CapsGuest.supports_apicN)r   r   r   r   r   os_typer   loaderr=   r   r:   rN   r?   r@   r6   r>   rM   rP   rT   rV   rW   r   r   r   r   rF   f   s    	rF   c                   @   s   e Zd ZdZdd ZdS )	_CapsInfozm
    Container object to hold the results of guest_lookup, so users don't
    need to juggle two objects
    c                 C   s`   || _ || _|| _| jj| _| jj| _| jj| _| jj| _| jjp$| jj| _| j| j| _	d S N)
connrG   r;   r<   rX   r   rY   r=   rM   r>   )r1   r\   rG   r;   r   r   r   __init__   s   



z_CapsInfo.__init__N)r   r   r   __doc__r]   r   r   r   r   rZ      s    rZ   c                   @   sN   e Zd Zdd ZdZeeddZeeZ	dd Z
dd	 Zd
d ZdddZdS )Capabilitiesc                 O   s"   t j| g|R i | i | _d S r[   )r   r]   _cpu_models_cache)r1   argskwargsr   r   r   r]      s   
zCapabilities.__init__capabilitiesTr    c                 C   sf   |g}|d u r| j jjd g}|D ] }| jD ]}|d u s |j|kr/|d u s)|j|kr/|    S qqd S r[   )r   r	   r   guestsrX   )r1   rX   r   archsagr   r   r   _guestForOSType   s   
zCapabilities._guestForOSTypec                 C   s   g }|j D ]}|r|j| krq|r|||vrq|| q|s%dS g d}|D ]}|D ]}|j|kr<|    S q/q+|d S )zl
        Return the recommended domain for use if the user does not explicitly
        request one.
        N)r$   xenr#   )rN   r<   lowerrM   rI   )r1   rG   dtyper7   rN   rO   prioritytr   r   r   _bestDomainType   s"   

zCapabilities._bestDomainTypec                 C   s   | j D ]}|jr dS qdS )zH
        Return True if there are any install options available
        TF)rd   rN   )r1   rG   r   r   r   has_install_options   s
   
z Capabilities.has_install_optionsNc           	      C   s   |dkrd}|  ||}|sC|r|rtd||d }t||r-tdd|i }t||r;tdd|i }t|td	}t|| |||}|d
u rq|ratd||j|j|d }t|td||j|jd }t|t| j||}|S )a  
        Simple virtualization availability lookup

        Convenience function for looking up 'Guest' and 'Domain' capabilities
        objects for the desired virt type. If type, arch, or os_type are none,
        we return the default virt type associated with those values. These are
        typically:

            - os_type : hvm, then xen
            - typ     : kvm over plain qemu
            - arch    : host arch over all others

        Otherwise the default will be the first listed in the capabilities xml.
        This function throws C{ValueError}s if any of the requested values are
        not found.

        :param typ: Virtualization type ('hvm', 'xen', ...)
        :param arch: Guest architecture ('x86_64', 'i686' ...)
        :param os_type: Hypervisor name ('qemu', 'kvm', 'xen', ...)
        :param machine: Optional machine type to emulate

        :returns: A _CapsInfo object containing the found guest and domain
        linuxri   zTHost does not support virtualization type '%(virttype)s' for architecture '%(arch)s')virttyper   zLHost does not support any virtualization options for architecture '%(arch)s'r   z8Host does not support virtualization type '%(virttype)s'rr   z0Host does not support any virtualization optionsNzHost does not support domain type %(domain)s with machine '%(machine)s' for virtualization type '%(virttype)s' with architecture '%(arch)s')r;   rr   r   r7   zpHost does not support domain type %(domain)s for virtualization type '%(virttype)s' with architecture '%(arch)s')r;   rr   r   )rh   _
ValueErrorro   rX   r   rZ   r\   )	r1   rX   r   typr7   rG   msgr;   capsinfor   r   r   guest_lookup   sL   	zCapabilities.guest_lookup)NNNN)r   r   r   r]   r   r   r   r   rF   rd   rh   ro   rp   rx   r   r   r   r   r_      s    	r_   )r-   loggerr   
xmlbuilderr   r   r   r   r   r   r   r6   r:   r?   rF   objectrZ   r_   r   r   r   r   <module>   s   
(>