a
    n^e6                     @   s   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlZddlmZ ejejeZG dd dZG dd	 d	eZG d
d deZdS )zThis module contains the base-class Package, and the derived classes
ConfigurePackage and MesonPackage, which implement the build commands
for these two systems.
    N)CallableDictListOptionalTextIO   )Configc                   @   s  e Zd ZdZeedddZeedddZ	eeddd	Z
eedd
dZeedddZeedddZeedddZeedddZeedddZeedddZeed  dddZedddZeeeegef dddZedd d!Zeedd"d#Zeee dd$d%Zeee dd&d'Zeeeef dd(d)Zeee eeed*d+d,Zeedd-d.Z eee dd/d0Z!eed1d2d3Z"d4S )5PackagezBase class for all packages.creturnc                 C   s   dS )z>Return whether this package is enabled for this configuration.T selfr   r   r   A/home/jjbenham/public_html/lilypond/release/binaries/lib/build.pyenabled,   s    zPackage.enabledr   c                 C   s   t dS )z"Return the version of this packageNNotImplementedErrorr   r   r   r   version1   s    zPackage.versionc                 C   s   t dS )znReturn the directory name for this package

        This usually includes the version of the package.
        Nr   r   r   r   r   	directory6   s    zPackage.directoryc                 C   s   t j|j| jS )z,Return the source directory for this package)ospathjoindependencies_src_dirr   r   r   r   r   src_directory>   s    zPackage.src_directoryc                 C   s   t j|j| jS )z+Return the build directory for this package)r   r   r   dependencies_build_dirr   r   r   r   r   build_directoryB   s    zPackage.build_directoryc                 C   s   t j|j| jS )z7Return the temporary install directory for this package)r   r   r   dependencies_install_dirr   r   r   r   r   install_directoryF   s    zPackage.install_directoryc                 C   s   t j|j| j dS )z0Return the path to the log file for this packagez.log)r   r   r   dependencies_log_dirr   r   r   r   r   log_pathJ   s    zPackage.log_pathc                 C   s   t dS )z|Return the archive file name of this package

        This is usually the directory name plus an archive extension.
        Nr   r   r   r   r   archiveN   s    zPackage.archivec                 C   s   t j|j| jS )zuReturn the full path to the archive file

        By default, the file is stored in the downloads directory.
        )r   r   r   downloads_dirr#   r   r   r   r   archive_pathV   s    zPackage.archive_pathc                 C   s   t dS )zlReturn the download URL for this package

        The build process expects an extractable archive.
        Nr   r   r   r   r   download_url]   s    zPackage.download_urlc                 C   s   g S )z(Return the dependencies for this packager   r   r   r   r   dependenciese   s    zPackage.dependenciesr   c                 C   sH   |  |}tj|r&td| j ntd| j t	j
| j| dS )zDownload the package's archive.z'%s' already downloadedzDownloading '%s'...N)r%   r   r   existsloggingdebugr#   infor&   urllibZrequestZurlretrieve)r   r   destr   r   r   downloadj   s
    
zPackage.download)r   filenamepatchc                 C   s   t j| ||}t|ddd}| }W d   n1 s@0    Y  ||}t|ddd}|| W d   n1 s0    Y  dS )z2Patch the content of a file with the given lambda.rutf-8encodingNw)r   r   r   r   openreadwrite)r   r   r0   r1   oldcontentnewr   r   r   
patch_files   s    &zPackage.patch_filec                 C   s   dS )z$Apply additional patches (if needed)Nr   r   r   r   r   apply_patches|   s    zPackage.apply_patchesc                 C   s|   |  |}tj|r(td| j dS | |}tj|sPtd| j dS tj	|  |}t
|| | | dS )z=Extract the archive and apply additional patches (if needed).z'%s' already extractedTz'%s' does not exist!F)r   r   r   r)   r*   r+   r#   r%   errordirnameshutilunpack_archiver>   )r   r   r   r#   extract_dirr   r   r   prepare_sources   s    


zPackage.prepare_sourcesc                 C   sP   t j| |dd}t j|r&|S t j| |dd}t j|rL|S dS )z'Return the pkgconfig path for this pathlib	pkgconfigZlibdataN)r   r   r   r    isdir)r   r   Zlib_pkgconfigZlibdata_pkgconfigr   r   r   pkgconfig_path   s    zPackage.pkgconfig_pathc                 C   sB   g }|  |D ]}|||7 }q| |}|dur>|| |S )z1Collect the pkgconfig paths for all dependencies.N)r'   collect_pkgconfig_pathsrH   append)r   r   Z	all_pathsZ
dependencyr   r   r   r   rI      s    

zPackage.collect_pkgconfig_pathsc                 C   s0   i }t jtd|d< t j| ||d< |S )z=Return additional environment mappings to build this package.zpkg-config-static.shZ
PKG_CONFIGZPKG_CONFIG_LIBDIR)r   r   r   lib_pathpathseprI   )r   r   envr   r   r   build_env_extra   s    zPackage.build_env_extra)r   argslogstager   c           
      C   s   d |}d| d}d dd | | D }d| d| | d| }t| ||d	  |  tj	
 }|| | tj|||| ||d
d}	|d |	jdkrtd||	j d
S dS )z6Run command in the build directory and log invocation.z' ''z
  c                 S   s   g | ]\}}| d | qS )=r   ).0keyvalr   r   r   
<listcomp>       z'Package.run_command.<locals>.<listcomp>zRunning command
  z
in directory
  'z+'
with additional environmental settings
  z!
*** Begin of command output ***
F)stdoutstderrcwdrM   checkz*** End of command output ***

r   z%s exited with code %dT)r   rN   itemsr   r*   r+   r9   flushr   environcopyupdate
subprocessrun
returncoder?   )
r   r   rO   rP   rQ   Zformatted_argsZformatted_envZ	log_entryZ	build_envresultr   r   r   run_command   s>    


	

zPackage.run_commandc                 C   s   t dS )z9Build the package and install it to a temporary location.Nr   r   r   r   r   build   s    zPackage.buildc                 C   s   g S )z1Return the list of license files for this packager   r   r   r   r   license_files   s    zPackage.license_files)destinationr   c                 C   sV   | j D ]J}tj| ||}tj|}tj|| j d| }t|| qdS )z7Copy the license files for this package to destination..N)	rh   r   r   r   r   basenamer   rA   r`   )r   ri   r   Zlicense_filesrcrk   dstr   r   r   copy_license_files   s
    
zPackage.copy_license_filesN)#__name__
__module____qualname____doc__r   boolr   propertystrr   r   r   r   r    r"   r#   r%   r&   r   r'   r/   r   r=   r>   rD   r   rH   rI   r   rN   r   rf   rg   rh   rn   r   r   r   r   r	   '   s8   		*r	   c                   @   s   e Zd ZdZeedddZeee dddZ	eee ddd	Z
eee dd
dZeee dddZeee dddZeedddZdS )ConfigurePackagez=A package that has a configure script and is built with make.r   c                 C   s   dS )z0Return the relative path to the configure script	configurer   r   r   r   r   configure_script   s    z!ConfigurePackage.configure_scriptr
   c                 C   s   d|j j d|j gS )z?Return the parameters to configure the package for the platformz--build=z--host=)native_configtripler   r   r   r   configure_args_triples   s    
z'ConfigurePackage.configure_args_triplesc                 C   s   ddgS )JReturn the parameters to configure the package for a static build (if any)z--disable-sharedz--enable-staticr   r   r   r   r   configure_args_static  s    z&ConfigurePackage.configure_args_staticc                 C   s   g S )a4  Return additional parameters to pass to the configure script

        The build process automatically adds options for the build and host
        triples (see configure_args_triples), a static library build (see
        configure_args_static), as well as the install prefix to the temporary
        location.r   r   r   r   r   configure_args  s    zConfigurePackage.configure_argsc                 C   s   g S )z;Return additional parameters to pass to the make invocationr   r   r   r   r   	make_args  s    zConfigurePackage.make_argsc                 C   s   g S )z4Return additional parameters to pass to make installr   r   r   r   r   make_install_args  s    z"ConfigurePackage.make_install_argsc                 C   sD  |  |}| |}| |}tj|dd t| |ddd}| d| j g}|| |7 }|| 	|7 }|d| g7 }|| 
|7 }| |||dsW d    d	S |jd
t|jg| | }| |||dsW d    d	S |jdg| | }| |||ds W d    d	S W d    n1 s60    Y  dS )NTexist_okr6   r3   r4   /	--prefix=rw   F-jmakeinstall)r   r   r    r   makedirsr7   r"   rx   r{   r}   r~   rf   make_commandru   jobsr   r   )r   r   r   r   r    rP   rO   r   r   r   rg     s&    


0zConfigurePackage.buildN)ro   rp   rq   rr   rt   ru   rx   r   r   r{   r}   r~   r   r   rs   rg   r   r   r   r   rv      s   	
rv   c                   @   sF   e Zd ZdZeee dddZeee dddZeddd	Z	d
S )MesonPackagez=A package that is configured with meson and built with ninja.r
   c                 C   s   dgS )r|   z--default-library=staticr   r   r   r   r   meson_args_staticF  s    zMesonPackage.meson_args_staticc                 C   s   g S )zReturn additional parameters to pass to 'meson setup'

        The build process automatically adds options for optimizations, a static
        library build (see meson_args_static) and the install prefix to the
        temporary location.r   r   r   r   r   
meson_argsK  s    zMesonPackage.meson_argsr(   c                 C   sT  |  |}| |}| |}tj|dd t| |ddd}g d}| rptj	t
d}|d| g7 }|| |7 }|d	d
| g7 }|dg7 }|| |7 }|||g7 }| |||dsW d    dS ddt|jg}| |||dsW d    dS ddg}| |||ds0W d    dS W d    n1 sF0    Y  dS )NTr   r6   r3   r4   )ZmesonZsetupz--buildtype=releasezmingw_cross.txtz--cross-file=z--libdir=libr   z--auto-features=disabledrw   FZninjar   rg   r   )r   r   r    r   r   r7   r"   is_mingwr   r   rK   r   r   rf   ru   r   )r   r   r   r   r    rP   rO   Zmingw_crossr   r   r   rg   T  s.    



0zMesonPackage.buildN)
ro   rp   rq   rr   r   r   ru   r   r   rg   r   r   r   r   r   C  s   	r   )rr   r*   r   rA   rb   typingr   r   r   r   r   Zurllib.requestr-   configr   r   r@   realpath__file__rK   r	   rv   r   r   r   r   r   <module>   s   	 LQ