o 4f1J@sddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z gdZ ddZdd ZGd d d eZGd d d eZGdddeZGdddeZedkre eZeD] ZedeqceddZedeeeddeedddSdS))absolute_importprint_functionN)DistInfo) is_mirror SourceEntry NullMatcher SourcesListSourceEntryMatchercCs|d}|d}||krdSz|dd}|dd}Wn ty*YdSwd|vr>||ddd|kr>dSdS)z check if the given add_url is idential or a mirror of orig_uri e.g.: master_uri = archive.ubuntu.com compare_uri = de.archive.ubuntu.com -> True z/ Tz//rF.N)rstripsplit IndexErrorindex) master_uri compare_uri compare_srv master_srvr8/usr/lib/python3/dist-packages/aptsources/sourceslist.pyr.s   rcCs tt|S)a. simple and efficient way to return uniq collection This is not intended for use with a SourceList. It is provided for internal use only. It does not have a leading underscore to not break any old code that uses it; but it should not be used in new code (and is not listed in __all__).)listsetsrrruniqLs rc@sJeZdZdZdddZddZddZd d Zd d Zd dZ ddZ dS)rz single sources.list entry NcCs|d|_d|_d|_g|_d|_d|_d|_g|_d|_||_ |dur.t j dt j d}||_||d|_g|_dS)NFzDir::EtcDir::Etc::sourcelist)invaliddisabledtype architecturestrusteduridistcompscommentlineapt_pkgconfigfind_dirfindfileparsetemplatechildren)selfr&r+rrr__init__Ys(   zSourceEntry.__init__cCsH|j|jko#|j|jko#|jd|jdko#|j|jko#|j|jkS)z- equal operator for two sources.list entries /)rrr"r r#r$)r/otherrrr__eq__ls    zSourceEntry.__eq__cCs|}g}d}d}d}tt|D]U}||dkr3|r*d}d}||||}qd}|||7}q||dkrBd}|||7}q|rV||sVd}||||}q||ra|sad}q|||7}qt|dkrs|||S)z a split() implementation that understands the sources.list format better and takes [] into account (for e.g. cdroms) rF[T]r)striprangelenappendisspace)r/r&piecestmpp_found space_foundirrrmysplitts6        zSourceEntry.mysplitc Cs||_|}|dks|dkrd|_dS|ddkr8d|_|dd}|ddvr2d|_dS|dd}|d}|dkrP||dd|_|d|}||}t|dkr`d|_dS|d|_ |j d vrqd|_dS|ddd kr| dd }|D]2}z |d d\}}Wn t yd|_Yqw|d kr|d|_ q|dkrt ||_qd|_q|d|_t|jdkrd|_|d|_t|dkr|dd|_dSg|_dS)z^ parse a given sources.list (textual) line and break it up into the field we have r#TNrr)rpmrpm-srcdebdeb-src)rDrErBrCr4z[]=arch,r!)r&r6rrr r*r%r@r8rpop Exceptionr r'string_to_boolr!r"r#r$)r/r&r;r?optionsoptionkeyvaluerrrr,sX            zSourceEntry.parsecCsH| |_|r|jd|_dS|jddkr"d|j|_dSdS)z# set a line to enabled or disabled rArN)rr&lstripr6)r/ new_valuerrr set_enableds zSourceEntry.set_enabledcCs |S)z debug helper )strr6)r/rrr__str__s zSourceEntry.__str__cCs|jr|jSd}|jr d}||j7}|jr,|jdur,|dd|j|jr&dndf7}n|jdur=|d|jr8dnd7}n |jrJ|d d|j7}|d |j|jf7}t |j d kre|d d |j 7}|j dkrq|d |j 7}|d7}|S)z# return the current line as string rz# Nz [arch=%s trusted=%s]rIyesnoz [trusted=%s]z [arch=%s]z %s %sr z # ) rr&rrr r!joinr"r#r8r$r%)r/r&rrrrUs*   zSourceEntry.strN) __name__ __module__ __qualname____doc__r0r3r@r,rTrVrUrrrrrVs %@ rc@seZdZdZddZdS)rz a Matcher that does nothing cCsdS)NTr)r/rrrrmatchszNullMatcher.matchN)r]r^r_r`rarrrrrs rc@szeZdZdZ  dddZddZdd Zd d Zd d dgfddZddZ ddZ dddZ ddZ ddZ ddZdS)r z8 represents the full sources.list + sources.list.d file T /usr/share/python-apt/templates/cCs*g|_|r t||_nt|_|dSr\)rr matcherrrefresh)r/ withMatcher matcherPathrrrr0 s   zSourcesList.__init__cCstg|_tjd}tj|r||tjd}t d|D]}||q!|jD] }|j s7|j |q,dS)z" update the list of known entries rDir::Etc::sourceparts %s/*.listN) rr'r( find_fileospathexistsloadr)globrrcra)r/r+partsdirsourcerrrrds       zSourcesList.refreshccs|jD]}|VqdS)zM simple iterator to go over self.list, returns SourceEntry types N)r)r/entryrrr__iter__&s zSourcesList.__iter__c/sndd}|jD]*|r|djdkrq tfddDr4tfdd|Dr4Vq dS)Nr"r1c3s"|] }t||kVqdSr\)getattr).0rPattrsrprr 1s z%SourcesList.__find..c3s|]}|VqdSr\r)rt predicate)rprrrw2)rKrr r"all)r/ predicatesrvr"rrur__find,s  zSourcesList.__findrNc s|}|d} | r|dd}t|dd} |jfdd| d|||d} | D]} | D]} | | jvrK| | | =t| dkrK| Sq2q.|jfd dd|||d } | D]1} | j| kryt| jt| krut | j| | _| S| jr| st| jt| krd| _| Sq]| rdnd |rd d nd ||g}| | |r| d|d dd|Dd}t |}|dur||_|j||dkr|j ||S|j|||S)z Add a new source to the sources.list. The method will search for existing matching repos and will try to reuse them as far as possible rArNct|jkSr\rr rr rrEz!SourcesList.add..F)rrrr"r#rcr~r\rrrrrrRr)rrr"r#rz [arch=%s]rIrYcss|]}|r|VqdSr\r)rtpartrrrrwkryz"SourcesList.add..rZ)r6 startswithrRr_SourcesList__findr$rr8rrr[extendr9rr+rcrarinsert)r/rr"r# orig_compsr%posr+r rr$sourcesrpnew_comppartsr& new_entryrrradd5sd            zSourcesList.addcCs|j|dS)z2 remove the specified entry from the sources.list N)rremove)r/ source_entryrrrrwszSourcesList.removecCs|tjd}tj||rtj|rt|||tjd}t d|D]}tj||r;t|||q)dS)z: restore sources.list files based on the backup extension rrgrhN) r'r(rirjrkrlshutilcopyr)rn)r/ backup_extr+rorrrrestore_backup{s  zSourcesList.restore_backupcCsXt}|dur td}|jD]}|j|vr)tj|jr)t |jd|j|fq|S)z make a backup of the current source files, if no backup extension is given, the current date/time is used (and returned) Nz %y%m%d.%H%Mz%s%s) rtimestrftimerr+rjrkrlrr)r/ralready_backupedrprrrbackups    zSourcesList.backupcCsxz*t|d}|D] }t||}|j|q WdWdS1s#wYWdSty;td|YdSw)z (re)load the current sources rNzcould not open file '%s' )openrrr9rLloggingwarning)r/r+fr&rprrrrms  & zSourcesList.loadc Csi}t|jdkr.tjd}d}t|d}||WddS1s'wYdSz,|jD]}|j|vrBt|jd||j<||j|q2W|D]}|| qPdS|D]}|| q]w)z save the current sources rrz## See sources.list(5) for more information, especialy # Remember that you can only use http, ftp or file URIs # CDROMs are managed through the apt-cdrom tool. wN) r8rr'r(rirwriter+rUclose)r/filesrkheaderrrprrrsaves,       zSourcesList.savecCsrg}i}|D].}|jdurq|jjr'|j}||vrg||<||}||qt|jjdkr4||q||fS)z5get all parent and child channels in the sources listNr)r-childr9r8r.)r/ sources_listparentsused_child_templatesrprPtemprrrcheck_for_relationss   zSourcesList.check_for_relations)Trbr\)r]r^r_r`r0rdrrrrrrrrmrrrrrrr s"    B   r c@s eZdZdZddZddZdS)r z} matcher class to make a source entry look nice lots of predefined matchers to make it i18n/gettext friendly cCsrg|_td|}|D]*}tj|}|d}|d|}t||d}|jD] }|jdur5|j|q(q dS)Nz %s/*.infoz.infor)base_dir) templatesrnrjrkbasenamer*r match_urir9)r/rf spec_filesrr?r#r-rrrr0s       zSourceEntryMatcher.__init__cCsd}|jD];}t|j|jr*t|j|jr*|j|jks"|jdkr*d}||_ |S| |jr@t|j|jr@d}||_ |Sq|S)z%Add a matching template to the sourceFrDT) rresearchrr"ra match_namer#rr-r)r/rpfoundr-rrrras&  zSourceEntryMatcher.matchN)r]r^r_r`r0rarrrrr s r __main__zentry %sz!http://archive.ubuntu.com/ubuntu/z$http://de.archive.ubuntu.com/ubuntu/zis_mirror(): %sz http://archive.ubuntu.com/ubuntuz#http://de.archive.ubuntu.com/ubuntu) __future__rrrnros.pathrjrrrr'distinfor__all__rrobjectrrr r r] init_configrrqinforUmirrorrrrrsD  -E(