
    i                     p    d dl Z d dlZd dlmZ d dlZd dlmZ d dlmZ  e       Z	 G d d      Z
 e
       Zy)    N)Optional)Console)get_headersc                       e Zd Zd ZdefdZdefdZdefdZddedefdZ	de
fdZd	ed
edee   fdZd	ed
edee   fdZy)BinaryPathsc                     | j                         | _        | j                         | _        t        j
                  j                  d      | _        d| _        d | _	        y )N~z8https://raw.githubusercontent.com/Arrowar/SC_Binary/main)
_detect_systemsystem_detect_archarchospath
expanduserhome_dirgithub_repopaths_cacheselfs    Z/home/nidran/Documenti/script/streaming-community/StreamingCommunity/setup/binary_paths.py__init__zBinaryPaths.__init__   sH    ))+%%'	**3/U    returnc                 v    t        j                         j                         }g d}||vrt        d|       |S )z/Detect and normalize the operating system name.)windowsdarwinlinuxzUnsupported OS: )platformr   lower
ValueError)r   r   supported_systemss      r   r
   zBinaryPaths._detect_system   s>    "((*:**/x899r   c                 x    t        j                         j                         }ddddd}|j                  |d      S )z-Detect and normalize the system architecture.x64arm64)amd64x86_64r$   aarch64)r   machiner   get)r   r(   arch_maps      r   r   zBinaryPaths._detect_arch'   s?    ""$**,	
 ||GU++r   c                    | j                   dk(  ret        j                  j                  t        j                  j	                  | j
                        d   t        j                  j                  z   d      S | j                   dk(  r+t        j                  j                  | j
                  dd      S t        j                  j                  | j
                  ddd      S )z<Get the binary directory path based on the operating system.r   r   binaryr   Applicationsz.localbin)r   r   r   join
splitdriver   sepr   s    r   get_binary_directoryz BinaryPaths.get_binary_directory2   s    ;;)#77<< 2 24== A! Drww{{ RT\]][[H$77<<~xHH77<<xIIr   modec                 V    | j                         }t        j                  ||d       |S )z0Create the binary directory if it doesn't exist.T)r3   exist_ok)r2   r   makedirs)r   r3   
binary_dirs      r   ensure_binary_directoryz#BinaryPaths.ensure_binary_directory;   s&    ..0

JTD9r   c                    | j                   | j                   S 	 | j                   d}t        j                  |dt	                     }|j                          |j                         | _         | j                   S # t        $ r i cY S w xY w)z)Load binary paths from GitHub repository.z/binary_paths.json
   timeoutheaders)r   r   httpxr)   r   raise_for_statusjson	Exception)r   urlresponses      r   _load_paths_jsonzBinaryPaths._load_paths_jsonA   s    '###	%%&&89Cyyb+-HH%%''}}D### 	I	s   AA: :BBtoolbinary_namec                     | j                         }t        j                  j                  ||      }t        j                  j	                  |      r|S y)a0  
        Get the full path to a binary from the repository.
        
        Args:
            tool: Tool name (ffmpeg, bento4, megatools)
            binary_name: Binary name (ffmpeg.exe, mp4decrypt, etc.)
        
        Returns:
            Full local path to the binary or None if not found
        N)r2   r   r   r/   isfile)r   rE   rF   r7   
local_paths        r   get_binary_pathzBinaryPaths.get_binary_pathO   s?     ..0
WW\\*k:
77>>*%r   c           
      
   | j                         }| j                   d| j                   d| }t        j	                  d| d| d| j                   d| j                          ||vry||   D ]  }|j                  |      s| j                   d| }t        j                  j                  | j                         |      }t        j	                  d| d	|        	 t        j                  |d
t                     }|j                          t        |d      5 }	|	j!                  |j"                         ddd       | j                  dk7  rt        j$                  |d       |c S  y# 1 sw Y   4xY w# t&        $ r Y  yw xY w)aB  
        Download a specific binary from GitHub repository directly to binary directory.
        
        Args:
            tool: Tool name (ffmpeg, bento4, megatools)
            binary_name: Binary name to download
        
        Returns:
            Full local path to the downloaded binary or None if failed
        _z[cyan]Downloading [red]z [cyan]for [yellow]z [cyan]on [red] Nz
/binaries/z[cyan]Downloading from [red]z [cyan]to [yellow]<   r;   wbr     )rD   r   r   consolelogendswithr   r   r   r/   r2   r>   r)   r   r?   openwritecontentchmodrA   )
r   rE   rF   
paths_jsonkeyrel_pathrB   rI   rC   fs
             r   download_binaryzBinaryPaths.download_binaryb   sx    **,
Qtyyk4&1-k]:MdVSbcgcncnboopquqzqzp{|}j "3 	 H  -))**XJ?WW\\$*C*C*E{S
:3%?QR\Q]^_ $yyb+-PH--/j$/ 21 0 012 {{i/U3%%#	 * 2 2 !   s*   <E5E)6.E5)E2	.E55	FFN)rP   )__name__
__module____qualname__r   strr
   r   r2   intr8   dictrD   r   rJ   r\    r   r   r   r      s      	,c 	,Jc JC C $ C c hsm &'C 'c 'hsm 'r   r   )r   r   typingr   r>   rich.consoler   $StreamingCommunity.utils.http_clientr   rQ   r   binary_pathsrc   r   r   <module>rh      s<    
      = )t tp }r   