Ticket #4180: trac_4180.patch

File trac_4180.patch, 2.5 KB (added by mhansen, 12 years ago)
  • sage/interfaces/expect.py

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1222215024 25200
    # Node ID 78a8841408f6937b0beda668f4ef7c7aaab5fa3e
    # Parent  8864c8449d87fa4521aa48589e2127a9c283477e
    Add magic environment variable SAGE_PEXPECT_LOG to automatically creat logfiles for all the pexpect
    interfaces.
    
    diff -r 8864c8449d87 -r 78a8841408f6 sage/interfaces/expect.py
    a b  
    202202        self._session_number = 0
    203203        self.__init_code = init_code
    204204        self.__max_startup_time = max_startup_time
     205
     206        #Handle the log file
    205207        if isinstance(logfile, basestring):
    206208            logfile = open(logfile,'w')
    207209        self.__logfile = logfile
     210
     211       
    208212        quit.expect_objects.append(weakref.ref(self))
    209213        self._available_vars = []
    210214        ParentWithBase.__init__(self, self)
     
    414418    def _start(self, alt_message=None, block_during_init=True):
    415419        self.quit()  # in case one is already running
    416420        global failed_to_start
    417         #if self.__name in failed_to_start:
    418         #    if alt_message:
    419         #        raise RuntimeError, alt_message
    420         #    else:
    421         #        raise RuntimeError, 'Unable to start %s (%s failed to start during this SAGE session; not attempting to start again)\n%s'%(self.__name, self.__name, self._install_hints())
    422421
    423422        self._session_number += 1
    424423        current_path = os.path.abspath('.')
     
    427426            os.makedirs(dir)
    428427        os.chdir(dir)
    429428
     429        #If the 'SAGE_PEXPECT_LOG' environment variable is set and
     430        #the current logfile is None, then set the logfile to be one
     431        #in .sage/pexpect_logs/
     432        if self.__logfile is None and 'SAGE_PEXPECT_LOG' in os.environ:
     433            from sage.misc.all import DOT_SAGE
     434            logs = '%s/pexpect_logs'%DOT_SAGE
     435            if not os.path.exists(logs):
     436                os.mkdir(logs)
     437
     438            filename = '%s/%s-%s-%s-%s.log'%(logs, self.name(), os.getpid(), id(self), self._session_number)
     439            self.__logfile = open(filename, 'w')
     440
    430441        cmd = self.__command
    431 ##         try:
    432 ##             cmd = _absolute(self.__command)
    433 ##         except RuntimeError:
    434 ##             failed_to_start.append(self.__name)
    435 ##             raise RuntimeError, "%s\nCommand %s not available."%(
    436 ##                  self._install_hints(), self.__name)
    437        
     442
    438443        if self.__verbose_start:
    439444            print cmd
    440445            print "Starting %s"%cmd.split()[0]