ó
SUc           @   sØ   d  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 Z e j	 j
 e j	 j e ƒ ƒ Z e j	 j e d ƒ Z d „  Z d „  Z d „  Z d „  Z e d	 k rÔ e  GHe e j d
 ƒ n  d S(   s6   
odk_validate.py
A python wrapper around ODK Validate
iÿÿÿÿN(   t   Popent   PIPEs   ODK_Validate.jarc            s¬   t  j ƒ  ‰  ‡  f d †  } t |  d t d t d t ƒ} | j } t  j | | d | f ƒ} | j ƒ  | j ƒ  \ } } | j ƒ  ˆ  j	 ƒ  } ˆ  j
 ƒ  | j | | | f S(   s´   
    Run a sub-program in subprocess.Popen, pass it the input_data,
    kill it if the specified timeout has passed.
    returns a tuple of resultcode, timeout, stdout, stderr
    c            s!   t  j |  t j ƒ ˆ  j ƒ  d  S(   N(   t   ost   killt   signalt   SIGTERMt   set(   t   pid(   t
   kill_check(    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   _kill_process_after_a_timeout   s    
t   stdint   stdoutt   stderrt   args(   t	   threadingt   EventR    R   R   t   Timert   startt   communicatet   cancelt   isSett   cleart
   returncode(   t   commandt   timeoutR	   t   pR   t   watchdogR   R   (    (   R   sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   run_popen_with_timeout   s    	


c          C   s1   t  d d g d t ƒ}  t |  j j ƒ  ƒ d k S(   Nt   whicht   javaR   i    (   R    R   t   lenR   t	   readlines(   R   (    (    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   _java_installed*   s    c   	         s0  d „  ‰  ‡  f d †  } d } t  j | d t  j ƒj | |  ƒ }  g  } d } xÒ |  j ƒ  D]Ä } | j d ƒ d k	 } | j d ƒ d k	 } | | k } | } | r[ | r[ | r[ | j d	 ƒ rÖ | j d	 d ƒ } n  | j d
 ƒ rú | j d
 d ƒ } n  | j d ƒ rq[ n  | j | ƒ q[ q[ Wd j	 | ƒ S(   Nc         S   s!   |  j  d ƒ } | t | ƒ d S(   Nt   /i   (   t   splitR   (   t   xpathStrt   l(    (    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   get_last_item/   s    c            s`   |  j  ƒ  } | j d ƒ sH | j d ƒ sH | j d ƒ sH | j d ƒ rL | Sd ˆ  |  j  ƒ  ƒ S(   Ns
   /html/bodys
   /root/items   /html/head/model/binds   /item/values   ${%s}(   t   groupt
   startswitht   endswith(   t   matcht   strmatch(   R%   (    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   replace_function3   s    s!   (/[a-z0-9\-_]+(?:/[a-z0-9\-_]+)+)t   flagst    s   .java:iÿÿÿÿs   	ats   java.lang.RuntimeException: s,   org.javarosa.xpath.XPathUnhandledException: s   java.lang.NullPointerExceptionu   
(
   t   ret   compilet   It   subt
   splitlinest   findR'   t   replacet   appendt   join(	   t   error_messageR+   t   patternt   kt   lastlinet   linet   has_java_filenamet   is_a_java_methodt   is_duplicate(    (   R%   sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   _cleanup_errors.   s*    	
	c         C   s¸   t  ƒ  s t d ƒ ‚ n  t d d t |  g d ƒ \ } } } } g  } | rR d g S| d k rw t d t | ƒ ƒ ‚ n= | d k r¡ | r | j d | ƒ n  | S| d k  r´ d	 g Sd
 S(   sa   
    Returns an array of warnings if the form is valid.
    Throws an exception if it is not
    s/   pyxform odk validate dependency: java not foundR   s   -jarid   s*   XForm took to long to completely validate.i    s   ODK Validate Errors:
s   ODK Validate Warnings:
s"   Bad return code from ODK Validate.N(   R    t   EnvironmentErrorR   t   ODK_VALIDATE_JARt	   ExceptionR?   R5   (   t   path_to_xformR   R   R   R   t   warnings(    (    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   check_xformY   s     	$t   __main__i   (   t   __doc__R   R.   t   syst
   subprocessR    R   R   R   t   patht   dirnamet   realpatht   __file__t   CURRENT_DIRECTORYR6   RA   R   R    R?   RE   t   __name__t   argv(    (    (    sK   /var/www/html/azizi.ilri.org/bower/pyxform/pyxform/odk_validate/__init__.pyt   <module>   s   			+	 