ó
¸9—^c           @   sö  d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l Z d  d l m Z m Z y4 d  d l	 m
 Z
 d  d l m Z d  d l m Z Wne e k
 rñ y4 d  d l m Z d  d l m
 Z
 d  d l m Z Wqò e k
 rí e d ƒ ‚ qò Xn Xd  d l m Z d  d	 l m Z yJ d  d
 l m Z d  d l m Z d  d l m Z m Z d  d l m Z WnM e k
 r«d  d
 l m Z d  d l m Z m Z m Z d  d l  m Z n Xy d  d l! Z! e" Z# Wn e k
 rÛe$ Z# n Xd e f d „  ƒ  YZ% d S(   iÿÿÿÿN(   t   LooseVersion(   t   BaseCommandt   CommandError(   t
   PipSession(   t   parse_requirements(   t   get_installed_distributionss#   Pip version 6 or higher is required(   t   color_style(   t   signalcommand(   t   urlparse(   t	   HTTPError(   t   Requestt   urlopen(   t   ServerProxy(   R	   R
   R   t   Commandc           B   s_   e  Z d  Z d „  Z e d „  ƒ Z d	 d „ Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z RS(
   s4   Scan pip requirement files for out-of-date packages.c         C   s   t  t |  ƒ j | ƒ | j d d d d d d d d ƒ| j d	 d
 d d d d d g  d d d d ƒf | j d d d d d d d d ƒd  S(   Ns   -ts   --github-api-tokent   actiont   storet   destt   github_api_tokent   helps"   A github api authentication token.s   -rs   --requirementt   appendt   requirementst   defaultt   metavart   FILENAMEse   Check all the packages listed in the given requirements file. This option can be used multiple times.s   -ns   --newert
   store_truet
   show_newers9   Also show when newer version then available is installed.(   t   superR   t   add_argumentst   add_argument(   t   selft   parser(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyR   /   s    
c   	   	   O   s]  t  ƒ  |  _ | |  _ | d r, | d } nÜ t j j d ƒ rJ d g } n¾ t j j d ƒ rÀ g  t j d ƒ D]K } t j j t j j d | ƒ ƒ rl | j	 ƒ  j
 d ƒ rl d j | ƒ ^ ql } nH t j j d ƒ rÞ d g } n* t j j d ƒ rü d g } n t d ƒ ‚ i  |  _ t ƒ  ­ } x£ | D]› } x’ t | d | ƒD]~ } | j rU| j n	 | j j } t t j ƒ t d	 ƒ k ri | d
 6| j d 6|  j | <q=i | d
 6| j d 6|  j | <q=Wq$WWd  QX| d rã| d |  _ n3 t j j d ƒ rt j j d ƒ |  _ n	 d  |  _ |  j ƒ  t r3|  j ƒ  n |  j j |  j j  d ƒ ƒ |  j! ƒ  d  S(   NR   s   requirements.txts   .txts   requirements/{0}s   requirements-dev.txts   requirements-prod.txts   Requirements file(s) not foundt   sessions   6.0.8t   pip_reqt   urlR   t   GITHUB_API_TOKENsY   Cannot check github urls. The requests library is not installed. ( pip install requests )("   R   t   stylet   optionst   ost   patht   existst   listdirt   isfilet   joint   lowert   endswitht   formatR   t   reqsR   R   t   namet   linkt   filenameR    t   pipt   __version__R!   R   t   environt   gett   Nonet
   check_pypit   HAS_REQUESTSt   check_githubt   stdoutt   writet   ERRORt   check_other(	   R   t   argsR$   t	   req_filest   fR   R1   t   reqR/   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyt   handle@   sH    	
N	"
	
c         C   s+   t  | d | ƒ} t j t | ƒ j ƒ  ƒ S(   s#   Shorcut for return contents as jsont   headers(   R
   t   jsont   loadsR   t   read(   R   R!   RC   RA   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyt   _urlopen_as_jsont   s    c         C   s   t  j d t | ƒ ƒ S(   Ns   ([ab]|rc|dev)\d+$(   t   ret   searcht   str(   R   t   version(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyt
   _is_stabley   s    c         C   sl   |  j  | ƒ rT g  | D]! } |  j  t | ƒ ƒ r | ^ q } | rT t | d ƒ Sn  | rh t | d ƒ Sd  S(   Ni    (   RL   R    R6   (   R   t   dist_versiont	   availablet   vt   stable(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyt   _available_version|   s
    .c   
      C   sé  xC t  ƒ  D]8 } | j } | |  j j ƒ  k r
 | |  j | d <q
 q
 Wt d ƒ } xt |  j j ƒ  ƒ D]y\ } } | d r„ qh n1d | k r©| d } t | j ƒ } | j	 | d j
 t ƒ pä | j	 | d j
 j d d ƒ t ƒ } |  j | | ƒ } | s|  j j d ƒ } nt |  j d rK| | k rK|  j j d	 j | ƒ ƒ } n= | | k ru|  j j d
 j | ƒ ƒ } n d } |  j | =qh |  j j d j d | ƒ ƒ }	 n d } | }	 |  j j d j d |	 d | ƒ ƒ |  j | =qh Wd S(   s>   If the requirement is frozen to pypi, check for a new version.t   dists   https://pypi.python.org/pypiR!   R    t   -t   _sF   release is not on pypi (check capitalization and/or --extra-index-url)R   s   {0} available (newer installed)s   {0} availables
   up to dates"   {dist.project_name} {dist.version}s   not installeds   {pkg_info:40} {msg}t   pkg_infot   msgN(   R   t   project_nameR.   t   keysR   t   listt   itemsR    RK   t   package_releasesR/   t   Truet   replaceRQ   R#   t   WARNR$   t   INFOR-   t   BOLDR:   R;   (
   R   RR   R/   t   pypiRA   RM   RN   t   available_versionRV   RU   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyR7   „   s6    	"

>
!"c         C   s”  xt  |  j j ƒ  ƒ D]v\ } } | d } | s8 q n  t | ƒ } | j d ƒ re d | k re q n  | j d0 ƒ rz q n  i d d 6} |  j r© d	 j |  j ƒ | d
 <n  yÂ t | ƒ j	 j
 d d ƒ d j d ƒ j d ƒ j
 d ƒ } t | ƒ d k r| \ } } nc d | k rP| d  \ } } | d | d j d d ƒ j d d ƒ 7} n |  j j d | f ƒ w WnB t t f k
 r¯} |  j j |  j j d | | f ƒ ƒ q n Xy t j d d | ƒj ƒ  }	 Wn. t k
 rÿ} |  j j d t | ƒ ƒ d SXd |	 k r<|	 d d k r<|  j j |  j j d ƒ ƒ d Sd |	 k r{|	 d j d ƒ r{|  j j |  j j d ƒ ƒ d Sd }
 d | k rÁ| j
 d ƒ \ } } | j d ƒ rå| d }
 qån$ d | k rå| j
 d ƒ \ } }
 n  |
 d k r|  j j d  ƒ } n  |
 rd! j | | ƒ } t j | d | ƒj ƒ  } d" j | | |
 ƒ } t j | d | ƒj ƒ  } d | k rª| d d# k rª|  j j d$ j |
 d%  | ƒ ƒ } q| d& g  | D] } | d' d& ^ q¸k rê|  j j d( ƒ } q|  j j d) j | d& d%  ƒ ƒ } n  d* | k r5d+ j d* | d* ƒ } n+ |
 d k rJ| } n d, j | |
 d%  ƒ } |  j j d- j d. | d/ | ƒ ƒ |  j | =q Wd S(1   s–  
        If the requirement is frozen to a github url, check for new commits.

        API Tokens
        ----------
        For more than 50 github api calls per hour, pipchecker requires
        authentication with the github api by settings the environemnt
        variable ``GITHUB_API_TOKEN`` or setting the command flag
        --github-api-token='mytoken'``.

        To create a github api token for use at the command line::
             curl -u 'rizumu' -d '{"scopes":["repo"], "note":"pipchecker"}' https://api.github.com/authorizations

        For more info on github api tokens:
            https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
            http://developer.github.com/v3/oauth/#oauth-authorizations-api

        Requirement Format
        ------------------
        Pipchecker gets the sha of frozen repo and checks if it is
        found at the head of any branches. If it is not found then
        the requirement is considered to be out of date.

        Therefore, freezing at the commit hash will provide the expected
        results, but if freezing at a branch or tag name, pipchecker will
        not be able to determine with certainty if the repo is out of date.

        Freeze at the commit hash (sha)::
            git+git://github.com/django/django.git@393c268e725f5b229ecb554f3fac02cfc250d2df#egg=Django
            https://github.com/django/django/archive/393c268e725f5b229ecb554f3fac02cfc250d2df.tar.gz#egg=Django
            https://github.com/django/django/archive/393c268e725f5b229ecb554f3fac02cfc250d2df.zip#egg=Django

        Freeze with a branch name::
            git+git://github.com/django/django.git@master#egg=Django
            https://github.com/django/django/archive/master.tar.gz#egg=Django
            https://github.com/django/django/archive/master.zip#egg=Django

        Freeze with a tag::
            git+git://github.com/django/django.git@1.5b2#egg=Django
            https://github.com/django/django/archive/1.5b2.tar.gz#egg=Django
            https://github.com/django/django/archive/1.5b2.zip#egg=Django

        Do not freeze::
            git+git://github.com/django/django.git#egg=Django

        R!   t   gits   github.com/s   .tar.gzs   .tar.bz2s   .zips   application/jsons   content-types	   token {0}t   Authorizationt   #i   i    t   /i   t   archivet   @iÿÿÿÿt    s   
Failed to parse %r
s   
Failed to parse %r: %s
s   https://api.github.com/django/RC   s   
%s
Nt   messages   Bad credentialss(   
Github API: Bad credentials. Aborting!
s   API Rate Limit Exceededs,   
Github API: Rate Limit Exceeded. Aborting!
s   .gits   repo is not frozens-   https://api.github.com/repos/{0}/{1}/branchess0   https://api.github.com/repos/{0}/{1}/commits/{2}s	   Not Founds*   {0} not found in {1}. Repo may be private.i
   t   shat   commits
   up to dates!   {0} is not the head of any branchRR   s"   {dist.project_name} {dist.version}s   {0} {1}s   {pkg_info:40} {msg}RU   RV   (   s   .tar.gzs   .tar.bz2s   .zip(   RY   R.   RZ   RJ   t
   startswithR,   R   R-   R   R&   t   splitt   stript   rstript   lenR]   R#   R<   t
   ValueErrort
   IndexErrorR:   R;   t   requestsR5   RD   R	   R6   R`   R_   (   R   R/   RA   t   req_urlRC   t
   path_partst   usert   repot   et	   test_autht   frozen_commit_shat	   repo_namet   frozen_commit_fullRV   t
   branch_urlt   branch_datat   frozen_commit_urlt   frozen_commit_datat   branchRU   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyR9   ¦   s~    /"

	:-&%+&	"c         C   sÄ   |  j  rÀ |  j j |  j j d ƒ ƒ x˜ |  j  j ƒ  D]„ \ } } d | k rf d j d | d ƒ } n+ d | k r‹ d j d | d ƒ } n d } |  j j |  j j d j d | ƒ ƒ ƒ q5 Wn  d	 S(
   sÃ   
        If the requirement is frozen somewhere other than pypi or github, skip.

        If you have a private pypi or use --extra-index-url, consider contributing
        support here.
        s7   
Only pypi and github based requirements are supported:RR   s"   {dist.project_name} {dist.version}R!   s   {url}s   unknown packages1   {pkg_info:40} is not a pypi or github requirementRU   N(   R.   R:   R;   R#   R<   RZ   R-   R`   (   R   R/   RA   RU   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyR=   )  s    	N(   t   __name__t
   __module__R   R   R   RB   R6   RG   RL   RQ   R7   R9   R=   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyR   ,   s   	4			"	ƒ(&   RD   R%   RH   t   distutils.versionR    R2   t   django.core.management.baseR   R   t   pip._internal.downloadR   t   pip._internal.req.req_fileR   t   pip._internal.utils.miscR   t   ImportErrort   pip.downloadt   pip.reqt"   django_extensions.management.colorR   t"   django_extensions.management.utilsR   t   urllib.parseR   t   urllib.errorR	   t   urllib.requestR
   R   t   xmlrpc.clientR   t   urllib2t	   xmlrpclibRt   R\   R8   t   FalseR   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/pipchecker.pyt   <module>   sB   

