
lRc           @   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 j Z	 d d l
 m Z m Z m Z m Z d d l m Z m Z d d l m Z m Z d d l m Z m Z d d l m Z d d l m Z d d l Z d d l Z d d	 l m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& d d
 l' m( Z( m) Z) m* Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 d   Z1 d   Z2 d   Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 d   Z: d   Z; e< d k re j= e; e j>   n  d S(   s>   run commands to build a debian package out of a git repositoryiN(   t   Commandt   RunAtCommandt   CommandExecFailedt
   RemoveTree(   t   GbpOptionParserDebiant   GbpOptionGroup(   t   GitRepositoryErrort   DebianGitRepository(   t   DebianSourcet   DebianSourceError(   t   GitVfs(   t   GbpError(   t
   index_namet   wc_namet   git_archive_submodulest   git_archive_singlet	   dump_treet   write_wct
   drop_index(   t   UpstreamSourcet   compressor_optst   compressor_aliasesc      	   C   sg  y t  | d } Wn! t k
 r5 t d |   n Xt j j | t j | |   } d | d | d f }	 yX |  j   r | r |  j	   t
 |  | | |	 | | |  n t | | |	 | | |  Wn t t f k
 r t j j d  t St k
 r"}
 t j j d | |
 d f  t St k
 r5  n. t k
 rb} t j j d | | f  t SXt S(   s@   create a compressed orig tarball in output_dir using git_archivei    s!   Unsupported compression type '%s's   %s-%st   Sources   Upstream-Versions%   Error generating submodules' archivess   Error creating %s: %s(   R   t   KeyErrorR   t   ost   patht   joint   dut	   orig_filet   has_submodulest   update_submodulesR   R   R   R   t   gbpt   logt   errt   Falset   OSErrort	   Exceptiont   True(   t   repot   cpt
   output_dirt   treeisht	   comp_typet
   comp_levelt   with_submodulest	   comp_optst   outputt   prefixR!   t   e(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   git_archive*   s2    !
c         C   s  t  |  | j | | j  | _ t j | | j  } | j r t j j d | | f  t j j	 | | | d t
 s t j j d | | f  q t j j d | | f  n  | j s | j rt j j | |  rt |  | | |  st |  | | |  } | j r|  j j | j | j | j  rBt j j d |  qt j j | |  } t j j d |  |  j j | |  qqn  d S(   sv   
    Make sure we have an upstream tarball. This involves loooking in
    tarball_dir, symlinking or building it.
    s%   Looking for orig tarball '%s' at '%s't   forces#   Orig tarball '%s' not found at '%s's   Orig tarball '%s' found at '%s's!   %s already on pristine tar branchs    Adding %s to pristine-tar branchN(   t   guess_comp_typeR*   t   tarball_dirR   R   R   R    t   debugt   DebianPkgPolicyt   symlink_origR%   t   infot   force_createt   no_create_origt   has_origt   pristine_tar_build_origt   git_archive_build_origt   pristine_tar_committ   pristine_tart
   has_committ   namet   upstream_versionR   R   R   t   commit(   R&   R'   t   optionsR4   R(   R   t   upstream_treet   archive(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   prepare_upstream_tarballK   s.    	)	c         C   s}   | j  rs | j t k r' |  j   } n' | j t k rE t |   } n	 | j } |  j |  sy t d |   qy n d } | S(   s   
    Write a tree of the index or working copy if necessary

    @param repo: the git repository we're acting on
    @type repo: L{GitRepository}
    @return: the sha1 of the tree
    @rtype: C{str}
    s   %s is not a valid treeishN(	   t
   export_dirt   exportR   t
   write_treeR   R   t   has_treeishR   t   None(   R&   RD   t   tree(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyRJ   o   s    			c         C   s   | j  rU | j   r$ t d   n  t t j j | t j | j	 | j
   |  n  t j j d | j | f  t |  | | | j  s t  n  d S(   s  
    Export a version of the source tree when building in a separate directory

    @param repo: the git repository to export from
    @type repo: L{gbp.git.GitRepository}
    @param source: the source package
    @param options: options to apply
    @param dest_dir: where to export the source to
    @param tarball_dir: where to fetch the tarball from in overlay mode
    @returns: the temporary directory
    s$   Cannot overlay Debian native packages   Exporting '%s' to '%s'N(   t   overlayt	   is_nativeR   t   extract_origR   R   R   R   R   t	   changelogR*   R   R    R8   RI   R   R,   (   R&   RM   t   sourceRD   t   dest_dirR4   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   export_source   s    	
c         C   sc   y t  j |   WnK t k
 r^ \ } } | t j k r_ t  j |  d |  t j   f  q_ n Xd S(   s#   move a build tree away if it existss   %s.obsolete.%sN(   R   t   mkdirR#   t   errnot   EEXISTt   renamet   time(   t   targetR0   t   msg(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   move_old_export   s
    c         C   s   t  j j d t j j |   | f  t |  t |   } | j |  | j	 | k r t  j j
 d | j	 | f  | d } t j | j	 |  t j |  t j | |  n  d S(   s<   extract orig tarball to export dir before exporting from gits   Extracting %s to '%s's   Moving %s to %ss   .newN(   R   R    R8   R   R   t   basenameR\   R   t   unpackt   unpackedR5   RX   t   rmdir(   t   orig_tarballRS   t   upstreamt   tmpdir(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyRP      s    &

c         C   sb   y8 | r! t  t |  |   } n t  d  } | j   Wn# t k
 r] } t d |   n X| S(   s=   Init source package info either from git or from working copyt   .s    Can't determine package type: %s(   R   R
   RO   R$   R   (   R&   RD   RM   RR   R0   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt
   source_vfs   s    c         C   sz   |  } |  s d } n  t  j j |  } y t  j |  Wn; t k
 ru \ } } | t j k rv t d |   qv n X| S(   s8   Prepare the directory where the build result will be puts   ..s   Cannot create output dir %s(   R   R   t   abspathRU   R#   RV   RW   R   (   t   dirR(   R0   R[   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   prepare_output_dir   s    	c         C   s   | j  r |  j |  j  s8 t j j d |  j  j  n  y* |  j  j | j | j	 | j
 |  t SWq t k
 r | j r t j j d  q   q Xn  t S(   sZ   
    build orig using pristine-tar
    @return: True: orig tarball build, False: noop
    s"   Pristine-tar branch "%s" not foundsP   pristine-tar checkout failed, will commit tarball due to '--pristine-tar-commit'(   R?   t
   has_brancht   pristine_tar_branchR   R    t   warnt   brancht   checkoutRA   RB   R*   R%   R   R>   R5   R"   (   R&   R'   R(   RD   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyR<      s    		
c         C   s   | j  j   d k r1 |  j | j | d  } nR | j  j   d k rz |  j | j  sn t d | j   n  | j } n	 | j  } |  j |  s t d |   n  | S(   s2   Determine the upstream tree from the given optionst   TAGs   Upstream-Versiont   BRANCHs   %s is not a valid branchs   %s is not a valid treeish(   RE   t   uppert   version_to_tagt   upstream_tagRi   t   upstream_branchR   RK   (   R&   R'   RD   RE   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   get_upstream_tree   s    	c         C   s   t  |  | |  } t j j d t j | | j  | f  t j j d | j | j f  t	 |  | | | | j | j | j
  s t d |   n  | S(   sc  
    Build orig tarball using git-archive

    @param cp: the changelog of the package we're acting on
    @type cp: L{ChangeLog}
    @param output_dir: where to put the tarball
    @type output_dir: C{Str}
    @param options: the parsed options
    @type options: C{dict} of options
    @return: the tree we built the tarball from
    @rtype: C{str}
    s%   %s does not exist, creating from '%s's3   Building upstream tarball with compression '%s -%s's&   Cannot create upstream tarball at '%s'(   Rt   R   R    R8   R   R   R*   R5   R+   R1   R,   R   (   R&   R'   R(   RD   RE   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyR=     s    	c         C   s  | d } | d } | d k r] t  j | |  } t j |  s] t j j d  d } q] n  | d k r|  j   s| s d } n  d } xY t j	   D]K } t
 j j t
 j | |  |  r | d k	 r t d   n  | } q q W| d k	 r | } qd } qd | | f } |  j | |  j  }	 |	 rP|	 d	 }
 t j j d
 |
  n	 |  j }
 |  j |
  d } t
 j j |  } t j j d |  | sd } t j j d | | f  qn  | S(   s   Guess compression typeR   s   Upstream-Versiont   autos$   Unknown compression type - guessing.s   ..s   Multiple orig tarballs found.t   gzips!   pristine-tar .* %s_%s\.orig.tar\.is!   Found pristine-tar commit at '%s't   subjects    Determined compression type '%s's+   Unknown compression type of %s, assuming %sN(   R   t   getR   t   has_keyR   R    Rk   t   has_pristine_tar_branchRL   t   keysR   R6   R;   R   R   t   grep_logRj   R5   t   get_commit_infot   get_compression(   R&   R*   R'   R4   t   srcpkgRB   t   detectedt   compt   regext   commitsRC   t   tarball(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyR3     sB    

	!		
	 c         C   s   |  j  s |  j r d |  _ d |  _ |  j t j d <|  j rP |  j t j d <n  |  j ri d t j d <n  |  j s d t j d <n  |  j	 r |  j	 t j d	 <q n  d
 S(   s$   setup everything to use git-pbuilders   git-pbuilders	   /bin/truet   DISTt   ARCHt   qemubuildert   BUILDERt   not   GIT_PBUILDER_AUTOCONFt   GIT_PBUILDER_OPTIONSN(
   t   use_pbuildert   use_qemubuildert   buildert   cleanert   pbuilder_distR   t   environt   pbuilder_archt   pbuilder_autoconft   pbuilder_options(   RD   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   setup_pbuilderG  s    						c         C   sZ   xS d d d d d g D]< } t  |  |  r t j j d |  t |  | d  q q Wd S(	   s&   Disable all hooks (except for builder)R   t
   postexportt   prebuildt	   postbuildt   posttags   Disabling '%s' hookt    N(   t   getattrR   R    R8   t   setattr(   RD   t   hook(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   disable_hooksW  s    c         C   s@   d |  k r d Sd |  k r  d St  j d d  p; t j   Sd S(   s   
    >>> changes_file_suffix(['-A'])
    'all'
    >>> changes_file_suffix(['-S'])
    'source'
    >>> changes_file_suffix([]) == du.get_arch()
    True
    s   -SRR   s   -At   allR   N(   R   t   getenvRL   R   t   get_arch(   t	   dpkg_args(    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   changes_file_suffix_  s
    	c      
   C   s  g  |  d D]% } | j  d |  d k r | ^ q } g  |  d D]% } | j  d |  d k rA | ^ qA } x3 d d d g D]" } | | k r| | j |  q| q| Wy) t d t j j |  d  d	 |  } Wn' t j k
 r } t j	 j
 |  d{ SXt | d
 d  } t | d d  } t | d d  }	 t | d d  }
 t | d d  } | j |  | j |
  | j |  | j |	  | j |  | j d d d d  | j d d d d d d t d d | j d d d d d  d!  | j d d" d d#  | j d d$ d d$ d  d!  | j d% d d d d& d t d d' | j d( d d d d) d t d d* | j d+ d d d d, d t d d- | j d d. d d/  | j d d0 d d0  | j d d1 d d2  | j d d3 d d4  |
 j d d5 d d6  |
 j d d7 d d8  |
 j d d9 d d:  |
 j d d; d d< d d= d d  |
 j d d> d d? d d@ d d  |
 j d dA d dB d  dC d dD  |
 j d dE d dF d dG  |
 j d dH d dI d dJ  | j d dK d dL  | j d dM d dN  | j d dO d dP  | j d dQ d dR  |	 j d dS d dS d dT  |	 j d dU d dU d dV  |	 j d dW d dW d dX  |	 j d dY d dY d dZ  |	 j d d[ d d[ d d\  |	 j d d] d d] d d^  |	 j d d_ d d`  |	 j d da d db  |	 j d dc d dd  |	 j d de d df  |	 j d dg d dh  |	 j d di d dj  |	 j d dk d dk  | j d dl d dm d  dC d dn  | j do d do d dp dq dr | j d ds d ds  | j dt d d d du d t d dv | j d dw d dw  | j |  \ } } t j	 j | j | j | j  | j s6t |  n  | j rw| j rw| j rwt j	 j
 dx | | | f  d| Sn  | j r| j rt j	 j
 dy  d} S| j rt j	 j dz  t | _  n  | | | f S(~   Ni   s   --%si    is   --helps   -hs	   --versiont   commandR/   s   tag optionss#   options related to git tag creations   branch optionss   branch layout optionss   external command optionss2   how and when to invoke external commands and hookss   orig tarball optionss3   options related to the creation of the orig tarballs   export build-tree optionss&   alternative build tree related optionst   option_names
   ignore-newt   destt
   ignore_news   --git-verboset   actiont
   store_truet   verboset   defaultt   helps   verbose command executiont   colort   typet   tristates   color-schemet   color_schemet   notifys	   --git-tagt   tags%   create a tag after a successful builds   --git-tag-onlyt   tag_onlys.   don't build, only tag and run the posttag hooks   --git-retagt   retags$   don't fail if the tag already existss	   sign-tagst	   sign_tagst   keyids
   debian-tagt
   debian_tags   upstream-tagRr   s   upstream-treeRE   s   pristine-tarR?   s   pristine-tar-commitR>   s   force-createR9   s   force creation of orig tarballs   no-create-origR:   s   don't create orig tarballs   tarball-dirR4   R   s&   location to look for external tarballst   compressionR*   s.   Compression type, default is '%(compression)s's   compression-levelR+   s5   Compression level, default is '%(compression-level)s's   upstream-branchRs   s   debian-brancht   debian_branchs   ignore-brancht   ignore_brancht
   submodulesR,   R   s=   command to build the Debian package, default is '%(builder)s'R   s;   command to clean the working copy, default is '%(cleaner)s'R   s8   command to run before a build, default is '%(prebuild)s'R   sK   command to run after exporting the source tree, default is '%(postexport)s'R   s=   hook run after a successful build, default is '%(postbuild)s'R   sC   hook run after a successful tag operation, default is '%(posttag)s't   pbuilderR   R   R   t   distR   t   archR   s   pbuilder-autoconfR   s   pbuilder-optionsR   t   hookss
   export-dirRH   sZ   before building the package export the source into EXPORT_DIR, default is '%(export-dir)s'RI   s6   export treeish object TREEISH, default is '%(export)s't   metavart   TREEISHt   purges   --git-dont-purget
   dont_purges&   deprecated, use --git-no-purge insteadRN   s4   '--%sretag' needs either '--%stag' or '--%stag-only's*   Overlay must be used with --git-export-dirs:   --git-dont-purge is depreceted, use --git-no-purge instead(   NNN(   NNN(   NNN(!   t   findt   appendR   R   R   R]   t   ConfigParsert   ParsingErrorR   R    R!   RL   R   t   add_option_groupt   add_boolean_config_file_optiont
   add_optionR"   t   add_config_file_optiont
   parse_argst   setupR   R   R   R   R   R   R   R   RN   RH   R   t   warningR   (   t   argvR/   t   argt   argsR   t   parserR!   t	   tag_groupt   branch_groupt	   cmd_groupt
   orig_groupt   export_groupRD   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyR   p  s    66)			c         C   sg  d } d } d  } d  } t |  |  \ } } } | s: d Sy t t j j  } Wn2 t k
 r t j j	 d t j j
 d   d SXt j j
 t j j  }	 zGyt | j d t   | j s| j   \ }
 } |
 st j j	 d  t j j	 |  t d   qn  y | j   } Wn  t k
 r>| j s?  q?n X| j r| j r| | j k rt j j	 d	 | j | f  t d
   qn  t | |  } t | | |  } | j sTt | j  } | j p| } | j   s| j rt j j d  qt | | j | | |  n  | j rIt j j | d | j   } t! | | | | | |  | j rt" | j d t d i | j# d 6| d 6d |  n  | j   r| j j$ n	 | j j% } t j j | d | j  | f  } t j j d | | f  t& |  t j' | |  | j   rI| j rIt | | j | | |  qIn  | j r[| } n |	 } | j( rt" | j( d t d i | j# d 6| d 6d |  n  t) |  t" | j* | d t d i | d 6d |  | j+ rTt j j
 d | | j  | j j, t- |  f  } t j j. d |  t | j+ d t d i | d 6| d 6  qTn  | j/ sf| j rWt j j d | j j0  | j1 | j2 | j j0  } | j3 r| j4 |  r| j5 |  n  | j6 d | d d | j  | j j0 f d | j7 d | j8  | j9 rW| j: d |  } t | j9 d t d i | d 6| p?d d  6| d! 6  qWn  Wn t; k
 rqd } nr t t f k
 r} t< | j=    rt j j	 |  n  d } n/ t> k
 r} t j j	 |  d  } d } n XWd  t?   X| j sc| j r!| j@ r!| r!tA |    n  | rct jB jC | j | | jC  rct j j	 d"  d } qcn  | S(#   Ni    s   git-i   s   %s is not a git repositoryRd   t   shells1   You have uncommitted changes in your source tree:s   Use --git-ignore-new to ignore.s&   You are not on branch '%s' but on '%s'sP   Use --git-ignore-branch to ignore or --git-debian-branch to set the branch name.s.   Postexport hook set, delaying tarball creations   %s-tmpt	   extra_envt   GBP_GIT_DIRt   GBP_TMP_DIRRg   s   %s-%ss   Moving '%s' to '%s't   GBP_BUILD_DIRs   %s/../%s_%s_%s.changess   Looking for changes file %st   GBP_CHANGES_FILEs
   Tagging %sRA   R[   s   %s Debian release %st   signR   s   %s^{}t   GBP_TAGs   (no branch)t
   GBP_BRANCHt   GBP_SHA1s   Failed to send notification(D   RL   R   R   R   R   t   curdirR   R   R    R!   Rf   R    R   R%   R   t   is_cleanR   t
   get_branchR   R   RJ   Re   R   Rh   RH   R4   RO   R   R8   RG   RQ   R   t	   sourcepkgRT   R   t   git_dirt   debian_versionRB   R\   RX   R   R   R   R   t   noepochR   R5   R   t   versionRq   R   R   t   has_tagt
   delete_tagt
   create_tagR   R   R   t	   rev_parseR   t   lent   __str__R	   R   R   R   t   notificationsR   (   R   t   retvalR/   RR   Rl   RD   t   gbp_argsR   R&   t   repo_dirt   rett   outRM   R(   R4   t   tmp_dirt   majorRH   t	   build_dirt   changesR   t   shaR!   (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   main  s      		
		
		"
			
		
		
			t   __main__(?   t   __doc__R   RV   R   t   os.patht   sysRY   t   gbp.debt   debR   t   gbp.command_wrappersR    R   R   R   t
   gbp.configR   R   t   gbp.deb.gitR   R   t   gbp.deb.sourceR   R	   t   gbp.git.vfsR
   t
   gbp.errorsR   t   gbp.logR   t   gbp.notificationst   gbp.scripts.common.buildpackageR   R   R   R   R   R   R   t   gbp.pkgR   R   R   R1   RG   RJ   RT   R\   RP   Re   Rh   R<   Rt   R=   R3   R   R   R   R   R   t   __name__t   exitR   (    (    (    s<   /usr/lib/python2.7/dist-packages/gbp/scripts/buildpackage.pyt   <module>   sF   "4	!	$											+				n	