o
    3g                     @  s~   d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZ G dd dZdddZdd	d
ZdS )    )annotationsN)ProcessExecutionErrorc                   @  s(   e Zd ZdZedd Zedd ZdS )
_fake_lockz6Needed to allow normal os.exit() to work without errorc                 C  s   dS )NT )_r   r   P/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/commands/daemons.pyacquire      z_fake_lock.acquirec                   C  s   d S )Nr   r   r   r   r   release   r	   z_fake_lock.releaseN)__name__
__module____qualname____doc__staticmethodr   r
   r   r   r   r   r      s    
r   Tc              	     sp  |d u rt j}|d u r|}d}t  \}}|  }t  }	|	dkrt | d}
z|zLt   t d tt jdd}t||r@dnddd}t||rKdnddd}t		t	j
t	j | j|d| | | d t |t jd	 W n# ty   d
}
dtjt  | d  }t ||d	 Y nw W t | t |
 nt | t |
 w t | t |	d\}}
t ||}t | tt |d	}W d    n1 sw   Y  |
dkr| rt |}nt!||
d|t"j#$t"j# d _%d  _&d  _'d  _(d  _)| _d _*d  _+t,  _-d _.| _/| _0 fdd} f fdd	}| _1| _2 S )Ni @  r   utf-8encodingawT)cwd	close_fdsstdinstdoutstderrutf8    Fc              
   S  sf   | j d u r0zt| jd W | j S  ty/ } z|jtjkr"d| _ n W Y d }~| j S d }~ww | j S )Nr   )
returncodeoskillpidOSErrorerrnoESRCH)selfexr   r   r   pollY   s   

zposix_daemonize.<locals>.pollc                   s0   | j d u r|  d u rtd | j d u s j S )Ng      ?)r   r&   timesleep)r$   procr   r   waite   s
   


zposix_daemonize.<locals>.wait)3r   devnullpipe	formulateforkclosesetsidumaskopensignalSIGHUPSIG_IGNpopenfilenowritestrr    encode	Exceptionjoin	tracebackformat_exceptionsysexc_info_exitwaitpidread
contextlibsuppressUnicodeErrordecodeisdigitintr   
subprocessPopen__new___child_createdr   r   r   r   universal_newlines_inputr   _waitpid_lock_communication_startedargsargvr&   r+   )commandr   r   r   appendMAX_SIZErfdwfdrT   firstpidrcr   tbtextr   output	secondpidr&   r+   r   r)   r   posix_daemonize   s   






r_   c                 C  s~   |d u rt j}|d u r|}d}tt jdd}t||rdnddd}t||r'dnddd}| j|| | | tj|B dS )N   r   r   r   r   )r   r   r   r   creationflags)r   r,   r3   r7   r8   rK   CREATE_NEW_PROCESS_GROUP)rU   r   r   r   rV   DETACHED_PROCESSr   r   r   r   win32_daemonizep   s   rd   )NNT)
__future__r   rE   r"   r   r4   rK   r@   r'   r>   plumbum.commands.processesr   r   r_   rd   r   r   r   r   <module>   s    
U