使用pexpect自动执行manage.py syncdb对话框 [英] Using pexpect to automate a manage.py syncdb dialogue

查看:82
本文介绍了使用pexpect自动执行manage.py syncdb对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用pexpect自动执行此对话.下面也是使用pexpect的python程序.当我运行代码...时,它等待输入您现在要创建一个吗?(是/否):",然后由于错误而超时.期望是或否.所以...我哪里出错了?字符串与第一个输入匹配吗?

I am trying to use pexpect to automate this dialogue. Also below is the python program using pexpect. When I run the code...it waits for input at "Would you like to create one now? (yes/no):" then timesout with an error. It is expecting a yes or no. So...where did I err? The string matches the first input?

ubuntu@ip-10-142-73-169:/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 ('david@gmail.com')
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

推荐答案

我相信 pexpect 使用正则表达式语法.

I believe pexpect uses regular expression syntax.

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

我猜上面的方法可以正常工作.
您可以选择转义?"可能还会得到更好的结果.

I'm guessing the above would work fine.
You could alternatively escape the '?' and possibly the brackets to get better results also.

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

这篇关于使用pexpect自动执行manage.py syncdb对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆