U
    jP^.                  
   @   s6  d dl mZ d dlZd dlZd dlZzd dlZW n ek
rL   d dlZY nX z^d dlZzd dl	m
Z
 dZW n$ ek
r   d dlm
Z
 dZY nX d dlmZ d dlmZ W n. ek
r Z zeeeW 5 dZ[X Y nX d dlmZ d d	lmZ d d
lmZ ddddgZddgZdZdd ZG dd dejZdS )    )logN)apidocFT)application)setup_command)git)options)versionautodoc_tree_index_modulesautodoc_index_modulesautodoc_tree_excludesautodoc_exclude_modulesAUTODOC_TREE_INDEX_MODULESAUTODOC_INDEX_MODULESzi%(heading)s
%(underline)s

.. automodule:: %(module)s
  :members:
  :undoc-members:
  :show-inheritance:
c                 C   s@   |D ]6}| dr|dkrd| d|dd|d d f < qd S )Nz.pyz__init__.pyTz%s.%s/.)endswithreplace)argdirnamefilesfilename r   ./usr/lib/python3/dist-packages/pbr/builddoc.py_find_modulesI   s    
r   c                   @   sT   e Zd ZdgZdZdZdd ZdddZd	d
 Zdd Z	dd Z
dd Zdd ZdS )LocalBuildDochtmlbuild_sphinxFc                 C   sj   | j d}| j d}|dd\}}d|krHtj|d d |}nd| }tj|sft| |S )Nr   pbrapi_doc_dir)NZapi
source_dir   zdoc/source/)distributionget_option_dictgetospathjoinexistsmakedirs)selfoption_dictZpbr_option_dict_r   r    r   r   r   _get_source_dirV   s    
zLocalBuildDoc._get_source_dirNc                    s2  t dtjtj  i }|  }| jjD ].}d|kr,t	|D ]\}}}t
||| qBq, fddtfdd| D }tj|d}	t|	d}
|
d	 |D ]x}tj|d
| }d| }dt| }t|||d}t d|  t|d}|t|  W 5 Q R X |
d|  qW 5 Q R X d S )Nz[pbr] Autodocumenting from %sr   c                    s   t  fddD  S )Nc                 3   s   | ]}t   |V  qd S N)fnmatch).0Zpatmoduler   r   	<genexpr>n   s   zDLocalBuildDoc.generate_autoindex.<locals>.include.<locals>.<genexpr>)anyr1   )excluded_modulesr1   r   includem   s    z1LocalBuildDoc.generate_autoindex.<locals>.includec                 3   s   | ]} |r|V  qd S r.   r   )r0   mod)r6   r   r   r3   q   s      z3LocalBuildDoc.generate_autoindex.<locals>.<genexpr>zautoindex.rstwz.. toctree::
   :maxdepth: 1

z%s.rstzThe :mod:`%s` Module=)r2   heading	underlinez[pbr] Generating %sz
   %s.rst
)r   infor%   r&   abspathcurdirr-   r"   Zpackageswalkr   sortedkeysr'   openwritelendict_rst_template)r*   r5   modulesr    pkgdirpathZdirnamesr   Zmodule_listZautoindex_filenameZ	autoindexr2   Zoutput_filenamer:   r;   valuesZoutput_filer   )r5   r6   r   generate_autoindexc   s:    
z LocalBuildDoc.generate_autoindexc                 C   s:   |   }ddd|dg}tr&|dd t|| j  d S )Nz-HZModulesz-or   r   r   )r-   apidoc_use_paddinginsertr   mainr   )r*   r    cmdr   r   r   _sphinx_tree   s
    zLocalBuildDoc._sphinx_treec                 C   s^  | j st }ntj}i }| jr*| j|d< | jr:| j|d< | jrJ| j|d< | jrZ| j|d< | j	rrddddd	g|d
< t
j| j| j| j| j| j||| j| jd	}d| _	z|j| jd W nt tk
r( } zTddlm} t||jrtjd tj|jd dd tjd n W 5 d }~X Y nX | jrZ|j j!|jj" }|j#d}t$%|| d S )Nprojectr   releasetodayzapp.add_directivezapp.add_rolezapp.add_generic_rolezapp.add_nodezimage.nonlocal_uriZsuppress_warnings)ZfreshenvZwarningiserrorT)Z	force_allr   )utilszreST markup error:
asciibackslashreplace
index)&verbose	cStringIOStringIOsysstdoutrQ   r   rR   rS   sphinx_initializedr   ZSphinxr    Z
config_dirZbuilder_target_dirZdoctree_dirbuilderZ	fresh_envwarning_is_errorZbuildZ	all_files	ExceptionZdocutilsrT   
isinstanceZSystemMessageZstderrC   stderrargsencodeZ
link_indexZconfigZ
master_docZ
out_suffixZget_outfilenamer%   symlink)r*   Zstatus_streamZconfoverridesZapperrrT   srcZdstr   r   r   _sphinx_run   s\    




       zLocalBuildDoc._sphinx_runc           	      C   st  | j d}t| t}ttdd t}|s8|rjd}|rP|dt| 7 }|r`|d| 7 }t	
| t rtj|d tj|d t|dd	}t|d
d}tds|r|   |r| t|dd dgd   |   tjtjtddk}|st	
d | jdgkr:t	
d |r:| j| _|rLtj !| S | jD ]}|| _|   | "  qRd S )Nr   c                 S   s
   t | S r.   )r%   getenv)xr   r   r   <lambda>       z#LocalBuildDoc.run.<locals>.<lambda>zThe autodoc and autodoc_tree features are deprecated in 4.2 and will be removed in a future release. You should use the sphinxcontrib-apidoc Sphinx extension instead. Refer to the pbr documentation for more information.z Deprecated options: %sz% Deprecated environment variables: %s)r+   r	   r   r
   r   ZSPHINX_DEBUGr    r!      zQ[pbr] Support for Sphinx < 1.6 will be dropped in pbr 4.0. Upgrade to Sphinx 1.6+r   a'  [pbr] Sphinx 1.6 added native support for specifying multiple builders in the '[sphinx_build] builder' configuration option, found in 'setup.cfg'. As a result, the '[sphinx_build] builders' option has been deprecated and will be removed in pbr 4.0. Migrate to the 'builder' configuration option.)#r"   r#   setrA   intersection_deprecated_optionslistfilter_deprecated_envsr   warnr   Z_git_is_installedZwrite_git_changelogZgenerate_authorsr   Zget_boolean_optionr%   rj   rP   rK   r$   splitfinalize_optionsr   ZSemanticVersionZfrom_pip_stringsphinx__version__buildersr_   r   BuildDocrunri   )	r*   r+   Z	warn_optsZwarn_envmsgZ
tree_indexZ
auto_indexZis_multibuilder_sphinxr_   r   r   r   r}      sf    





zLocalBuildDoc.runc                 C   s   t j|  dg| _d S )Nzsetup.py)r   r|   initialize_optionsr   )r*   r   r   r   r      s    z LocalBuildDoc.initialize_optionsc                 C   s   ddl m} tj|  | jd}d|dg gd krH|d d | _t	| jt
sh| jrh| jd| _| j | _| j | _| j | _d}| jd	}||kr||| d | _t| d
sd| _d S )Nr   )utilr   zcommand liner_   r!   ,r   r   r`   F)r   r   r   r|   rx   r"   r#   r$   r{   rb   rs   rw   Zget_namerQ   Zget_versionr   rR   Zsplit_multiliner   hasattrr`   )r*   r   r+   Zoptr   r   r   rx     s$    

zLocalBuildDoc.finalize_options)N)__name__
__module____qualname__r{   Zcommand_namer^   r-   rK   rP   ri   r}   r   rx   r   r   r   r   r   P   s   
#,Gr   )Z	distutilsr   r/   r%   r\   rZ   ImportErroriory   Z
sphinx.extr   rL   r   r   ra   estrr   r   r   r   rr   ru   rF   r   r|   r   r   r   r   r   <module>   s<   
 
