
    'j#>                        % S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  S SKJrJr  S SKJr  S SKJrJrJrJrJrJrJr  S SKJr  S SKrS SKrS SKrS SKJr  S S	KJr  S S
K J!r!  S SK"J#r#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*J+r+J,r,  \\\RZ                  \4   r.\!\/S'   SSSSSSSSSSS.
r0\\1\14   \/S'   SSSS.r2\\1\S   4   \/S'   \R                  \2;  a  \+" S\R                   S35      e\2\R                     r3\S   \/S'   \" \" S5      5      r4\\/S'   \" \ Rj                  Rm                  \75      5      Rp                  r9\\/S '   SS/SS// S!QS".r:\\1\\1   4   \/S#'   S$S%S&S".r;S' r<\
\ " S( S)5      5       5       r=\=R}                  5       u  r?r@ " S* S+5      rA " S, S-\A5      rBS.\14S/ jrCS>S0\DS.\4S1 jjrES2\1S.\14S3 jrFS.\14S4 jrG   S?S5\1S6\\1   S7\\.   S8\DS.\.4
S9 jjrH  S@S:\.S;\1S6\\1   S8\DS.S4
S< jjrIS2\1S.\\1\4   4S= jrJg)A    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                  8    [         R                  " U 0 UDSS0D6  g )NboldT)clicksecho)aks     S/home/ice/.hermes/hermes-agent/venv/lib/python3.11/site-packages/camoufox/pkgman.pyrprintr<   H   s    	KK#a#d#    c                       \ rS rSr% Sr\\S'   Sr\\   \S'   SS jr	\
S\4S j5       rS\4S	 jrS\4S
 jrS\4S jr\SS\\   SS 4S jj5       r\S\S\4S j5       r\S\S   4S j5       rSrg)VersionL   z`
A version string that can be compared to other version strings.
Stores versions up to 5 parts.
releaseNversionreturnc           
          [        / S U R                  R                  S5       5       QS [        SU R                  R	                  S5      -
  5       5       Q5      U l        g )Nc              3      #    U  H5  oR                  5       (       a  [        U5      O[        US    5      S-
  v   M7     g7f)r   i   N)isdigitintord).0xs     r;   	<genexpr>(Version.__post_init__.<locals>.<genexpr>[   s0     _G^!IIKK#a&S1Y-==G^s   =?.c              3   &   #    U  H  nS v   M	     g7f)r   N )rI   _s     r;   rK   rL   \   s     @?!?s      )tuplerA   splitrangecount
sorted_relselfs    r;   __post_init__Version.__post_init__W   sV    _t||GYGYZ]G^_@U1t||'9'9#'>#>?@
r=   c                 8    U R                    SU R                   3$ )N-rB   rA   rW   s    r;   full_stringVersion.full_string`   s    ,,q//r=   c                 4    U R                   UR                   :H  $ NrV   rX   others     r;   __eq__Version.__eq__d   s    %"2"222r=   c                 4    U R                   UR                   :  $ ra   rb   rc   s     r;   __lt__Version.__lt__g   s    !1!111r=   c                 <    [         U s=:*  =(       a	    [        :  $ s  $ ra   )VERSION_MINVERSION_MAXrW   s    r;   is_supportedVersion.is_supportedj   s    d00[0000r=   pathc                 8   U =(       d    [         S-  n[        R                  R                  U5      (       d  [	        SU S35      e[        US5       n[        R                  " UR                  5       5      n[        S0 UD6sSSS5        $ ! , (       d  f       g= f)z&
Get the version from the given path.
version.jsonz!Version information not found at ). Please run `camoufox fetch` to install.rbNrO   )
r2   osro   existsFileNotFoundErroropenorjsonloadsreadr?   )ro   version_pathfversion_datas       r;   	from_pathVersion.from_pathm   s~    
 +~=ww~~l++#3L> B: :  ,%!<<1L*\* &%%s   /B
Bc                 :    [         R                  U 5      [        :  $ )z6
Check if the version at the given path is supported.
)r?   r~   rk   )ro   s    r;   is_supported_pathVersion.is_supported_path|   s    
   &+55r=   )r?   r?   c                  ^    [        [        R                  S9[        [        R                  S94$ )N)rA   )r?   r   MIN_VERSIONMAX_VERSIONrO   r=   r;   build_minmaxVersion.build_minmax   s#    {667I`I`9aaar=   rb   rC   Nra   )__name__
__module____qualname____firstlineno____doc__str__annotations__rB   r   rY   propertyr^   boolre   rh   rm   staticmethodr   r~   r   r   r   __static_attributes__rO   r=   r;   r?   r?   L   s    
 L!GXc]!
 0S 0 03t 32t 21d 1 + +) + + 6 6 6 6 b% 45 b br=   r?   c                   T    \ rS rSrSrS\SS4S jrS\S\4S jr	SS	 jr
S\4S
 jrSrg)GitHubDownloader   z2
Manages fetching and installing GitHub releases.
github_reporC   Nc                 &    Xl         SU S3U l        g )Nzhttps://api.github.com/repos/z	/releases)r   api_url)rX   r   s     r;   __init__GitHubDownloader.__init__   s    &6{m9Mr=   assetc                 $    UR                  S5      $ )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)rX   r   s     r;   check_assetGitHubDownloader.check_asset   s     yy/00r=   c                 4    [        SU R                   S35      e):
Raise a MissingRelease exception if no release is found.
z"Could not find a release asset in rM   )r   r   rW   s    r;   missing_asset_error$GitHubDownloader.missing_asset_error   s!     A$BRBRASSTUVVr=   c                    [         R                  " U R                  SS9nUR                  5         UR	                  5       nU H-  nUS    H!  nU R                  U5      =n(       d  M  Us  s  $    M/     U R                  5         g)zr
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   )rX   respreleasesrA   r   datas         r;   	get_assetGitHubDownloader.get_asset   sq    
 ||DLL"599;G *++E2242K +  
 	  "r=   )r   r   r   )r   r   r   r   r   r   r   r	   r   r   r   r   r   rO   r=   r;   r   r      sB    NC ND N
1 
1# 
1W#3 #r=   r   c                   <  ^  \ rS rSrSrSU 4S jjrS\S\\\	\
4      4S jrSS jr\S\
4S	 j5       rSS
 jr\S\S\
S\4S j5       rS\SS4S jr\S\4S j5       rSS jrSS jr\S\
4S j5       r\S\
4S j5       r\S\
4S j5       r\S\
4S j5       rSrU =r$ )CamoufoxFetcher   zA
Handles fetching and installing the latest version of Camoufox.
rC   Nc                    > [         TU ]  S5        U R                  5       U l        S U l        [
        R                  " S[         SU R                   S35      U l        U R                  5         g )Nzdaijro/camoufoxz)camoufox-(?P<version>.+)-(?P<release>.+)-z\.z\.zip)
superr   get_platform_archarch_version_objrecompiler0   patternfetch_latest)rX   	__class__s    r;   r   CamoufoxFetcher.__init__   s]    *+**,	/3#%::8	DII;eT$
 	r=   r   c                     U R                   R                  US   5      nU(       d  g[        US   US   S9nUR                  5       (       d  gX1S   4$ )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
nameNrA   rB   )rA   rB   r   )r   matchr?   rm   )rX   r   r   rB   s       r;   r   CamoufoxFetcher.check_asset   s]     ""5=1 %	"2E)<LM##%% 4555r=   c           	      n    [        S[         SU R                   S[        R                  " 5        S35      e)r   zNo matching release found for  z in the supported range: (z$). Please update the Python library.)r   r0   r   r   as_rangerW   s    r;   r   #CamoufoxFetcher.missing_asset_error   sA     ,WIQtyyk B!!,!5!5!7 8 900
 	
r=   c                      [         R                  " 5       R                  5       n U [        ;  a  [	        SU  S35      e[        U    nU[
        [           ;  a  [	        SU S[         35      eU$ )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   !CamoufoxFetcher.get_platform_arch   sq     $$&,,.	H$)M)DU*VWW	" ~g..)M$?UV]U^*_``r=   c                 B    U R                  5       nUu  U l        U l        g)a  
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)rX   release_datas     r;   r   CamoufoxFetcher.fetch_latest  s!     ~~' (4$49r=   fileurlc                 0    [        SU 35        [        XS9$ )z
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_fileCamoufoxFetcher.download_file  s     	&se,-S&&r=   zip_filec                 Z    [        S[         35        [        U[        [        5      5        g)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   )rX   r   s     r;   extract_zipCamoufoxFetcher.extract_zip  s#     	&{m45hK()r=   c                      [         R                  5       (       a-  [        S[          35        [        R                  " [         5        gg)z 
Clean up the old installation.
zCleaning up cache: TF)r2   ru   r<   shutilrmtreerO   r=   r;   cleanupCamoufoxFetcher.cleanup(  s5    
 (67MM+&r=   c                     [        [        S-  S5       nUR                  [        R                  " U R
                  U R                  S.5      5        SSS5        g! , (       d  f       g= f)z6
Set the version in the INSTALL_DIR/version.json file
rq   wbr]   N)rw   r2   writerx   dumpsrB   rA   )rX   r|   s     r;   set_versionCamoufoxFetcher.set_version3  sC     +.5GGFLLT\\dll!STU 655s   =A
A(c                 4   U R                  5          [        R                  SSS9  [        R                  " 5        nU R                  XR                  5        U R                  U5        U R                  5         SSS5        [        S:w  a:  [        R                  " S[        R                  " [        [        5      5       35        [        SSS9  g! , (       d  f       N]= f! [          a-  n[        S	[        U5       35        U R                  5         e SnAff = f)
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   rt   systemshlexquoter   r<   	Exception)rX   	temp_filees      r;   installCamoufoxFetcher.install:  s     		dT: ,,.)""9hh7  +  " / %		M%++c+6F*G)HIJ7HE /.  	0Q9:LLN	s/   )C  =C8AC  
CC   
D*(DDc                 J    U R                   c  [        S5      eU R                   $ )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)
z:Url is not available. Make sure to run fetch_latest first.)r   
ValueErrorrW   s    r;   r   CamoufoxFetcher.urlW  s$     99YZZyyr=   c                     U R                   b  U R                   R                  (       d  [        S5      eU R                   R                  $ )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)
zBVersion is not available. Make sure to run the fetch_latest first.)r   rB   r   rW   s    r;   rB   CamoufoxFetcher.versionf  s<     $D,=,=,E,Eabb  (((r=   c                 ^    U R                   c  [        S5      eU R                   R                  $ )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)
zNRelease information is not available. Make sure to run the installation first.)r   r   rA   rW   s    r;   rA   CamoufoxFetcher.releasev  s4     $`    (((r=   c                 ^    U R                   c  [        S5      eU R                   R                  $ )zt
Fetches the version and release in version-release format

Returns:
    str: The version of the installed camoufox
zBVersion is not available. Make sure to run the installation first.)r   r   r^   rW   s    r;   verstrCamoufoxFetcher.verstr  s.     $a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   r   r   r   r   r   rB   rA   r  r   __classcell__)r   s   @r;   r   r      s!   	6 6(5#3F*G 6*
 s  04 'N ' ' ' '*N *t * T  V: S   ) ) ) ) ) )" 	- 	- 	-r=   r   rC   c                  >    [         R                  5       R                  $ )z8
Get the full version string of the installed camoufox.
)r?   r~   r^   rO   r=   r;   installed_verstrr    s     ***r=   download_if_missingc                    [         R                  R                  [        5      (       a  [         R                  " [        5      (       d  U (       d  [        S[         35      eOg[         R                  R                  [        5      (       a-  [        R                  5       R                  5       (       a  [        $ U (       d  [        S5      e[        5       R                  5         [        5       $ )z#
Full path to the camoufox folder.
z!Camoufox executable not found at z Camoufox executable is outdated.)rt   ro   ru   r2   listdirrv   r?   r~   rm   r   r   r   camoufox_path)r  s    r;   r	  r	    s     77>>+&&bjj.E.E"#&G}$UVV # 
	$	$):):)<)I)I)K)K #$%GHH ?r=   r   c                     [         S:X  a3  [        R                  R                  [	        5       S-  S-  S-  U -  5      $ [        [	        5       U -  5      $ )*
Get the path to the camoufox executable.
r(   zCamoufox.appContents	Resources)r0   rt   ro   abspathr	  r   )r   s    r;   get_pathr    sH     %ww}?*L{Z]aabb}%&&r=   c                      [        [        [           5      n [        R                  R                  U 5      (       d  [        S[        5        S35      eU $ )r  zCamoufox is not installed at rr   )r  LAUNCH_FILEr0   rt   ro   ru   r   r	  )launch_paths    r;   r  r    sK     ;w/0K77>>+&&"+MO+<<ef
 	
 r=   r   descr   barc                    [         R                  " U SS9nUR                  5         [        UR                  R                  SS5      5      nSnUc
  [        5       n[        USU(       a  SOSSUS	9 nUR                  U5       H%  nUR                  U5      n	UR                  U	5        M'     SSS5        UR                  S5        U$ ! , (       d  f       N!= f)
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   rG   headersr   r   iter_contentr   updateseek)
r   r  r   r  response
total_size
block_sizeprogress_barr   sizes
             r;   r   r     s    ( ||C-HX%%))*:A>?JJ~	4$@
 
))*5D<<%D% 6
 KKNM
 
s   0;C
Cr   extract_pathc                     [        U 5       n[        UR                  5       X#(       a  SOSS9 H  nUR                  XQ5        M     SSS5        g! , (       d  f       g= f)a  
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     sI      
	bKKMB^
F JJv,
 
		s   :A
Ac                 z    [        [        U -  S5       n[        U[        S9sSSS5        $ ! , (       d  f       g= f)z9
Loads a local YAML file and returns it as a dictionary.
r)LoaderN)rw   r3   r   r   )r   r|   s     r;   	load_yamlr/    s+     
j4	%Ag& 
&	%	%s   ,
:)T)NNT)NT)Krt   r   r   r   r   sysr   dataclassesr   	functoolsr   ior   r   pathlibr   typingr   r	   r
   r   r   r   r   zipfiler   r7   rx   r   platformdirsr   r   typing_extensionsr   yamlr   r   __version__r   
exceptionsr   r   r   r   r   _TemporaryFileWrapperr   r   r'   r   r.   r0   r2   ro   r  __file__parentr3   r4   r  r<   r?   r   rk   rl   r   r   r  r   r	  r  r  r   r   r/  rO   r=   r;   <module>r?     s   	  	   
  ! $ &  C C C     '  '  $  "'8+I+I>"YZ	 Z $sCx.  >CU]b2cS'-../ c<<v
#cll^+<=
>>(.s||(<$	% <
34T 41299
D 9 fg&(S$s)^$  $ 
7b 7b  7bv #//1 [*# *#ZY-& Y-x+# +t t 0'3 '3 '
S 
 '+	(	(
3-( ^$( 
	(
 (\ 	--- 3-- 
	-
 
-.'C 'DcN 'r=   