2012-11-27 14 views
5

मेरे पास एक कोड है जो कोड में कुछ प्रक्रियाओं और कुछ प्रिंटों को नियंत्रित करने के लिए pexpect का उपयोग करता है। मुख्य लक्ष्य (इस प्रश्न में) pexpect आउटपुट और कुछ लॉग फ़ाइल में लॉग इन प्रिंट करना है। जिस समस्या में मैंने भाग लिया वह यह है कि pexpect लाइनें (भेजा और प्राप्त डेटा) प्रिंट के साथ कोई स्पष्ट तर्क नहीं मिला है। मैं उम्मीद कर रहा था कि प्रिंट स्ट्रिंग और pexpect आउटपुट जारी किए गए क्रम में लॉग इन होंगे।प्रिंट और pexpect लॉगिंग

नमूना कोड निम्नलिखित है:

#!/usr/bin/env python 

import pexpect 
import time, sys, os 

############################################################################### 
# Subclass of file object to avoid recording extensive whitespace characters 
class CleanFile(file): 
    def write (self, text): 
     # Remove the whitespaces 
     out_text = '' 
     # process the backspace properly 
     bline = '' 
     for c in text: 
      if (ord(c) == 0x8): 
       if (len(bline) == 0): 
        # Move the file pointer. 
        file.seek(self, -1, os.SEEK_CUR); 
       else: 
        bline = bline[:-1] 
      else: 
       bline += c 

     # remove whitespaces from inside a line 
     out_text += ''.join(c for c in bline if (ord(c) >= 32 or ord(c) == 10)); 

     file.write(self, out_text); 

############################################################################### 
def main(): 
    fout = CleanFile ("options.log_file.log", 'w') 

    sys.stdout = os.fdopen (sys.stdout.fileno(), 'w', 0) 
    os.dup2 (fout.fileno(), sys.stdout.fileno()); 

    p = pexpect.spawn ('tclsh') 
    p.logfile = fout 

    print "Got into tclsh." 
    p.sendline('ls'); 
    p.expect (['%',pexpect.EOF]) 

    p.sendline('info tclversion'); 
    p.expect (['%',pexpect.EOF]) 

    print "Got the version\n" 

    p.sendline('info commands %'); 
    p.expect (['%',pexpect.EOF]) 

    p.sendline('exit'); 

    print 'Ended session' 

############################################################################### 
if __name__ == "__main__": 
    main() 

यह आउटपुट लॉग फ़ाइल सामग्री है:

Got into tclsh. 
ls 
% lsinfo tclversion 

log options.log_file.log pexpect_test.py runtests.py runtests_steinway.py 
% info tclversionGot the version 

info commands % 

8.4 
% info commands %exit 
Ended session 

वहाँ pexpect और प्रिंट आउटपुट अनुक्रमिक बनाने के लिए कोई तरीका है?


अद्यतन: pexpectmanual page के आधार पर: "कृपया ध्यान दें, तथापि, कि बफरिंग इस व्यवहार के बाद से इनपुट अप्रत्याशित मात्रा में आता है प्रभावित कर सकते हैं"। तो यह लॉगिंग को संभावित रूप से प्रभावित कर सकता है।

उत्तर

2

यदि आप परिणामों के लिए स्क्रिप्ट समाप्त होने तक प्रतीक्षा कर सकते हैं, तो pexpect कमांड के लिए लॉग फ़ाइल सेट न करें, कमांड के परिणामों को सहेजने के लिए, और अंत में सबकुछ प्रिंट करें।

ध्यान दें कि आप info commands कमांड का आउटपुट खो रहे हैं। इसे tclsh दुभाषिया को कमांड के अंत से '%' को शुरू करने और हटाने के लिए एक उम्मीद() जोड़कर तय किया जा सकता है। मुझे लगता है कि एक टाइपो था।

मुख्य कार्य को संशोधित होने के लिए:

def main():               
    fout = CleanFile ("options.log_file.log", 'w')     

    sys.stdout = os.fdopen (sys.stdout.fileno(), 'w', 0)    
    os.dup2 (fout.fileno(), sys.stdout.fileno());      

    p = pexpect.spawn ('tclsh')          
    p.expect (['%',pexpect.EOF])          

    p.sendline('ls');             
    p.expect (['%',pexpect.EOF])          
    ls = p.before              

    p.sendline('info tclversion');         
    p.expect (['%',pexpect.EOF])          
    tclversion = p.before            

    p.sendline('info commands');          
    p.expect (['%',pexpect.EOF])          
    commands = p.before            

    p.sendline('exit');            
    p.close()               

    print "Got into tclsh."           
    print ls               
    print tclversion             
    print "Got the version\n"           
    print commands             
    print "Ended session"            

उत्पादन तो है:

Got into tclsh.              
ls                 
options.log_file.log pexpect_test.py         

info tclversion              
8.5                 

Got the version              

info commands           
tell socket subst open eof pwd glob list pid exec auto_load_index time unknown 
eval lassign lrange fblocked lsearch auto_import gets case lappend proc break v 
ariable llength auto_execok return linsert error catch clock info split array i 
f fconfigure concat join lreplace source fcopy global switch auto_qualify updat 
e close cd for auto_load file append lreverse format unload read package set bi 
nary namespace scan apply trace seek while chan flush after vwait dict continue 
uplevel foreach lset rename fileevent regexp lrepeat upvar encoding expr unset 
load regsub history interp exit puts incr lindex lsort tclLog string   

Ended session               
+0

अच्छा विचार है। क्या अभी भी तुरंत काम छापेगा? इसे तब 'प्रिंट' विधि द्वारा नियंत्रित किया जाना चाहिए। मेरे पास एक सवाल है: क्या पी। पहले + पी। मैच + पी। बाद में इस्तेमाल किया जाना चाहिए? – ilya1725

+0

तुरंत प्रिंटिंग काम नहीं करती है क्योंकि डेटा आपकी मूल लिपि में असीमित रूप से वापस आ जाता है। पी। इससे मेल खाने वाली स्ट्रिंग से पहले सब कुछ लौटाता है। चूंकि मिलान की गई स्ट्रिंग प्रॉम्प्ट है, इसलिए सभी कमांड आउटपुट पहले से ही आ चुके हैं। – Edu

संबंधित मुद्दे