D[G@sddlmZmZddlZddlZddlZddlZddlZddl Z ddl Z ddl m Z ddddd gZ d dZd d ZGd ddeZGdddeZGdddeZGdd d eZedkre jeZx%eD]Zejdejq%WeddZejdeejeddejeddndS))absolute_importprint_functionN)DistInfo is_mirror SourceEntry NullMatcher SourcesListSourceEntryMatcherc Cs|jd}|jd}||kr.dSy*|jdd}|jdd}Wntk rpdSYnXd|kr||jddd|krdSdS)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)Z master_uriZ compare_uriZ compare_srvZ master_srvr9/usr/lib/python3/dist-packages/DistUpgrade/sourceslist.pyr.s    #cCstt|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__).)listset)srrruniqLsrc@smeZdZdZdddZddZddZd d Zd d Zd dZ ddZ dS)rz single sources.list entry NcCsd|_d|_d|_g|_d|_d|_d|_g|_d|_||_ |dkrt j j dt j j d}n||_|j|d|_g|_dS)NFzDir::EtczDir::Etc::sourcelist)invaliddisabledtype architecturestrusteduridistcompscommentlineapt_pkgconfigfind_dirfindfileparsetemplatechildren)selfr r%rrr__init__Ys"               zSourceEntry.__init__cCsX|j|jkoW|j|jkoW|j|jkoW|j|jkoW|j|jkS)z- equal operator for two sources.list entries )rrrrr)r)otherrrr__eq__ms zSourceEntry.__eq__cCsF|j}g}d}d}d}xtt|D]}||dkr|ryd}d}|j|||}qd}|||7}q7||dkrd}|||7}q7|r||j rd}|j|||}q7||jr| rd}q7|||7}q7Wt|dkrB|j|n|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 piecesZtmpZp_foundZ space_foundirrrmysplitus6      zSourceEntry.mysplitc Cs|jj}|dks'|dkr4d|_dS|ddkrd|_|ddjj}|ddkrd|_dS|dd}n|jd}|dkr||dd|_|d|}n|j|}t|d kr d|_dS|dj|_ |j dkr;d|_dS|djdd kr |j djd j}x|D]}y|jdd\}}Wnt k rd|_YqzX|dkr|jd|_ qz|dkrt j||_qzd|_qzWn|dj|_t|jdkr@d|_n|dj|_t|d kr{|d d|_n g|_dS)z^ parse a given sources.list (textual) line and break it up into the field we have r#TNrrrpmrpm-srcdebdeb-srcr-z[]=Zarch,r)r8r9r:r;)r:r;r8r9)r r/rrr r$rr6r1rpop Exceptionrr!Zstring_to_boolrrrr)r)r r4r5ZoptionsZoptionkeyvaluerrrr&sT           zSourceEntry.parsecCs^| |_|r.|jjjd|_n,|jjddkrZd|j|_ndS)z# set a line to enabled or disabled r7rN)rr lstripr/)r)Z new_valuerrr set_enableds  zSourceEntry.set_enabledcCs|jjS)z debug helper )strr/)r)rrr__str__szSourceEntry.__str__cCsQ|jr|jSd}|jr(d}n||j7}|jr|jdk r|ddj|j|jrqdndf7}nU|jdk r|d|jrdnd7}n&|jr|d dj|j7}n|d |j|jf7}t |j d kr |d d j|j 7}n|j dkrC|d |j 7}n|d7}|S)z# return the current line as string rz# Nz [arch=%s trusted=%s]r>yesnoz [trusted=%s]z [arch=%s]z %s %sr z # ) rr rrrrjoinrrr1rr)r)r rrrrFs(    ,   zSourceEntry.str) __name__ __module__ __qualname____doc__r*r,r6r&rErGrFrrrrrVs   % @ c@s"eZdZdZddZdS)rz a Matcher that does nothing cCsdS)NTr)r)rrrrmatchszNullMatcher.matchN)rMrNrOrPrQrrrrrs c@seZdZdZddddZddZdd Zd d Zd ddgddZddZ ddZ dddZ ddZ ddZ ddZdS)r z8 represents the full sources.list + sources.list.d file Tz /usr/share/python-apt/templates/cCs;g|_|r!t||_n t|_|jdS)N)rr matcherrrefresh)r)Z withMatcher matcherPathrrrr* s   zSourcesList.__init__cCsg|_tjjd}|j|tjjd}x(tjd|D]}|j|qNWx-|jD]"}|jso|jj |qoqoWdS)z" update the list of known entries zDir::Etc::sourcelistzDir::Etc::sourcepartsz %s/*.listN) rr!r" find_fileloadr#globrrRrQ)r)r%partsdirsourcerrrrSs   zSourcesList.refreshccs#x|jD] }|Vq WtdS)zM simple iterator to go over self.list, returns SourceEntry types N)r StopIteration)r)entryrrr__iter__&s zSourcesList.__iter__c/saxZ|jD]OtfddDr tfdd|Dr Vq q WdS)Nc3s(|]}t||kVqdS)N)getattr).0rB)attrsrYrr /sz%SourcesList.__find..c3s|]}|VqdS)Nr)r^Z predicate)rYrrr`0s)rall)r)Z predicatesr_r)r_rYrZ__find-s"zSourcesList.__findrrNc  st|dd} |jfddddddd|d|d |} xW| D]O} xF| D]>} | | jkri| | j| =t| d kr| SqiqiWq\W|jfd dddd|d|d |} xb| D]Z} | jr t| jt| kr d| _| S| jst| j| | _| SqW|} rm| d d j7} n| d||f7} x| D]}| d|} qW|dkrd| |f} n| d} t| }|dk r||_ n|j j ||j 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 Ncst|jkS)N)rr)r)rrr?sz!SourcesList.add..rFrrrrrcst|jkS)N)rr)r)rrrrbLsz [arch=%s]r>z %s %srJrz%s #%s rK)r_SourcesList__findrrr1rrrLrr%rRrQrinsert)r)rrrZ orig_compsrposr%rrsourcesrYZnew_compr cZ new_entryr)rradd3sF    $        zSourcesList.addcCs|jj|dS)z2 remove the specified entry from the sources.list N)rremove)r)Z source_entryrrrrihszSourcesList.removecCstjjd}tjj||rQtjj|rQtj|||ntjjd}xHt j d|D]3}tjj||rwtj|||qwqwWdS)z: restore sources.list files based on the backup extension zDir::Etc::sourcelistzDir::Etc::sourcepartsz %s/*.listN) r!r"rUospathexistsshutilcopyr#rW)r) backup_extr%rXrrrrestore_backupls(zSourcesList.restore_backupcCst}|dkr'tjd}nxX|jD]M}|j|kr1tjj|jr1tj |jd|j|fq1q1W|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) rtimeZstrftimerr%rjrkrlrmrn)r)roZalready_backupedrYrrrbackupws  'zSourcesList.backupcCslyLt|d7}x-|D]%}t||}|jj|qWWdQXWntjd|YnXdS)z (re)load the current sources rNzcould not open file '%s' )openrrr2loggingZwarning)r)r%fr rYrrrrVs zSourcesList.loadcCsi}t|jdkr\tjjd}d}t|d}|j|WdQXdSz]xV|jD]K}|j|krt|jd||js8           (