3
Y]                 @   s<   d Z ddlmZ dddZdd Zdd	 Zed
kr8e  dS )zRun Python code using ``mpi4py``.

Run Python code (scripts, modules, zip files) using the ``runpy``
module. In case of an unhandled exception, abort execution of the MPI
program by calling ``MPI.COMM_WORLD.Abort()``.
    )print_functionNc                s  ddl  ddlm}m} d fdd	}| dk	r2| n jdd  jdd<  jd d	krt jj }||d
dd	d n jd dkr jjd}||d
ddd nv jd dkr̈ jd= | jd d
dd nJddlm	}m
} t jdds|| jd  jd< | jd d
d dS )ag  Run command line ``[pyfile | -m mod | -c cmd | -] [arg] ...``.

    * ``pyfile`` : program read from script file
    * ``-m mod`` : run library module as a script
    * ``-c cmd`` : program passed in as a command string
    * ``-``      : program read from standard input (``sys.stdin``)
    * ``arg ...``: arguments passed to program in ``sys.argv[1:]``
    r   N)
run_modulerun_path<string>-cc                sB   ddl m}  jd	krdnd}t| |ddd}||||f||iS )
Nr   )_run_module_code      script_nameZ	mod_fnameexec   )r   r	   )runpyr   version_infocompile)stringinit_globalsrun_namefilenameargv0r   Zkargcode)sys )lib/python3.6/site-packages/mpi4py/run.py
run_string   s    z$run_command_line.<locals>.run_stringr   -__main__z<stdin>)r   r   r   z-mT)r   	alter_sys)realpathdirnameisolated)r   )NNr   r   )r   r   r   r   argvstdinreadpopos.pathr   r   getattrflagspath)argsr   r   r   cmdr   r   r   )r   r   run_command_line   s$    
 $
r*   c             C   sf   ddl }t| tr| n| dkr"dnd} tp6tjdd }|jj|d }|dk	r^| r^|j|  |j	 S )a  Terminate MPI execution environment at Python exit.

    Terminate MPI execution environment at Python exit by calling
    ``MPI.COMM_WORLD.Abort(status)``. This function should be called
    within an ``except`` block. Afterwards, exceptions should be
    re-raised.
    r   Nr   .z.MPI)
r   
isinstanceint__package____name__
rpartitionmodulesgetZ_set_abort_statusexc_info)Zstatusr   pkgZmpir   r   r   set_abort_status2   s    
r5   c                 s   ddl  ddlfddd fdd	dfdd	} d	d
 }|  \}}|| yt| W nB tk
r } zt|j  W Y dd}~X n   td  Y nX dS )z-Entry-point for ``python -m mpi4py.run ...``.r   Nc                 s*   ddl m}  tt|  jd  jd d S )Nr   )__version__)filer   ) r6   printr.   stdoutexit)r6   )r   r   r   versionK   s    zmain.<locals>.versionc                s   ddl m} tdkrtd }nt} jjj}t||d}|dj j	f |}|dj j	f |}|dj }| rt
| jd	 t
|jd	 t
|jd	 jd
 n&t
|jd	 t
|jd	 jd d S )Nr   )dedentr   z.run)progpythonz
        usage: {python} -m {prog} [options] <pyfile> [arg] ...
           or: {python} -m {prog} [options] -m <mod> [arg] ...
           or: {python} -m {prog} [options] -c <cmd> [arg] ...
           or: {python} -m {prog} [options] - [arg] ...
        zC
        Try `{python} -m {prog} -h` for more information.
        an  
        options:
          --version            show version number and exit
          -h|--help            show this help message and exit
          -rc <key=value,...>  set 'mpi4py.rc.key=value'
          -p|--profile <pmpi>  use <pmpi> for profiling
          --mpe                profile with MPE
          --vt                 profile with VampirTrace
        )r7   r   )textwrapr=   r/   r.   r'   basename
executabledictstripformatr9   stderrr;   r:   )Zerrmessr=   Z	prog_nameZ
python_exeZsubsZcmdlineZhelptipoptions)osr   r   r   usageP   s(    

zmain.<locals>.usagec                sl  G dd dt }fdd}| }| d kr8 jdd  n
| d d  } x| oX| d jdr| d dkrjP | d dkr|  | d dkr  yd| d }|jdrd|k r|jd}|d| ||d d   }}|dkr||g }| dd< n|dd  }|dkrx|| jdD ]}|jd}|d | j }	||d d  j }
|	 s`|
 rht|yt|
i |j|	< W n  t	k
r   |
|j|	< Y nX qW nF|d kr|| pd |_
n*|d!kr|dd  |_
nd| d   | d= W qH tk
r   d| d   Y qHX qHW t| dk r8d n,| d d"krdt| dk rdd| d   || fS )#Nc               @   s   e Zd Zi ZdZdS )z1main.<locals>.parse_command_line.<locals>.OptionsN)r/   
__module____qualname__rc_argsprofiler   r   r   r   Options{   s   rN   c                s4   t | dk s| d jdr* d| d   | jdS )N   r   r   zArgument expected for option: r   )len
startswithr#   )r(   )rI   r   r   poparg   s    z0main.<locals>.parse_command_line.<locals>.popargr   r   r   -m-c-h-help--help-version	--versionz--=-rc-profile,-p-mpe-vtzUnknown option: zCannot parse option: zNo path specified for executionrO   zArgument expected for option: )rS   rT   r   )rU   rV   rW   )rX   rY   )r[   r\   )r^   r\   )r_   r`   )rS   rT   )objectr    rQ   indexsplitrD   
ValueErrorevalrL   	NameErrorrM   	ExceptionrP   )r(   rN   rR   rG   Zarg0iZoptargentrykeyval)r   rI   r<   r   r   parse_command_linex   sX    "








z main.<locals>.parse_command_linec             S   s>   | j rddlm} |f | j  | jr:ddlm} || j d S )Nr   )rc)rM   )rL   r8   rn   rM   )rG   rn   rM   r   r   r   	bootstrap   s    zmain.<locals>.bootstrapr   )N)N)rH   r   r*   
SystemExitr5   r   )rm   ro   rG   r(   excr   )rH   r   rI   r<   r   mainE   s     (=	

rr   r   )N)__doc__Z
__future__r   r*   r5   rr   r/   r   r   r   r   <module>   s   
& 	