ó
AqzRc           @   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 m Z d d l m	 Z	 d d l
 Z d Z d Z d Z d „  Z d	 „  Z d
 „  Z d „  Z e d „ Z d „  Z d S(   s<   Common functionality for Debian and RPM buildpackage scriptsiÿÿÿÿN(   t   CatenateTarArchive(   t   GbpErrort   INDEXt   WCs   .git/gbp_indexc         C   s   |  r |  j  d ƒ d Sd S(   sÄ   
    Sanitize the prefix used for generating source archives

    >>> sanitize_prefix('')
    '/'
    >>> sanitize_prefix('foo/')
    'foo/'
    >>> sanitize_prefix('/foo/bar')
    'foo/bar/'
    t   /(   t   strip(   t   prefix(    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt   sanitize_prefix%   s    c         C   s\  t  | ƒ } | j d d ƒ d } t j ƒ  } t j j | d ƒ }	 z|  j d d d | d | d	 | ƒ x |  j | ƒ D]Œ \ }
 } |
 |
 d
 g |
 j	 d ƒ } t
 j j d |
 | d d !f ƒ |  j d d d d | | f d |	 d	 | d |
 ƒ t | ƒ |	 ƒ qx Wt j d | | | | f ƒ } | rFt d | | f ƒ ‚ n  Wd t j | ƒ Xd S(   sð   
    Create tar.gz of an archive with submodules

    since git-archive always writes an end of tarfile trailer we concatenate
    the generated archives using tar and compress the result.

    Exception handling is left to the caller.
    t   .i   i    s   submodule.tart   formatt   tarR   t   outputt   treeishi   s   ./s   Processing submodule %s (%s)i   s   %s%s/t   cwds   %s -%s %s %ss   Error creating %s: %dN(   R   t   rsplitt   tempfilet   mkdtempt   ost   patht   joint   archivet   get_submodulest
   startswitht   gbpt   logt   debugR    t   systemR   t   shutilt   rmtree(   t   repoR   R   R   t	   comp_typet
   comp_levelt	   comp_optst   tarfilet   tempdirt   submodule_tarfilet   subdirt   committ   tarpatht   ret(    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt   git_archive_submodules5   s"    	!c         C   s„   t  | ƒ } t j ƒ  } | j d | |  f d ƒ | j d | | | f d ƒ | j d | ƒ } | r€ t d | | f ƒ ‚ n  d S(   sg   
    Create tar.gz of an archive without submodules

    Exception handling is left to the caller.
    s'   git archive --format=tar --prefix=%s %ss   .-s   %s -c -%s %ss   --t    s   Error creating %s: %dN(   R   t   pipest   Templatet   prependt   appendt   copyR   (   R   R   R   R   R   R    t   pipeR'   (    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt   git_archive_singleX   s    c         C   sG  t  j j | ƒ } t t  j j | ƒ ƒ } t j ƒ  } | j d | | f d ƒ | j d | d ƒ t  j j	 t  j j
 ƒ } z¶y-| j d d ƒ } | r© t d ƒ ‚ n  | r«|  j ƒ  rÈ |  j ƒ  n  xà |  j | ƒ D]Ì \ }	 }
 t j j d |	 |
 d d	 !f ƒ |	 |	 d
 g |	 j d ƒ } t  j |	 ƒ t j ƒ  } | j d | | |
 f d ƒ | j d | d ƒ | j d d ƒ } t  j | ƒ | rØ t d |	 ƒ ‚ qØ qØ Wn  Wn‚ t k
 rà} t j j d | | d f ƒ t St k
 r} t j j | ƒ t St k
 r0} t j j d | | f ƒ t SXWd t  j | ƒ Xt S(   s   dump a tree to output_dirs'   git archive --format=tar --prefix=%s %ss   .-s   tar -C %s -xf -s   -.R)   s   Error in dump_tree archive pipes   Processing submodule %s (%s)i    i   i   s   ./s*   git archive --format=tar --prefix=%s%s/ %ss/   Error in dump_tree archive pipe in submodule %ss   Error dumping tree to %s: %sN(   R   R   t   dirnameR   t   basenameR*   R+   R,   R-   t   abspatht   curdirR.   R   t   has_submodulest   update_submodulesR   R   R   t   infoR   t   chdirt   OSErrort   errt   Falset	   Exceptiont   True(   R   t
   export_dirR   t   with_submodulest
   output_dirR   R/   t   topR'   R$   R%   R&   R:   t   e(    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt	   dump_treeh   sJ     !		c         C   s2   |  j  |  j d | d t ƒ|  j d t ƒ } | S(   s6   write out the current working copy as a treeish objectt   forcet
   index_file(   t	   add_filesR   t   wc_indext
   write_tree(   R   RD   t   tree(    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt   write_wc“   s    c           C   s&   t  j j t ƒ r" t  j t ƒ n  d S(   s   drop our custom indexN(   R   R   t   existsRG   t   unlink(    (    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt
   drop_indexš   s    (   t   __doc__R   t   os.pathR*   R   R   t   gbp.command_wrappersR    t
   gbp.errorsR   t   gbp.logR   t
   index_namet   wc_nameRG   R   R(   R0   RC   R=   RJ   RM   (    (    (    sC   /usr/lib/python2.7/dist-packages/gbp/scripts/common/buildpackage.pyt   <module>   s    		#		+