
    'j#>                     P   U 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mZmZ d dlmZ d dlmZmZmZmZmZmZ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 d d
l 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, eeej-        ef         Z.e!e/d<   ddddddddddd
Z0ee1e1f         e/d<   ddddZ2ee1ed         f         e/d<   ej        e2vr e+dej         d          e2ej                 Z3ed         e/d<    e ed                    Z4ee/d<    ee j5        6                    e7                    j8        Z9ee/d <   ddgddgg d!d"Z:ee1ee1         f         e/d#<   d$d%d&d"Z;d' Z<e
e G d( d)                                  Z=e=>                                \  Z?Z@ G d* d+          ZA G d, d-eA          ZBd.e1fd/ZCd?d1eDd.efd2ZEd3e1d.e1fd4ZFd.e1fd5ZG	 	 	 d@d6e1d7ee1         d8ee.         d9eDd.e.f
d:ZH	 	 dAd;e.d<e1d7ee1         d9eDd.df
d=ZId3e1d.ee1ef         fd>ZJdS )B    N)	dataclass)total_ordering)BufferedWriterBytesIO)Path)AnyDictListLiteralOptionalTupleUnion)ZipFile)user_cache_dir)tqdm)	TypeAlias)CLoaderload   )CONSTRAINTS)CamoufoxNotInstalledMissingReleaseUnsupportedArchitectureUnsupportedOSUnsupportedVersionDownloadBufferx86_64i686arm64)
amd64r   x86r   i386r   aarch64armv5larmv6larmv7lARCH_MAPmaclinwin)darwinlinuxwin32)r(   r*   r)   OS_MAPzOS  is not supportedOS_NAMEcamoufoxINSTALL_DIR
LOCAL_DATA)r   r   r   )r*   r(   r)   OS_ARCH_MATRIXzcamoufox.exez../MacOS/camoufoxzcamoufox-binc                  0    t          j        | i |ddi d S )NboldT)clicksecho)aks     S/home/ice/.hermes/hermes-agent/venv/lib/python3.11/site-packages/camoufox/pkgman.pyrprintr<   H   s(    	K#a##d######    c                       e Zd ZU dZeed<   dZee         ed<   ddZe	defd            Z
defdZdefd	Zdefd
Zeddee         dd fd            Zededefd            Zeded         fd            ZdS )Versionzl
    A version string that can be compared to other version strings.
    Stores versions up to 5 parts.
    releaseNversionreturnc           
          t          g d | j                            d          D             d t          d| j                            d          z
            D                       | _        d S )Nc              3      K   | ]?}|                                 rt          |          nt          |d                    dz
  V  @dS )r   i   N)isdigitintord).0xs     r;   	<genexpr>z(Version.__post_init__.<locals>.<genexpr>[   sH      __!AIIKK=#a&&&S1YY-=______r=   .c              3      K   | ]}d V  dS )r   N )rH   _s     r;   rJ   z(Version.__post_init__.<locals>.<genexpr>\   s"      @@!@@@@@@r=      )tupler@   splitrangecount
sorted_relselfs    r;   __post_init__zVersion.__post_init__W   sv    __t|GYGYZ]G^G^___@@U1t|'9'9#'>'>#>??@@@
 
r=   c                 $    | j          d| j         S )N-rA   r@   rU   s    r;   full_stringzVersion.full_string`   s    ,/////r=   c                 "    | j         |j         k    S NrT   rV   others     r;   __eq__zVersion.__eq__d   s    %"222r=   c                 "    | j         |j         k     S r]   r^   r_   s     r;   __lt__zVersion.__lt__g   s    !111r=   c                 6    t           | cxk    o
t          k     nc S r]   )VERSION_MINVERSION_MAXrU   s    r;   is_supportedzVersion.is_supportedj   s$    d0000[00000r=   pathc                 6   | pt           dz  }t          j                            |          st	          d| d          t          |d          5 }t          j        |                                          }t          di |cddd           S # 1 swxY w Y   dS )z6
        Get the version from the given path.
        version.jsonz!Version information not found at ). Please run `camoufox fetch` to install.rbNrM   )
r2   osrh   existsFileNotFoundErroropenorjsonloadsreadr?   )rh   version_pathfversion_datas       r;   	from_pathzVersion.from_pathm   s    
 +~=w~~l++ 	#:L : : :   ,%% 	+!<11L**\**	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   2BBBc                 H    t                               |           t          k    S )zF
        Check if the version at the given path is supported.
        )r?   rw   re   )rh   s    r;   is_supported_pathzVersion.is_supported_path|   s    
   &&+55r=   )r?   r?   c                  j    t          t          j                  t          t          j                  fS )N)r@   )r?   r   MIN_VERSIONMAX_VERSIONrM   r=   r;   build_minmaxzVersion.build_minmax   s)    {6777I`9a9a9aaar=   rB   Nr]   )__name__
__module____qualname____doc__str__annotations__rA   r   rW   propertyr[   boolra   rc   rg   staticmethodr   rw   ry   r   r}   rM   r=   r;   r?   r?   L   sd         
 LLL!GXc]!!!
 
 
 
 0S 0 0 0 X03t 3 3 3 32t 2 2 2 21d 1 1 1 1 + + +) + + + \+ 6 6 6 6 6 \6 b% 45 b b b \b b br=   r?   c                   F    e Zd ZdZdeddfdZdedefdZd
dZ	defd	Z
dS )GitHubDownloaderz:
    Manages fetching and installing GitHub releases.
    github_reporB   Nc                 *    || _         d| d| _        d S )Nzhttps://api.github.com/repos/z	/releases)r   api_url)rV   r   s     r;   __init__zGitHubDownloader.__init__   s     &M{MMMr=   assetc                 ,    |                     d          S )z
        Compare the asset to determine if it's the desired asset.

        Args:
            asset: Asset information from GitHub API

        Returns:
            Any: Data to be returned if this is the desired asset, or None/False if not
        browser_download_url)get)rV   r   s     r;   check_assetzGitHubDownloader.check_asset   s     yy/000r=   c                 2    t          d| j         d          )J
        Raise a MissingRelease exception if no release is found.
        z"Could not find a release asset in rK   )r   r   rU   s    r;   missing_asset_errorz$GitHubDownloader.missing_asset_error   s!     U$BRUUUVVVr=   c                    t          j        | j        d          }|                                 |                                }|D ]*}|d         D ]}|                     |          x}r|c c S  +|                                  dS )z
        Fetch the latest release from the GitHub API.
        Gets the first asset that returns a truthy value from check_asset.
           )timeoutassetsN)requestsr   r   raise_for_statusjsonr   r   )rV   respreleasesr@   r   datas         r;   	get_assetzGitHubDownloader.get_asset   s    
 |DL"55599;; 	  	 G *    ++E2224  KKKKK   	  """""r=   r~   )r   r   r   r   r   r   r	   r   r   r   r   rM   r=   r;   r   r      s         NC ND N N N N
1 
1# 
1 
1 
1 
1W W W W#3 # # # # # #r=   r   c                   L    e Zd ZdZd fdZdedeeee	f                  fdZ
ddZede	fd            Zdd	Zed
ede	defd            ZdeddfdZedefd            ZddZddZede	fd            Zede	fd            Zede	fd            Zede	fd            Z xZS )CamoufoxFetcherzI
    Handles fetching and installing the latest version of Camoufox.
    rB   Nc                    t                                          d           |                                 | _        d | _        t          j        dt           d| j         d          | _        | 	                                 d S )Nzdaijro/camoufoxz)camoufox-(?P<version>.+)-(?P<release>.+)-z\.z\.zip)
superr   get_platform_archarch_version_objrecompiler0   patternfetch_latest)rV   	__class__s    r;   r   zCamoufoxFetcher.__init__   s|    *+++**,,	/3#%:TTTDITTT$
 $
 	r=   r   c                     | j                             |d                   }|sdS t          |d         |d                   }|                                sdS ||d         fS )z
        Finds the latest release from a GitHub releases API response that
        supports the Camoufox version constraints, the OS, and architecture.

        Returns:
            Optional[Tuple[Version, str]]: The version and URL of a release
        nameNr@   rA   )r@   rA   r   )r   matchr?   rg   )rV   r   r   rA   s       r;   r   zCamoufoxFetcher.check_asset   st     ""5=11 	4 %	"2E)<LMMM##%% 	4 4555r=   c           	      j    t          dt           d| j         dt          j                     d          )r   zNo matching release found for  z in the supported range: (z$). Please update the Python library.)r   r0   r   r   as_rangerU   s    r;   r   z#CamoufoxFetcher.missing_asset_error   sP     0W 0 0ty 0 0!,!5!7!70 0 0
 
 	
r=   c                      t          j                                                    } | t          vrt	          d|  d          t          |          }|t
          t                   vrt	          d| dt                     |S )z
        Get the current platform and architecture information.

        Returns:
            str: The architecture of the current platform

        Raises:
            UnsupportedArchitecture: If the current architecture is not supported
        zArchitecture r/   z is not supported for )platformmachinelowerr'   r   r4   r0   )	plat_archr   s     r;   r   z!CamoufoxFetcher.get_platform_arch   s     $&&,,..	H$$)*V)*V*V*VWWW	" ~g...)*_$*_*_V]*_*_```r=   c                 L    |                                  }|\  | _        | _        dS )aB  
        Fetch the URL of the latest camoufox release for the current platform.
        Sets the version, release, and url properties.

        Raises:
            requests.RequestException: If there's an error fetching release data
            ValueError: If no matching release is found for the current platform
        N)r   r   _url)rV   release_datas     r;   r   zCamoufoxFetcher.fetch_latest  s(     ~~'' (4$4999r=   fileurlc                 H    t          d|            t          ||           S )a1  
        Download a file from the given URL and return it as BytesIO.

        Args:
            file (DownloadBuffer): The buffer to download to
            url (str): The URL to download the file from

        Returns:
            DownloadBuffer: The downloaded file content as a BytesIO object
        zDownloading package: )buffer)r<   webdl)r   r   s     r;   download_filezCamoufoxFetcher.download_file  s.     	,s,,---S&&&&r=   zip_filec                 x    t          dt                      t          |t          t                               dS )z
        Extract the contents of a zip file to the installation directory.

        Args:
            zip_file (DownloadBuffer): The zip file content as a BytesIO object
        zExtracting Camoufox: N)r<   r2   unzipr   )rV   r   s     r;   extract_zipzCamoufoxFetcher.extract_zip  s9     	4{44555hK(()))))r=   c                      t                                           r2t          dt                       t          j        t                      dS dS )z0
        Clean up the old installation.
        zCleaning up cache: TF)r2   rn   r<   shutilrmtreerM   r=   r;   cleanupzCamoufoxFetcher.cleanup(  sI    
  	666777M+&&&4ur=   c                     t          t          dz  d          5 }|                    t          j        | j        | j        d                     ddd           dS # 1 swxY w Y   dS )zF
        Set the version in the INSTALL_DIR/version.json file
        rj   wbrZ   N)rp   r2   writerq   dumpsrA   r@   )rV   ru   s     r;   set_versionzCamoufoxFetcher.set_version3  s     +.55 	VGGFLT\dl!S!STTUUU	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	V 	Vs   5AA"Ac                    |                                   	 t                              dd           t          j                    5 }|                     || j                   |                     |           |                                  ddd           n# 1 swxY w Y   t          dk    r;t          j        dt          j        t          t                                          t          dd           dS # t           $ r9}t          d	t          |                      |                                    d}~ww xY w)
z
        Download and install the latest version of camoufox.

        Raises:
            Exception: If any error occurs during the installation process
        T)parentsexist_okNr*   zchmod -R 755 z!
Camoufox successfully installed.yellow)fgzError installing Camoufox: )r   r2   mkdirtempfileNamedTemporaryFiler   r   r   r   r0   rm   systemshlexquoter   r<   	Exception)rV   	temp_filees      r;   installzCamoufoxFetcher.install:  sn    		dT::: ,.. #)""9dh777  +++  """# # # # # # # # # # # # # # # %	I%+c+6F6F*G*GIIJJJ7HEEEEEE 	 	 	9Q99:::LLNNN	s=   /C: AB
C: BC: BAC: :
D=4D88D=c                 <    | j         t          d          | j         S )z
        Url of the fetched latest version of camoufox.

        Returns:
            str: The version of the installed camoufox

        Raises:
            ValueError: If the version is not available (fetch_latest not ran)
        Nz:Url is not available. Make sure to run fetch_latest first.)r   
ValueErrorrU   s    r;   r   zCamoufoxFetcher.urlW  s#     9YZZZyr=   c                 ^    | j         | j         j        st          d          | j         j        S )z
        Version of the fetched latest version of camoufox.

        Returns:
            str: The version of the installed camoufox

        Raises:
            ValueError: If the version is not available (fetch_latest not ran)
        NzBVersion is not available. Make sure to run the fetch_latest first.)r   rA   r   rU   s    r;   rA   zCamoufoxFetcher.versionf  s3     $D,=,E$abbb ((r=   c                 F    | j         t          d          | j         j        S )z
        Release of the fetched latest version of camoufox.

        Returns:
            str: The release of the installed camoufox

        Raises:
            ValueError: If the release information is not available (fetch_latest not ran)
        NzNRelease information is not available. Make sure to run the installation first.)r   r   r@   rU   s    r;   r@   zCamoufoxFetcher.releasev  s0     $`    ((r=   c                 F    | j         t          d          | j         j        S )z
        Fetches the version and release in version-release format

        Returns:
            str: The version of the installed camoufox
        NzBVersion is not available. Make sure to run the installation first.)r   r   r[   rU   s    r;   verstrzCamoufoxFetcher.verstr  s(     $abbb ,,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   rA   r@   r   __classcell__)r   s   @r;   r   r      s        	 	 	 	 	 	6 6(5#3F*G 6 6 6 6*
 
 
 
 s    \04 4 4 4 'N ' ' ' ' ' \'*N *t * * * * T    \V V V V   : S    X ) ) ) ) X) ) ) ) ) X)" 	- 	- 	- 	- X	- 	- 	- 	- 	-r=   r   rB   c                  >    t                                           j        S )z@
    Get the full version string of the installed camoufox.
    )r?   rw   r[   rM   r=   r;   installed_verstrr     s     **r=   Tdownload_if_missingc                    t           j                            t                    rt          j        t                    s| st          dt                     ngt           j                            t                    r2t                                                                          rt          S | st          d          t                                                       t                      S )z+
    Full path to the camoufox folder.
    z!Camoufox executable not found at z Camoufox executable is outdated.)rm   rh   rn   r2   listdirro   r?   rw   rg   r   r   r   camoufox_path)r   s    r;   r   r     s     7>>+&& Ibj.E.E I" 	W#$U$U$UVVV	W 
	$	$ I):):)<)<)I)I)K)K I # 	I$%GHHH ??r=   r   c                     t           dk    r7t          j                            t	                      dz  dz  dz  | z            S t          t	                      | z            S )2
    Get the path to the camoufox executable.
    r(   zCamoufox.appContents	Resources)r0   rm   rh   abspathr   r   )r   s    r;   get_pathr     sS     %w}?*L{Z]aabbb}%&&&r=   c                      t          t          t                             } t          j                            |           st          dt                       d          | S )r   zCamoufox is not installed at rk   )r   LAUNCH_FILEr0   rm   rh   rn   r   r   )launch_paths    r;   r   r     sX     ;w/00K7>>+&& 
"fMOOfff
 
 	
 r=   r   descr   barc                    t          j        | d          }|                                 t          |j                            dd                    }d}|t                      }t          |d|rdndd|	          5 }|                    |          D ],}|                    |          }	|	                    |	           -	 ddd           n# 1 swxY w Y   |
                    d           |S )
a  
    Download a file from the given URL and return it as BytesIO.

    Args:
        url (str): The URL to download the file from
        buffer (Optional[BytesIO]): A BytesIO object to store the downloaded file
        bar (bool): Whether to show the progress bar

    Returns:
        DownloadBuffer: The downloaded file content as a BytesIO object

    Raises:
        requests.RequestException: If there's an error downloading the file
    T)streamzcontent-lengthr   i    NiB{desc}: {percentage:3.0f}%)totalunit
bar_format
unit_scaler   )r   r   r   rF   headersr   r   iter_contentr   updateseek)
r   r   r   r   response
total_size
block_sizeprogress_barr   sizes
             r;   r   r     sA   ( |C---HX%))*:A>>??JJ~	@44$@
 
 
 	& 
))*55 	& 	&D<<%%D%%%%	&	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& KKNNNMs   =ACCCr   extract_pathc                     t          |           5 }t          |                                ||rdnd          D ]}|                    ||           	 ddd           dS # 1 swxY w Y   dS )a7  
    Extract the contents of a zip file to the installation directory.

    Args:
        zip_file (BytesIO): The zip file content as a BytesIO object

    Raises:
        zipfile.BadZipFile: If the zip file is invalid or corrupted
        OSError: If there's an error creating directories or writing files
    Nr   )r   r   )r   r   infolistextract)r   r  r   r   zfmembers         r;   r   r     s      
		 -bKKMM1^B^
 
 
 	- 	-F JJv|,,,,	-- - - - - - - - - - - - - - - - - -s   AA  A$'A$c                     t          t          | z  d          5 }t          |t                    cddd           S # 1 swxY w Y   dS )zA
    Loads a local YAML file and returns it as a dictionary.
    r)LoaderN)rp   r3   r   r   )r   ru   s     r;   	load_yamlr    s     
j4	%	% 'Ag&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 's   <A A )T)NNT)NT)Krm   r   r   r   r   sysr   dataclassesr   	functoolsr   ior   r   pathlibr   typingr   r	   r
   r   r   r   r   zipfiler   r7   rq   r   platformdirsr   r   typing_extensionsr   yamlr   r   __version__r   
exceptionsr   r   r   r   r   _TemporaryFileWrapperr   r   r'   r   r.   r0   r2   rh   r   __file__parentr3   r4   r   r<   r?   r}   re   rf   r   r   r   r   r   r   r   r   r   r  rM   r=   r;   <module>r     sQ   					  				   



  ! ! ! ! ! ! $ $ $ $ $ $ & & & & & & & &       C C C C C C C C C C C C C C C C C C          ' ' ' ' ' '       ' ' ' ' ' '         $ $ $ $ $ $              "'8+I>"YZ	 Z Z Z  $sCx.    >CU]b2c2cS'-../ c c c<v
-=cl===
>
>>(.s|(<$	% < < <D
3344T 4 4 4411229
D 9 9 9 fg&&&( (S$s)^$     $ $ $ 
7b 7b 7b 7b 7b 7b 7b  7bv #//11 [*# *# *# *# *# *# *# *#ZY- Y- Y- Y- Y-& Y- Y- Y-x+# + + + + t t    0'3 '3 ' ' ' '
S 
 
 
 
 '+	( (	(
3-( ^$( 
	(
 ( ( ( (\ 	- --- 3-- 
	-
 
- - - -.'C 'DcN ' ' ' ' ' 'r=   