Format: application/x-python-code
⬇ Download File (10.7 KB)
   1  o
   2  
   3  ��h�3�@s�dZddlZddlZddlZddlZddlZddlZddlZddlZddl	m
   4  Z
   5  ddlmZddl
   6  mZmZmZmZddlmZddlmZddlmZmZmZmZdd	lmZd
   7  d�ZGdd
   8  �d
   9  e�ZeGdd�de��Z dS)z5Magic functions for running cells in various scripts.�N)�CalledProcessError)�Thread)�Any�Dict�List�default)�magic_arguments)�
  10  _AsyncIOProxy)�Magics�
  11  cell_magic�
  12  line_magic�magics_class)�	arg_splitc
  13  Csbtjdtdd�tjdtdd�tjdddd	�tjd
  14  tdd�tjdd
  15  ddd�g}|D]}||�}q(|S)z'single decorator for adding script argsz--outz�The variable in which to store stdout from the script.
  16              If the script is backgrounded, this will be the stdout *pipe*,
  17              instead of the stderr text itself and will not be auto closed.
  18              )�type�helpz--errz�The variable in which to store stderr from the script.
  19              If the script is backgrounded, this will be the stderr *pipe*,
  20              instead of the stderr text itself and will not be autoclosed.
  21              z--bg�
  22  store_truez�Whether to run the script in the background.
  23              If given, the only way to see the output of the command is
  24              with --out/err.
  25              )�actionrz--procztThe variable in which to store Popen instance.
  26              This is used only when --bg option is given.
  27              z--no-raise-error�store_false�raise_errorz�Whether you should raise an error message in addition to
  28              a stream on stderr if you get a nonzero exit code.
  29              )r�destr)rZargument�str)�f�args�arg�r�Pc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/magics/script.py�script_argss0������#
  30  rc@seZdZdS)�RaiseAfterInterruptN)�__name__�
  31  __module__�__qualname__rrrrrFsrcs�eZdZUdZedd�Zedd�jdd�Zee	d<e
  32  d�dd	��Zed
  33  d�jdd�Z
  34  d"�fdd
  35  �	Zdd�Zdd�Zdd�Ze��eed�dd����Zejdke_dd�Zed�d#dd��Zdd�Zd d!�Z�ZS)$�ScriptMagicsz�Magics for talking to scripts
  36      
  37      This defines a base `%%script` cell magic for running a cell
  38      with a program in a subprocess, and registers a few top-level
  39      magics that call %%script with common interpreters.
  40      z�
  41          The event loop on which to run subprocesses
  42  
  43          Not the main event loop,
  44          because we want to be able to make blocking calls
  45          and have certain requirements we don't want to impose on the main loop.
  46          )rz�Extra script cell magics to define
  47          
  48          This generates simple wrappers of `%%script foo` as `%%foo`.
  49          
  50          If you want to add script magics that aren't on your path,
  51          specify them in script_paths
  52          T)�config�
  53  script_magicscCs"gd�}tjdkr|�dg�|S)z$default to a common list of programs)�sh�bash�perl�ruby�python�python2�python3�pypy�nt�cmd)�os�name�extend)�self�defaultsrrr�_script_magics_defaultgs
  54  
  55  �z#ScriptMagics._script_magics_defaultz�Dict mapping short 'ruby' names to full paths, such as '/opt/secret/bin/ruby'
  56          
  57          Only necessary for items in script_magics where the default path will not
  58          find the right interpreter.
  59          Ncs0tt|�j|d�|��g|_t�|j�dS)N)�shell)�superr!�__init__�_generate_script_magics�bg_processes�atexit�register�kill_bg_processes)r1r4��	__class__rrr6�szScriptMagics.__init__cCs|��dS�N)r;�r1rrr�__del__�szScriptMagics.__del__cCs(|jd}|jD]	}|�|�||<qdS)N�cell)Zmagicsr#�_make_script_magic)r1Zcell_magicsr/rrrr7�s
  60  
  61  �z$ScriptMagics._generate_script_magicscs@�j�||��t��t��fdd���}djdit���|_|S)zAmake a named magic, that calls %%script with a particular programcs"|r	d�|f}n�}��||�S)Nz%s %s)�shebang)�linerA��scriptr1rr�named_script_magic�sz;ScriptMagics._make_script_magic.<locals>.named_script_magicz�%%{name} script magic
  62          
  63          Run cells with {script} in a subprocess.
  64          
  65          This is a shortcut for `%%script {script}`
  66          Nr)�script_paths�getrr�format�locals�__doc__)r1r/rGrrErrB�s
  67  ���zScriptMagics._make_script_magicrFc
  68  s��jdur$tjdkrt�����nt�����_t�jdd�}|��n�j��fdd�}dd����fd	d
  69  ����fdd�}t	|tj�
  70  d
  71  �d�}�jj�
  72  |�\�}z|tj|tjjtjjtjjd����Wn"ty�}z|jtjkr�td|d�WYd}~dS�d}~ww��d�s��d7���dd���jr��j������g��jr�t�j���jj�j<n���j��j r�t�j!���jj�j <n���j!���"����fdd���j#r�t���}	t�j��|	_t�j!��|	_!|	�jj�j#<dSz	||����Wn�t$�y�z[��%t&j'�|tj(��)�dd���j*du�r5td��j+�r1t,��WYdS��-�|tj(��)�dd���j*du�rYtd��j+�rUt,��WYdS��.�td��j+�rht,��Wn.t,�ysYn%t�y|Ynt/�y�}ztd�j0|f�WYd}~nd}~ww�j+�r�t1�j*��d�YdSw�j+�r��j*dk�r��j*�p�d}
  73  t1|
  74  ���dSdS)ahRun a cell via a shell command
  75  
  76          The `%%script` line is like the #! line of script,
  77          specifying a program (bash, perl, ruby, etc.) with which to run.
  78  
  79          The rest of the cell is run by that program.
  80  
  81          .. versionchanged:: 9.0
  82            Interrupting the script executed without `--bg` will end in
  83            raising an exception (unless `--no-raise-error` is passed).
  84  
  85          Examples
  86          --------
  87          ::
  88  
  89              In [1]: %%script bash
  90                 ...: for i in 1 2 3; do
  91                 ...:   echo $i
  92                 ...: done
  93              1
  94              2
  95              3
  96          N�win32T)�target�daemoncst�|����S)z&Call a coroutine on the asyncio thread)�asyncio�run_coroutine_threadsafe�result)�coro)�
  97  event_looprr�	in_thread�sz'ScriptMagics.shebang.<locals>.in_threadc
  98  �s|�z	|�d�IdHWStjjy!}z	|jWYd}~Sd}~wtjjy=}z|�|j�IdHWYd}~Sd}~ww)N�d)�readrP�
  99  exceptions�IncompleteReadError�partial�LimitOverrunError�consumed)�stream�errr�
 100  _readchunk�s����z(ScriptMagics.shebang.<locals>._readchunkc�sJ�	�|�IdHjddd�}|sdS|r|�jj|<n	|�|�|��q)NT�utf8�replace)�errors)�decoder4�user_ns�write�flush)r]Z
 101  stream_argZfile_object�chunk)r_r1rr�_handle_stream�s�
 102  �z,ScriptMagics.shebang.<locals>._handle_streamc�sn�|j�|�|j��t��|j�jtj��}t��|j�j	tj��}t�
 103  ||g�IdH|�
 104  �IdHdSr>)�stdinre�closerP�create_task�stdout�out�sys�stderr�err�wait)�processrAZstdout_taskZstderr_task)rhrrr�_stream_communicate�s�
 105  ��z1ScriptMagics.shebang.<locals>._stream_communicate�win)�posix)rlrorizCouldn't find program: %rr�
 106  r`racst��������Sr>)rP�Task�_run_scriptr)rA�pr1�to_closerr�<lambda>sz&ScriptMagics.shebang.<locals>.<lambda>皙�����?)�timeoutzProcess was interrupted.zProcess was terminated.zProcess was killed.z/Error while terminating subprocess (pid=%i): %si����)2rTrn�platformrP�WindowsProactorEventLoopPolicy�new_event_loopr�run_forever�startr�
 107  startswithrC�parserZparse_known_args�create_subprocess_exec�
 108  subprocess�PIPE�OSError�errno�ENOENT�print�endswith�encode�bgr8�append�_gc_bg_processesrmr	rlr4rdrpro�call_soon_threadsafe�proc�KeyboardInterrupt�send_signal�signal�SIGINT�wait_forrq�
 109  returncoderr�	terminate�kill�	Exception�pidr)r1rDrAZasyncio_threadrUrs�argvr-r^Z
 110  proc_proxy�rcr)rhr_rrArTryr1rzrrC�s�
 111  
 112  
 113  ����
 114  �
 115  ����!
 116  �zScriptMagics.shebangruc�sj�|j�|�|j��IdH|j��|j��IdH|��IdH|D]	}|��IdHq%|��dS)z1callback for running the script in the backgroundN)rire�drainrj�wait_closedrqrWr�)r1ryrArz�srrrrxTs�
 117  zScriptMagics._run_script�
 118  killbgscripts�cCs|��td�dS)z9Kill all BG processes started by %%script and its family.z%All background processes were killed.N)r;r�)r1Z_nouse_rrrr�bszScriptMagics.killbgscriptscCs�|jsdS|jD]}|jdurz|�tj�WqYqqt�d�|��|js,dS|jD]}|jdurBz|��Wq/Yq/q/t�d�|��|jsQdS|jD]}|jdurgz|�	�WqTYqTqT|��dS)z.Kill all BG processes which are still running.Nr|)
 119  r8r�r�r�r��time�sleepr�r�r�)r1ryrrrr;hs@
 120  
 121  �
 122  
 123  
 124  �
 125  
 126  
 127  �zScriptMagics.kill_bg_processescCsdd�|jD�|_dS)NcSsg|]	}|jdur|�qSr>)r�)�.0ryrrr�
 128  <listcomp>�sz1ScriptMagics._gc_bg_processes.<locals>.<listcomp>)r8r?rrrr��szScriptMagics._gc_bg_processesr>)r�)rrr rLrrTr�tagr#�__annotations__rr3rrHr6r@r7rBrrrrCr.r/Z__skip_doctest__rxrr�r;r��
 129  __classcell__rrr<rr!Js@
 130  �
 131  ��
 132  
 133  ��$ r!)!rLrPZasyncio.exceptionsr9r�r.r�rnr�r�r�	threadingrZ	traitletsrrrrZIPython.corerZIPython.core.async_helpersr	ZIPython.core.magicr
 134  rrr
 135  ZIPython.utils.processrrr�rr!rrrr�<module>s(*