o
    !wi1                     @   s   d dl Z d dlZg dZd dlmZmZ G dd deZG dd deZde	fd	d
Z
e
ZG dd dZG dd deZG dd deZejfddZG dd deZdS )    N)getDefaultJVMPathget_default_jvm_pathJVMNotFoundExceptionJVMNotSupportedException)SequenceTuplec                   @      e Zd ZdZdS )r   aH  Exception raised when no JVM was found in the search path.

    This exception is raised when the all of the places searched did not
    contain a JVM. The locations searched depend on the machine architecture.
    To avoid this exception specify the JAVA_HOME environment variable as a
    valid jre or jdk root directory.
    N__name__
__module____qualname____doc__ r   r   M/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jpype/_jvmfinder.pyr          r   c                   @   r   )r   ah  Exception raised when the JVM is not supported.

    This exception is raised after a search found a valid Java home directory
    was found, but the JVM shared library found is not supported. Typically
    this occurs when the JVM does not match the architecture of Python
    32 vs 64 bit, or the JVM is older than the version used to compile
    JPype.
    Nr	   r   r   r   r   r   )   r   r   returnc                  C   s>   t jdkrt } |  S t jdkrt } |  S t } |  S )aK  Retrieves the path to the default or first found JVM library.

    Returns:
      The path to the JVM shared library file

    Raises:
      JVMNotFoundException: If there was no JVM found in the search path.
      JVMNotSupportedException: If the JVM was found was not compatible with
        Python due to cpu architecture.

    win32darwin)sysplatformWindowsJVMFinderDarwinJVMFinderLinuxJVMFinderget_jvm_path)finderr   r   r   r   4   s   

r   c                   @   sn   e Zd ZU dZdZeed< dZeedf ed< dd Z	d	d
 Z
edd Zdd Zdd Zdd Zdd ZdS )	JVMFinderzJVM library finder base class.	libjvm.so_libfile)/usr/lib/jvm	/usr/java.
_locationsc                 C   s   | j | jf| _d S N)_get_from_java_home_get_from_known_locations_methodsselfr   r   r   __init__T   s   
zJVMFinder.__init__c                 C   s   d}d}t t|dd dD ]#\}}}| j|v r2tj|d }||v r(d}qtj|| j  S q|r<td|t	d	)
zRecursively looks for the given file.

        Parameters:
            java_home(str): A Java home folder

        Returns:
            The first found file path, or None
        )cacaojamvmFc                 S   s   t | d tjS )Nr   )lensplitossep)tr   r   r   <lambda>f   s    z'JVMFinder.find_libjvm.<locals>.<lambda>)key   TzySorry '{0}' is known to be broken. Please ensure your JAVA_HOME contains at least another JVM implementation (eg. server)zsSorry no JVM could be found. Please ensure your JAVA_HOME environment variable is pointing to correct installation.)
sortedr,   walkr   pathr+   joinr   formatr   )r&   	java_homenon_supported_jvmfound_non_supported_jvmroot_names	candidater   r   r   find_libjvmY   s   	 
zJVMFinder.find_libjvmc                 c   s    g }d}| D ]D}t j|sqtt |D ]3}t jt j||}||v s.t j|s/qt j|	 }|D ]}||v rI|
| |V   nq9qqdS )a*  
        Generator that looks for the first-level children folders that could be
        Java installations, according to their name

        Parameters:
            parents (str[]): A list of parent directories

        Returns:
            A list of the possible JVM installation folders
        )jrejdkjavaN)r,   r4   existsr2   listdirrealpathr5   isdirbasenamelowerappend)parentshomes
java_namesparent	childnamer4   	real_name	java_namer   r   r   find_possible_homes|   s(   
zJVMFinder.find_possible_homesc                 C   s   dS )z
        Check if the jvm is valid for this architecture.

        This method should be overriden for each architecture.

        Raises:
            JVMNotSupportedException: If the jvm is not supported.
        Nr   r&   jvmr   r   r   check   s   	zJVMFinder.checkc                 C   s   d}| j D ];}z| }|r| | W n$ ty   Y q ty$   Y q ty7 } z|}W Y d}~qd}~ww |dur@|  S q|durG|td| j)z
        Retrieves the path to the default or first found JVM library

        Returns:
            The path to the JVM shared library file

        Raises:
            ValueError: No JVM library found or No Support JVM found
        NzcNo JVM shared library file ({0}) found. Try setting up the JAVA_HOME environment variable properly.)r$   rS   NotImplementedErrorr   r   r6   r   )r&   jvm_notsupport_extmethodrR   er   r   r   r      s.   


zJVMFinder.get_jvm_pathc                 C   sN   t d}|r#t j|r%t j|}t j|st d}| |S dS dS )z
        Retrieves the Java library path according to the JAVA_HOME environment
        variable

        Returns:
            The path to the JVM library, or None
        	JAVA_HOMEN)r,   getenvr4   rB   rD   r>   )r&   r7   r   r   r   r"      s   
	

zJVMFinder._get_from_java_homec                 C   s0   |  | jD ]}| |}|dur|  S qdS )z
        Retrieves the first existing Java library path in the predefined known
        locations

        Returns:
            The path to the JVM library, or None
        N)rP   r    r>   )r&   homerR   r   r   r   r#      s   
z#JVMFinder._get_from_known_locationsN)r
   r   r   r   r   str__annotations__r    r   r'   r>   staticmethodrP   rS   r   r"   r#   r   r   r   r   r   L   s   
 #
#&r   c                       s4   e Zd ZdZdZdZdZ fddZdd Z  Z	S )	r   zLinux JVM library finder class.z/usr/bin/javar   )r   r   z/opt/sunc                    s    t    | j| j| jf| _d S r!   )superr'   r"   _get_from_binr#   r$   r%   	__class__r   r   r'      s
   

zLinuxJVMFinder.__init__c                 C   sF   t j| j}t j|r!t jt jt j|d}| |S dS )z
        Retrieves the Java library path according to the real installation of
        the java executable

        :return: The path to the JVM library, or None
        z..N)	r,   r4   rD   _javarB   abspathr5   dirnamer>   )r&   java_binr7   r   r   r   r_     s   
zLinuxJVMFinder._get_from_bin)
r
   r   r   r   rb   r   r    r'   r_   __classcell__r   r   r`   r   r      s    r   c                       s0   e Zd ZdZdZdZ fddZdd Z  ZS )r   z+
    Mac OS X JVM library finder class
    zlibjli.dylib)z!/Library/Java/JavaVirtualMachinesc                    s(   t    t| j| _| j| j dS )z!
        Sets up members
        N)r^   r'   listr$   rH   _javahome_binaryr%   r`   r   r   r'   "  s   
zDarwinJVMFinder.__init__c                 C   sT   ddl }ddl}ddlm} || d dd }|d|kr(|dg S dS )z
        for osx > 10.5 we have the nice util /usr/libexec/java_home available. Invoke it and
        return its output. It seems this tool has been removed in osx 10.9.
        r   N)Version   z10.6z/usr/libexec/java_home)r   
subprocesspackaging.versionri   mac_vercheck_outputstrip)r&   r   rk   ri   currentr   r   r   rh   +  s   z DarwinJVMFinder._javahome_binary)	r
   r   r   r   r   r    r'   rh   rf   r   r   r`   r   r     s    	r   c                 C   s   dd l }d}d}d}|dk}t| d;}|d}|dkr!td	|d
 |d}|d|d }	||	d  |d}|d|d }
W d    n1 sQw   Y  |
|krb|r`tdd S |
|ksj|
|krr|sptdd S td)Nr   iL  i   id  l        rb   s   MZzJVM not valid<   rj   z<Lz<Hz1JVM mismatch, python is 64 bit and JVM is 32 bit.z1JVM mismatch, python is 32 bit and JVM is 64 bit.zUnable to determine JVM Type)structopenreadr   seekunpack)jvmPathmaxsizert   IMAGE_FILE_MACHINE_I386IMAGE_FILE_MACHINE_IA64IMAGE_FILE_MACHINE_AMD64is64fsheader_offsetmachiner   r   r   _checkJVMArch;  s:   



r   c                       s@   e Zd ZdZddgZdZ fddZdd Zed	d
 Z	  Z
S )r   z*
    Windows JVM library finder class
    z*SOFTWARE\JavaSoft\Java Runtime EnvironmentzSOFTWARE\JavaSoft\JREzjvm.dllc                    s   t    | j| jf| _d S r!   )r^   r'   r"   _get_from_registryr$   r%   r`   r   r   r'   c  s   
zWindowsJVMFinder.__init__c                 C   s   t | d S r!   )r   rQ   r   r   r   rS   i  s   zWindowsJVMFinder.checkc               	   C   s   zddl } W n
 ty   Y dS w tjD ]7}z,| | j|}| |d}| ||d }| | | |d}| | |d W   S  tyK   Y qw dS )z
        Retrieves the path to the default Java installation stored in the
        Windows registry

        :return: The path found in the registry, or None
        r   NCurrentVersion
RuntimeLib)	winregImportErrorr   reg_keysOpenKeyHKEY_LOCAL_MACHINEQueryValueExCloseKeyOSError)r   locationjreKeycv
versionKeyr   r   r   r   l  s$   


z#WindowsJVMFinder._get_from_registry)r
   r   r   r   r   r   r'   rS   r]   r   rf   r   r   r`   r   r   Y  s    r   )r,   r   __all__typingr   r   
ValueErrorr   r   r[   r   r   r   r   r   rz   r   r   r   r   r   r   <module>   s   
 (&"