
    eCi                     >   d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ej                            ej                            ej                            e                              Zej                            d          Zej        dk    Zej                            d          Z ej        d	          Zd$dZd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d%dZ+d Z,d Z-d  Z.d%d!Z/d" Z0ej1        Z2d# Z3dS )&zGeneral purpose utility functions.  The code in this file should mostly be
not emscripten-specific, but general purpose enough to be useful in any command
line utility.    N)Path   )diagnosticswindarwinlinuxutilsTc                 t   t           j                                         t           j                                         |                    dd           |                    dd           t          j        | g|R ||d|}|rdnddt          j        |           }t          
                    |           |S )	aB  Runs a subprocess returning the exit code.

  By default this function will raise an exception on failure.  Therefore this should only be
  used if you want to handle such failures.  For most subprocesses, failures are not recoverable
  and should be fatal.  In those cases the `check_call` wrapper should be preferred.
  textTencodingutf-8)checkinputzsuccessfully  z	executed )sysstdoutflushstderr
setdefault
subprocessrunshlexjoinloggerdebug)cmdr   r   argskwret
debug_texts          //b/s/w/ir/x/w/install/emscripten/tools/utils.pyrun_processr"      s     **----
G$$$sBtBB%uBBrBB#5:"B//"B"BEJsOOOT*,,z	*    c                 0   t           r7t          | t          j        d          j        }t          j        |           d S t          j                                         t          j                                         t          j
        | d         |            d S )NF)stdinr   r   )WINDOWSr"   r   r%   
returncodeexitr   r   r   osexecvp)r   rtns     r!   execr,   2   sx     
c%
8
8
8
CCHSMMMMMJJIc!fcr#   c                 *    t          j        | g|R   d S N)r   error)msgr   s     r!   exit_with_errorr1   <   s!    C$r#   c                  @    t          t          t          g| R            S r.   )strr   __rootpath__	pathelemss    r!   path_from_rootr7   @   s     	T,++++	,	,,r#   c                  .    t          t          |            S r.   )find_exer7   r5   s    r!   exe_path_from_rootr:   D   s    	.),	-	--r#   c                 L    t           j                            |           d         S )zReturn the file extensionr   r)   pathsplitextnames    r!   suffixrA   H   s    			$			""r#   c                      t          j        j        |  }t          r.dD ]+}t           j                            ||z             r||z   c S ,|S )N)z.exez.bat)r)   r=   r   r&   isfile)r6   r=   exts      r!   r9   r9   M   s_    	y	!$     	s
	#	# cz 
+r#   c                 n    |d         dk    sJ t           j                            |           d         |z   S )Nr   .r<   )filename
new_suffixs     r!   replace_suffixrI   Z   s7    	A#							(	#	#A	&	33r#   c                 L    t           j                            |           d         S )zqReturn the filename without the extension.

  If there are multiple extensions this strips only the final one.
  r   r<   r?   s    r!   
unsuffixedrK   _   s    
 
		$			""r#   c                 Z    t           j                            t          |                     S r.   )r)   r=   basenamerK   r?   s    r!   unsuffixed_basenamerN   g   s     			*T**	+	++r#   c                     | rBt           j                            |           \  } }|dd                                         s|S | BdS )zParses the essential suffix of a filename, discarding Unix-style version
  numbers in the name. For example for 'libz.so.1.2.8' returns '.so'r   Nr   )r)   r=   r>   isdigit)rG   rA   s     r!   get_file_suffixrQ   k   sX     	 w''11Hf!"": m 	  
r#   c                 V    |                      dd                               dd          S )a  Normalize path separators to UNIX-style forward slashes.

  This can be useful when converting paths to URLs or JS strings,
  or when trying to generate consistent output file contents
  across all platforms.  In most cases UNIX-style separators work
  fine on windows.
  \/z//)replace)r=   s    r!   normalize_pathrV   u   s(     
dC	 	 	(	(s	3	33r#   c                 2    t          j        | d           d S )NT)exist_ok)r)   makedirsdirnames    r!   safe_ensure_dirsr\      s    +g%%%%%%r#   c                     t           j                            |           sJ t          j        t          j        |           j                  }t          j        | |t          j        z             d S r.   )r)   r=   existsstatS_IMODEst_modechmodS_IWUSR)rG   old_modes     r!   make_writablere      sW    		!	!!!	!\"'(++344((8X,-----r#   c                    t                               d| |           t          j                            |           } t          j                            |          }t          j                            |          r=t          j                            |t          j                            |                     }| |k    rd S |t          j        k    rd S t          j
        | |           t          |           d S )Nzcopy: %s -> %s)r   r   r)   r=   abspathisdirr   rM   devnullshutilcopyre   )srcdsts     r!   	safe_copyrn      s    ,,c***
#
#W]]3 3
',,sBG,,S11
2
2CCZZ
FBJ
F+c3 r#   c                     |t           j        k    r#t           j                            |           sJ d S t	          |           }t          | ||           d S )N)line_endings)r)   linesepr=   r^   	read_file
write_file)rG   to_eolr   s      r!   convert_line_endings_in_fileru      sV    rz7>>(#####
F	8		$Xt&111111r#   c                     t          | d          5 }|                                cddd           S # 1 swxY w Y   dS )z$Read from a file opened in text moder   r   Nopenread	file_pathfhs     r!   rr   rr      s    I((( B7799                 s   377c                 ~    t          | d          5 }|                                cddd           S # 1 swxY w Y   dS )z&Read from a file opened in binary moderbNrx   r{   s     r!   read_binaryr      s~    It 7799                 s   266c                     |rK|t           j        k    r;|                    d|          }t          | |                    d                     dS t          | dd          5 }|                    |           ddd           dS # 1 swxY w Y   dS )z#Write to a file opened in text mode
r   wrw   N)r)   rq   rU   write_binaryencodery   write)r|   r   rp   r}   s       r!   rs   rs      s     lbj00<<l++DDKK0011111	iw	/	/	/ 2hhtnnn                 s    BB
Bc                     t          | d          5 }|                    |           ddd           dS # 1 swxY w Y   dS )z%Write to a file opened in binary modewbN)ry   r   )r|   contentsr}   s      r!   r   r      s    It HHX                 s   488c                 p    t           j                            |           rt          j        |            dS dS )zDelete a file (if it exists).N)r)   r=   lexistsremove)rG   s    r!   delete_filer      s8    W__X Ih r#   c                 p    t           j                            |           sdS t          j        |            dS )z"Delete a directory (if it exists).N)r)   r=   r^   rj   rmtreerZ   s    r!   
delete_dirr      s3    		 	  
F-r#   c                 @   t           j                            |           sdS t          j        |           D ]g}|r||v r	t           j                            | |          }t           j                            |          rt          |           Xt          |           hdS )zKDelete the contents of a directory without removing
  the directory itself.N)r)   r=   r^   listdirr   rh   r   r   )r[   excludeentrys      r!   delete_contentsr      s     
	 	  
Fz'""  e 5G##GLL%((E	w}}U % r#   c                  B   t          t          d          rt          j                    } nJt          t          d          r"t          t          j        d                    } nt          j                    } t          t          j                            d|                     S )Nprocess_cpu_countsched_getaffinityr   
EMCC_CORES)	hasattrr)   r   lenr   	cpu_countintenvironget)r   s    r!   get_num_coresr      s}    
 R$%% $&&IIr&'' B(++,,III	RZ^^L)44	5	55r#   c                     t          d          } t          |                                                               d          ad t                              d          d                             d          D             }|\  aaad S )Nzemscripten-version.txt"c                 ,    g | ]}t          |          S  )r   ).0xs     r!   
<listcomp>z'set_version_globals.<locals>.<listcomp>   s    
G
G
Ga3q66
G
G
Gr#   -r   rF   )r7   rr   stripEMSCRIPTEN_VERSIONsplitEMSCRIPTEN_VERSION_MAJOREMSCRIPTEN_VERSION_MINOREMSCRIPTEN_VERSION_TINY)rG   partss     r!   set_version_globalsr      s    455( **002288==
G
G-33C88;AA#FF
G
G
G%PUM46M6M6Mr#   )TNr.   )4__doc__	functoolsloggingr)   r   rj   r_   r   r   pathlibr   r   r   r=   rg   r[   __file__r4   platform
startswithr&   MACOSLINUX	getLoggerr   r"   r,   r1   r7   r:   rA   r9   rI   rK   rN   rQ   rV   r\   re   rn   ru   rr   r   rs   r   r   r   r   r   cachememoizer   r   r#   r!   <module>r      s         				        



            wrwrwx/H/HIIJJ
,
!
!%
(
( ((		7	#	#   (       - - -. . .# # #

 
 
4 4 4
# # #, , ,  4 4 4& & &. . .  "2 2 2                6 6 6 /V V V V Vr#   