a
    nã^e¥L  ã                   @   s
  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m	Z	m
Z
 ddlZddlmZmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZmZ dd	lmZ dd
lm Z m!Z! G dd„ deƒZ"e#dœdd„Z$e#dœdd„Z%G dd„ dƒZ&dS )z+This module defines a package for LilyPond.é    N)ÚDictÚListé   )ÚPackageÚConfigurePackage)ÚConfig)Úall_dependencies)ÚfreetypeÚ
fontconfigÚghostscriptÚgettextÚglibÚguileÚpangoÚlibpngÚcairoÚpythonÚembeddable_python)Ú	all_fonts)ÚtexgyreÚ	urwbase35c                       s¢  e Zd ZU dZeed< 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„ƒZe
ee dœdd„Ze
ee dœdd„Ze
eeef dœ‡ f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œ‡ fd(d)„Zeee dœd*d+„ƒZeee dœd,d-„ƒZedœd.d/„Z ‡  Z!S )0ÚLilyPondz:A package definition to build static binaries of LilyPond.Ú_archive_path)Úarchivec                 C   sŽ   t j |¡}|| _d}t |d¡@}| ¡ }|d u r<tdƒ‚|j}| 	|¡sTtdƒ‚W d   ƒ n1 sh0    Y  |t
|ƒd … | _|| _d S )Nz	lilypond-ÚrzSpecified archive is emptyz'Specified archive has invalid structure)ÚosÚpathÚrealpathr   ÚtarfileÚopenÚnextÚ
ValueErrorÚnameÚ
startswithÚlenÚ_versionÚ
_directory)Úselfr   Zdirectory_prefixÚtarÚinfoÚ	directory© r+   úD/home/jjbenham/public_html/lilypond/release/binaries/lib/lilypond.pyÚ__init__7   s    
&zLilyPond.__init__©Úreturnc                 C   s   | j S ©N)r%   ©r'   r+   r+   r,   ÚversionK   s    zLilyPond.versionc                 C   s   | j S r0   )r&   r1   r+   r+   r,   r*   O   s    zLilyPond.directory)Úcr/   c                 C   s   t j |jd| j¡S )NÚlilypond)r   r   ÚjoinÚbase_dirr*   ©r'   r3   r+   r+   r,   Úsrc_directoryS   s    zLilyPond.src_directoryc                 C   s   t j |jdd¡S )Nr4   Úbuild©r   r   r5   r6   r7   r+   r+   r,   Úbuild_directoryV   s    zLilyPond.build_directoryc                 C   s   t j |jdd¡S )Nr4   Úinstallr:   r7   r+   r+   r,   Úinstall_directoryY   s    zLilyPond.install_directoryc                 C   s   t j |jdd¡S )Nr4   zlilypond.logr:   r7   r+   r+   r,   Úlog_path\   s    zLilyPond.log_pathc                 C   s   t j |  |¡d¡S )z=Return the directory where FlexLexer.h is stored (for mingw).Z	FlexLexer)r   r   r5   r;   r7   r+   r+   r,   Úflexlexer_directory_   s    zLilyPond.flexlexer_directoryc                 C   s   t j | j¡S r0   )r   r   Úbasenamer   r1   r+   r+   r,   r   c   s    zLilyPond.archivec                 C   s   | j S r0   )r   r7   r+   r+   r,   Úarchive_pathg   s    zLilyPond.archive_pathc                 C   s   t ‚d S r0   )ÚNotImplementedErrorr1   r+   r+   r,   Údownload_urlj   s    zLilyPond.download_urlc              	   C   sR   g }|  ¡ s| ¡ s| ¡ r"tg}tg}| ¡ r6tg}|tttt	t
tttg | S r0   )Ú
is_freebsdÚis_macosÚis_mingwr   r   r   r	   r
   r   r   r   r   r   r   )r'   r3   Úgettext_depZ
python_depr+   r+   r,   Údependenciesn   s(    øÿõÿzLilyPond.dependenciesc                 C   s   g S r0   r+   r7   r+   r+   r,   Úconfigure_args_static„   s    zLilyPond.configure_args_staticc                    sh   t ƒ  |¡}t |j¡|d< t |j¡|d< t |j¡|d< | ¡ sT| ¡ sT| 	¡ rd| 
t |¡¡ |S )NZGHOSTSCRIPTZGUILEZPYTHON)ÚsuperÚbuild_env_extrar   Úexe_pathÚnative_configr   r   rD   rE   rF   Úupdater   Úget_env_variables)r'   r3   Úenv©Ú	__class__r+   r,   rK   ˆ   s    zLilyPond.build_env_extrac                 C   sB   g }|  ¡ sdg}g }| ¡ r4|  |¡}d|› g}|dg | S )Nz--enable-static-gxxz--with-flexlexer-dir=z--disable-documentation)rE   rF   r?   )r'   r3   ZstaticZ	flexlexerÚflexlexer_dirr+   r+   r,   Úconfigure_args‘   s    þ
þÿûÿzLilyPond.configure_argsc                 C   s   g }|  ¡ s|ddg7 }|S )NÚallÚbytecode©rF   ©r'   r3   Úargsr+   r+   r,   Ú	make_args§   s    zLilyPond.make_argsc                 C   s   g }|  ¡ s|dg7 }|S )Nzinstall-bytecoderW   rX   r+   r+   r,   Úmake_install_args­   s    
zLilyPond.make_install_argsc                    s8   |  ¡ r,|  |¡}tj|dd t d|¡ tƒ  |¡S )NT)Úexist_okz/usr/include/FlexLexer.h)rF   r?   r   ÚmakedirsÚshutilÚcopyrJ   r9   )r'   r3   rS   rQ   r+   r,   r9   ³   s
    
zLilyPond.buildc                 C   s   g d¢S )z9Return a list of all Python scripts installed by default.)	Zabc2lyz
convert-lyZetf2lyZlilymidizlilypond-bookzlilypond-invoke-editorZlilysongZmidi2lyZmusicxml2lyr+   r1   r+   r+   r,   Úpython_scripts½   s    zLilyPond.python_scriptsc                 C   s   dgS )NÚCOPYINGr+   r1   r+   r+   r,   Úlicense_filesÌ   s    zLilyPond.license_filesc                 C   s   d| j › S )Nz	LilyPond )r2   r1   r+   r+   r,   Ú__str__Ð   s    zLilyPond.__str__)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r-   Úpropertyr2   r*   r   r8   r;   r=   r>   r?   r   rA   rC   r   r   rH   rI   r   rK   rT   rZ   r[   Úboolr9   r`   rb   rc   Ú__classcell__r+   r+   rQ   r,   r   2   s:   
	
r   )Úbinaryc                 C   s   d| g}t j|dd dS )z=Strip debugging symbols from an executable or shared library.ÚstripT)ÚcheckN)Ú
subprocessÚrun)rm   rY   r+   r+   r,   rn   Ô   s    rn   )r   c                 C   s2   t  | ¡j}|tjtjB tjB O }t  | |¡ dS )zMake a file executable.N)r   ÚstatÚst_modeÚS_IXUSRÚS_IXGRPÚS_IXOTHÚchmod)r   Úmoder+   r+   r,   Úchmod_xÚ   s    ry   c                   @   s$  e Zd ZU dZeed< eed< 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	dœdd„Ze	e	dœdd„Ze	dœdd„Ze	dœd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'„ Zd(d)„ Ze	e	e	d*œd+d,„Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7S )8ÚLilyPondPackagerzIA class to package the built binaries of LilyPond and other needed files.r4   r3   ©r4   r3   c                 C   s   || _ || _d S r0   r{   )r'   r4   r3   r+   r+   r,   r-   ç   s    zLilyPondPackager.__init__r.   c                 C   s   t j | jjd¡S )z=Return the temporary directory to stage the files to package.Úpackage)r   r   r5   r3   r6   r1   r+   r+   r,   Úpackage_dirë   s    zLilyPondPackager.package_dirc                 C   s   t j | jd¡S )z:Return the path to bin in the temporary package directory.Úbin©r   r   r5   r}   r1   r+   r+   r,   Úbin_dirð   s    zLilyPondPackager.bin_dirc                 C   s   t j | jd¡S )zàReturn the path to libexec in the temporary package directory.

        Auxiliary binaries are put here instead of bin/ so they do not end up
        replacing the system-provided versions if bin/ is added to $PATH.
        Zlibexecr   r1   r+   r+   r,   Úlibexec_dirõ   s    zLilyPondPackager.libexec_dir)Úsrcc                 G   s:   t jj|g|¢R Ž }t jj| jg|¢R Ž }t ||¡ d S r0   )r   r   r5   r}   r^   Úcopytree)r'   r‚   ÚpathsÚdstr+   r+   r,   Ú_copy_recursiveþ   s    z LilyPondPackager._copy_recursive)r‚   Údst_dirc                 C   s.   t j |t j |¡¡}t ||¡ t|ƒ d S r0   )r   r   r5   r@   r^   r_   rn   )r'   r‚   r‡   r…   r+   r+   r,   Ú_copy_to_dir_and_strip  s    z'LilyPondPackager._copy_to_dir_and_stripc                 C   s   |   || j¡ d S r0   )rˆ   r   ©r'   r‚   r+   r+   r,   Ú_copy_to_libexec_and_strip  s    z+LilyPondPackager._copy_to_libexec_and_stripc                 C   s   |   || j¡ d S r0   )rˆ   r€   r‰   r+   r+   r,   Ú_copy_to_bin_and_strip  s    z'LilyPondPackager._copy_to_bin_and_stripc                 C   s2   t  | j¡}|  |dd¡ |  |ddt jd¡ d S )NÚsharer   ÚlibZccache)r   r=   r3   r†   Úmajor_version)r'   Zguile_installr+   r+   r,   Ú_copy_guile_files  s
    ÿz"LilyPondPackager._copy_guile_filesc                 C   s   t  | j¡}|  |dd¡ d S )NÚetcÚfonts)r
   r=   r3   r†   )r'   Zfontconfig_installr+   r+   r,   Ú_copy_fontconfig_files  s    z'LilyPondPackager._copy_fontconfig_filesc           
      C   s(  t j | jdd¡}t j || jj¡}t j |dd¡}t | j¡}dD ]H}dD ]>}d|› d|› d	}t j ||¡}t j ||¡}t	 
||¡ qLqDt | j¡}	d
D ]8}d|› d	}t j |	|¡}t j ||¡}t	 
||¡ qždD ]F}dD ]<}|› d|› d	}t j |	|¡}t j ||¡}t	 
||¡ qäqÜd S )NrŒ   r4   r‘   Úotf)ZcursorZherosZschola)ZregularZboldZ
bolditalicZitalicr   ú-z.otf)ZRomanZBdItaÚBoldÚItaliczC059-)ZNimbusMonoPSZ
NimbusSans)ZRegularr•   Z
BoldItalicr–   )r   r   r5   r}   r4   r2   r   r=   r3   r^   r_   r   )
r'   Zshare_lilypondZ	fonts_otfZtexgyre_installÚfamilyÚstyler“   r‚   r…   Zurwbase35_installr+   r+   r,   Ú_copy_font_files  s,    z!LilyPondPackager._copy_font_filesc                 C   sL   t j t j t j t¡¡¡}t j |d¡}t j | jdd¡}t ||¡ d S )NZrelocater   )	r   r   Údirnamer   Ú__file__r5   r}   r^   rƒ   )r'   Z	root_pathZrelocate_srcZrelocate_dstr+   r+   r,   Ú_copy_relocation_files6  s    z'LilyPondPackager._copy_relocation_filesc                 C   sx   | j  | jj¡}tj |dd¡}tj | jdd¡}t 	||¡ t 
|¡D ]*\}}}|D ]}t tj ||¡d ¡ qVqHd S )Nr   r4   )r4   r=   r3   rM   r   r   r5   r}   r^   rƒ   ÚwalkÚutime)r'   Úlilypond_installr‚   r…   ÚrootÚ_Úfilesr"   r+   r+   r,   Ú_copy_native_bytecode=  s    z&LilyPondPackager._copy_native_bytecodec                 C   sx   |   ¡  t | j¡}tj |dd¡}|  |¡ t | j¡}dD ]}|  tj |d|¡¡ q>tj |dd¡}|  |¡ d S )Nr~   zlibintl-8.dll)zlibgio-2.0-0.dllzlibglib-2.0-0.dllzlibgmodule-2.0-0.dllzlibgobject-2.0-0.dllzgspawn-win64-helper-console.exe)	r£   r   r=   r3   r   r   r5   r‹   r   )r'   Úgettext_installZlibintl_dllZglib_installr   Zgspawnr+   r+   r,   Ú_copy_mingw_filesJ  s    
z"LilyPondPackager._copy_mingw_filesc                 C   s8   t  | j¡}t |¡D ]}t tj ||¡| j	¡ qd S r0   )
r   r=   r3   r   Úlistdirr^   r_   r   r5   r€   )r'   Úpython_installZpython_filer+   r+   r,   Ú_copy_mingw_python_  s    z#LilyPondPackager._copy_mingw_pythonc                 C   sD   | j jD ]6}tj | j|¡}tj | j|› d¡}t ||¡ qd S )Nz.py)r4   r`   r   r   r5   r€   Úrename)r'   Úscriptr‚   r…   r+   r+   r,   Ú_move_scriptsd  s    zLilyPondPackager._move_scriptsc                 C   s    t  | j¡}t j}|  |d|¡ tj | jd|¡}tj |dd¡}t	j	|ddD ]}t
 |¡ qPt
 t	 	tj |d¡¡d ¡ dD ]}t
 tj ||¡¡ q‚d S )	Nr   z**ÚtestT)Ú	recursivezconfig-*r   )Z	distutilsZidlelibZlib2to3)r   r=   r3   Úpython_with_major_versionr†   r   r   r5   r}   Úglobr^   Úrmtree)r'   r§   Zpython_libdirZ
tests_pathr*   r+   r+   r,   Ú_copy_python_filesj  s    z#LilyPondPackager._copy_python_files)rª   ÚshebangÚwrapper_templatec                 C   sô   t j | j|¡}t j | j|¡}t|ddd}| ¡ }W d   ƒ n1 sL0    Y  |d g|dd …  }t|ddd}	|	 |¡ W d   ƒ n1 sš0    Y  t|ddd}
|
 || ¡ W d   ƒ n1 sÖ0    Y  t	|ƒ t	|ƒ d S )Nr   zutf-8)ÚencodingÚ
r   Úw)
r   r   r5   r€   r   r   Ú	readlinesÚ
writelinesÚwritery   )r'   rª   r²   r³   Zbin_pathZlibexec_pathÚorigZ
orig_linesZ	new_linesÚnewÚwrapperr+   r+   r,   Ú_create_wrapper‚  s    &(,z LilyPondPackager._create_wrapperc                 C   s6   t j}d}d|› d}| jjD ]}|  |||¡ qd S )Nz#!/usr/bin/env python3az  #!/bin/sh
script="$0"
if test -L "$script"; then
    # readlink is not POSIX, but present on all platforms we care about.
    # On macOS, it does not support the recursive following of symlinks,
    # so we only resolve one link and hope for the best.
    script="$(readlink -f "$script" 2>/dev/null || readlink "$script")"
fi
root="$(dirname "$script")/.."
exec "$root/libexec/z" "$root/libexec/%s" "$@"
)r   r®   r4   r`   r½   )r'   Zpython_interpreterZpython_shebangZpython_wrapper_templaterª   r+   r+   r,   Ú_create_python_wrappers’  s    	÷z(LilyPondPackager._create_python_wrappersc                 C   sP   t j | jd¡}t  |¡ tt | jg D ] }| | j	¡s<q*| 
|| j	¡ q*d S )NZlicenses)r   r   r5   r}   r]   r   r   r4   Úenabledr3   Úcopy_license_files)r'   Údestinationr|   r+   r+   r,   Ú_copy_license_files¤  s    
z$LilyPondPackager._copy_license_filesc                 C   s  t j | j¡rt | j¡ t d| j¡ | j 	| j
¡}t || j¡ d| j
j› }t j | j|¡}t|ƒ |  ¡  |  ¡  |  ¡  |  ¡  | j
 ¡ rœ|  ¡  t  | j¡ |  t | j
¡¡ | j
 ¡ rÖ|  ¡  |  ¡  n"|  t | j
¡¡ |  ¡  |  ¡  |   ¡  dS )zJPrepare the package by copying all needed files into a temporary location.zCopying files into '%s'...r4   N)!r   r   Úexistsr}   r^   r°   ÚloggingÚdebugr4   r=   r3   rƒ   Úprogram_suffixr5   r€   rn   r   r’   r™   rœ   rF   r¥   r]   r   rŠ   r   rL   r¨   r«   r   r±   r¾   rÂ   )r'   rŸ   Zlilypond_exer+   r+   r,   Úprepare_package­  s.    


z LilyPondPackager.prepare_packagec                 C   sÀ   |   ¡  | jjj}| jj}| jj› d|› d|› d}tj 	| jj
|¡}tj |¡r\t |¡ t d|¡ tjdœdd„}t |d¡(}|j| j| jjd|d	 W d
  ƒ n1 s²0    Y  d
S )z2Create a .tar.gz archive of the LilyPond binaries.r”   z.tar.gzúCreating archive '%s'...©Útarinfoc                 S   s   d | _ | _d | _| _| S )Nr   r    )ÚuidÚgidÚunameÚgnamerÉ   r+   r+   r,   Úresetâ  s    z+LilyPondPackager.package_tar.<locals>.resetzx:gzT)Zarcnamer­   ÚfilterN)rÇ   r3   ÚplatformÚvalueÚarchitecturer4   r*   r   r   r5   r6   rÃ   ÚremoverÄ   rÅ   r   ZTarInfor   Úaddr}   )r'   rÑ   rÓ   r   rA   rÏ   r(   r+   r+   r,   Úpackage_tarÔ  s     

üzLilyPondPackager.package_tarc           
   	      s
  |   ¡  | jjj}| jj}| jj› d|› d|› d}tj 	| jj
|¡}tj |¡r\t |¡ t d|¡ ‡ fdd„‰ t |d¡v}| j}| jj}| ||¡ tt |¡ƒ}dd„ |D ƒd	g }|D ]$}	ˆ |tj 	||	¡tj 	||	¡ƒ qÂW d
  ƒ n1 sü0    Y  d
S )z;Create a .zip archive of the LilyPond binaries (for mingw).r”   z.ziprÈ   c                    sr   t j |¡r|  ||tj¡ nPt j |¡rn|r:|  ||¡ tt  |¡ƒD ]$}ˆ | t j 	||¡t j 	||¡ƒ qHd S r0   )
r   r   Úisfiler¹   ÚzipfileÚZIP_DEFLATEDÚisdirÚsortedr¦   r5   )Úzip_archiver   Zzippathr"   ©Ú
add_to_zipr+   r,   rÞ   þ  s    ýz0LilyPondPackager.package_zip.<locals>.add_to_zipr¶   c                 S   s   g | ]}|d kr|‘qS )r   r+   )Ú.0r"   r+   r+   r,   Ú
<listcomp>  ó    z0LilyPondPackager.package_zip.<locals>.<listcomp>r   N)rÇ   r3   rÑ   rÒ   rÓ   r4   r*   r   r   r5   r6   rÃ   rÔ   rÄ   rÅ   rØ   ÚZipFiler}   r¹   rÛ   r¦   )
r'   rÑ   rÓ   r   rA   rÜ   r}   Zlilypond_dirZ	dir_namesr"   r+   rÝ   r,   Úpackage_zipï  s*    

ýzLilyPondPackager.package_zipN) rd   re   rf   rg   r   ri   r   r-   rj   rh   r}   r€   r   r†   rˆ   rŠ   r‹   r   r’   r™   rœ   r£   r¥   r¨   r«   r±   r½   r¾   rÂ   rÇ   rÖ   rã   r+   r+   r+   r,   rz   á   s:   
		'rz   )'rg   r¯   rÄ   r   r^   rr   rp   r   Útypingr   r   rØ   r9   r   r   Úconfigr   rH   r   r	   r
   r   r   r   r   r   r   r   r   r   r‘   r   r   r   r   rh   rn   ry   rz   r+   r+   r+   r,   Ú<module>   s(   4 #