
    u*e                       d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlmZmZmZ d dlmZmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlmZ d dlZd d	lm Z  d d
l!m"Z" d dl#m$Z$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z-m.Z. ddl/m0Z0 ddl0m1Z1m2Z2m3Z3 ddl/m4Z4m5Z5  ej6        e7          Z8dZ9dZ:d Z; G d de          Z<d Z=dZ>dZ?dZ@g ZAi ZB ejC                    pdZDe	jE        dk    r eFdeD          ZDdZGdZHd ZI G d d e          ZJdd!ddeDd!d!dd"dd!d"d!d!fd8d%ZKd& ZLdZMdZNdZOd'ZPd(ZQd)ZRd*ZSd+ ZTd, ZUd- ZVd. ZWd/ ZXd9d0ZYd1 ZZd2 Z[d:d3Z\d4 Z]d5 Z^ G d6 d7          Z_dS );    )annotationsN)ExecutorProcessPoolExecutorThreadPoolExecutor)datetimetimezone)Number)abspathbasenamegetmtimegetsizeisfilejoin)
NamedTuple)uuid4)context)
SubdirData)	MatchSpecResolveVersionOrderhuman_bytes)Channel)package_streaming)EnvironmentPackageLoader   )utils)CONDA_PACKAGE_EXTENSION_V1CONDA_PACKAGE_EXTENSION_V2CONDA_PACKAGE_EXTENSIONS   )rsssqlitecache   c                 J    ddl } | j        j                                         dS )zBCalled by package extraction subprocesses to re-configure logging.r   N)conda_index.index.logutilindexlogutil	configure)conda_indexs    :lib/python3.11/site-packages/conda_index/index/__init__.pylogging_configr-   2   s,    $$$$'')))))    c                      e Zd Zd Zd ZdS )DummyExecutorc              '  8   K   |D ]}|D ]} ||          V  d S N )selffunc	iterablesiterablethings        r,   mapzDummyExecutor.map;   sJ      ! 	" 	"H! " "d5kk!!!!"	" 	"r.   c                >     G fdd          } |            S )Nc                      e Zd Z fdZdS )$DummyExecutor.submit.<locals>.futurec                     i S r2   r3   )r4   argsr5   kwargss    r,   resultz+DummyExecutor.submit.<locals>.future.resultB   s    tT,V,,,r.   N)__name__
__module____qualname__r@   )r>   r5   r?   s   r,   futurer<   A   s8        - - - - - - - - -r.   rD   r3   )r4   r5   r>   r?   rD   s    ``` r,   submitzDummyExecutor.submit@   sN    	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- vxxr.   N)rA   rB   rC   r9   rE   r3   r.   r,   r0   r0   :   s2        " " "
    r.   r0    win320   i0*  z.lockc                    |dhD ]U}t           j                            | |          }t           j                            |          st          j        |           Vd S )Nnoarch)ospathr   isdirmakedirs)local_foldersubdirfolderrL   s       r,   _ensure_valid_channelrR   ^   s\    8$  w||L&11w}}T"" 	K r.   c                  2    e Zd ZU dZded<   ded<   ded<   dS )FileInfoz1
    Filename and a bit of stat information.
    strfnr	   st_mtimest_sizeN)rA   rB   rC   __doc____annotations__r3   r.   r,   rT   rT   e   s:           GGGOOOOOr.   rT   FTthreads
int | Nonec                H   t           j                            |           \  }}|t          j        v r+|	rt
                              d           t          d          t          | |||||||||
  
        }|	                    ||||
           |
                                 dS )a  
    High-level interface to ``ChannelIndex``. Index all subdirs under
    ``dir_path``. Output to `output_dir`, or under the input directory if
    `output_dir` is not given. Writes updated ``channeldata.json``.

    The input ``dir_path`` should at least contain a directory named ``noarch``.
    The path tree therein is treated as a full channel, with a level of subdirs,
    each subdir having an update to repodata.json. The full channel will also
    have a channeldata.json file.
    zThe update_index function has changed to index all subdirs at once.  You're pointing it at a single subdir.  Please update your code to point it at the channel root, rather than a subdir. Use -s=<subdir> to update a single subdir.zRDoes not accept a single subdir, or a path named like one of the standard subdirs.)subdirsr[   deep_integrity_checkdebugoutput_root	write_bz2	write_zstwrite_run_exports)patch_generatorverboseprogresscurrent_index_versionsN)rK   rL   splitr   DEFAULT_SUBDIRSlogwarn
ValueErrorChannelIndexr(   update_channeldata)dir_path
output_dir	check_md5channel_namere   r[   rf   rg   r^   rl   rh   r`   rb   rc   rd   _dirnamechannel_indexs                     r,   update_indexrw   o   s    6 x((JAw%''' 	HH=  
 0
 
 	

 !&+  M '5	     $$&&&&&r.   c                :    t          |           } | dk    r| dz  } | S )N   A   )int)	timestamps    r,   _make_secondsr}      s-    II<	
	 r.   zrepodata.jsonzrepodata_from_packages.jsonzrun_exports.json)descriptiondev_urldoc_urldoc_source_urlhomelicensereference_package
source_urlsource_git_urlsource_git_tagsource_git_revsummaryversionr^   icon_url	icon_hashrun_exportsbinary_prefixtext_prefix
activate.ddeactivate.dpre_link	post_link
pre_unlinktagsidentifierskeywordsrecipe_origincommitsc                   |                     dg            t          j        |                    di           |                    di           dd           d |                    di                                           D             }t          j        |                    di           |dd           t          j        |                    di           |                    di           dd           |                    dd          D ]}d	D ]}|                    t                    r&|dk    r |                    t          t                    }|||         v r8d
||         |         d<   ||         |         d         	                    d           |                    dd          D ]}d	D ]{}|                    t                    r&|dk    r |                    t          t                    }||         
                    |d           }|r|d         	                    |           ||d                                          |S )NremovedpackagesF)mergeadd_missing_keysc                V    i | ]&\  }}|                     t          t                    |'S r3   )replacer   r   .0kvs      r,   
<dictcomp>z'_apply_instructions.<locals>.<dictcomp>   s>       Aq 	
		,.HII1  r.   packages.condarevoker3   )r   r   Trevokeddependspackage_has_been_revokedremove)
setdefaultr   merge_or_update_dictgetitemsendswithr   r   r   appendpopsort)rP   repodatainstructionsnew_pkg_fixesrV   keypoppeds          r,   _apply_instructionsr      s   	2&&&	Z$$R((	     $$Z44::<<  M
 
%r**	    
%r**)2..	    x,, P P1 	P 	PC{{566 X3BR;R;RZZ :<VWWXc]""/3b!),b!),334NOOO	P x,, / /1 	/ 	/C{{566 X3BR;R;RZZ :<VWWc]&&r400F /#**2...	/ YOr.   c                     d } d }t          t          dd                    }t          |j        d<   | |j        d<   ||j        d<   d	|_        d	|_        |S )
Nc                    t          | t          t          f          r+| dk    r| dz  } t          j        | t
          j                  } |                     |          S )Nry   rz   )tz)
isinstancer{   floatr   fromtimestampr   utcstrftime)dt	dt_formats     r,   _filter_strftimez1_get_jinja2_environment.<locals>._filter_strftime  sU    b3,'' 	=L  t'x|<<<B{{9%%%r.   c                    |rQd| dg}|d |                                 D             z  }d                    d                    |          |           S | S )Nzhref=""c                $    g | ]\  }}| d | dS )z="r   r3   r   s      r,   
<listcomp>zE_get_jinja2_environment.<locals>._filter_add_href.<locals>.<listcomp>  s(    DDDTQqLLALLLDDDr.   z<a {}>{}</a> )r   formatr   )textlinkr?   kwargs_lists       r,   _filter_add_hrefz1_get_jinja2_environment.<locals>._filter_add_href  se     	+D+++,KDDV\\^^DDDDK!((+)>)>EEEKr.   r+   	templates)loaderr   r   add_hrefT)r   r   r   filterstrim_blockslstrip_blocks)r   r   environments      r,   _get_jinja2_environmentr     s    & & &   ]K88  K *5K&&6K
#&6K
#"K $Kr.   c                    t                      }|                    d          }|                    d                    | pd|          |t	          j        t          j                  |          }|S )Nzsubdir-index.html.j2z{}/{}rF   )titler   current_timeextra_paths)r   get_templaterenderr   r   nowr   r   )rs   rP   repodata_packagesr   r   templaterendered_htmls          r,   _make_subdir_index_htmlr   -  sl    )++K''(>??HOOnn\/R88"\(,//	 $  M r.   c                    t                      }|                    d          }|                    | |d         |d         t          j        t
          j                            }|S )Nzchanneldata-index.html.j2r   r^   )r   r   r^   r   )r   r   r   r   r   r   r   )rs   channeldatar   r   r   s        r,   _make_channeldata_index_htmlr   9  sc    )++K''(CDDHOOZ(I&\(,//	 $  M r.   c                ,    t          j        | |          S r2   )r"   get_rss)rs   r   s     r,   	_make_rssr   E  s    ;|[111r.   c                   i }i }|s| t           j        t           j        d||d}t          d|            }t	          |          }t          j        t          j        |                    }|                    |           d|_	        |t          j
        |                    d          <   d |p|j        D             }t          ||f          }	|	S )N)rP   archplatform)infor   r   z)https://conda.anaconda.org/dummy-channel/T)with_credentialsc                    i | ]}||S r3   r3   )r   precs     r,   r   z'_get_resolve_object.<locals>.<dictcomp>c  s    AAADT4AAAr.   )channels)r   	arch_namer   r   r   jsonloadsdumps_process_raw_repodata_loaded_cache_url_package_recordsr   )
rP   precsr   r   conda_packageschannelsdrepodata_copyr(   rs
             r,   _get_resolve_objectr   I  s    HN 	
 !)#, 
 !,
 
 J&JJKKG	G		B Jtz(3344M ]+++BJ=?Jw{{D{99:AAE$@R-@AAAE
+++AHr.   c                   t          j        | j                  }t                      }| j                                        D ]\  }}|D ]}|j        D ]}||v rt          |          }|                     |          s|                    |          }	|	rw|	d         j        }
t          |	                    |j
        g                     t          |                    t          |j
         d|
                               z  ||j
        <   |                    |           όٌd |                                D             S )zZFor each package in new_r, if any deps are not satisfiable, backfill them from original_r.r   =c                    g | ]	}|D ]}|
S r3   r3   r   grouppkgs      r,   r   z%_add_missing_deps.<locals>.<listcomp>}  %    GGGEGG#CGGGGr.   )copydeepcopygroupssetr   r   r   find_matchesr   r   nameaddvalues)new_r
original_rexpanded_groups
seen_specsg_nameg_recsg_recdep_specmsmatchesr   s              r,   _add_missing_depsr  h  sa    mEL11OJ,,,.. ) ) 	) 	)E!M ) )z))x(())"-- (55b99G ")!*"436+//<<4 4&33I>T>T7>T>T4U4UVV 40
 x(((()	) HG_3355GGGGr.   c                   t          j        | j                  }| j        D ]}t          d | j        |         D                       s(t	          t          | j        |         d         j                            }||j        v rd }t          t          |j        |                             D ]L}|j        |         |         }t	          t          |j                            |k    r|j	        s|j
        s|} nM|*|ht          |                    |g                     z  ||<   d |                                D             S )Nc              3  2   K   | ]}|j         p|j        V  d S r2   )track_featuresfeatures)r   ms     r,   	<genexpr>z-_add_prev_ver_for_features.<locals>.<genexpr>  s,      PPa1#1qzPPPPPPr.   r   c                    g | ]	}|D ]}|
S r3   r3   r  s      r,   r   z._add_prev_ver_for_features.<locals>.<listcomp>  r  r.   )r  r  r	  anyr   rU   r   rangelenr  r  r
  r   r  )r  orig_rr  r  latest_versionkeep_mi_ms           r,   _add_prev_ver_for_featuresr)    sW   mEL11O,  PP5<;OPPPPP 	 &c%,v*>q*A*I&J&JKKV]"" F3v}V45566  ]6*1-BJ00NBB% C)+ C  FE!+1(S#''336 6 +' HG_3355GGGGr.   c                   i }|pi }|j                                         D ]\  }}|j         |         d         j        }t          |                    t          | d|                               }||v rw||         D ]n}|                    t          | d|                     d         j        }|                    |                    t          | d|                                o|||<   t          | d |                                D                       }	t          | t          |	|                    }	t          t          |	|                    S )aH  Captures only the newest versions of software in the resolve object.

    For things where more than one version is supported simultaneously (like Python),
    pass pins as a dictionary, with the key being the package name, and the value being
    a list of supported versions.  For example:

    {'python': ["2.7", "3.6"]}
    r   r  c                    g | ]	}|D ]}|
S r3   r3   r  s      r,   r   z*_shard_newest_packages.<locals>.<listcomp>  s%    IIIu5IICsIIIIr.   )r   )r	  r   r   r
  r  r   updater   r  r  r)  )
rP   r   pinsr	  r  r  r   r  	pin_valuer  s
             r,   _shard_newest_packagesr/    so    F:2D(..** ! !(6"1%-annY&/D/D7/D/D%E%EFFGGT>>!&\ Q Q	..f3J3Jy3J3J)K)KLLQOWq~~i68M8MG8M8M.N.NOOPPPP v  IIIII  E  .?q.I.IJJJE )%33444r.   c                   t          |           }t          | ||          }fdt                                                    ddhz
  D             }i }i }|D ]}|j                            t                    r~d         |j                 ||j        <   |j                            t          t                    }	d         	                    |	i           	                    d          ||j                 d<   |j                            t                    rd         |j                 ||j        <   ||d<   ||d<   |S )N)r   c                "    i | ]}||         S r3   r3   )r   r   r   s     r,   r   z+_build_current_repodata.<locals>.<dictcomp>  s,       8A;  r.   r   r   md5legacy_bz2_md5)
r   r/  r
  keysrV   r   r   r   r   r   )
rP   r   r-  r   	keep_pkgsnew_repodatar   r   keep_pkgcounterparts
    `        r,   _build_current_repodatar9    se   FX666A&vq$77I    #HMMOO 4 4
DT7U U  L HN F F; :;; 	F*23C*DX[*QN8;' #+--*,F K $((b99==eDD 8;'(899 [!!"<== 	F$,Z$8$EHX[!'L%3L!"r.   c                    | s|dk    rt                      n(t          |t          t          j        d                    S )Nr!   spawn)initializer
mp_context)r0   r   r-   multiprocessingget_context)r`   r[   s     r,   thread_executor_factoryr@    sK     	
\\ 	 &&27;;
 
 
r.   c            
          e Zd ZdZdedddej        ddddf
d+dZ	 	 	 d,dZd-dZ	d.dZ
d Zd Zd/dZd Zd0dZd Zd Zd Zd Zd Zd Zd Zd/dZd Zd1d Zd! Zd" Zd1d2d%Zd.d3d(Zd3d)Zd* ZdS )4rn   z
    Class implementing ``update_index``. Allows for more fine-grained control of
    output.

    See the implementation of ``conda_index.cli`` for usage.
    NFTr[   r\   c                |   |t           }|| _        t          |          | _        |rt          |          n| j        | _        |p!t          |                    d                    | _        || _        t          j
        t          ||          | _        || _        || _        |	| _        |
| _        || _        || _        d S )N/)MAX_THREADS_DEFAULTcache_classr
   channel_rootra   r   rstriprs   _subdirs	functoolspartialr@  r`   r_   rb   rc   rd   compact_json)r4   rF  rs   r^   r[   r_   r`   ra   rE  rb   rc   rd   rK  s                r,   __init__zChannelIndex.__init__  s     ?)G&#L113>U7;///DDU(NH\5H5H5M5M,N,N'0'8#UG(
 (
$ 
$8!""!2(r.   c                   	
 r t                               dt                                                      
t	          j        t	          j         j                  gd          5   
fd}                                 5 		 fd |            D             }|D ]3}|	                                }t           
                    d|            4	 ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z|
        Examine all changed packages under ``self.channel_root``, updating
        ``index.html`` for each subdir.
        zYChannelIndex.index(verbose=...) is a no-op. Alter log levels for %s to control verbosity.i  )timeoutc                 |    t          d          } fd}dfd}|                     | |                      S )Nr!   )max_workersc               3  z   K   D ]4} t          j        |            t          j        |           }| |fV  5d S r2   )rR   rF  r   )rP   subdir_pathrg   r4   r^   rf   s     r,   extract_argszJChannelIndex.index.<locals>.extract_subdirs_to_cache.<locals>.extract_args(  sa      ") G G-d.?HHH&*4+<f&E&E%w+FFFFF	G Gr.   r>   tuplec                n    | \  }}}}                     |          }                    |||||          S r2   )cache_for_subdirextract_subdir_to_cache)r>   rP   rf   rg   rR  cacher4   s         r,   extract_wrapperzMChannelIndex.index.<locals>.extract_subdirs_to_cache.<locals>.extract_wrapper/  sG    =A:FGX{ 11&99E77;  r.   )r>   rT  )r   r9   )executorrS  rY  rg   r4   r^   rf   s      r,   extract_subdirs_to_cachez4ChannelIndex.index.<locals>.extract_subdirs_to_cache%  s}    -!<<<G G G G G G G G       ||O\\^^DDDr.   c                t    g | ]4}                     t          j        j        |                     5S ))rP   rf   rg   re   rh   )rE   rI  rJ  index_prepared_subdir)r   rP   rh   index_processre   rg   r4   rf   s     r,   r   z&ChannelIndex.index.<locals>.<listcomp>?  se         "((!) 6#)$+%-,;3I  	 	  r.   z
Completed N)rk   r`   rA   detect_subdirsr   try_acquire_locksget_lockrF  r@  r@   r   )r4   re   rf   rg   rh   r[  futuresrD   r@   r^  r^   s   `````    @@r,   r(   zChannelIndex.index  s     	IIk  
 %%'' $enT5F&G&G%HRUVVV .	4 .	4E E E E E E E E2 --// 4=         #;":"<"<   & 4 4F#]]__FHH2&22333344 4 4 4 4 4 4 4 4 4 4 4 4 4 4;.	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4 .	4s7   ,D 	AC(D (C,	,D /C,	0D  DDrP   rU   rf   boolrg   c                   t                               d|           |                     |||          }t                               d|           |                     ||t                     t                               d|           |                     |||          \  }}t                               d|           t                               d|           |                     ||t                     t                               d|           t                               d|           t          |||	          }	t                               d
|           t                               d|           |                     ||	d           | j	        rht                               d|           | 
                    |          }
t                               d|           |                     ||
t                     t                               d|           t                               d|           |                     ||           t                               d|           |S )z
        Create repodata_from_packages.json by calling index_subdir, then apply
        any patches to create repodata.json.
        zSubdir: %s Gathering repodata)rf   rg   z%s Writing pre-patch repodataz%s Applying patch instructionsz%s Writing patched repodataz%s write patched repodataz#%s Building current_repodata subsetz%s build current_repodata)r-  z"%s Writing current_repodata subsetz%s write current_repodatazcurrent_repodata.json)json_filenamez%s Building run_exports dataz%s Writing run_exports.jsonz%s Writing index HTMLz%s write index.htmlz	%s finish)rk   r   index_subdir_write_repodataREPODATA_FROM_PKGS_JSON_FN_patch_repodatar`   REPODATA_JSON_FNr9  rd   build_run_exports_dataRUN_EXPORTS_JSON_FN_write_subdir_index_html)r4   rP   rf   rg   re   rh   repodata_from_packagespatched_repodatart   current_repodatarun_exports_datas              r,   r]  z"ChannelIndex.index_prepared_subdirQ  sZ    	0&999!%!2!2Gh "3 "
 "
 	0&999"&	
 	
 	
 	16:::"22*O
 
! 	.777		-v666V%57GHHH6???		-v6662$+A
 
 
 	5v>>>		-v6661 	 	
 	
 	
 ! 		HH3V<<<#::6BBHH2F;;;   1 !    	(&111		'000%%f.>???		+v&&&r.   c                f   |                                  }i }|                                 }t          j                            |          r;t          |          5 }t          j        |          }ddd           n# 1 swxY w Y   |D ]}t          	                    d|           t          
                    d|           t          t          j                            | j        |t                              5 }t          j        |          }ddd           n# 1 swxY w Y   |                     |||           t          
                    d|           |r|                     |           |                     |           t          
                    d           |                     |           dS )z
        Update channeldata based on re-reading output `repodata.json` and existing
        `channeldata.json`. Call after index() if channeldata is needed.
        NzChanneldata subdir: %sz%s read repodataz%s channeldata finishedzwrite channeldata)r_  channeldata_pathrK   rL   r   openr   loadrk   r   r`   r   ra   rj  _update_channeldata
_write_rss_write_channeldata_index_html_write_channeldata)	r4   r"   r^   channel_datachanneldata_filefrP   r   ro  s	            r,   ro   zChannelIndex.update_channeldata  s   
 %%''
 00227>>*++ 	,&'' ,1#y||, , , , , , , , , , , , , , ,  
	9 
	9FHH-v666II(&111T-v7GHH  7#'9X#6#6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 $$\3CVLLLII/8888  	*OOL))) 	**<888		%&&&-----s$   A::A>A>7DD	D	c                j   | j         sWd t          j        | j                  D             }t                              d|           t          |dhz            | _        nOt          t          | j                             | _        d| j        vr t          	                    d| j                   | j        S )Nc                j    h | ]0}|j         t          j        v |                                )|j         1S r3   )r  r   rj   is_dir)r   rP   s     r,   	<setcomp>z.ChannelIndex.detect_subdirs.<locals>.<setcomp>  sA          ;%"777FMMOO7 777r.   zfound subdirs %srJ   z%Indexing %s does not include 'noarch')
rH  rK   scandirrF  rk   r`   sortedr^   r
  rl   )r4   detected_subdirss     r,   r_  zChannelIndex.detect_subdirs  s    } 	P    j):;;     
 II(*:;;;!"2hZ"?@@DLL!#dm"4"455DLt|++@$,OOO|r.   c                P    t           j                            | j        d          }|S )Nchanneldata.json)rK   rL   r   ra   )r4   r{  s     r,   rs  zChannelIndex.channeldata_path  s"    7<<(8:LMMr.   c                    t          | j        |          }|                     |          }t                              d|           |                                \  }}||d|it          g d}|S )z
        Return repodata from the cache without reading old repodata.json

        Must call `extract_subdir_to_cache()` first or will be outdated.
        zBuilding repodata for %srP   )r   r   r   repodata_versionr   )r   rF  rV  rk   r`   indexed_packagesREPODATA_VERSION)	r4   rP   rf   rg   rR  rX  new_repodata_packagesnew_repodata_conda_packagesr6  s	            r,   rf  zChannelIndex.index_subdir  s     4,f55%%f--		,k:::=B=S=S=U=U:: .9& !1
 
 r.   c                t    |                      | j        |          }|j        r|                                 |S )N)rF  rP   )rE  rF  cache_is_brand_newconvert)r4   rP   rX  s      r,   rV  zChannelIndex.cache_for_subdir  sC    -1-=-=*6 .> .
 .
 # 	MMOOOr.   rX  sqlitecache.CondaIndexCachec           	     j                        |           t                              d|           fd                                D             }t                              d|t	          |                     t          j        j        | j        |          }t          j	                    }d}	| 
                                5 }
|
                    ||          D ]L\  }}}}|	|z  }	|r>|r<|rt                              dt          j                            ||                     Mt          j	                    }	 |	||z
  z  }n# t           $ r d}Y nw xY wddd           n# 1 swxY w Y   t                              d|t%          |	          t	          |          t%          |                     |S )ze
        Extract all changed packages into the subdir cache.

        Return name of subdir.
        z%s find packages to extractc                ~    g | ]9}t                              |d                    |d         |d                   :S )rL   mtimesize)rV   rW   rX   )rT   
plain_path)r   rowrX  s     r,   r   z8ChannelIndex.extract_subdir_to_cache.<locals>.<listcomp>  s]     
 
 
  ##CK00WF  
 
 
r.   z%s extract %d packagesr   zPackage at %s did not contain valid index.json data.  Please check the file and remove/redownload if necessary to obtain a valid package.Nz*%s cached %s from %s packages at %s/second)save_fs_staterk   r`   changed_packagesr#  rI  rJ  extract_to_cache_info_objectrF  timer@  r9   errorrK   rL   r   ZeroDivisionErrorr   r   )r4   rP   rf   rg   rR  rX  extractextract_func
start_timesize_processedthread_executorrV   r  r  
index_jsonend_time	bytes_secs        `           r,   rW  z$ChannelIndex.extract_subdir_to_cache  s)    	K(((		/888
 
 
 
 --//
 
 
 			*FCLLAAA !(.0A6
 
 Y[[
))++ 	/>/B/Bg0 0  +E4 $& 	% 	! 		/ GLLb99	   y{{H*h.CD		$   			'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	* 	8''LL	""	
 	
 	
 s7   A7E#<EE#EE#EE##E'*E'c                   t          | j        ||          }|                     |          }|                     ||d          }|dz   }|dz   }|st          j                            |          s| j        r>t          j	        |
                    d                    }	|                     ||	           n|                     |           | j        r]t          j        t          t                     	                    |
                    d                    }
|                     ||
           n|                     |           |S )zH
        Write repodata to :json_filename, but only if changed.
        F)write_newline_end.bz2z.zstutf-8)levelr[   )r   rF  
json_dumps_maybe_writerK   rL   existsrb   bz2compressencode_maybe_removerc   	zstandardZstdCompressorZSTD_COMPRESS_LEVELZSTD_COMPRESS_THREADS)r4   rP   r   re  repodata_json_pathr6  write_resultrepodata_bz2_pathrepodata_zst_pathbz2_contentrepodata_zst_contents              r,   rg  zChannelIndex._write_repodata9  sT    "$"3V]KKx00(( ) 
 

 /7.7 	6rw~~.?@@ 	6~ 6!l<+>+>w+G+GHH!!"3[AAAA""#4555~ 6'0'?-7L( ( ((<..w7788 % !!"35IJJJJ""#4555r.   c                    |d         }|d         }i }|                     |           |                     |           t           j        |          } fd}i } ||t          |t                                j        r" ||t          |t          dz                         ||t          |t
                                j        r" ||t          |t
          dz                         ||t          |d                     t           j        |||          }	|	sJ t          |d          }
                     |
|	          S )Nr   r   c                    t          t          j        |                    rXt          j        |d          \  }}t          |          t          t          |                    ||d| t          |          <   d S d S )N)r2  sha256)r  r|   r  r2  )	r   r   rF  r   	checksumsr   r{   r   r   )r   rL   md5sum	sha256sumr4   s       r,   _add_extra_pathz>ChannelIndex._write_subdir_index_html.<locals>._add_extra_path_  s~    d4,d3344 $)OD:K$L$L!	#DMM!$Xd^^!4!4'!	/ /HTNN+++ r.   r  patch_instructions.json
index.html)	r,  r   rF  rj  rb   rh  r   rs   r  )r4   rP   r   repodata_legacy_packagesrepodata_conda_packagesr   rR  r  r   r   
index_paths   `          r,   rm  z%ChannelIndex._write_subdir_index_htmlU  s   #+J#7 "*+;"<  !9:::  !89994,f55	 	 	 	 	 T+7G%H%HIII> 	WOKk;Kf;T)U)UVVVT+7Q%R%RSSS> 	OT+/IF/RSS   	T+7P%Q%QRRR/v'8+
 
 +|44
  ];;;r.   c                    t                               d           t          | j        |          }t	          | j        d          }|                     ||           t                               d           d S )Nz	Build RSSzrss.xmlz	Built RSS)rk   r   r   rs   r   rF  r  )r4   r   r"   rss_paths       r,   rw  zChannelIndex._write_rss{  sf    );77)955(C(((r.   c                    t          | j        |          }|sJ t          | j        d          }|                     ||           d S )Nr  )r   rs   r   rF  r  )r4   r   r   r  s       r,   rx  z*ChannelIndex._write_channeldata_index_html  sN    4T5FTT$+\::
*m44444r.   c                   |                      |          }|d         |d         }t                                                    d |                                D             z
  }|                                }|                    fd|D                        |                    di           }d }	 |	|          }
d }g g }}|
rt          |
 \  }}|j        }|                                 5 }t          ||	                    ||                    D ]J\  }}|rA|                    |           |d         }|                    |i           }|                    dd	          }|                    dd	          }t          |          t          |          k    pM||k    oGt          |                    d
d                    t          |                    d
d                    k    }|                    |i           ||<   dD ]} |||         ||||           dD ]B}t          |                    |          |                    |          f          ||         |<   Ct          t          t          |                    dg           |gz                                 ||         d<   |                    di           }|                    d          }|r|                    d          ||<   |||         d<   t          t          |                    d
d          |                    |i                               d
d                              ||         d
<   L	 d d d            n# 1 swxY w Y   |                    t           t          t          t          |                    dg           |gz                                 |d           d S )Nr   r   c                2    h | ]}|d d         t           z   S )Ni)r   )r   r   s     r,   r  z3ChannelIndex._update_channeldata.<locals>.<setcomp>  s3     ?
 ?
 ?
45AcrcF//?
 ?
 ?
r.   c                "    i | ]}||         S r3   r3   )r   r   legacy_packagess     r,   r   z4ChannelIndex._update_channeldata.<locals>.<dictcomp>  s     BBBqQ"BBBr.   c                D   i }|                                  D ]i\  }}|d         |d         f}|                    dd          }|                    |          }|r |d                             dd          |k     r||f||<   jt          |                                          S )Nr  r   r|   r   r!   )r   r   listr  )all_repodata_packagesnameverrV   packager   r|   existings          r,   newest_by_name_and_versionzDChannelIndex._update_channeldata.<locals>.newest_by_name_and_version  s    G4::<< 1 1Gv	(:;#KKQ77	";;s++ 18A;??;#B#BY#N#N$&=GCL(()))r.   c                    |                     |          r$|s|                     |          s||         | |<   d S |                     |          | |<   d S r2   )r   )pddataexisting_record
data_newerr   s        r,   _replace_if_newer_and_presentzGChannelIndex._update_channeldata.<locals>._replace_if_newer_and_present  s\    xx{{ /
 //2E2Ea2H2H /Q1'++A..1r.   r  r   0r|   r   )r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r^   r   )channeldata_versionr^   r   )rV  r
  r4  r  r,  r   zipload_all_from_cacher@  r9   r   r}   r!  r  r  maxCHANNELDATA_VERSION)r4   rz  r   rP   rX  r   use_these_legacy_keysr  package_datar  r	  r  fnsfn_dicts	load_funcr  fn_dictr  r  r  data_verec_vr  r   r   exports_from_this_versionr  s                             @r,   rv  z ChannelIndex._update_channeldata  s   %%f--":.!"23 #O$8$8$:$: ; ; ?
 ?
9G9L9L9N9N?
 ?
 ?
 !
 !/ 3 3 5 5$$BBBB,ABBB	
 	
 	
 $''
B77
	* 
	* 
	* ,+,ABB	/ 	/ 	/ BX 	)LMC-	))++ E	!$X/B/B9c/R/R!S!S D D BKK(((<D&2&6&6tR&@&@O!XXi55F,00C@@F!-f!5!5V8L8L!L "&( M)$((;*B*BCC'(;(;K(K(KLLM  *6)9)9$)C)CL&  $ 65(.ozST   
   14!XXa[[/*=*=a*@*@A1 1T*1-- 5;S!4!4Y!C!Cvh!NOOPP5 5L&y1 #2"5"5mR"H"HK040G0G-0 F.2hh}.E.EF+8CL&}56C HH[!44(,,T266::;JJ 7 7L&{3DE	 E	 E	 E	 E	 E	 E	 E	 E	 E	 E	 E	 E	 E	 E	N 	':!\--i<<xGHHII  ) 	
 	
 	
 	
 	
s   0I1M..M25M2c                r    | j         rt          j        |dd          S t          j        |dd          dz   S )z4
        Format json based on class policy.
        T),:)	sort_keys
separatorsr   )r  indent
)rK  r   r   )r4   r  s     r,   r  zChannelIndex.json_dumps  sC      	E:ddzJJJJ:dd1===DDr.   c                2   |                     di                                           D ],\  }}d |                                D             |d         |<   -t          | j        d          }|                     |          }|                     ||d           d S )Nr   c                *    i | ]\  }}||dk    ||S )Nr   r3   r   s      r,   r   z3ChannelIndex._write_channeldata.<locals>.<dictcomp>  s.     , , ,AQ]qI~~1~~~r.   r  T)r   r   r   rF  r  r  )r4   r   r  pkg_dictrs  contents         r,   ry  zChannelIndex._write_channeldata  s    (__Z<<BBDD 	 	MC, ,!)!1!1, , ,K
#C((   13EFF//+..*GT:::::r.   c                   t          | j        |          }|                     |          }t                              d|           i }i }|j                            d|j        f          D ]z}|\  }	}
dt          j	        |
pd          i}
|	
                    t                    r|
||	<   ?|	
                    t                    r|
||	<   _t                              d|	           {|||t          dd}|S )z
        Return CEP-12 compliant run_exports metadata from the db cache.

        Must call `extract_subdir_to_cache()` first or will be outdated.
        zBuilding run_exports for %sz
            SELECT path, run_exports FROM stat
            LEFT JOIN run_exports USING (path)
            WHERE stat.stage = ?
            ORDER BY path
            r   z{}z$%s doesn't look like a conda package)rP   r   )r   r   r   )r   rF  rV  rk   r`   dbexecuteupstream_stager   r   r   r   r   rl   RUN_EXPORTS_VERSION)r4   rP   rf   rg   rR  rX  run_exports_packagesrun_exports_conda_packagesr  rL   rq  new_run_exports_datas               r,   rk  z#ChannelIndex.build_run_exports_data!  s4    4,f55%%f--		/===!%'" 8## !#
 
 	G 	GC &)"D" -tz:J:Rd/S/ST}}788 G-=$T**9:: G3C*400?FFFF -8 .  
  
 $#r.   c                    i }d                     |df          }t          j        |d          D ]E\  }}|j        |k    r5|                    |          }|s
J d            t          j        |          }F|S )NrC  r  r  )	componentz!tar member was not a regular file)r   r   stream_conda_componentr  extractfiler   ru  )r4   rP   re   r   targettarmemberreaders           r,    _load_patch_instructions_tarballz-ChannelIndex._load_patch_instructions_tarballN  s    6#<=>>,Cu
 
 
 	1 	1KC {f$$00BBBBBB#y00r.   c                   |pt          | j        d          }t          |          rt                              d| d|            ddlm}m}  |d|          }|r-|j        r& ||          }|j        	                    |           nt                      |                    ||          }	|	                    dd          dk    rt          d	          |	S |r"t          d
                    |                    i S )Nzgen_patch.pyzusing patch generator z for r   )module_from_specspec_from_file_locationa_bpatch_instructions_versionr!   'Incompatible patch instructions versionzSpecified metadata patch file '{}' does not exist.  Please try an absolute path, or examine your relative path carefully with respect to your cwd.)r   rF  r   rk   r`   importlib.utilr  r  r   exec_moduleImportErrorri  r   RuntimeErrorrm   r   )
r4   rP   r   re   gen_patch_pathr  r  specmodr   s
             r,   _create_patch_instructionsz'ChannelIndex._create_patch_instructions[  s?   (SD1BN,S,S.!! 	IIL~LLFLLMMM QPPPPPPP**5.AAD $ $&&t,,'',,,,!mm#..x@@L <a@@1DD"#LMMM  ^^d^d'_ _   Ir.   c                    |                      |          }t          | j        |d          }|                     ||d           d S )Nr  T)r  r   rF  r  )r4   rP   r   	new_patchpatch_instructions_paths        r,   _write_patch_instructionsz&ChannelIndex._write_patch_instructionsz  sO    OOL11	"&v'@#
 #
 	19dCCCCCr.   c                R   t          | j        |d          }t          |          rt                              d|           t          |          5 }t          j        |          }|                    dd          dk    rt          d          |cd d d            S # 1 swxY w Y   i S )Nr  zusing patch instructions %sr	  r   r!   r
  )
r   rF  r   rk   r`   rt  r   ru  r   r  )r4   rP   r  fhr   s        r,   _load_instructionszChannelIndex._load_instructions  s    "&v'@#
 #
 )** 	$II35LMMM-.. $"#y}}##$@!DDqHH&'PQQQ#	$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
 	s   ?BB #B re   
str | Nonec                j   |r1|                     t                    r|                     ||          }n|                     |||          }|r|                     ||           n|                     |          }|                    dd          dk    rt          d          t          |||          |fS )Nr	  r   r!   r
  )	r   r    r  r  r  r  r   r  r   )r4   rP   r   re   r   s        r,   ri  zChannelIndex._patch_repodata  s     	778PQQ 	@@ LL  ::/ L  	;**6<@@@@226::L8!<<q@@HIII"68\BBLPPr.   r  str | bytesc                   | dt                       }t          j                            | j        t          j                            || j                            }t          j                            | j        t          j                            || j                            }t          j        t          j                            |          d           t          
                    d| d|            |                     ||||          S )N.T)exist_okz_maybe_write z to )r   rK   rL   r   ra   relpathrF  rN   ru   rk   r`   _maybe_write_output_paths)r4   rL   r  r  	temp_pathoutput_pathoutput_temp_paths          r,   r  zChannelIndex._maybe_write  s     ''egg''	 gllrwtT5FGG
 
 7<<rwy$:KLL
 
 	BGOO$455EEEE		9$99K99:::--["24E
 
 	
r.   c                   t          |t                    r	d}d}d}d}nd}d}d}d}t          ||||          5 }	|	                    |           |r|	                    |           ddd           n# 1 swxY w Y   t	          |          r+t          j        ||          rt          j        |           dS t          j	        ||           d	S )
z+
        Internal to _maybe_write.
        wr  r  wbN   
)modeencodingnewlineFT)
r   rU   rt  writer   r   file_contents_matchrK   unlinkmove_with_fallback)
r4   r  r#  r$  r  r)  r*  r+  newline_optionr  s
             r,   r!  z&ChannelIndex._maybe_write_output_paths  s;    gs## 		"DHG!NNDHG!N "	
 
 
 	"
 HHW  "!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" + 	()9;GG 	*+++u !1;???ts   -A33A7:A7c                    t           j                            | j        t           j                            || j                            }t                              d| d|            t          |          rt          j	        |           dS dS )zR
        Remove path if it exists, rewriting to respect self.output_root.
        z_maybe_remove z from N)
rK   rL   r   ra   r   rF  rk   r`   r   r.  )r4   rL   r#  s      r,   r  zChannelIndex._maybe_remove  s     gllrwtT5FGG
 
 			<4<<{<<===+ 	#Ik"""""	# 	#r.   r[   r\   )FFN)rP   rU   rf   rc  rg   rc  )F)FF)rX  r  r2   )re   r  )r  r  ) rA   rB   rC   rY   rD  r#   CondaIndexCacherL  r(   r]  ro   r_  rs  rf  rV  rW  rg  rm  rw  rx  rv  r  ry  rk  r  r  r  r  ri  r  r!  r  r3   r.   r,   rn   rn     s(          3"/ )  )  )  )  )J #B4 B4 B4 B4HJ J J JX#. #. #. #.J          4  ? ? ? ?B  8$< $< $<L  5 5 5D
 D
 D
LE E E; ; ;+$ +$ +$ +$Z     >D D D  Q Q Q Q Q$
 
 
 
 
,$ $ $ $L# # # # #r.   rn   r2  )NNr2   )`
__future__r   r  r  rI  r   loggingr>  rK   sysr  concurrent.futuresr   r   r   r   r   numbersr	   os.pathr
   r   r   r   r   r   typingr   uuidr   r  conda.base.contextr   conda.core.subdir_datar   conda.exportsr   r   r   r   conda.models.channelr   conda_package_streamingr   jinja2r   r   rF   r   r   r   r    r"   r#   	getLoggerrA   rk   r  r  r-   r0   local_index_timestampcached_indexlocal_subdirlocal_output_foldercached_channelsrz  	cpu_countrD  r   minLOCK_TIMEOUT_SECSLOCKFILE_NAMErR   rT   rw   r}   r  r  r  rj  rh  rl  CHANNELDATA_FIELDSr   r   r   r   r   r   r  r)  r/  r9  r@  rn   r3   r.   r,   <module>rM     sg   # " " " " " 



            				 



  P P P P P P P P P P ' ' ' ' ' ' ' '       F F F F F F F F F F F F F F F F                 & & & & & & . - - - - - G G G G G G G G G G G G ( ( ( ( ( ( 5 5 5 5 5 5 - - - - - - - -               
        g!!   * * *    H       #blnn) LG#b"566       z    /	
<' <' <' <' <'~      " : (  B- - -`  8	 	 		 	 	2 2 2   >H H H0H H H85 5 5 5@  6	 	 	F# F# F# F# F# F# F# F# F# F#r.   