
                         n    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	m
Z
 d
dZd Zdd	Zy)zUtilities for local mode.    )absolute_import)division)unicode_literalsN)filesc                 H   |t         j                  }t        j                  | t        j                  t        j                  t        j
                  dd      5 }|r*|j                  j                  |j                  d             |j                  j                          t        j                  |j                  d      }|D ]#  }|j                  |       |j                          % |j                          	 ddd       |j                  S # 1 sw Y   j                  S xY w)aF  Executes shell commands in subprocess.

  Executes the supplied command with the supplied standard input string, streams
  the output to stdout, and returns the process's return code.

  Args:
    cmd: (List[str]) Strings to send in as the command.
    input_str: (str) if supplied, it will be passed as stdin to the supplied
      command. if None, stdin will get closed immediately.
    file: optional file-like object (stream), the output from the executed
      process's stdout will get sent to this stream. Defaults to sys.stdout.

  Returns:
    return code of the process
  NF   )stdinstdoutstderruniversal_newlinesbufsizezutf-8 )newline)sysr
   
subprocessPopenPIPESTDOUTr	   writeencodecloseioTextIOWrapperflush
returncode)cmd	input_strfilepoutlines         ;lib/googlecloudsdk/command_lib/ai/custom_jobs/local_util.pyExecuteCommandr#      s      
\::D	OO__ ggmmI$$W-.GGMMO


188R
0C
jj
jjl 
 jjl%( 
)( 
s   B DD!c                 \    | j                  dt        j                  j                        dz   S )zConverts the supplied python module into corresponding python file.

  Args:
    module_name: (str) A python module name (separated by dots)

  Returns:
    A string representing a python file path.
  .z.py)replaceospathsep)module_names    r"   ModuleToPathr+   G   s$     
		S"''++	.	66    c                    | xs t        j                         } d}t        j                  |       D ]  }t        j                  j                  | |      }t        j                  j                  |      r|dk(  sHt        j                  |       d}`t        j                  j                  |      \  }}|dv st        j                  |       d} |S )z@Removes generic `__pycache__` folder and  '*.pyc' '*.pyo' files.F__pycache__T)z.pycz.pyo)
r   GetCWDr'   listdirr(   joinisdirRmTreesplitextremove)root_dir
is_cleanednameitem_exts         r"   ClearPyCacher<   S   s    '(*jj"d77<<$'D	ww}}T		T
ww%fa	 	 
		$
 # 
r,   )NN)N)__doc__
__future__r   r   r   r   r'   r   r   googlecloudsdk.core.utilr   r#   r+   r<    r,   r"   <module>rA      s2      &  ' 	 	  
 *'T	7r,   