
                         T    S r SSKJr  SrSSKrSSKrSSKrS rS rS r	S r
S	 rS
 rg)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                 
   [         R                  S   R                  S5       HP  n[         R                  R	                  X5      n[         R
                  " U[         R                  5      (       d  MN  Us  $    [        SU -  5      e)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   ?   sg     jj &&s+dww||D+H	yy277##o , 	BXM	O O    c                     U R                  S5      n[        U5      S:  aO  SR                  USS S/-   USS -   S/-   5      n[        R                  " U[        R
                  5      (       a  U/$ S	S
/$ )a9  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   s}     ||C *_xx
1R(E72"23(+3*5 6H	yy277##Z r   c                 @    U S   U:X  a  U S   U:X  d   eU SS S4$ U S4$ )Nr   r      TF )sbegends      r   StripDelimitersr'   j   s7    qTS[R5C<<aGT?u:r   c                 P    [        U SS5      u  pU(       d  [        U SS5      u  pU $ )N"')r'   )r$   strippeds     r   StripQuotesr,   r   s*    !!S#.-1	#AsC0MQ	
(r   c                      [        S[        R                  S   -  5        [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        g	)
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                 P   [         R                  SS nSnSnSnSnU(       a  US   S:X  a   [        5         [         R                  " S5        US   S:X  a	  SnUSS nMB  US   S	:X  a	  SnUSS nMT  US   S
:X  a	  SnUSS nMf  [        R
                  " SUS   5      nUb!  [        UR                  S5      5      nUSS nM   U R                  n[        R                  R                  U5      (       d
   SU-  5       e[        R                  " U[        R                  5      (       d
   SU-  5       eU/U-   nU(       a  UR                  5       nUS   n	[        R                  R                  U	5      (       d  [!        U	5      n	[         R"                  n
U
/nU(       a  UR%                  ['        U
5      5        U	/USS -   U-   U-   nOLU(       a!  [         R"                  /['        W	5      -   U-   nO$[         R"                  n	[         R"                  /U-   nU(       a0  [)        SU	-  5        [)        SU5        [         R                  " S5         [         R*                  R-                  5         [        R.                  " X5        g! [0         a  n[3        USS5      (       d  Xl        e SnAff = f)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      s}    
!"$,(,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HHW	 
1j$''j	
s   4I= =
J%J  J%)__doc__
__future__r   
__author__r   r5   r/   r   r    r'   r,   r1   rO   r#   r   r   <module>rS      s>    %N &3
 	 	 
O(.-"P
r   