o
    fi                  
   @   s  U 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m	Z	 dZ
e  ejddZeed< ee j ejdd	dd
d7 Zee  ejd	d7 Zeje
rYee
g7 Zefde	eee f de	eee f ddfddZdedee ddfddZdeddfddZd$deddfddZefde	eee f ddfddZdedededdfddZdedededdfd d!Zefdedede	eee f ddfd"d#ZdS )%    N)Sequence)pprint)Unionzrequirements.txtrequirementsz*.txtREQUIREMENT_FILES_ALLz**T)	recursivepyproject.tomlpackages	req_filesreturnc                 C   s8   t | tr| g} t |tr|g}|D ]}t||  qdS )zRemove one or more packages from the specified requirement files.

    Args:
        packages: A package name or list of package names to remove.
        req_files: A path or list of paths to requirement files to process.

    N)
isinstancestr_prune_packages)r	   r
   req r   b/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/lightning_utilities/cli/dependencies.pyprune_packages_in_requirements   s   


r   req_filec                    s   t | }| }W d   n1 sw   Y  t|tr |g}|D ]  fdd|D }q"t| t | d}|| W d   dS 1 sHw   Y  dS )zRemove all occurrences of the given packages (by line prefix) from a requirements file.

    Args:
        req_file: Path to a requirements file.
        packages: Package names to remove. Lines starting with any of these names will be dropped.

    Nc                    s   g | ]	}|  s|qS r   )
startswith).0lnpkgr   r   
<listcomp>5   s    z#_prune_packages.<locals>.<listcomp>w)open	readlinesr   r   r   
writelines)r   r	   fplinesr   r   r   r   '   s   


"r   c                 C   sr   t | }| dd}W d   n1 sw   Y  t | d}|| W d   dS 1 s2w   Y  dS )zReplace all occurrences of '>=' with '==' in a plain text requirements file.

    Args:
        req_file: Path to the requirements.txt-like file to update.

    >===Nr   )r   readreplacewrite)r   fopenr   fwr   r   r   _replace_min_req_in_txt<   s   
"r'   	proj_filec                 C   s   ddl }t| dd}| }W d   n1 sw   Y  ||}|di d}|s0dS t|D ]\}}|dd||< q4t| d	dd}||| W d   dS 1 s\w   Y  dS )
zReplace all '>=' with '==' in the [project.dependencies] section of a standard pyproject.toml.

    Preserves formatting and comments using tomlkit.

    Args:
        proj_file: Path to the pyproject.toml file.

    r   Nutf-8encodingprojectdependenciesr    r!   r   )	tomlkitr   r"   parseget	enumerater#   r$   dumps)r(   r.   fcontentdocdepsir   r   r   r   "_replace_min_req_in_pyproject_tomlI   s   	

"r8   c                 C   sd   t | tr| g} | D ]%}|drt| q
tj|dkr#t| q
tj	d| dt
dd q
dS )a#  Convert minimal version specifiers (>=) to pinned ones (==) in the given requirement files.

    Supports plain *.txt requirements and pyproject.toml files. Unsupported file types trigger a warning.

    Args:
        req_files: A path or list of paths to requirement files to process.

    .txtr   ^Only *.txt with plain list of requirements or standard pyproject.toml are supported.Provided '' is not supported.   
stacklevelN)r   r   endswithr'   ospathbasenamer8   warningswarnUserWarning)r
   fnamer   r   r   replace_oldest_versionh   s   
	


rG   old_packagenew_packagec                 C   s   t | }| }W d   n1 sw   Y  t|D ]\}}tdt| d ||||< qt | d}|| W d   dS 1 sGw   Y  dS )a   Rename a package in a plain text requirements file, preserving version specifiers and markers.

    Args:
        req_file: Path to the requirements.txt-like file to update.
        old_package: The original package name to replace.
        new_package: The new package name to use.

    N^z(?=[ <=>#]|$)r   )r   r   r1   resubescaper   )r   rH   rI   r%   r   r7   r   r&   r   r   r   _replace_package_name_in_txt   s   


""rN   c           
      C   s   ddl }t| dd}| }W d   n1 sw   Y  ||}|di d}|s0dS t|D ]\}}	tdt| d ||	||< q4t| d	dd}|	|
| W d   dS 1 sdw   Y  dS )
a  Rename a package in the [project.dependencies] section of a standard pyproject.toml, preserving constraints.

    Args:
        proj_file: Path to the pyproject.toml file.
        old_package: The original package name to replace.
        new_package: The new package name to use.

    r   Nr)   r*   r,   r-   rJ   z(?=[ <=>]|$)r   )r.   r   r"   r/   r0   r1   rK   rL   rM   r$   r2   )
r(   rH   rI   r.   r3   r4   r5   r6   r7   r   r   r   r   '_replace_package_name_in_pyproject_toml   s   	

""rO   c                 C   sl   t |tr|g}|D ])}|drt|| | q
tj|dkr't|| | q
tj	d| dt
dd q
dS )a  Rename a package across multiple requirement files while keeping version constraints intact.

    Supports plain *.txt requirements and pyproject.toml files. Unsupported file types trigger a warning.

    Args:
        old_package: The original package name to replace.
        new_package: The new package name to use.
        req_files: A path or list of paths to requirement files to process.

    r9   r   r:   r;   r<   r=   N)r   r   r?   rN   r@   rA   rB   rO   rC   rD   rE   )rH   rI   r
   rF   r   r   r   replace_package_in_requirements   s   

rP   )r   )globos.pathr@   rK   rC   collections.abcr   r   typingr   REQUIREMENT_ROOTrA   joinr   list__annotations__isfiler   r   r   r'   r8   rG   rN   rO   rP   r   r   r   r   <module>   sJ   


" 