
    yf:                        S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	SSK
Jr  SSKJrJrJrJr  SSKJr  \" 5       rSSKJrJrJrJr  \ R0                  S 5       rS r\ R0                  S	S
.S j5       r " S S\5      r " S S\5      r " S S\5      r " S S\5      r \ R0                  S 5       r!S r" " S S5      r#\#" 5       r$S r%SS\&4S jjr'\RP                  4S jr)SS jr*\ R0                  S S j5       r+g)!    N   )__version__)is_win32is_linux
is_freebsd	is_darwin)create_logger)EXIT_SUCCESSEXIT_WARNINGEXIT_SIGNAL_BASEErrorc               #   .  #    SSK Jn   U " 5       n[        R                  " 5       nU(       a;  [        n US 4v   [        R                  SU-  5        [        R                  " U5        [        R                  " 5         [        R                  " 5       nU(       a  [        R                  " S5        [        R                  " S5        [        R                  " S5        [        R                  " S5        [        R                  " [        R                  [        R                  5      n[        R                   " US5        [        R                   " US5        U " 5       n X4v   [        R                  " S5        [        R                   " US5        g ! [
         a  nUR                  n S nAGNoS nAff = f! [        R                  SU-  5        [        R                  " U5        f = f! [        R                  " S5        [        R                   " US5        f = f7f)Nr   )get_process_idz:Daemonizing: Foreground process (%s, %s, %s) is now dying.r   /   )platformr   osforkr
   _ExitCodeException	exit_codeloggerdebug_exitsetsidchdircloseopendevnullO_RDWRdup2)r   old_idpidr   efdnew_ids          6/usr/lib/python3/dist-packages/borg/helpers/process.py
_daemonizer'      sN    )F
'')C
 		 $, LLUX^^_HHYIIK
'')C

HHSMHHQKHHQK	RYY	'BGGBNGGBNFn 	
A- " 	$I	$ LLUX^^_HHY" 	
AsR   0HF DHG# .H
F-F("F0 (F--F0 00G  H#/HHc                  V    [        5        u  pX4sSSS5        $ ! , (       d  f       g= f)zkDetach process from controlling terminal and run in background

Returns: old and new get_process_id tuples
N)r'   )r!   r%   s     r&   	daemonizer)   7   s    
 
)&~ 
s   
(   )timeoutc              #   b  #    [        5        u  pUc  [        R                  SU-  5        [        n[	        S[        [        5      5         [	        S[        [        5      5         [	        S[        [        5      5          U S:  a  [        R                  " U 5        [        R                  S5        [        n[)        U5      e[*        R,                  n[        R                  SU-  5         X4v   [        R                  SU-  5          [0        R2                  " US   U5        SSS5        g! [         a     N[         a	    [        n N[         a$    [        R                  SU-  5        [        S	-   n N[         aa  n[        R                  S
SR                  [         R"                  " UR$                  XDR&                  5      5      -   5        [        n SnAGN$SnAff = f! [)        U5      e= f! , (       d  f       O= f SSS5        O! , (       d  f       O= fSSS5        GMg  ! , (       d  f       GNv= f! [         ag  n[*        R.                  n[        R                  SSR                  [         R"                  " UR$                  XDR&                  5      5      -   5        UeSnAff = f! [         a[  n[        R5                  SSR                  [         R"                  " UR$                  XDR&                  5      5      -   5         SnAGNSnAff = f!  [0        R2                  " US   U5        f ! [         aZ  n[        R5                  SSR                  [         R"                  " UR$                  XDR&                  5      5      -   5         SnAf SnAff = f= f! , (       d  f       g= f7f)a  Like daemonize(), but as context manager.

The with-body is executed in the background process,
while the foreground process survives until the body is left
or the given timeout is exceeded. In the latter case a warning is
reported by the foreground.
Context variable is (old_id, new_id) get_process_id tuples.
An exception raised in the body is reported by the foreground
as a warning as well as propagated outside the body in the background.
In case of a warning, the foreground exits with exit code EXIT_WARNING
instead of EXIT_SUCCESS.
NzQDaemonizing: Foreground process (%s, %s, %s) is waiting for background process...SIGINTSIGHUPSIGTERMr   zGDaemonizing: Background process did not respond (timeout). Is it alive?z=Daemonizing: Foreground process (%s, %s, %s) received SIGINT.r   zEDaemonizing: Foreground process received an exception while waiting:
 z;Daemonizing: Background process (%s, %s, %s) is starting...z9Daemonizing: Background process (%s, %s, %s) has started.zDDaemonizing: Background process raised an exception while starting:
r   zHDaemonizing: Trying to kill the foreground process raised an exception:
)r'   r   r   r
   signal_handlerraising_signal_handlerKeyboardInterruptSigHupSigTermtimesleepwarningr   r   BaseExceptionjoin	tracebackformat_exception	__class____traceback__r   signalr/   r.   r   killerror)r+   r!   r%   r   r#   sig_to_foregrounds         r&   daemonizingrC   @   s     
)&>LLlouuv$I*@AR*ST*@*HI	+A'+JK8{

7+$ NN#lm ,I -Y77 #NNRU[[\	c.  LLTW]]^cq	#45e 
   - !-I( 5LL!`ci!ij 01 4I$ -NN#k#%779+E+EakkSTVeVe+f#g$h i ,I	- -Y775 LKK JII UTTTF  	 &NNb779#=#=akk1oo#^_` aG		 ! chWWY%?%?QP_P_%`ab c cccq	#45  chWWY%?%?QP_P_%`ab c ccg 
sF  N/=N
H)$H>G2	 D-G#7G2	(N+H;0L
J/$	N/-
G 7G#9G G#
*G 4G#6	G ?AGG#G  G##G//G2	2
H <H	H)
HH)N)
H8	3N;
J,A"J''J,,L/
L9AL	NLNNL43N4
N	>AN	NN	NN
N,(N/c                       \ rS rSrS rSrg)r      c                     Xl         g Nr   )selfr   s     r&   __init___ExitCodeException.__init__   s    "    rH   N)__name__
__module____qualname____firstlineno__rJ   __static_attributes__ rL   r&   r   r      s    #rL   r   c                       \ rS rSrSrSrg)SignalException   z*base class for all signal-based exceptionsrR   NrM   rN   rO   rP   __doc__rQ   rR   rL   r&   rT   rT      s    4rL   rT   c                       \ rS rSrSrSrg)r4      zraised on SIGHUP signalrR   NrV   rR   rL   r&   r4   r4      s    !rL   r4   c                       \ rS rSrSrSrg)r5      zraised on SIGTERM signalrR   NrV   rR   rL   r&   r5   r5      s    "rL   r5   c              #   
  #    [        U [        5      (       a  [        [        U S5      n U b  [        R                  " X5      n Sv   U b  [        R                  " U W5        gg! U b  [        R                  " U W5        f f = f7f)a  
when entering context, set up signal handler <handler> for signal <sig>.
when leaving context, restore original signal handler.

<sig> can bei either a str when giving a signal.SIGXXX attribute name (it
won't crash if the attribute name does not exist as some names are platform
specific) or a int, when giving a signal number.

<handler> is any handler value as accepted by the signal.signal(sig, handler).
N)
isinstancestrgetattrr?   )sighandlerorig_handlers      r&   r1   r1      sm      #sfc4(
}}S2-?MM#|, 3?MM#|, s   A BA# B#B  Bc                    ^  U 4S jnU$ )Nc                 R   > [         R                   " U [         R                  5        TerG   )r?   SIG_IGN)sig_noframeexc_clss     r&   ra   'raising_signal_handler.<locals>.handler   s     	ffnn-rL   rR   )rh   ra   s   ` r&   r2   r2      s     NrL   c                   D    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
rg)SigIntManager   c                     SU l         SU l        SU l        [        SU R                  5      U l        SU l        S U l        g )NFr-   i -1)_sig_int_triggered_action_triggered_action_doner1   ra   ctxdebounce_intervallastrI   s    r&   rJ   SigIntManager.__init__   s;    "'!&!!(DLL9!)	rL   c                     U R                   $ rG   )rn   rt   s    r&   __bool__SigIntManager.__bool__   s    &&&rL   c                     U R                   $ rG   )ro   rt   s    r&   action_triggeredSigIntManager.action_triggered   s    %%%rL   c                     U R                   $ rG   )rp   rt   s    r&   action_doneSigIntManager.action_done   s       rL   c                      SU l         SU l        g )NFT)ro   rp   rt   s    r&   action_completedSigIntManager.action_completed   s     "' rL   c                     [         R                  " 5       nU R                  c  X0l        SU l        SU l        g X0R                  -
  U R
                  :  a  U R                  S S S 5        [        eg )NT)r6   monotonic_nsrs   rn   ro   rr   __exit__r3   )rI   rf   stacknows       r&   ra   SigIntManager.handler   sb     !99I&*D#%)D"99_ 6 66 MM$d+## 7rL   c                 8    U R                   R                  5         g rG   )rq   	__enter__rt   s    r&   r   SigIntManager.__enter__   s    rL   c                 n    U R                   (       a$  U R                   R                  XU5        S U l         g g rG   )rq   r   )rI   exception_typeexception_valuer;   s       r&   r   SigIntManager.__exit__   s)    88HHnyIDH rL   )rp   ro   rn   rq   rr   rs   N)rM   rN   rO   rP   rJ   rw   rz   r}   r   ra   r   r   rQ   rR   rL   r&   rk   rk      s*    '&!!$$rL   rk   c                  j    [         R                   " [         R                  [         R                  5        g)a?  
Ignore SIGINT, see also issue #6912.

Ctrl-C will send a SIGINT to both the main process (borg) and subprocesses
(e.g. ssh for remote ssh:// repos), but often we do not want the subprocess
getting killed (e.g. because it is still needed to cleanly shut down borg).

To avoid that: Popen(..., preexec_fn=ignore_sigint)
N)r?   r-   re   rR   rL   r&   ignore_sigintr      s     MM&--0rL   cmd_linec                    UR                  S5      (       a   S5       e [        R                  " U 5      nU(       d  [        S5      e [        R                  SX5         [        R                  " U40 UD6$ ! [         a   n[        R                  SX5         SnAgSnAff = f! [         a    [        R                  SXS   5         g[         a    [        R                  S	XS   5         gf = f)
aW  
Handle typical errors raised by subprocess.Popen. Return None if an error occurred,
otherwise return the Popen object.

*cmd_line* is split using shlex (e.g. 'gzip -9' => ['gzip', '-9']).

Log messages will be prefixed with *log_prefix*; if set, it should end with a space
(e.g. log_prefix='--some-option: ').

Does not change the exit code.
shellz Sorry pal, shell mode is a no-noz&an empty command line is not permittedz%s%sNz%scommand line: %sz%sexecutable not found: %sr   z%spermission denied: %s)getshlexsplit
ValueErrorr   rA   r   
subprocessPopenFileNotFoundErrorPermissionError)r   
log_prefixkwargscommandves        r&   popen_with_error_handlingr     s     zz'""F$FF"++h'EFF 
 LL%z;2622  VZ,  1:qzJ .
AJGs/   (A6 B# 6
B  BB ##C-"C-,C-c                     [        U S5      =(       a<    U R                  5       =(       a%    [        (       + =(       d    S[        R                  ;   $ )NisattyANSICON)hasattrr   r   r   environ)r$   s    r&   is_terminalr   !  s2    2x ^RYY[^(l6]iSUS]S]F]^rL   c                 b   [        Ub  UO[        R                  5      nU (       an  SnUR                  US-   5      nUb  X1U'   OPUR                  U5      nUb<  [	        [
        SS5      (       a&  [        [
        S5      (       a  UR                  U5        UR                  SS5        [        US'   U$ )	ag  
Prepare the environment for a subprocess we are going to create.

:param system: True for preparing to invoke system-installed binaries,
               False for stuff inside the pyinstaller environment (like borg, python).
:param env: optionally give a environment dict here. if not given, default to os.environ.
:return: a modified copy of the environment
NLD_LIBRARY_PATH_ORIGfrozenF_MEIPASSBORG_PASSPHRASEBORG_VERSION)	dictr   r   r   r_   sysr   popr   )systemenvlp_keylp_origlps        r&   prepare_subprocess_envr   %  s     cos2::
6C
 #''&7*+!K B~'#x"?"?GCQ[D\D\GGt$%CJrL   c           	   #     #    U (       a  UnUn[        SS9nU(       a!  [        U [        R                  USU[        S9nO [        U [        R                  USU[        S9nU(       d  [        SU  S35      eU(       a  UR                  OUR                  nU(       + n Uv   Sn U(       a  UR                  5         U (       aq  [        R                  S
5        WR                  5       n	[        R                  SU	5        W(       a  WR                  5         U(       a  U	(       a  [        SX	4-  5      eg g g ! [         a0    [        R                  S5        U (       a  WR                  5         S	ne f = f! U(       a  UR                  5         U (       aq  [        R                  S
5        WR                  5       n	[        R                  SU	5        W(       a  WR                  5         W(       a  U	(       a  [        SX	4-  5      ef f f = f7f)NT)r   zfilter-process: )stdoutstdinr   r   
preexec_fn)r   r   r   r   r   zfilter z: process creation failedz Exception, killing the filter...Fz"Done, waiting for filter to die...zfilter cmd exited with code %dzfilter %s failed, rc=%d)r   r   r   PIPEr   r   r   r   	Exceptionr   r   r@   r   wait)
cmdstreamstream_closeinboundfilter_streamfilter_stream_closer   procborg_succeededrcs
             r&   create_filter_processr   K  s    
 *$D1 ,SP]8JPS`moD -S
P]8JPS`moD'#&?@AA 'TZZ #{C LLNLL=>BLL92>"##%"5	ABB #%~   78IIK LLNLL=>BLL92>"##%"5	ABB #%~ s2   BHD3  E0 #BH3:E--E0 0BHH)r0   rG   )T),
contextlibr   os.pathrer   r?   r   r   r6   r;   r0   r   platformflagsr   r   r   r   r   r	   helpersr
   r   r   r   contextmanagerr'   r)   rC   r9   r   rT   r4   r5   r1   r2   rk   sig_intr   r^   r   r   r   r   r   rR   rL   r&   <module>r      s#    	  	    
    E E "	 I I  B  Bc BcJ# #
5m 5"_ "#o # - -,4 4r /
1 > :: _#L 0C 0CrL   