
,]c           @  s  d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d l Z d d l	 Z	 d d l
 j Z d d l Z d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d	 l m Z m Z m Z d
 e f d     YZ d d  Z d   Z d   Z  d   Z! d   Z" d d  Z# d   Z$ d d d  Z% d d g  g  d  Z& d   Z' d   Z( d d  Z) d   Z* d   Z+ d   Z, d d d d  Z- d   Z. d   Z/ d   Z0 d   Z1 d S(    s   Running programs utilitiesi(   t   print_function(   t   LooseVersion(   t   getuserN(   t   is_anaconda(   t   encoding(   t   get_python_executable(   t   PY2t   is_text_stringt   to_text_stringt   ProgramErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyR	      s   c         C  s   t  j   g } t j d k r. | j d  n# t j t    } | j d |  |  d k	 rm | j |   n  t	 j
 |   } t	 j |  s t j |  n  | S(   sP   
    Return temporary Spyder directory, checking previously that it exists.
    t   ntt   spyders   spyder-N(   t   tempfilet
   gettempdirt   ost   namet   appendR   t   to_unicode_from_fsR   t   Nonet   ospt   joint   isdirt   mkdir(   t   suffixt   to_joint   usernamet   tempdir(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   get_temp_dir!   s    c         C  sM   xF t  j d j t  j  D]+ } t j | |   } t j |  r | Sq Wd S(   sX   
    Return program absolute path if installed in PATH.

    Otherwise, return None
    t   PATHN(   R   t   environt   splitt   pathsepR   R   t   isfile(   t   basenamet   patht   abspath(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   is_program_installed8   s     c         C  s   |  g } t  j d k rW d } |  j |  sW g  | D] } |  | ^ q4 |  g } qW n  x$ | D] } t |  } | r^ | Sq^ Wd S(   s   
    Find program in PATH and return absolute path

    Try adding .exe or .bat to basename on Windows platforms
    (return None if not found)
    R   s   .exes   .bats   .cmdN(   s   .exes   .bats   .cmd(   R   R   t   endswithR&   (   R#   t   namest
   extensionst   extR   R$   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   find_programD   s    	*c          K  sU   |  j  d t j d k  t j d k rQ d } d } | | O} |  j  d |  n  |  S(   sR  
    Given a dict, populate kwargs to create a generally
    useful default setup for running subprocess processes
    on different platforms. For example, `close_fds` is
    set on posix and creation of a new console window is
    disabled on Windows.

    This function will alter the given kwargs and return
    the modified dict.
    t	   close_fdst   posixR   i    i   t   creationflags(   t
   setdefaultR   R   (   t   kwargst   CONSOLE_CREATION_FLAGSt   CREATE_NO_WINDOW(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt#   alter_subprocess_kwargs_by_platformW   s    
c         K  s   d | k r& | d r& t  d   n
 t | d <d | k rR t j d  | d <n  x* d d d g D] } | j | t j  qb Wt |   } t j |  |  S(   sJ  
    Execute the given shell command.
    
    Note that *args and **kwargs will be passed to the subprocess call.

    If 'shell' is given in subprocess_kwargs it must be True,
    otherwise ProgramError will be raised.
    .
    If 'executable' is not given in subprocess_kwargs, it will
    be set to the value of the SHELL environment variable.

    Note that stdin, stdout and stderr will be set by default
    to PIPE unless specified in subprocess_kwargs.

    :str cmdstr: The string run as a shell command.
    :subprocess_kwargs: These will be passed to subprocess.Popen.
    t   shellsB   The "shell" kwarg may be omitted, but if provided it must be True.t
   executablet   SHELLt   stdint   stdoutt   stderr(	   R	   t   TrueR   t   getenvR/   t
   subprocesst   PIPER3   t   Popen(   t   cmdstrt   subprocess_kwargst   stream(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   run_shell_commandm   s    
	c         K  s   d | k r% | d r% t  d   n  t |   } | sJ t  d |    n  | g | pY g  } x* d d d g D] } | j | t j  qm Wt |   } t j | |  S(   s  
    Run program in a separate process.

    NOTE: returns the process object created by
    `subprocess.Popen()`. This can be used with
    `proc.communicate()` for example.

    If 'shell' appears in the kwargs, it must be False,
    otherwise ProgramError will be raised.

    If only the program name is given and not the full path,
    a lookup will be performed to find the program. If the
    lookup fails, ProgramError will be raised.

    Note that stdin, stdout and stderr will be set by default
    to PIPE unless specified in subprocess_kwargs.

    :str program: The name of the program to run.
    :list args: The program arguments.
    :subprocess_kwargs: These will be passed to subprocess.Popen.
    R4   sN   This function is only for non-shell programs, use run_shell_command() instead.s   Program %s was not foundR7   R8   R9   (   R	   R+   R/   R<   R=   R3   R>   (   t   programt   argsR@   t   fullcmdRA   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   run_program   s    	c         C  sC   d d l  m } d d l m } |   } | j |   | j |  S(   s   
    Generalized os.startfile for all platforms supported by Qt

    This function is simply wrapping QDesktopServices.openUrl

    Returns True if successfull, otherwise returns False.
    i(   t   QUrl(   t   QDesktopServices(   t   qtpy.QtCoreRG   t
   qtpy.QtGuiRH   t   setUrlt   openUrl(   t   filenameRG   RH   t   url(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt
   start_file   s
    	c         C  s   | d k	 s t  yI |  d k r7 t j |  d } n# t j t j |   d |  d } Wn t k
 ro d SXt j |  s | d 7} n  t j |  r | Sd S(   s   
    Return absolute path if Python script exists (otherwise, return None)
    package=None -> module is in sys.path (standard library modules)
    i   s   .pyNt   w(   R   t   AssertionErrort   impt   find_moduleR   R   t   ImportErrorR"   (   t   packaget   moduleR$   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   python_script_exists   s    'c         C  sp   | d k	 s t  t | t t f  r< t | t t f  sB t  t |  |  } t t j | | g |  d S(   sv   
    Run Python script in a separate process
    package=None -> module is in sys.path (standard library modules)
    N(	   R   RQ   t
   isinstancet   tuplet   listRW   RF   t   sysR5   (   RU   RV   RD   t   p_argsR$   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   run_python_script   s    0c         C  sm   t  |   s t  d } g  } xH t j | |   D]4 } | j   r1 | j | j d  j d   q1 q1 W| S(   sc  
    Split the string `text` using shell-like syntax

    This avoids breaking single/double-quoted strings (e.g. containing
    strings with spaces). This function is almost equivalent to the shlex.split
    function (see standard library `shlex`) except that it is supporting
    unicode strings (shlex does not support unicode until Python 2.7.3).
    s/   (\s+|(?<!\\)".*?(?<!\\)"|(?<!\\)\'.*?(?<!\\)\')t   "t   '(   R   RQ   t   reR    t   stripR   (   t   textt   patternt   outt   token(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   shell_split   s    	&c         C  s   g  } | d k	 r% | | j   7} n  | r; | j d  n  | rW | j d d g  n  |  d k	 r t j d k r | r | j t j |   j t j	 d   q | j |   n  | r | j t
 |   n  | S(   s&   Construct Python interpreter argumentss   -is   -mt   pdbR   t   /N(   R   R    R   t   extendR   R   R   t   normpatht   replacet   sepRf   (   t   fnamet   python_argst   interactt   debugt   end_argsR\   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   get_python_args   s    (c         C  s  | d k r t   } n  t j d k rX d |  d }  | j d d  } d | d } n  | g } | t |  | | | |  7} t j d k rrd } | r | d | d 7} n  | d j |  d d	 7} t r t j	 |  } t j	 |  } n  y' | rt
 | d
 | n
 t
 |  Wqt k
 rnd d l m }	 d d l m }
 |	 j d |
 d  |
 d  |	 j  qXnt j d k ri d d 6d d 6d d 6i d d 6d d 6d d 6i d d 6d d 6d d 6i d d 6d d 6d d 6g } x | D] } t | d  rg  } | d r1| r1| | d | g 7} n  | j | d  | | 7} | rlt | d | d
 | n t | d |  d SqWn t  d S(   sl   
    Run Python script in an external system terminal.

    :str wdir: working directory, may be empty.
    R   R^   Rh   s   \s   start cmd.exe /K "s   cd s    && t    s
    ^&^& exitt   cwdi(   t   QMessageBox(   t   _t   Runs<   It was not possible to run this file in an external terminalR-   s   gnome-terminalt   cmds   --working-directorys   wdir-options   -xs   execute-optiont   konsoles	   --workdirs   -es   xfce4-terminalt   xtermN(   R   R   R   R   Rk   Rr   R   R   R   t   to_fs_from_unicodeRB   t   WindowsErrort   qtpy.QtWidgetsRu   t   spyder.config.baseRv   t   criticalt   OkR&   R   RF   t   NotImplementedError(   Rm   t   wdirRD   Ro   Rp   Rn   R5   R\   Rx   Ru   Rv   t   programsRC   t   arglist(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   run_python_script_in_terminal  sf    		




c         C  sI   t  |  t  s! |  j d  }  n  |  d } t j d |  sA t St Sd S(   s   
    A stable version has no letters in the final component, but only numbers.

    Stable version example: 1.2, 1.3.4, 1.0.5
    Not stable version: 1.2alpha, 1.3.4beta, 0.1.0rc1, 3.0.0dev
    t   .is   [a-zA-Z]N(   RX   RY   R    R`   t   searchR:   t   False(   t   versiont	   last_part(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   is_stable_version[  s    
c         C  s  t  |  t  r: d j g  |  D] } t |  ^ q  }  n  t |  r{ t |   r{ |  j |  r{ | |  k r{ | d } nA t |   r t |  r | j |   r | |  k r |  d }  n  y | d k r t |   t |  k S| d k rt |   t |  k S| d k r%t |   t |  k S| d k rGt |   t |  k  S| d k rit |   t |  k St SWn t k
 rt	 SXd S(	   s  
    Check version string of an active module against a required version.

    If dev/prerelease tags result in TypeError for string-number comparison,
    it is assumed that the dependency is satisfied.
    Users on dev branches are responsible for keeping their own packages up to
    date.
    
    Copyright (C) 2013  The IPython Development Team

    Distributed under the terms of the BSD License.
    R   t   zzt   >s   >=t   =t   <s   <=N(
   RX   RY   R   t   strR   t
   startswithR   R   t	   TypeErrorR:   (   t   actverR   t   cmp_opt   i(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   check_versionl  s,    +c         C  s(   t  |   } t | d t | d d   S(   s<   Return module version or None if version can't be retrieved.t   __version__t   VERSIONN(   t
   __import__t   getattrR   (   t   module_namet   mod(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   get_module_version  s    c         C  s  | rt  j |  rd | k rt j t  } t j t  } t j t  } t j t  } t j	 d d d d t
   d t } z"| j }	 | j d d  | j d	 d
  | j d  | j | d
  | j | d
  | j | d
  | j | d
  | r | j d |  | f  n | j d |   | j   t j |  | j   y( t | |	 g  }
 |
 j   \ } } Wn t j k
 rt SXt | j    SWd | j s| j   n  t j |	  Xqt Sn&| d k ry t |   } Wqt SXn | } | d k r | d k	 r t S| d k r0t Sd | k rut } x, | j d  D] } | ojt |  |  } qRW| St j d |  } | d k	 st d   | | j     } | sd } n  | d k st d |   | | j    } t | | |  Sd S(   s  
    Return True if module *module_name* is installed

    If version is not None, checking module version
    (module must have an attribute named '__version__')

    version may starts with =, >=, > or < to specify the exact requirement ;
    multiple conditions may be separated by ';' (e.g. '>=0.13;<1.0')

    interpreter: check if a module is installed with a given version
    in a determined interpreter
    t   pythont   wtR   s   .pyt   dirt   deletes   # -*- coding: utf-8 -*-s   

s*   from distutils.version import LooseVersions   
s	   import res%   print(is_module_installed('%s','%s'))s    print(is_module_installed('%s'))Nt   ;s   [0-9]s   Invalid version numberR   s   >=R   R   s   <=s   Invalid version condition '%s's   import re

(   s   >=R   R   R   s   <=(!   R   R"   t   inspectt	   getsourceR   R   R   t   is_module_installedR   t   NamedTemporaryFileR   R   R   t   writet   flushR   t   fsynct   closeRF   t   communicateR<   t   CalledProcessErrorR:   t   evalt   decodet   closedt   removeR   R    R`   R   RQ   t   start(   R   R   t   installed_versiont   interpretert   checkvert
   get_modvert
   stable_vert
   ismod_instt   ft   scriptt   proct   outputt   _errR   t   vert   matcht   symb(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyR     st    		

		c         C  s3   d } t  j | |  d t  j d k r+ t St Sd S(   s8   Check that the python interpreter file has a valid name.s   .*python(\d\.?\d*)?(w)?(.exe)?$t   flagsN(   R`   R   t   IR   R   R:   (   RM   Rc   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt    is_python_interpreter_valid_name  s    !c         C  s   t  j j |   } t j |  s/ t |   r3 t St |   r t  j d k rh t	 j
 |  sa t St Sq t j d k r t   r t	 j
 |  r t St	 j
 |  s t St Sq t Sn t	 j
 |  r t St |   Sd S(   s6   Evaluate wether a file is a python interpreter or not.R   t   darwinN(   R   R$   t   realpathR   R"   R   R   t
   is_pythonwR   R   t   is_text_fileR:   R[   t   platformR   t   check_python_help(   RM   t   real_filename(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   is_python_interpreter  s&    c         C  s3   d } t  j | |  d t  j d k r+ t St Sd S(   s0   Check that the python interpreter has 'pythonw'.s   .*python(\d\.?\d*)?w(.exe)?$R   N(   R`   R   R   R   R   R:   (   RM   Rc   (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyR     s    !c         C  sa   yR t  |  d g  } t | j   d  } d } d | k rM | | k rM t St SWn t SXd S(   s3   Check that the python interpreter can execute help.s   -hi    s?   Options and arguments (and corresponding environment variables)s   usage:N(   RF   R   R   R:   R   (   RM   R   R   t   valid(    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyR   %  s    (2   t   __doc__t
   __future__R    t   distutils.versionR   t   getpassR   RR   R   R   t   os.pathR$   R   R`   R<   R[   R   t   spyder.config.utilsR   t   spyder.utilsR   t   spyder.utils.miscR   t   spyder.py3compatR   R   R   t	   ExceptionR	   R   R   R&   R+   R3   RB   RF   RO   RW   R]   Rf   Rr   R   R   R   R   R   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/spyder/utils/programs.pyt   <module>   sJ   				#&			J		*	W		 		