o
    ùjã&  ã                   @   sœ   d dl Z e  e¡Zd dlZd dlZd dlZd dlmZ e 	¡ Z
zd dlZW n ey0   d ZY nw ze d¡ZW n eyC   dZY nw G dd„ deƒZdS )é    N)ÚYELÚSC_OPEN_MAXi   c                   @   sV   e Zd ZdZeƒ Z	 ddd„Zdd„ Zdd	„ Zd
d„ Z	e
dd„ ƒZdd„ Zdd„ ZdS )ÚAutoReloadera\  Monitor which re-executes the process when files change.

    This :ref:`plugin<plugins>` restarts the process (via :func:`os.execv`)
    if any of the files it monitors change (or is deleted). By default, the
    autoreloader monitors all imported modules; you can add to the
    set by adding to ``autoreload.files``::

        spyne.util.autorel.AutoReloader.FILES.add(myFile)


        spyne.util.autorel.AutoReloader.match = r'^(?!cherrypy).+'

    The autoreload plugin takes a ``frequency`` argument. The default is
    1 second; that is, the autoreloader will examine files once each second.
    é   ú.*c                 C   s*   t | _i | _ttjƒ| _|| _	 || _d S )N)	Ú	MAX_FILESÚmax_cloexec_filesÚmtimesÚsetr   ÚFILESÚfilesÚmatchÚ	frequency)Úselfr   r   © r   úR/var/www/html/LicenciasMiG/venv/lib/python3.10/site-packages/spyne/util/autorel.pyÚ__init__j   s   zAutoReloader.__init__c                 C   s&   ddl m} || jƒ}| | j¡ |S )Nr   )ÚLoopingCall)Útwisted.internet.taskr   ÚrunÚstartr   )r   r   Úretvalr   r   r   r   {   s   
zAutoReloader.startc              	   C   s¬   t ƒ }ttj ¡ ƒD ]I\}}t | j|¡rSt|dƒr%t|jdƒr%|jj	}n)zt
|ddƒ}W n ty8   d}Y nw |durNtj |¡sNtj tj t|¡¡}| |¡ q
|S )z1Return a Set of sys.modules filenames to monitor.Ú
__loader__ÚarchiveÚ__file__N)r
   ÚlistÚsysÚmodulesÚitemsÚrer   Úhasattrr   r   ÚgetattrÚImportErrorÚosÚpathÚisabsÚnormpathÚjoinÚ_module__file__baseÚadd)r   r   ÚkÚmÚfr   r   r   Úsysfiles‚   s(   ÿ
þ
ÿÿ
€zAutoReloader.sysfilesc              	   C   sÈ   |   ¡ | jB D ]Z}|ra| d¡r|dd… }| j |d¡}|du r"qzt |¡j}W n ty5   d}Y nw || jvrA|| j|< q|du sI||krat	 
d| ¡ ddlm} | ¡  |  ¡   dS qdS )z:Reload the process if registered files have been modified.z.pycNéÿÿÿÿr   z$Restarting because '%s' has changed.)Úreactor)r-   r   Úendswithr	   Úgetr#   ÚstatÚst_mtimeÚOSErrorÚloggerÚinfoÚtwisted.internetr/   ÚstopÚ	_do_execv)r   ÚfilenameÚoldtimeÚmtimer/   r   r   r   r   š   s0   
þ
ÿ€äzAutoReloader.runc                 C   sH   dt j }|  dd¡}tjd dko| |¡ }|r"|| | d< dS dS )aõ  
        If sys.path[0] is an empty string, the interpreter was likely
        invoked with -m and the effective path is about to change on
        re-exec.  Add the current directory to $PYTHONPATH to ensure
        that the new process sees the same path.

        This issue cannot be addressed in the general case because
        Python cannot reliably reconstruct the
        original command line (http://bugs.python.org/issue14208).

        (This idea filched from tornado.autoreload)
        Ú.Ú
PYTHONPATHÚ r   N)r#   Úpathsepr1   r   r$   Ú
startswith)ÚenvÚpath_prefixÚexisting_pathÚneeds_patchr   r   r   Ú_extend_pythonpathº   s   

þÿzAutoReloader._extend_pythonpathc              	   C   sR   t d| jƒD ] }z	t |tj¡}W n	 ty   Y qw t |tj|tjB ¡ qdS )aÞ  Set the CLOEXEC flag on all open files (except stdin/out/err).

        If self.max_cloexec_files is an integer (the default), then on
        platforms which support it, it represents the max open files setting
        for the operating system. This function will be called just before
        the process is restarted via os.execv() to prevent open files
        from persisting into the new process.

        Set self.max_cloexec_files to 0 to disable this behavior.
        é   N)Úranger   ÚfcntlÚF_GETFDÚIOErrorÚF_SETFDÚ
FD_CLOEXEC)r   ÚfdÚflagsr   r   r   Ú_set_cloexecÓ   s   ÿûzAutoReloader._set_cloexecc                 C   sÖ   t jdd… }|  tj¡ t dd |¡ ¡ t d¡ t dtdƒtdƒ¡ t d¡ t j	dd… dkr=d	d
l
m} |‚| d	t j¡ t j	dkrPdd„ |D ƒ}t t¡ t dt¡ | jrb|  ¡  t t j|¡ dS )zÀRe-execute the current process.

        This must be called from the main thread, because certain platforms
        (OS X) don't allow execv to be called in a child thread very well.
        NzRe-spawning %sú r?   z
%s Bye! %sz#-----------------------------------é   Újavar   )ÚSystemRestartÚwin32c                 S   s   g | ]}d | ‘qS )z"%s"r   )Ú.0Úargr   r   r   Ú
<listcomp>ú   s    z*AutoReloader._do_execv.<locals>.<listcomp>zChange working directory to: %s)r   ÚargvrF   r#   Úenvironr5   r6   r'   r   ÚplatformÚ_systemrestartrT   ÚinsertÚ
executableÚchdirr(   Údebugr   rP   Úexecv)r   ÚargsrT   r   r   r   r9   å   s"   



zAutoReloader._do_execvN)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r-   r   ÚstaticmethodrF   rP   r9   r   r   r   r   r   V   s    
 
r   )ÚloggingÚ	getLoggerrc   r5   r#   r   r   Úspyne.util.colorr   Úgetcwdr(   rI   r"   r   ÚsysconfÚAttributeErrorÚobjectr   r   r   r   r   Ú<module>   s    0
ÿÿ