o 4f\@sddlZddlZddlZddlZddlZddlmZddlmZGddde Z Gddde Z Gdd d e Z Gd d d e ZGd d d eZddZddZGdddZddddgfddZdS)N) ElementTree)gettextc@s eZdZdS)NoDistroTemplateExceptionN)__name__ __module__ __qualname__rr3/usr/lib/python3/dist-packages/aptsources/distro.pyr%src@speZdZgfddZddZdddZdd Zd d Z  dd dZddZ ddZ ddZ ddZ ddZ dS) DistributioncCs.||_||_||_||_||_d|_d|_dS)z2 Container for distribution specific informations debzdeb-srcN)idcodename descriptionreleaseis_like binary_type source_type)selfr r rrrrrr __init__+s zDistribution.__init__cCs||_d|_g|_g|_g|_g|_g|_g|_g|_g|_ d|_ g|_ d|_ d|_ d|_g|_|jjjD]}||jrH|j|jkrH||_nq5|jdurXtd|j|jfg}g}g}g}|jjD]}|js||jr|jr|jjr||jjr|jdr|js|j ||!|j"n[|jdr|jr|j |nK|j#|j$kr|js|j ||!|j"| |jn/|j#|j$kr|jr|j |n|j#|j%kr|js|j |n|j#|j%kr|jr|j ||js$|j|jj&vr$|js |j#|j$kr |j |qd|js|j#|j%kr|j |qd|j |qdt'||_t'||_|!||!|t'||_t'||_ |(dS)zf Find the corresponding template, main and child sources for the distribution NFz7Error: could not find a distribution template for %s/%scdrom:)) sourceslistsource_template child_sources main_sourcesdisabled_sources cdrom_sourcesdownload_comps enabled_comps cdrom_comps used_mediaget_source_codesource_code_sourcesdefault_server main_servernearest_server used_serversmatcher templates is_codenamename distributionr rr listinvaliddisttemplateofficialuri startswithdisabledappendextendcompstyperrchildrenset get_mirrors)rrr/mediar6rrsourcerrr get_sources7s                         zDistribution.get_sourcesNc Cs|jj|_|jD] }|ds|j|qt|jdkr"|j|_ n|jdj |_ i|_ d}t j |rt|d}t|dddurE|j|_|d}|D]9}z|jd}Wntyc|jd }Ynwz|jd }Wntyy|jd }Ynwtd ||j |<qLd|_d|_t jd dd} | d} | d} | dkrt| } | | d| } |r|| |_| |j vr|j | |_| |_dSdS)zR Provide a set of mirrors where you can get the distribution from rrz%/usr/share/xml/iso-codes/iso_3166.xml)fileiterNiso_3166_entry common_namer* alpha_2_code alpha_3_codeiso_3166LANGen_UK)default_.)rbase_urir$r r2r&r4lenrr#r1 countriesospathexistsrgetattr getiteratorr?attribKeyErrorrdgettextlowercountry country_codegetenvfindr%) rmirror_templatemediumfnameetitelmdescrcodelocaleazrYrrr r:sV                zDistribution.get_mirrorscCsfd}|d}|d}|dkr|dkr||td|}||jvr,td|j|Sd|dS)zu Try to get a human readable name for the main mirror of a country Customize for different distributions Nz://z.archive.ubuntu.comrJ Server for %s%s/ )r[rMrNrHrstriprserverrXilirrr _get_mirror_names   zDistribution._get_mirror_namecCsdd}g}t|jdkst|jdkr:||jd|jr:|td|jdg|jr8|||j|jdg|St|jdkr||jd|js|td|jdg|jd}|jrp|||jsp|||j|jdg|r}||||dg|St|jdkr|td|jdg|jr|||j|jdg|tdd dg|jD] }|||dg}|||js|||jrq||vr||q|S) z- Return a list of used and suggested servers cSst|d|dS)zl Helper function that handles comaprision of mirror urls that could contain trailing slashesri)rematchstriprj)mir1mir2rrr compare_mirrorssz5Distribution.get_server_list..compare_mirrorsrKrz Main serverTFzCustom serversN)rMr&r$r4rHr%ro)rrumirrorsrl mirror_entryrrr get_server_listsT!      zDistribution.get_server_listrc Cs|dur|j}|dur|j}|durt|j}|dur|j}|j|||||}|jrH||jkrJ|jj|j|||||j |jj |dddSdSdS)z3 Add distribution specific sources NrK)r>pos) r#r r,rrraddr!rr>index)rr7r1r.r6comment new_sourcerrr add_sources    zDistribution.add_sourcecCsXt|g}|jD]}|jjD]}|j|kr|jr||jqq|D]}||q"dS)z Enable a component in all main, child and source code sources (excluding cdrom based sources) comp: the component that should be enabled N)r9rr/ componentsr*parent_componentrz_enable_component)rcompr6r<crrr enable_component%s     zDistribution.enable_componentc sZfdd}g}||j||ji}i}|D]!}|j|jkr;|j|vr-t||j<|jD] }||j|q0q|j D]!}|j|j kr`|j|vrRt||j<|jD] }||j|qUq?t |jdkrr|j dgdn|D]}|||qt|j D]}|||q|j rt |j dkr|j |j dgddS|j D] }|||qdSdS)Ncs@|j|vrdS||jvrdS|j||jdS)z Check if we already added the component to the repository, since a repository could be splitted into different apt lines. If not add the component N)r.r6r4rz)r<comps_per_distrrr add_component_only_once7s  z?Distribution._enable_component..add_component_only_oncerKrh)r6)r7r6)r5rrr7rr.r9r6rzr"rrMr~r!) rrrsourcesrcomps_per_sdistsrr<rrr r5sB                 zDistribution._enable_componentcCsg}||j||j||j||jvr!g}||j|D]}||jvr=|j|t|jdkr=|j|q#dS)zx Disable a component in all main, child and source code sources (excluding cdrom based sources) rKN) r5rrr"rr6removerMr)rrrr<rrr disable_componentss        zDistribution.disable_componentcsfdd}g}g}|_jD]}||||qjD]}|jjdus+|jj|jkr1||||qjD]}||||q5dS)zN Change the server of all distro specific sources to a given host csh||_|jD]}|j|j|g|vr|j|q||j|j|gqt|jdkr2j|dSdS)NrK)r1r6r.rr4rMr)r<r1seenrrrr change_server_of_sources z;Distribution.change_server..change_server_of_sourceN)r#rrr/rLr1r")rr1r seen_binary seen_sourcer<rrr change_servers       zDistribution.change_servercCs||jkrdSdS)z1 Compare a given name with the release codename. TF)r rr*rrr r)s zDistribution.is_codenameN)NNNNr)rrrrr=r:rorxr~rrrrr)rrrr r )s  ]93 > r c@s(eZdZdZddZddZddZdS) DebianDistributionz+ Class to support specific Debian features cCs||jks |jdvr dSdS)z Compare a given name with the release codename and check if if it can be used as a synonym for a development releases )testingunstableTF)r rrrrr r)szDebianDistribution.is_codenamecCsvd}|d}|d}|dkr|dkr||td|}||jvr4tdtd|j|Sd|dS) z_ Try to get a human readable name for the main mirror of a country Debian specific Nz://ftp.z .debian.orgrJrgrDrhri)r[rMrNrHrrVrjrkrrr ros    z#DebianDistribution._get_mirror_namecCtj|dddS)Nz http://ftp.%s.debian.org/debian/r\r r:rrrr r: zDebianDistribution.get_mirrorsN)rrr__doc__r)ror:rrrr rs  rc@eZdZdZddZdS)UbuntuDistributionz+ Class to support specific Ubuntu features cCr)Nz$http://%s.archive.ubuntu.com/ubuntu/rrrrrr r:rzUbuntuDistribution.get_mirrorsNrrrrr:rrrr r rc@r)UbuntuRTMDistributionz/ Class to support specific Ubuntu RTM features cCs|jj|_dSr)rrLr$rrrr r:sz!UbuntuRTMDistribution.get_mirrorsNrrrrr rrrc Csddlm}m}ddl}ddddd}z!|d d g|d d}|d }|d d|dDW|Sty[}z|j|j krPt d|WYd}~|SWYd}~|Sd}~ww)z-Call lsb_release --idrc and return a mapping.rPopenPIPENsidDebianzDebian GNU/Linux unstable (sid)r)CodenameDistributor ID DescriptionRelease lsb_releasez-idrc)stdoutzutf-8css"|] }d|vr|dVqdS)z: N)split).0linerrr s z_lsb_release.. z#lsb_release failed, using defaults:) subprocessrrerrno communicatedecodeupdaterOSErrorENOENTloggingwarning)rrrresultoutexcrrr _lsb_releases&   rc Csddlm}m}ddl}zddlm}Wnty%ttjtj }Ynwz(|ddg||dd d}| D]}| d rK| d d d WSq9WdStyu}z|j|jkrjtd |WYd}~dSWYd}~dSd}~ww) z=Get the current channel from system-image-cli -i if possible.rrN)DEVNULLzsystem-image-cliz-iT)rstderruniversal_newlinesz channel: z: rKz+system-image-cli failed, using defaults: %s)rrrrr ImportErrorrOopendevnullO_RDWRr splitlinesr2rrrrr)rrrrrrrrrr _system_image_channels<      rc@s>eZdZdZdZdddZddZddZd d Zd d Z d S) _OSReleasez/etc/os-releaseTcCsHi|_d|_tj|_tj|jsdS|d|_|r"| dSdS)NFT) rvalidrOS_RELEASE_FILEr>rOrPisfileparseinject_lsb_compat)r lsb_compatrrr rs z_OSRelease.__init__cCsd|jd|jd<|jd|jd<|jd|jd<|jds'|jd|jd<|jd|jd <dS) NIDr PRETTY_NAMErVERSION_CODENAMErUBUNTU_CODENAME VERSION_IDr)rgetrrrr rs  z_OSRelease.inject_lsb_compatcCsBt|jd}|D]}|}|sq|j|ddq|dS)Nr=rK)rr>rr parse_entryrclose)rfrrrr rs  z_OSRelease.parsecCs4||}|dkrt|tr|d}||j|<dS)NID_LIKE ) parse_value isinstancestrrr)rkeyvaluerrr r's  z_OSRelease.parse_entrycCs"t|}t|dkr|dS|S)NrKr)shlexrrM)rrvaluesrrr r/s  z_OSRelease.parse_valueN)T) rrrDEFAULT_OS_RELEASE_FILErrrrrrrrrr rs   rc Cs|r|r|r|sRt}g}t}|jr|j}t}|d}|d}|d}|d}|dg}|dkrRt}|durRd|vrRd }|d d d d d d }|}|}|dkr^t|||||S|d krjt |||||S|dkrvt |||||St |||||S)z Check the currently used distribution and return the corresponding distriubtion class that supports distro specific features. If no paramter are given the distro will be auto detected via a call to lsb-release rrrrrUbuntuNz ubuntu-rtm/z Ubuntu-RTM/rK-rr) rrrrrrrsplitrrrrr ) r r rrr os_release os_result lsb_resultchannelrrr get_distro6s8    r)rrrprrOxml.etree.ElementTreerapt_pkgrH Exceptionrobjectr rrrrrrrrrrr s*  5