
                         T    d Z ddlmZ dZddlZddlZddlZd Zd Zd Z	d Z
d	 Zd
 Zy)a  Script for running Google-style applications.

Unlike normal scripts run through setuptools console_script entry points,
Google-style applications must be run as top-level scripts.

Given an already-imported module, users can use the RunScriptModule function to
set up the appropriate executable environment to spawn a new Python process to
run the module as a script.

To use this technique in your project, first create a module called e.g.
stubs.py with contents like:

  from google.apputils import run_script_module

  def RunMyScript():
    import my.script
    run_script_module.RunScriptModule(my.script)

  def RunMyOtherScript():
    import my.other_script
    run_script_module.RunScriptModule(my.other_script)

Then, set up entry points in your setup.py that point to the functions in your
stubs module:

  setup(
      ...
      entry_points = {
          'console_scripts': [
              'my_script = my.stubs:RunMyScript',
              'my_other_script = my.stubs.RunMyOtherScript',
              ],
          },
      )

When your project is installed, setuptools will generate minimal wrapper scripts
to call your stub functions, which in turn execv your script modules. That's it!
    )print_functionz$dborowitz@google.com (Dave Borowitz)Nc                     t         j                  d   j                  d      D ]J  }t         j                  j	                  ||       }t        j
                  |t         j                        sH|c S  t        d| z        )zFind the program in the system path.

  Args:
    progname: The name of the program.

  Returns:
    The full pathname of the program.

  Raises:
    AssertionError: if the program was not found.
  PATH:z:Could not find an executable named '%s' in the system path)osenvironsplitpathjoinaccessX_OKAssertionError)prognamer
   fullnames      <platform/bq/third_party/google/apputils/run_script_module.pyFindEnvr   ?   sf     jj &&s+dww||D(+H	yy277#o , 	BXM	O O    c                     | j                  d      }t        |      dk\  rIdj                  |dd dgz   |dd z   dgz         }t        j                  |t        j
                        r|gS d	d
gS )aG  Try to get the path to pdb.py and return it in a list.

  Args:
    python: The full path to a Python executable.

  Returns:
    A list of strings. If a relevant pdb.py was found, this will be
    ['/path/to/pdb.py']; if not, return ['-m', 'pdb'] and hope for the best.
    (This latter technique will fail for Python 2.2.)
  /   r   libNzpdb.pyz-mpdb)r	   lenr   r   r   R_OK)python
componentspdb_paths      r   
GetPdbArgsr    S   sz     ||C *_xx
1R(E72"23(+3*5 6H	yy277#Z r   c                 <    | d   |k(  r| d   |k(  sJ | dd dfS | dfS )Nr   r      TF )sbegends      r   StripDelimitersr'   j   s7    qTS[R5C<<aGT?u:r   c                 J    t        | dd      \  } }|st        | dd      \  } }| S )N"')r'   )r$   strippeds     r   StripQuotesr,   r   s.    !!S#.-1h	#AsC0MQ	
(r   c                      t        dt        j                  d   z         t        d       t        d       t        d       t        d       t        d       t        d       y	)
z Print usage for the stub script.z)Stub script %s (auto-generated). Options:r   z3--helpstub               Show help for stub script.zK--debug_binary           Run python under debugger specified by --debugger.zL--debugger=<debugger>    Debugger for --debug_binary. Default: 'gdb --args'.zN--debug_script           Run wrapped script with python debugger module (pdb).zH--show_command_and_exit  Print command which would be executed and exit.zeThese options must appear first in the command line, all others will be passed to the wrapped script.N)printsysargvr#   r   r   PrintOurUsager1   y   si    3chhqkAB 
& ' 
> ? 
? @ 
A B 
; < 
, -r   c                    t         j                  dd }d}d}d}d}|r|d   dk(  rt                t        j                  d       |d   dk(  rd}|dd }9|d   d	k(  rd}|dd }I|d   d
k(  rd}|dd }Yt	        j
                  d|d         }| t        |j                  d            }|dd }	 | j                  }t        j                  j                  |      s
J d|z         t        j                  |t        j                        s
J d|z         |g|z   }|r~|j                         }|d   }	t        j                  j                  |	      st!        |	      }	t         j"                  }
|
g}|r|j%                  t'        |
             |	g|dd z   |z   |z   }nG|r!t         j"                  gt'        	      z   |z   }n$t         j"                  }	t         j"                  g|z   }|r/t)        d|	z         t)        d|       t        j                  d       	 t         j*                  j-                          t        j.                  |	|       y# t0        $ r}t3        |dd      s|	|_         d}~ww xY w)zRun a module as a script.

  Locates the module's file and runs it in the current interpreter, or
  optionally a debugger.

  Args:
    module: The module object to run.
  r"   NFz
gdb --argsr   z
--helpstubz--debug_binaryTz--debug_scriptz--show_command_and_exitz--debugger=(.+)z!Cannot exec() %r: file not found.z$Cannot exec() %r: file not readable.zprogram: "%s"zargs:filename)r/   r0   r1   exitrematchr,   group__file__r   r
   existsr   r   r	   isabsr   
executableextendr    r.   stdoutflushexecvEnvironmentErrorgetattrr3   )moduleargsdebug_binarydebuggerdebug_scriptshow_command_and_exitmatchobjmain_filenamedebugger_argsprogrampython_pathcommand_veces                r   RunScriptModulerO      su    
!"$,(,Aw,o	hhqkAw""l!"XdAw""l!"XdAw++"!"Xdxx)473HX^^A./h!"Xd	
 //-		& 8)L)6*7 8	&	="''	* K .:<I.J K	* 4	$NN$MAG77==! g..K-KK019}QR((;6=DNNj11D8D nnGNNd"D	/G
#$	'4HHQK
JJHHWd	 
1j$'aj	
s   4I 	I+I&&I+)__doc__
__future__r   
__author__r   r5   r/   r   r    r'   r,   r1   rO   r#   r   r   <module>rS      s>    %N &3
 	 	 
O(.-"P
r   