+
    F6!jD9                       a  R t0 t R t^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
HtHt ^ RIHtHt RtR R ltR R	 ltR
 R ltR R ltR R ltR]R]R]/t] ^ k RR R lltR R ltRR R lltR t]R8X  d
   ]! 4        R# R# )a-  Read attribution data from installer and write it into a token file.

This module provides functions for reading attribution data embedded in
Anaconda installers across all platforms:

- Windows (.exe): Attribution stored in PE certificate table after `ANACONDA_ATTR` tag
- Linux/macOS (.sh): Attribution exported as `ANACONDA_ATTR` environment variable
- macOS (.pkg): Attribution appended as trailing data after xar archive

This module is designed to be called during the post-install step of
Anaconda installers to extract and save the installer token.
N)Path)CallableDictOptional)parse_qsunquotez.installer_tokenc                0    V ^8  d   QhR\         R\        /#    filepathreturn)r   str)formats   "?lib/python3.14/site-packages/anaconda_anon_usage/attribution.py__annotate__r      s     Y Y Y# Y    c           	        V P                  RR7      ;_uu_ 4       p\        P                  ! VP                  4       ^ \        P                  R7      p \	        V4      ^@8  d   \        R4      h\        P                  ! RVR,          4      ^ ,          pW#V^,            R8w  d   \        R4      hV^,           p\        P                  ! R	W$V^,            4      ^ ,          pVR
8X  d   V^,           pM VR8X  d   V^,           pM\        RVR 24      h\        P                  ! RW&V^,            4      ^ ,          p\        P                  ! RW&^,           V^,            4      ^ ,          pV^ 8X  g   V^ 8X  d   \        R4      hRp	VP                  WWx,           4      p
V
R8X  d   \        R4      hV
\	        V	4      ,           pV
R,           p\        Wx,           V4      pW+V pVP                  R4      pVR8w  d   VRV pVP                  R4      VP                  4        uuRRR4       #   TP                  4        i ; i  + '       g   i     R# ; i)a  Read attribution data from a signed PE file.

This function looks for the `ANACONDA_ATTR` tag in the certificate table
and returns the data that follows it (up to the next null byte or end of space).

Parameters
----------
filepath: Path
    Path to the installer file

Returns
-------
str
    The attribution data string (URL-encoded query string format)

Raises
------
RuntimeError:
    When the file is not a valid PE file or does not contain any attribution data.
ValueError:
    When the PE format is unknown.

rb)mode)accessz/File is not a valid PE file: PE header missing.z<I:<   @   Ns   PE  z2File is not a valid PE file: invalid PE signature.z<Hi  i  z!Unknown PE format. Magic number: XzFile is not signed.   ANACONDA_ATTRz0Could not find tag `ANACONDA_ATTR` in signature.i       Nutf-8)openmmapfilenoACCESS_READlenRuntimeErrorstructunpack
ValueErrorfindmindecodeclose)r   filemappedpe_header_offsetoptional_header_offsetpe_magic_numbercert_dir_entry_offsetcert_table_offsetcert_table_sizetag	tag_index
data_startmax_data_enddata_endraw_data
null_indexs   &               r   "read_installer_attribution_windowsr9      s;   0 
D	!	!T4;;=!D4D4DE?	6{T!"#TUU%}}T6)3DEaH )9A)=>-O"#WXX%5%:" %mmf6Lq6PQO %'(>(D% E)(>(D% 77JK  !'f5JQ5NO!! %mmfQ69NQR9RSO !A%A)="#899"C(9(KI B"#UVV #SX-J$t+L,>MH 2H "w/JR#KZ0??7+LLNC 
"	!B LLNC 
"	!	!s$   6H6F4H!H6!H33H66I	c                :    V ^8  d   QhR\         \        ,          /# )r
   r   )r   r   )r   s   "r   r   r      s     + +Xc] +r   c                 @    \         P                  P                  R4      # )a6  Read attribution data from the ANACONDA_ATTR environment variable.

This is the primary method for post-install scripts to access attribution data,
as the installer exports this variable before running post-install scripts.

Returns
-------
Optional[str]
    Attribution data string if found, None otherwise.

ANACONDA_ATTR)osenvironget r   r   #read_installer_attribution_from_envrA      s     ::>>/**r   c                F    V ^8  d   QhR\         R\        \        ,          /# r	   r   r   r   )r   s   "r   r   r      s     $ $d $x} $r   c                b    V P                  RRRR7      ;_uu_ 4       pV F  pVP                  4       P                  R4      '       g   K*  RV9   dS   VP                  R4      p\	        V4      ^8  d0   V^,          P                  4       pV'       d   Vu uuRRR4       # K  K  RV9   g   K  VP                  R4      p\	        V4      ^8  g   K  V^,          P                  4       pV'       g   K  Vu uuRRR4       # 	  RRR4       R#   + '       g   i     R# ; i  \
         d     R# i ; i)	aF  Read attribution data from a shell script file.

Looks for an export statement that sets ANACONDA_ATTR environment variable.
Format: export ANACONDA_ATTR='<data>'

Parameters
----------
filepath: Path
    Path to the shell script installer

Returns
-------
Optional[str]
    Attribution data string if found, None otherwise.

rr   ignore)encodingerrorszexport ANACONDA_ATTR='N")r   strip
startswithsplitr!   	Exception)r   flinepartsdatas   &    r   'read_installer_attribution_from_sh_filerS      s    "]]3]BBa::<**+BCCd{ $

3u:?#(8>>#3D#'+ CB  $ +  $

3u:?#(8>>#3D#t'+ CB C$ % CB$   se   D (DADD
D  D-DD1D4
D ?DD D	D D D.-D.c                F    V ^8  d   QhR\         R\        \        ,          /# r	   rC   )r   s   "r   r   r      s      D Xc] r   c                @    \        4       pV'       g   \        V 4      pV# )aY  Read attribution data from a shell script installer.

First tries to read from ANACONDA_ATTR environment variable (primary method),
then falls back to parsing the shell script file.

Parameters
----------
filepath: Path
    Path to the shell script installer

Returns
-------
Optional[str]
    Attribution data string if found, None otherwise.

)rA   rS   )r   attribution_datas   & r   read_installer_attribution_shrW      s!    " ;<B8Lr   c                F    V ^8  d   QhR\         R\        \        ,          /# r	   rC   )r   s   "r   r   r      s     4 4T 4hsm 4r   c                    V P                  R4      ;_uu_ 4       pVP                  ^4      pVR8w  d    RRR4       R# VP                  ^ 4       VP                  4       pRpVP                  V4      pVR8X  d    RRR4       R# V\	        V4      ,           pV\	        V4      8  d    RRR4       R# W6R p VP                  R4      uuRRR4       #   \         d;     TP                  R4      u uuRRR4       #   \         d      RRR4       R# i ; ii ; i  + '       g   i     R# ; i  \         d     R# i ; i)a  Read attribution data from a .pkg installer's trailing data.

The attribution is stored as "ANACONDA_ATTR" marker followed by the data,
appended after the xar archive. This does not affect code signing,
notarization, or Gatekeeper validation.

Parameters
----------
filepath: Path
    Path to the .pkg installer

Returns
-------
Optional[str]
    Attribution data string if found, None otherwise.

r   s   xar!Nr   r   zlatin-1r   )r   readseekrfindr!   r(   UnicodeDecodeErrorOSError)r   r*   magicrR   r2   idxr4   attributions   &       r   read_installer_attribution_pkgrb      s,   $"]]4  DIIaLE	 !  IIaL99;D"C**S/Cby ! " s3xJSY&' ! * {+K "))'23 ! 4 &   &--i889 ! : *  = ! :  5 !  @  s   D. DD. <D;D. #D(D. 2D7C
D. DC;.D/D0
D. ;D	DDD. D	DDD+	%D. +D. .D=<D=windowslinuxdarwinc                h    V ^8  d   QhR\         R\        \        ,          R\        \        ,          /# )r
   r   platform_namer   rC   )r   s   "r   r   r     s,     0 00#+C=0c]0r   c                t   V P                  4       '       g   R# V P                  P                  4       pVR8X  d   \        V 4      # VR8X  d   \	        V 4      # T;'       g$    \
        P                  ! 4       P                  4       p\        P                  V4      pVf   R#  V! V 4      #   \         d     R# i ; i)a>  Read attribution data from an installer file.

Automatically detects the platform and file type to use the appropriate
reader. For .sh files on any platform, uses the shell script reader.
For .pkg files, uses the pkg reader. For .exe files, uses the Windows
PE reader.

Parameters
----------
filepath: Path
    Path to the installer file.
platform_name: Optional[str]
    Override platform detection. Options: 'windows', 'darwin', 'linux'.
    If not provided, uses the current platform.

Returns
-------
Optional[str]
    Attribution data as string, or None if not found.

Nz.pkgz.sh)
existssuffixlowerrb   rW   platformsystemPLATFORM_READERSr?   rN   )r   rg   rj   system_platformreaders   &&   r   read_installer_attributionrq     s    0 ?? __""$F -h77 ,X66 $@@x'8'>'>'@O!!/2F~h s    B( (B76B7c                0    V ^8  d   QhR\         R\        /# )r
   rV   r   )r   dict)r   s   "r   r   r   M  s     # ## #$ #r   c                    \        V 4      p\        V4      pVP                  4        UUu/ uF!  w  r4T\        V4      ^8X  d
   V^ ,          MTbK#  	  pppV# u uppi )a  Parse URL-encoded attribution data into a dictionary.

Parameters
----------
attribution_data: str
    URL-encoded query string with attribution parameters

Returns
-------
dict
    Parsed attribution parameters. Values are unwrapped from
    lists if they contain a single item.

)r   r   itemsr!   )rV   decoded_attribution_dataparsed_attribution_datakvs   &    r   parse_installer_attributionrz   M  si      ''78&'?@ 3J2O2O2Q2Q$!3q6Q;1Q4A%2Q   #"s   'Ac                ^    V ^8  d   QhR\         R\         R\        \        ,          R\        /# )r
   installer_fileinstaller_token_out_filerg   r   )r   r   r   bool)r   s   "r   r   r   e  s4         "  C=  
	 r   c                    \        W4      pV'       g   R# \        V4      pVP                  R4      pV'       g   \        R4      hVP                  P                  RRR7       VP                  V4       R# )a  Save installer attribution data to local .installer_token file.

Parameters
----------
installer_file: Path
    Path to the installer file to read attribution from.
installer_token_out_file: Path
    The path to save the .installer_token file.
platform_name: Optional[str]
    Override platform detection. Options: 'windows', 'darwin', 'linux'.

Returns
-------
bool
    True if the token was saved successfully, False otherwise.

Finstaller_tokenz,No installer_token found in attribution dataT)exist_okparents)rq   rz   r?   r"   parentmkdir
write_text)r|   r}   rg   rV   rw   r   s   &&&   r   save_installer_attributionr   e  sk    , 2.P9:JK-112CDOIJJ##))4)F''8r   c                 l   ^ RI Hp  V ! R\         R2R7      pVP                  RRR7       VP                  RR	R
R7       VP                  R. RORR7       VP	                  4       p\        VP                  4      pVP                  4       '       g6   \        RV R2\        P                  R7       \        P                  ! ^4       \        VP                  4      \        ,          p \        W4VP                  4      pV'       d   \        RV 24       R# \        R4       R#   \         d@   p\        RT 2\        P                  R7       \        P                  ! ^ 4        Rp?R# Rp?i\          d@   p\        RT 2\        P                  R7       \        P                  ! ^4        Rp?R# Rp?ii ; i)z>CLI for extracting installer attribution and saving the token.)ArgumentParserzDReads the attribution data from an installer and writes it into the z token file.)descriptionr|   zPath to the installer file)helpz--prefixz=Path to the directory where the installer token will be savedT)r   requiredz
--platformzOverride platform detection)choicesr   zError: z does not exist.)r*   zInstaller token saved to z'No attribution data found in installer.z	Warning: N)rc   re   rd   )argparser   INSTALLER_TOKEN_FILE_NAMEadd_argument
parse_argsr   r|   is_fileprintsysstderrexitprefixr   rl   r"   rN   )r   parserargsr|   
token_filesavedes          r   _clir     si   'R()7F )   L  
 .*  
 D$--.N!!##''78szzJdkk"%>>J*>t}}U-j\:;;< 	!oCJJ/ sm#**-s0   &,D! D! !F3,4E&&F33F344F..F3__main__c                    V ^8  d   Qh/ ^ \         9   d8   \        \        \        \        .\
        \        ,          3,          3,          ;R&   # )r
   rn   )__conditional_annotations__r   r   r   r   r   )r   s   "r   r   r      s>      Z $sHdVXc]%:;;< [r   )N)r   __doc__r   r=   rl   r#   r   pathlibr   typingr   r   r   urllib.parser   r   r   r9   rA   rS   rW   rb   rn   rq   rz   r   r   __name__r   )r   s   @r   <module>r      s     	   
  + + *. YB+$N84z 1*+@  0p#0 P)X zF r   