B
    0ªa„%  ã               @   s  d dl m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	Z	da
dd„ Zdd	„ Ze d
¡Ze d¡Ze d¡Zdd„ Zdd„ Ze d¡Zdd„ Ze d¡Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd+d%d&„Zd,d'd(„Zd)d*„ ZdS )-é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsNc             C   s6   t dkr.tj dd¡ d¡a dt kr.t  d¡ | t kS )a(  Check for whether a specific debugging feature is enabled.
Well-known flags:
 * start: Drop into debugger just after processing arguments
 * exception: Drop into debugger on fatalexception
 * warning: Drop into debugger on warning
 * posttrans: Drop into debugger just before introspectable pass
NZGI_SCANNER_DEBUGÚ ú,)Ú_debugflagsÚosÚenvironÚgetÚsplitÚremove)Úflag© r   ú1/usr/lib/gobject-introspection/giscanner/utils.pyÚhave_debug_flag#   s
    	
r   c             C   s   t | ƒrdd l}| ¡  d S )Nr   )r   ÚpdbZ	set_trace)r   r   r   r   r   Úbreak_on_debug_flag3   s    r   z([^A-Z])([A-Z])z([A-Z][A-Z])([A-Z][0-9a-z])z^([A-Z])([A-Z])c             C   s,   t  d| ¡} t d| ¡} tjd| dd} | S )aJ  Converts a typename to the equivalent underscores name.
    This is used to form the type conversion macros and enum/flag
    name variables.
    In particular, and differently from to_underscores_noprefix(),
    this function treats the first character differently if it is
    uppercase and followed by another uppercase letter.z\1_\2é   )Úcount)Ú_upperstr_pat1ÚsubÚ_upperstr_pat2Ú_upperstr_pat3)Únamer   r   r   Úto_underscores>   s    r   c             C   s   t  d| ¡} t d| ¡} | S )zsLike to_underscores, but designed for "unprefixed" names.
    to_underscores("DBusFoo") => dbus_foo, not d_bus_foo.z\1_\2)r   r   r   )r   r   r   r   Úto_underscores_noprefixK   s    r   zdlname='([A-z0-9\.\-\+]+)'
c          	   C   s>   t | ƒ}| ¡ }W d Q R X t |¡}|r6| ¡ d S d S d S )Nr   )ÚopenÚreadÚ_libtool_patÚsearchÚgroups)Úla_fileÚfÚdataÚmr   r   r   Ú_extract_dlname_fieldV   s    

r&   zlibdir='([^']+)'c          	   C   s>   t | ƒ}| ¡ }W d Q R X t |¡}|r6| ¡ d S d S d S )Nr   )r   r   Ú_libtool_libdir_patr    r!   )r"   r#   r$   r%   r   r   r   Ú_extract_libdir_fieldc   s    

r(   c             C   sX   t | ƒ}|d krd S t ¡ dkrLtj |¡}t| ƒ}|d kr@|S |d | S tj |¡S )NÚDarwinú/)r&   ÚplatformÚsystemr	   ÚpathÚbasenamer(   )r"   ÚdlnameZ
dlbasenameZlibdirr   r   r   Úextract_libtool_shlibo   s    r0   c             C   sL   t | ƒ}|d krtd|  ƒ‚tj tj | ¡d|¡}| dd¡ dd¡}|S )Nz'%s has no dlname. Not a shared library?z.libsz.libs/.libsz.libs\.libs)r&   Ú
ValueErrorr	   r-   ÚjoinÚdirnameÚreplace)r"   r/   Zlibnamer   r   r   Úextract_libtool   s    r5   c          	   C   sx   | j  }|sd S | j}|r$| d¡S d}t ¡ dkr8d}ytj|dgttj	dƒd W n tj
tfk
rp   d S X |gS )Nú Zlibtoolr)   Zglibtoolz	--versionÚw)Ústdout)Z	nolibtoolÚlibtool_pathr   r+   r,   Ú
subprocessZ
check_callr   r	   ÚdevnullZCalledProcessErrorÚOSError)ZoptionsZlibtool_infectionr9   Zlibtool_cmdr   r   r   Úget_libtool_command   s    

r=   c          
   C   sr   t | dƒ^}t |dƒH}| d¡}| d¡}x(||krT|dkrT| d¡}| d¡}q.W ||kS Q R X W d Q R X d S )NÚrbi    ó    )r   r   )Zpath1Zpath2Úf1Úf2Zbuf1Zbuf2r   r   r   Úfiles_are_identicalª   s    


rB   c             C   s&   |   d¡s| S dtj | dd … ¡ S )Nz-Ié   )Ú
startswithr	   r-   Úrealpath)Zcflagr   r   r   Úcflag_real_include_path´   s    
rF   c             C   sª   dd„ }dd„ }t j | ¡\}}|rL|| ƒr0| S t jdkr¦|| ƒr¦| d S nZxXt jd  t j¡D ]B}| d¡}t j || ¡}||ƒrˆ|S t jdkr`||ƒr`|d S q`W d S )	Nc             S   s   t j | ¡ot  | t j¡S )N)r	   r-   ÚisfileÚaccessÚX_OK)Úfpathr   r   r   Úis_exe¼   s    zwhich.<locals>.is_exec             S   s2   |   ¡  d¡ o0tj | d ¡o0t | d tj¡S )Nz.exe)ÚlowerÚendswithr	   r-   rG   rH   rI   )rJ   r   r   r   Ú	is_nt_exe¿   s    zwhich.<locals>.is_nt_exeÚntz.exeÚPATHú")r	   r-   r   r   r
   ÚpathsepÚstripr2   )ZprogramrK   rN   rJ   Zfnamer-   Zexe_filer   r   r   Úwhich»   s     

rT   éÿ  Fc          
   C   sä   t j | ¡\}}|s$t j |¡\}}|rŠ|rŠt j |¡sŠyt|||ƒ W n4 ttfk
r| } z|jtjkrl‚ W dd}~X Y nX |t	krŠdS yt  
| |¡ W nD ttfk
rÞ } z"|rÌ|jtjksÌt j | ¡sÎ‚ W dd}~X Y nX dS )a:  Super-mkdir; create a leaf directory and all intermediate ones.  Works like
    mkdir, except that any intermediate path segment (not just the rightmost)
    will be created if it does not exist. If the target directory already
    exists, raise an OSError if exist_ok is False. Otherwise no exception is
    raised.  This is recursive.

    Note: This function has been imported from Python 3.4 sources and adapted to work
    with Python 2.X because get_user_cache_dir() uses the exist_ok parameter. It can
    be removed again when Python 2.X support is dropped.
    N)r	   r-   r   ÚexistsÚmakedirsÚIOErrorr<   ÚerrnoZEEXISTÚcdirÚmkdirÚisdir)r   ÚmodeÚexist_okÚheadÚtailÚer   r   r   rW   Ö   s     rW   c             C   s¬   t j d¡}|dk	rN| dk	r*t j || ¡}yt|ddd W n   Y nX |S t j d¡}|dk	r¨t j |d¡}| dk	r†t j || ¡}yt|ddd W n
   dS |S dS )zý
    This is a Python reimplemention of `g_get_user_cache_dir()` because we don't want to
    rely on the python-xdg package and we can't depend on GLib via introspection.
    If any changes are made to that function they'll need to be copied here.
    ZXDG_CACHE_HOMENií  T)r]   r^   ú~z.cache)r	   r
   r   r-   r2   rW   Ú
expanduser)ÚdirZxdg_cache_homeZhomedirZcachedirr   r   r   Úget_user_cache_dirô   s(    re   c              C   sF   dd„ t j dd¡ t j¡D ƒ} | sBt jdkrB|  d¡ |  d¡ | S )zÿ
    This is a Python reimplemention of `g_get_system_data_dirs()` because we don't want to
    rely on the python-xdg package and we can't depend on GLib via introspection.
    If any changes are made to that function they'll need to be copied here.
    c             S   s   g | ]}|‘qS r   r   )Ú.0Úxr   r   r   ú
<listcomp>  s    z(get_system_data_dirs.<locals>.<listcomp>ZXDG_DATA_DIRSr   rO   z/usr/local/sharez
/usr/share)r	   r
   r   r   rR   r   Úappend)Zxdg_data_dirsr   r   r   Úget_system_data_dirs  s
     

rj   )rU   F)N) Z
__future__r   r   r   r   rY   Úrer	   r:   r+   r   r   r   Úcompiler   r   r   r   r   r   r&   r'   r(   r0   r5   r=   rB   rF   rT   rW   re   rj   r   r   r   r   Ú<module>   s:   








"