o
    Wod|                     @  s  d dl mZ d dlmZ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mZ d dlmZ d dlZd dlZd dlZddlmZ ddlmZmZmZmZmZ dd	lmZmZmZm Z m!Z!m"Z"m#Z# ej$rd d
l%m&Z& ej'ej( 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*Z0d1eZ2d:dd Z3G d!d" d"Z4ed#d$G d%d& d&Z5d;d*d+Z6d;d,d-Z7d<d0d1Z8d=d3d4Z9d>d8d9Z:dS )?    )annotations)	dataclassInitVarN)ThreadPoolExecutor)Path   )mlog)	quiet_gitGitException
Popen_safeMesonExceptionwindows_proof_rmtree)ResolverWrapException	ALL_TYPESPackageDefinitionparse_patch_urlupdate_wrap_fileget_releases)Protocolc                   @  s>   e Zd ZU ded< ded< ded< ded< ded	< d
ed< dS )	Argumentsstr	sourcedirintnum_processesT.List[str]subprojectstypeszT.Callable[[], bool]subprojects_funcboolallow_insecureN__name__
__module____qualname____annotations__ r&   r&   vhome/ych/rk3568/buildroot/output/rockchip_rk3568_recovery/host/lib/python3.10/site-packages/mesonbuild/msubprojects.pyr      s   
 r   c                   @     e Zd ZU ded< ded< dS )UpdateArgumentsr   rebaseresetNr!   r&   r&   r&   r'   r)   "      
 r)   c                   @     e Zd ZU ded< ded< dS )UpdateWrapDBArgumentsr   forcezT.Dict[str, T.Any]releasesNr!   r&   r&   r&   r'   r.   &   r,   r.   c                   @  r-   )CheckoutArgumentsr   br   branch_nameNr!   r&   r&   r&   r'   r1   *   r,   r1   c                   @  r-   )ForeachArgumentsr   commandr   argsNr!   r&   r&   r&   r'   r4   .   r,   r4   c                   @  r(   )PurgeArgumentsr   confirminclude_cacheNr!   r&   r&   r&   r'   r7   2   r,   r7   c                   @  r(   )PackagefilesArgumentsr   applysaveNr!   r&   r&   r&   r'   r:   6   r,   r:   , pathr   	base_pathreturnT.Set[Path]c                   s   | j dkr)t| d} fdd| D }W d    |S 1 s"w   Y  |S t| } fdd|D }W d    |S 1 sCw   Y  |S )Nz.ziprc                      h | ]} |j  qS r&   )filename.0ir?   r&   r'   	<setcomp>?       z%read_archive_files.<locals>.<setcomp>c                   rC   r&   )namerE   rH   r&   r'   rI   B   rJ   )suffixzipfileZipFileinfolisttarfileopen)r>   r?   Zzip_archivearchive_filesZtar_archiver&   rH   r'   read_archive_files<   s   


rS   c                   @  s>   e Zd ZdddZdddZdd	d
ZdddZdddZdS )Loggertotal_tasksr   r@   Nonec                 C  s(   t  | _|| _d| _t | _d| _d S )Nr    )	threadingLocklockrU   completed_taskssetrunning_tasksshould_erase_line)selfrU   r&   r&   r'   __init__F   s
   

zLogger.__init__c                 C  s"   | j rt| j dd d| _ d S d S )NendrW   )r^   printr_   r&   r&   r'   flushM   s   
zLogger.flushc                 C  s   d| j  d| j }t jt| }d| j}t|d |kr+|d |d  d }|d| d }t| j	|d	d
d d| _	d S )Nz
Progress: z / r=         ...z ()rW   ra   )seprc   z[K)
r[   rU   shutilget_terminal_sizecolumnslenjoinr]   rd   r^   )r_   lineZmax_lenZrunningr&   r&   r'   print_progressR   s   
zLogger.print_progress	wrap_namer   c                 C  s@   | j  | j| |   W d    d S 1 sw   Y  d S N)rZ   r]   addrr   )r_   rs   r&   r&   r'   start\   s   
"zLogger.start	log_queue,T.List[T.Tuple[mlog.TV_LoggableList, T.Any]]c                 C  st   | j - |   |D ]\}}tj|i | q
| j| |  jd7  _|   W d    d S 1 s3w   Y  d S )Nr   )rZ   rf   r   logr]   remover[   rr   )r_   rs   rw   r6   kwargsr&   r&   r'   donea   s   
"zLogger.doneN)rU   r   r@   rV   r@   rV   )rs   r   r@   rV   )rs   r   rw   rx   r@   rV   )r"   r#   r$   r`   rf   rr   rv   r|   r&   r&   r&   r'   rT   E   s    




rT   F)eqc                   @  sD  e Zd ZU ded< ded< ded< ded< d	ed
< dOddZdPddZdQddZedRddZdQddZ	dQddZ
dSd"d#ZdTd$d%ZdUd&d'ZdUd(d)ZdVd+d,ZdVd-d.ZdWdXd1d2ZdVd3d4ZdVd5d6ZdQd7d8ZdUd9d:ZdQd;d<ZdQd=d>ZdQd?d@ZdQdAdBZdQdCdDZdQdEdFZdQdGdHZedYdJdKZdQdLdMZdNS )ZRunnerrT   loggerzInitVar[Resolver]rB   r   wrapr   repo_dir'Arguments'optionsr   r@   rV   c                 C  sH   t  || _tj|j| jj| j_| j| j_| j	j
| | _g | _d S rt   )copywrap_resolverosr>   rp   Zsubdir_rootr   	directorydirnamer   r   __get__
run_methodrw   )r_   rB   r&   r&   r'   __post_init__s   s
   

zRunner.__post_init__r6   mlog.TV_Loggabler{   T.Anyc                 O  s   | j t||f d S rt   )rw   appendlist)r_   r6   r{   r&   r&   r'   ry   |   s   z
Runner.logr   c              
   C  st   | j | jj z|  }W n ty- } z| tdt	| d}W Y d }~nd }~ww | j 
| jj| j |S )NzError:F)r   rv   r   rK   r   r   ry   r   redr   r|   rw   )r_   resulter&   r&   r'   run   s   z
Runner.run'UpdateWrapDBArguments'c                 C  s   t | j| _d S rt   )r   r    r0   r   r&   r&   r'   pre_update_wrapdb   s   zRunner.pre_update_wrapdbc                 C  s^  |  d| jj d td| j}|j| jj}|s#|  d dS z| jd}|dd\}}W n5 t	yh   z| jd	}t
|\}}W n t	ye   |js_|  d
td Y Y dS d  }}Y nw Y nw |d d }|dd\}}	||ks|	|kr| jjr| jjn| jj d}
t|
| jj||	|j |  dt| dS |  dt| dS )Nz#Checking latest WrapDB version for ri   r.   z  -> Wrap not found in wrapdbTwrapdb_version-r   	patch_url  ->zBCould not determine current version, use --force to update any wayFZversionsr   z.wrapz  -> New version downloaded:z  -> Already at latest version:)ry   r   rK   Tcastr   r0   getsplitr   r   r/   r   r   rsplitZhas_wraprD   r   r    blue)r_   r   infor   branchrevisionr   Zlatest_versionZ
new_branchZnew_revisionrD   r&   r&   r'   update_wrapdb   sB   

zRunner.update_wrapdbc              
   C  s   t d| j}|jr?t| j z| j| jj	d | 
d W dS  ty> } z| 
dtt| W Y d }~dS d }~ww | 
d | 
d dS )	Nr)   mesonz  -> New version extractedTr   Fzd  -> Subproject has not changed, or the new source/patch needs to be extracted on the same location.z<     Pass --reset option to delete directory and redownload.)r   r   r   r+   r   r   r   resolver   rK   ry   r   r   r   r   )r_   r   r   r&   r&   r'   update_file   s   



zRunner.update_filecmdr   c                 C  s   t || jddd S )NT)checkr   )r	   r   r_   r   r&   r&   r'   
git_output      zRunner.git_outputc                 C  s   |  | | d S rt   )ry   r   r   r&   r&   r'   git_verbose   r   zRunner.git_verbosec                 C  s.   t g d| jd  r| g d d S d S )N)statusz--porcelain":!/.meson-subproject-wrap-hash.txtr   )Zstashpushz--allr   .)r	   r   stripr   re   r&   r&   r'   	git_stash   s   zRunner.git_stashc              
   C  sZ   |  g d}dd |dD }| dt|d t|d |d t|d	  d S )
N)Zshowz--quietz!--pretty=format:%h%n%d%n%s%n[%an]c                 S     g | ]}|  qS r&   r   )rF   sr&   r&   r'   
<listcomp>       z#Runner.git_show.<locals>.<listcomp>
r   r   r      rg   )r   r   ry   r   Zyellowr   r   )r_   Zcommit_messagepartsr&   r&   r'   git_show   s   8zRunner.git_showr   c                 C  s   z
|  g d W dS  ty? } z)| dt| jdt| | t|j | tt| W Y d }~dS d }~ww )N)z-czrebase.autoStash=truer*   
FETCH_HEADz  -> Could not rebaseZontoFT)	r   r
   ry   r   boldr   r   outputr   r_   r   r   r&   r&   r'   
git_rebase   s   zRunner.git_rebasec                 C  s   z|    | g d | j  | j  W dS  tyM } z)| dt| j	dt| | t
|j | t
t| W Y d }~dS d }~ww )N)r+   z--hardr   z  -> Could not resettoFT)r   r   r   apply_patchZapply_diff_filesr
   ry   r   r   r   r   r   r   r   r&   r&   r'   	git_reset   s   
zRunner.git_resetFcreatec                 C  s   ddg}|r| d ||dg7 }z|   | | W dS  tyR } z)| dt|dt| j | t|j	 | tt
| W Y d }~dS d }~ww )	Ncheckoutz--ignore-other-worktrees-bz--z  -> Could not checkoutinFT)r   r   r   r
   ry   r   r   r   r   r   r   )r_   r   r   r   r   r&   r&   r'   git_checkout   s   
zRunner.git_checkoutc                 C     |  |}|r| |}|S rt   )r   r   r_   r   successr&   r&   r'   git_checkout_and_reset     

zRunner.git_checkout_and_resetc                 C  r   rt   )r   r   r   r&   r&   r'   git_checkout_and_rebase  r   zRunner.git_checkout_and_rebasec                 C  s  t d| j}tjtj| jdsV|jrJt	| j z| j
| jjd |   W dS  tyI } z| dtt| W Y d }~dS d }~ww | d | d dS | jjd	}| jjd
}| jjd}|ro|sv| d dS z| g d }W n0 ty } z$| dt| j | t|j | tt| W Y d }~dS d }~ww |jrz| ddd|g |r| dddd|g W nB ty } z$| dt| j | t|j | tt| W Y d }~dS d }~ww ||kr| d|d| dS z| g d }|dkr |nd}W n1 tyT } z$| dt| j | t|j | tt| W Y d }~dS d }~ww | j
|rstdd|d g| jd rs| dd|g nIzd }d!}	| dd"|d"|	d|g W n6 ty } z)| d#t|d$t| j | t|j | tt| W Y d }~dS d }~ww |dkr|jr| |}
n*| |}
n$||kr|jr| |}
n|  |}
n|jr| |}
n|  |}
|
r|   |
S )%Nr)   z.gitr   Tr   Fz  -> Not a git repository.z7Pass --reset option to delete directory and redownload.r   urlzpush-urlz"  -> No revision or URL specified.)remotezget-urloriginz.  -> Failed to determine current origin URL inr   zset-urlr   z--pushz"  -> Failed to reset origin URL inz  -> URL changed from  to )	rev-parsez--abbrev-refHEADr   rW   z*  -> Failed to determine current branch inr   z--verifyz	^{commit}r   Zfetchr   z#+refs/heads/*:refs/remotes/origin/*z+refs/tags/*:refs/tags/*z--refmapz  -> Could not fetch revisionr   )!r   r   r   r   r>   existsrp   r   r+   r   r   r   r   rK   update_git_doner   ry   r   r   r   valuesr   r   r   r
   r   r   Zis_git_full_commit_idr	   r   r   r   r   )r_   r   r   r   r   Zpush_urlZ
origin_urlr   Zheads_refmapZtags_refmapr   r&   r&   r'   
update_git  s   







zRunner.update_gitc                 C  s   |  g d |   d S )N)Z	submoduleupdatez
--checkoutz--recursive)r   r   re   r&   r&   r'   r     s   zRunner.update_git_donec                 C  sx   | j d}| dkrtjddg| jd dS tjdd|g| jddkr:tjddg| jd tjdd|g| jd dS )	Nr   ZtiphgZpullcwdr   r   T)r   r   lower
subprocesscallr   
check_call)r_   revnor&   r&   r'   	update_hg  s   zRunner.update_hgc                 C  sv   | j d}tdddd| jg\}}}|}||krdS | dkr-tjddg| jd dS tjddd	|g| jd dS )
Nr   svnr   z--show-itemTheadr   r   z-r)r   r   r   r   r   r   r   r   )r_   r   _outZcurrent_revnor&   r&   r'   
update_svn  s   zRunner.update_svnc                 C  s   |  d| jj d d}tj| js|  d | jj }nF| jj	dkr*| 
 }n;| jj	dkr5|  }n0| jj	dkr@|  }n%| jj	dkrK|  }n| jj	d u r\|  d	 | jj }n	|  d
| jj	d |rutj| jru| j| j |S )Nz	Updating ri   Fz  -> Not used.filegitr   r   z/  -> Cannot update subproject with no wrap filez  -> Cannot updateZ
subproject)ry   r   rK   r   r>   isdirr   r   r   typer   r   r   r   Zupdate_hash_cache)r_   r   r&   r&   r'   r     s(   





zRunner.updatec                 C  s   t d| j}| jjdkstj| jsdS |j	r|j	n| j
d}|s&dS | d| d| jj d | j||jdrB|   dS d	S )
Nr1   r   Tr   z	Checkout z in ri   )r   F)r   r   r   r   r   r   r>   r   r   r3   r   ry   rK   r   r2   r   )r_   r   r3   r&   r&   r'   r     s   zRunner.checkoutc              
   C  s   |  d| jj d tj| jr|  d dS z| j| jjd |  d W dS  t	yH } z|  dt
t| W Y d }~dS d }~ww )	Nz	Download ri   z  -> Already downloadedTr   z	  -> doner   F)ry   r   rK   r   r>   r   r   r   r   r   r   r   r   )r_   r   r&   r&   r'   download  s   
zRunner.downloadc                 C  s   t d| j}| d| j  tj| js| d dS |jg|j	 }t
|tj| jd\}}}|jdkrRdd||j}| d	t| | j|d
d dS | j|d
d dS )Nr4   zExecuting command in   -> Not downloaded yetT)stderrr   r   z.Command '{}' returned non-zero exit status {}. z  -> rW   rb   F)r   r   r   ry   r   r   r>   r   r5   r6   r   r   STDOUT
returncodeformatrp   r   r   )r_   r   r   pr   r   Zerr_messager&   r&   r'   foreach  s   

zRunner.foreachc              
   C  s$  t d| j}| jjsdS | jjr(t| jj }|j	r |
  td|  | jjdkrEt| jj }|j	r=|
  | d|  |jrt| jj }z|| jd }| rl|j	rd|
  | d|  W n	 tyv   Y nw z|| jd }| r|j	r|
  | d|  W n	 ty   Y nw zt| stt| W n	 ty   Y nw t| j}|j |j }| r|j	r|
  | d|  dS | sdS z|j	rtt| | d|  W dS  ty } ztd| d|  W Y d }~d	S d }~ww )
Nr7   Tz	Deleting Zredirectsource_filenameZpatch_filenamezUnable to remove: z: F) r   r   r   r   r   Z
redirectedr   Zoriginal_filenamer   r8   unlinkr   ry   rD   r9   r   cachedirr   is_filer   anyiterdirr   r   FileNotFoundErrorr   parentrK   
is_symlinkis_dirOSErrorerror)r_   r   Zredirect_fileZpackagecacheZsubproject_cache_fileZsubproject_patch_fileZsubproject_source_dirr   r&   r&   r'   purge  sz   
zRunner.purge'PurgeArguments'c                 C  s"   | j std td d S d S )NrW   z<Nothing has been deleted, run again with --confirm to apply.)r8   r   ry   r   r&   r&   r'   
post_purge1  s   
zRunner.post_purgec                 C  s  t d| j}|jr|jrtd dS |jr6| d| jj d t	j
| js/| d dS | j  dS |jrd| jjvrFtd	 dS d
| jjvrStd dS t| jj| jjd
 }t| jjdd}t| j}t| jj| jjd }|r||n|j}t||}t|d}| d| jj d| d t| || D ]}	|	 sq|	|}
||
 }|jjddd t |	| qdS )Nr:   z0error: --apply and --save are mutually exclusiveFz#Re-applying patchfiles overlay for ri   r   TZpatch_directoryz-can only save packagefiles to patch_directoryr   z-can only save packagefiles from a [wrap-file]lead_directory_missingz**/*zSaving r   )parentsexist_ok)!r   r   r   r;   r<   rd   ry   r   rK   r   r>   r   r   r   r   r   r   r  r   r   r   r   Zfilesdirr   rS   r\   globrl   rmtreer   relative_tomkdircopyfile)r_   r   Zarchive_pathr  r   packagefilesr?   rR   Zdirectory_filesZsrc_pathrel_pathZdst_pathr&   r&   r'   r  7  sF   







zRunner.packagefilesN)rB   r   r@   rV   )r6   r   r{   r   r@   rV   )r@   r   )r   r   r@   rV   )r   r   r@   r   )r   r   r@   rV   r}   )r   r   r@   r   )F)r   r   r   r   r@   r   )r   r  r@   rV   )r"   r#   r$   r%   r   ry   r   staticmethodr   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'   r   k   sD   
 

	



(










e






Hr   r   argparse.ArgumentParserrV   c                 C  sP   | j dddd | j dddt dd | j d	d td
d | j ddddd d S )Nz--sourcedirr   zPath to source directory)defaulthelpz--typesrW   z?Comma-separated list of subproject types. Supported types are: z (default: all)z--num-processesz2How many parallel processes to use (Since 0.59.0).)r  r   r  z--allow-insecureF
store_truez"Allow insecure server connections.r  actionr  )add_argumentALL_TYPES_STRINGr   r   r&   r&   r'   add_common_argumentsa  s   



r  c                 C  s   | j dddd d S )Nr   *z"List of subprojects (default: all)nargsr  )r  r  r&   r&   r'   add_subprojects_argumentk  s   
r  
subparsers'SubParsers'c                 C  sP   | j ddd}|jddddd t| t| |jtjd	 |jtjd
 |S )Nr   z,Update wrap files from WrapDB (Since 0.63.0)r  z--forceFr  z4Update wraps that does not seems to come from WrapDBr  r   )pre_func)
add_parserr  r  r  set_defaultsr   r   r   )r  r   r&   r&   r'   add_wrap_update_parsero  s   
r&  parserc                 C  s  | j ddd}d|_|jddd}|jddd	d
d |jddd	dd t| t| |jtjd |jddd}|jddd	dd |jdddd t| t| |jtj	d |jddd}t| t| |jtj
d |jddd}|jdddd |jdtjtjd t| |jg d  |jtjd |jd!d"d}t| t| |jd#d	dd$d% |jd&d	dd'd% |jtjd |jtjd( |jd)d*d}t| t| |jd+d	dd,d% |jd-d	dd.d% |jtjd d S )/NZCommandsr5   )titledestTr   z&Update all subprojects from wrap filesr!  z--rebaser  zeRebase your branch on top of wrap's revision. Deprecated, it is now the default behaviour. (git only)r  z--resetFzBCheckout wrap's revision and hard reset to that commit. (git only)r"  r   zCheckout a branch (git only)r   zCreate a new branchr3   ?zMName of the branch to checkout or create (default: revision set in wrap file)r  r   zEnsure subprojects are fetched, even if not in use. Already downloaded subprojects are not modified. This can be used to pre-fetch all subprojects and avoid downloads during configure.r   z/Execute a command in each subproject directory.zcommand ...z/Command to execute in each subproject directory)metavarr  r6   r   r  z*Remove all wrap-based subproject artifactsz--include-cachez Remove the package cache as well)r  r  r  z	--confirmz+Confirm the removal of subproject artifacts)	post_funcr  zManage the packagefiles overlayz--applyz$Apply packagefiles to the subprojectz--savez%Save packagefiles from the subproject)Zadd_subparsersrequiredr$  r  r  r  r%  r   r   r   r   argparseZ	REMAINDERZSUPPRESSr   r  r  r  )r'  r  r   r&   r&   r'   add_argumentsy  sb   



r0  r   r   r   c                   s  t jt j j}t jt j|ds"tdt	|d dS t j|d}t j
|s;tdt	|d dS t|dd jd	} jrT fd
d|j D }nt|j } jridd  jdD ng D ]}|tvr}td|dt qmg }g }t }t| t j}	rfdd|D }t dd }
|
r|
  tt|}|D ]$}t ||j!" }t#|||| }|$|	|j%}|&| |&|j' q|(tj)| }|*  t dd }|r|  dd t+||D }|rd}|d|7 }t,| t|S )Nzmeson.buildZ	Directoryz-does not seem to be a Meson source directory.r   r   z"does not seem to have subprojects.r   T)Zwrap_frontendr    c                   s   g | ]\}}| j v r|qS r&   r,  )rF   rK   r   r   r&   r'   r     s    zrun.<locals>.<listcomp>c                 S  r   r&   r   )rF   tr&   r&   r'   r     r   ,zUnknown subproject type z, supported types are: c                   s   g | ]	}|j  v r|qS r&   )r   )rF   r   )r   r&   r'   r     s    r#  r-  c                 S  s   g | ]\}}|s|qS r&   r&   )rF   rK   r   r&   r&   r'   r     s    zlPlease check logs above as command failed in some subprojects which could have been left in conflict state: r=   )-r   r>   relpathrealpathr   isfilerp   r   r  r   r   ry   r   r    r   wrapsitemsr   r   r   r   r   r   r  asyncioZnew_event_loopZset_event_loopr   r   getattrrT   ro   r   r   as_posixr   Zrun_in_executorr   r   rK   Zrun_until_completeZgatherrf   zipwarning)r   src_dirZsubprojects_dirrB   r6  r1  tasksZ
task_namesZloopZexecutorr#  r   r   r   runnerZtaskresultsr-  Zfailuresmr&   )r   r   r'   r     sZ    



r   )r>   r   r?   r   r@   rA   )r   r  r@   rV   )r  r   r@   r  )r'  r  r@   rV   )r   r   r@   r   );
__future__r   dataclassesr   r   r   r   r/  r8  rX   r   rl   Zconcurrent.futures.threadr   pathlibr   typingr   rP   rM   rW   r   Zmesonlibr	   r
   r   r   r   Z	wrap.wrapr   r   r   r   r   r   r   TYPE_CHECKINGtyping_extensionsr   Z_SubParsersActionArgumentParserZ
SubParsersr   r)   r.   r1   r4   r7   r:   rp   r  rS   rT   r   r  r  r&  r0  r   r&   r&   r&   r'   <module>   sN    $

	&   
x




6