2012-04-17 3 views
0

Я пытаюсь использовать pexpect для автоматизации этого диалога. Также ниже представлена ​​программа python с использованием pexpect. Когда я запускаю код ... он ждет ввода в «Вы хотите создать его сейчас?» (Да/нет): «Затем перерыв с ошибкой. Ожидается «да» или «нет». Итак ... где я ошибался? Строка соответствует первому входу?Использование pexpect для автоматизации диалога manage.py syncdb

[email protected]:/opt/graphite/webapp/graphite$ sudo python manage.py syncdb 
Creating tables ... 
Creating table account_profile 
Creating table account_variable 
Creating table account_view 
Creating table account_window 
Creating table account_mygraph 
Creating table dashboard_dashboard_owners 
Creating table dashboard_dashboard 
Creating table events_event 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_user_permissions 
Creating table auth_user_groups 
Creating table auth_user 
Creating table auth_message 
Creating table django_session 
Creating table django_admin_log 
Creating table django_content_type 
Creating table tagging_tag 
Creating table tagging_taggeditem 

You just installed Django's auth system, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): 
Username (Leave blank to use 'root'): 
E-mail address: 
Password: 
Password (again): 

Python скрипт:

import pexpect 
child = pexpect.spawn ('python /opt/graphite/webapp/graphite/manage.py syncdb') 
child.expect ('Would you like to create one now? (yes/no):') 
child.sendline ('yes') 
child.expect ("""Username (Leave blank to use 'root'):""") 
child.sendline ('admin') 
child.expect ("E-mail address:") 
child.sendline ('[email protected]') 
child.expect ('Password:') 
child.sendline ('test') 
child.expect ('Password (again):') 
child.sendline ('test') 





Creating tables ... 
Creating table account_profile 
Creating table account_variable 
Creating table account_view 
Creating table account_window 
Creating table account_mygraph 
Creating table dashboard_dashboard_owners 
Creating table dashboard_dashboard 
Creating table events_event 
Creating table auth_permission 
Creating table auth_group_permissions 
Creating table auth_group 
Creating table auth_user_user_permissions 
Creating table auth_user_groups 
Creating table auth_user 
Creating table auth_message 
Creating table django_session 
Creating table django_admin_log 
Creating table django_content_type 
Creating table tagging_tag 
Creating table tagging_taggeditem 

You just installed Django's auth system, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): Traceback (most recent call last): 
    File "test.py", line 5, in <module> 
    child.expect ('Would you like to create one now? (yes/no):') 
    File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1311, in expect 
    return self.expect_list(compiled_pattern_list, timeout, searchwindowsize) 
    File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1325, in expect_list 
    return self.expect_loop(searcher_re(pattern_list), timeout, searchwindowsize) 
    File "/usr/lib/python2.7/dist-packages/pexpect.py", line 1409, in expect_loop 
    raise TIMEOUT (str(e) + '\n' + str(self)) 
pexpect.TIMEOUT: Timeout exceeded in read_nonblocking(). 
<pexpect.spawn object at 0x7ffb875847d0> 
version: 2.3 ($Revision: 399 $) 
command: /usr/bin/python 
args: ['/usr/bin/python', 'manage.py', 'syncdb'] 
searcher: searcher_re: 
    0: re.compile("Would you like to create one now? (yes/no):") 
buffer (last 100 chars): em, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): 
before (last 100 chars): em, which means you don't have any superusers defined. 
Would you like to create one now? (yes/no): 
after: <class 'pexpect.TIMEOUT'> 
match: None 
match_index: None 
exitstatus: None 
flag_eof: False 
pid: 12938 
child_fd: 3 
closed: False 
timeout: 30 
delimiter: <class 'pexpect.EOF'> 
logfile: <open file '<stdout>', mode 'w' at 0x7ffb876c51e0> 
logfile_read: None 
logfile_send: None 
maxread: 2000 
ignorecase: False 
searchwindowsize: None 
delaybeforesend: 0.05 
delayafterclose: 0.1 
delayafterterminate: 0.1 
+0

указать 'тайм-аут = 300' в pexpect.spawn называют себя, если этот процесс занимает много времени, а также проверить и показать выход child.before/child.after – avasal

+0

Hi .. Я добавил трассировку. Время в 30 секунд. Этот процесс не так долго прослеживается. Он висит на первом входе. – Tampa

ответ

1

Я считаю, что pexpect использует синтаксис регулярных выражений.

child.expect ('Would you like to create one now.*?:') 

Я предполагаю, что вышеуказанное будет работать нормально.
Вы также можете избежать «?» и, возможно, скобки, чтобы получить лучшие результаты.

child.expect ('Would you like to create one now\? \(yes\/no\):') 
+0

Существует также метод 'expect_exact', который соответствует регулярным строкам. –

1

Синтаксис регулярного выражения также меня отключил. Вот код автоматизации, который работал для меня:

p = pexpect.spawn("python manage.py syncdb") 
p.logfile = sys.stdout 
p.expect(r"Would you like to create one now\? \(yes\/no\): ") 
p.sendline("yes") 
p.expect(r"Username \(leave blank to use '.*'\): ") 
p.sendline("admin") 
p.expect(r"Email address: ") 
p.sendline("[email protected]") 
p.expect(r"Password: ") 
p.sendline("admin") 
p.expect(r"Password \(again\): ") 
p.sendline("admin") 
Смежные вопросы