ó
šH—Vc           @   s‡  d  d l  Z  d  d l Z d  d l Z d Z d Z d e f d „  ƒ  YZ d d d „  ƒ  YZ d „  Z d	 d d
 „  ƒ  YZ	 d „  Z
 d d „ Z d d e d „ Z e d k rƒd  d l Z e j e j d d ƒ \ Z Z y e d Z Wn( e k
 re e j d ƒ e ‚ n+ Xe d Z e s6e e j d ƒ e ‚ n  d Z e rpe d d d k rpe d d Z d e GHn  e e e e ƒ n  d S(   iÿÿÿÿNs   .jss   @requires?:?\s+(\S*)\s*
t   MissingImportc           B   s   e  Z d  Z RS(   s>   Exception raised when a listed import is not found in the lib.(   t   __name__t
   __module__t   __doc__(    (    (    s   ../tools/mergejs.pyR    0   s   t
   SourceFilec           B   s5   e  Z d  Z d „  Z d „  Z e d e d d ƒ Z RS(   s3   
    Represents a Javascript source code file.
    c         C   s‰   | |  _  | |  _ g  |  _ g  |  _ t j t |  j ƒ } x@ | D]8 } t | | ƒ rh |  j j | ƒ q@ |  j j | ƒ q@ Wg  |  _	 d S(   s	   
        N(
   t   filepatht   sourcet   excludedFilest   requiredFilest   ret   findallt
   RE_REQUIREt	   undesiredt   appendt
   requiredBy(   t   selfR   R   t
   cfgExcludet   auxReqt   filename(    (    s   ../tools/mergejs.pyt   __init__8   s    				c         C   s   |  j  S(   sl   
        Extracts the dependencies specified in the source code and returns
        a list of them.
        (   R   (   R   (    (    s   ../tools/mergejs.pyt   _getRequirementsJ   s    t   fgett   doct    (   R   R   R   R   R   t   propertyt   requires(    (    (    s   ../tools/mergejs.pyR   3   s   		c         C   s   d |  GHd S(   s#   
    Displays a usage message.
    s3   %s [-c <config file>] <output.js> <directory> [...]N(    (   R   (    (    s   ../tools/mergejs.pyt   usageU   s    t   Configc           B   s   e  Z d  Z d „  Z RS(   sá  
    Represents a parsed configuration file.

    A configuration file should be of the following form:

        [first]
        3rd/prototype.js
        core/application.js
        core/params.js
        # A comment

        [last]
        core/api.js # Another comment

        [exclude]
        3rd/logger.js
        exclude/this/dir

    All headings are required.

    The files listed in the `first` section will be forced to load
    *before* all other files (in the order listed). The files in `last`
    section will be forced to load *after* all the other files (in the
    order listed).

    The files list in the `exclude` section will not be imported.

    Any text appearing after a # symbol indicates a comment.
    
    c         C   sæ   g  t  | ƒ D]C } | j ƒ  r | j ƒ  j d ƒ r t j d d | ƒ j ƒ  ^ q } | | j d ƒ d | j d ƒ !|  _ | | j d ƒ d | j d ƒ !|  _ | | j d ƒ d | j d ƒ !|  _ | | j d ƒ d |  _	 d	 S(
   sM   
        Parses the content of the named file and stores the values.
        t   #s   #.*?$R   s   [first]i   s   [last]s	   [include]s	   [exclude]N(
   t   opent   stript
   startswithR	   t   subt   indext
   forceFirstt	   forceLastt   includet   exclude(   R   R   t   linet   lines(    (    s   ../tools/mergejs.pyR   |   s    C&&&(   R   R   R   R   (    (    (    s   ../tools/mergejs.pyR   \   s   c         C   s_   |  | k } | s[ xF | D]; } | j  d ƒ s; | d 7} n  |  j | ƒ r t } Pq q Wn  | S(   Nt   /(   t   endswithR   t   True(   R   t   excludesR%   t   excludepath(    (    s   ../tools/mergejs.pyR   Š   s    c         C   s   t  |  d  | t ƒ S(   N(   t   runt   NoneR*   (   t   sourceDirectoryt
   configFile(    (    s   ../tools/mergejs.pyt   getNames˜   s    c         C   s³  d  } | r t | ƒ } n  g  } xò t j |  ƒ D]á \ } } } xÏ | D]Ç }	 |	 j t ƒ rG |	 j d ƒ rG t j j | |	 ƒ t	 |  ƒ d }
 |
 j
 d d ƒ }
 | rá | j rá |
 | j k sÎ |
 | j k r| j |
 ƒ qq| sû t |
 | j ƒ r| j |
 ƒ qqG qG Wq1 Wd d d d d	 d d d
 } i  } x` | D]X }
 d |
 GHt j j |  |
 ƒ j ƒ  } t | d ƒ j ƒ  } t |
 | | j ƒ | |
 <qEWHd d l m } t } d } xé | s©t } d | GH| d 7} xÃ | j ƒ  D]µ \ }
 } x¦ | j D]› } | j | ƒ st } t j j |  | ƒ j ƒ  } t j j | ƒ r…d | GHt | d ƒ j ƒ  } t | | | j ƒ | | <qžt d | |
 f ƒ ‚ qqWqíWqÁWi  } x' | j ƒ  D] \ }
 } | j | |
 <q½Wd GH| | ƒ } | r>d GH| j g  | D]* } | | j k r| | j k r| ^ q| j } n  Hg  } | rÙxx | D]p } t j j t j j |  | ƒ ƒ j
 d d ƒ } d G| GH| | } x | j D] } d | GHq¢W| j | ƒ qRWd t	 | ƒ GH| SxŠ | D]‚ } | | } d G| j GHx | j D] } d | GHqW| j | | j ƒ | j  } | j | ƒ | j d ƒ sà| j d ƒ qàqàWd t	 | ƒ GH| r¦d | GHt | d ƒ j! d j | ƒ ƒ n  d j | ƒ S(   Nt   .i   s   \R(   s   /* t   =iF   s   
    %s
s      s    */

s   Importing: %st   Uiÿÿÿÿ(   t   toposorts   Resolution pass %s... s'   File '%s' not found (required by '%s').s
   Sorting...s   Re-ordering files...s   Append: s!     Required file "%s" is excluded.s   
Total files: %d s   Exporting: s   
s   
Total files merged: %d s   
Generating: %st   wR   ("   R.   R   t   ost   walkR)   t   SUFFIX_JAVASCRIPTR   t   patht   joint   lent   replaceR$   R"   R   R   R%   R   R   t   readR   R5   t   FalseR*   t   itemsR   t   has_keyt   existsR    R#   t   normpathR   R   R   t   write(   R/   t   outputFilenameR0   t   returnAsListOfNamest   cfgt   allFilest   roott   dirst   filesR   R   t   HEADERt   fullpatht   contentR5   t   completet   resolution_passt   infoR:   t   dependenciest   ordert   itemt   resultt   fpt   fNamet   ft   fExcludeR   (    (    s   ../tools/mergejs.pyR-   œ   s”    #"			
	%H-	

		"t   __main__i   s   -c:i    s   -cs   Parsing configuration file: %s(    (    (   R	   R7   t   sysR9   R   t	   ExceptionR    R   R   R   R   R.   R1   R?   R-   R   t   getoptt   argvt   optionst   argsRE   t
   IndexErrort
   SystemExitR/   R0   R   (    (    (    s   ../tools/mergejs.pyt   <module>(   s:   "	.	m

	