ó
œt•Pc           @   s  d  Z  d d l m Z d d l m Z m Z d d l m Z m Z m	 Z	 m
 Z
 m Z d d l Z d d l Z d d l Z d d l m Z d Z d Z e
 j e d	 ƒ r¶ e j e ƒ n  d
 Z d Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d d „ Z d S(   s:  hooks for integrating with the CIA.vc notification service

This is meant to be run as a changegroup or incoming hook. To
configure it, set the following options in your hgrc::

  [cia]
  # your registered CIA user name
  user = foo
  # the name of the project in CIA
  project = foo
  # the module (subproject) (optional)
  #module = foo
  # Append a diffstat to the log message (optional)
  #diffstat = False
  # Template to use for log messages (optional)
  #template = {desc}\n{baseurl}{webroot}/rev/{node}-- {diffstat}
  # Style to use (optional)
  #style = foo
  # The URL of the CIA notification service (optional)
  # You can use mailto: URLs to send by email, e.g.
  # mailto:cia@cia.vc
  # Make sure to set email.from if you do this.
  #url = http://cia.vc/
  # print message instead of sending it (optional)
  #test = False
  # number of slashes to strip for url paths
  #strip = 0

  [hooks]
  # one of these:
  changegroup.cia = python:hgcia.hook
  #incoming.cia = python:hgcia.hook

  [web]
  # If you want hyperlinks (optional)
  baseurl = http://server/path/to/repo
iÿÿÿÿ(   t   _(   t   bint   short(   t   cmdutilt   patcht	   templatert   utilt   mailN(   t   saxutilst   internali   t   setdefaulttimeouts   0.1s$   http://hg.kublai.com/mercurial/hgciat   ciamsgc           B   sS   e  Z d  Z d „  Z d „  Z d „  Z d d d „ Z d „  Z d „  Z	 d „  Z
 RS(	   s    A CIA message c         C   sF   | |  _  | |  _ |  j  j |  _ |  j rB |  j |  j  j 7_ n  d  S(   N(   t   ciat   ctxt   urlt   root(   t   selfR   R   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   __init__A   s
    			c         C   s?   | r d t  j | ƒ } n  d | t  j | ƒ t  j | ƒ f S(   Ns    uri=%ss   <file%s action=%s>%s</file>(   R   t	   quoteattrt   escape(   R   t   patht   urit   action(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   fileelemH   s    c      	   C   sL  |  j  j ƒ  } |  j j j |  j  j ƒ  j ƒ  | ƒ } |  j pB d } | rh | d d k rh | d  } n  g  } xM | d D]A } d | t | ƒ | f } | j |  j	 | | o° | d ƒ ƒ qy WxM | d D]A } d | t | ƒ | f } | j |  j	 | | o | d	 ƒ ƒ qÉ Wx. | d
 D]" } | j |  j	 | d d ƒ ƒ qWd j
 | ƒ S(   Nt    iÿÿÿÿt   /i    s   %s/diff/%s/%st   modifyi   s   %s/file/%s/%st   addi   t   removes   
(   R   t   nodeR   t   repot   statust   p1R   R   t   appendR   t   join(   R   t   nt   fR   t   elemsR   R   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt	   fileelemsN   s    '&& c         C   sy   d d t  j | ƒ g } | r< | j d t  j | ƒ ƒ n  | r_ | j d t  j | ƒ ƒ n  | j d ƒ d j | ƒ S(   Ns   <source>s   <project>%s</project>s   <module>%s</module>s   <branch>%s</branch>s	   </source>s   
(   R   R   R!   R"   (   R   t   projectt   modulet   brancht   msg(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt
   sourceelema   s    c         C   sc   d t  f d „  ƒ  Y} |  j j ƒ  } | ƒ  } t j |  j j | g d | ƒt j | j	 ƒ pb d S(   Nt   patchbufc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         S   s   g  |  _  d |  _ d  S(   NR   (   t   linest   name(   R   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyR   m   s    	c         S   s   |  j  | j t ƒ 7_  d  S(   N(   R-   t
   splitlinest   True(   R   t   data(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   writeq   s    c         S   s   d  S(   N(    (   R   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   closes   s    (   t   __name__t
   __module__R   R2   R3   (    (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyR,   l   s   		t   fpR   (
   t   objectR   R   R   t   exportR   R   R   t   diffstatR-   (   R   R,   R#   t   pbuf(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyR9   k   s
    
	c         C   s–   |  j  j r |  j ƒ  p d } |  j  j j ƒ  |  j  j j |  j d |  j j ƒ  d |  j  j j d d ƒ d |  j	 d | d |  j  j
 ƒ|  j  j j ƒ  S(   NR   t   changest   baseurlt   webR   R9   t   webroot(   R   R9   t   uit
   pushbufferR   t   showR   t	   changesett   configR   R   t	   popbuffer(   R   R9   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   logmsg{   s    $c   
      C   s?  t  |  j j ƒ  ƒ } |  j |  j j d |  j j d |  j j ƒ  ƒ} |  j j ƒ  } | d } t	 j
 |  j j ƒ  ƒ } d |  j j ƒ  | f } t	 j
 |  j ƒ  ƒ } |  j } | rÐ | d d k rÐ | d  } n  | rï d t	 j
 | ƒ | f pò d } d	 t t	 j
 t ƒ t	 j
 |  j j ƒ | | | | | |  j ƒ  | f
 }	 |	 S(
   NR(   R)   i    s   %d:%siÿÿÿÿR   s   <url>%s/rev/%s</url>R   sP  
<message>
  <generator>
    <name>Mercurial (hgcia)</name>
    <version>%s</version>
    <url>%s</url>
    <user>%s</user>
  </generator>
  %s
  <body>
    <commit>
      <author>%s</author>
      <version>%s</version>
      <log>%s</log>
      %s
      <files>%s</files>
    </commit>
  </body>
  <timestamp>%d</timestamp>
</message>
(   R   R   R   R+   R   R'   R(   R)   t   dateR   R   t   usert   revRE   R   t   HGCIA_VERSIONt	   HGCIA_URLR&   (
   R   R#   t   srct   dtt	   timestampt   authorRH   t   logR   R*   (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   xml„   s"    
	%!N(   R4   R5   t   __doc__R   R   R&   t   NoneR+   R9   RE   RP   (    (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyR   ?   s   			
			t   hgciac           B   s>   e  Z d  Z d Z d Z d „  Z d „  Z d „  Z d „  Z RS(   s    CIA notification class s   {desc}s   {desc}
-- 
{diffstat}c      	   C   s¯  | |  _  | |  _ |  j  j d d d ƒ |  _ |  j  j d d ƒ |  _ |  j  j d d ƒ |  _ |  j  j d d ƒ |  _ |  j  j d d ƒ |  _ |  j  j d d	 ƒ |  _	 |  j  j d d
 ƒ |  _
 |  j  j d d ƒ |  _ t |  j  j d d d ƒ ƒ |  _ |  j |  j j ƒ |  _ |  j  j d d ƒ } |  j  j d d ƒ } | s\|  j rP|  j pV|  j } n  t j | d t ƒ} t j |  j  |  j t d  | t ƒ } | j | ƒ | |  _ d  S(   NR   R   s   http://cia.vcRG   R'   R(   R9   t   emailt   fromt   testR=   R<   t   stripiÿÿÿÿt   stylet   templatet   quoted(   R?   R   RC   t   ciaurlRG   R'   R(   t
   configboolR9   t	   emailfromt   dryrunR   t   intt
   stripcountRW   R   t
   dstemplatet   deftemplateR   t   parsestringt   FalseR   t   changeset_templaterRR   t   use_template(   R   R?   R   RX   RY   t   t(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyR   ·   s*    		!c         C   sv   t  j | ƒ } |  j } | d k  r( d SxG | d k rq | j d ƒ } | d k rV Pn  | | d } | d 8} q+ W| S(   s?   strip leading slashes from local path, turn into web-safe path.i    R   R   iÿÿÿÿi   (   R   t   pconvertR`   t   find(   R   R   t   countt   c(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyRW   Ñ   s    	c         C   se   t  j |  j ƒ } | j j | ƒ } | t k	 ra | d k ra t j t d ƒ |  j | f ƒ ‚ n  d  S(   Ns   queued.s   %s returned an error: %s(	   t	   xmlrpclibt   ServerR[   t   hubt   deliverR0   R   t   AbortR    (   R   R*   t   srvt   res(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   sendrpcà   s
    c         C   s±   t  j j ƒ  } | j | ƒ } t j d d ƒ | d <| | d <|  j | d <d | d <d | d	 <| j ƒ  } |  j j t	 d
 ƒ | ƒ t
 j |  j t j  |  j ƒ | g | ƒ d  S(   Nt   formats   %a, %d %b %Y %H:%M:%S %1%2t   Datet   Tot   Fromt
   DeliverXMLt   Subjects   text/xmls   Content-types   hgcia: sending update to %s
(   RT   t   Parsert   parsestrR   t   datestrR]   t	   as_stringR?   R   R    R   t   sendmail(   R   t   addressR1   t   pR*   t   msgtext(    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt	   sendemailç   s    


(	   R4   R5   RQ   Rb   Ra   R   RW   Rs   R‚   (    (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyRS   ±   s   			c            sö   ‡  f d †  } t  | ƒ } t ˆ  | ƒ } | j sD ˆ  j d ƒ d S| j s^ ˆ  j d ƒ d S| d k rÖ | j j | ƒ }	 t | j ƒ }
 xd t |	 |
 ƒ D]4 } | j j	 | ƒ } | j
 | ƒ } | | | ƒ q› Wn | j
 | ƒ } | | | ƒ d S(   s    send CIA notification c            sŒ   t  |  | ƒ j ƒ  } |  j r. ˆ  j | ƒ nZ |  j j d ƒ r{ |  j sa t j t	 d ƒ ƒ ‚ n  |  j
 |  j d | ƒ n |  j | ƒ d  S(   Ns   mailto:s0   email.from must be defined when sending by emaili   (   R   RP   R^   R2   R[   t
   startswithR]   R   Rp   R    R‚   Rs   (   R   R   R*   (   R?   (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   sendmsgø   s    		s   cia: no user specifiedNs   cia: no project specifiedt   changegroup(   R   RS   RG   t   debugR'   t	   changelogRH   t   lent   xrangeR   t	   changectx(   R?   R   t   hooktypeR   R   t   kwargsR„   R#   R   t   startt   endRH   R   (    (   R?   s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   hookö   s$    		(   RQ   t   mercurial.i18nR    t   mercurial.nodeR   R   t	   mercurialR   R   R   R   R   t   email.ParserRT   t   socketRl   t   xml.saxR   t
   testedwitht   socket_timeoutt   safehasattrR
   RI   RJ   R7   R   RS   RR   R   (    (    (    s/   /sys/lib/python2.7/site-packages/hgext/hgcia.pyt   <module>*   s   (rE