xref: /openbsd-src/gnu/llvm/lldb/third_party/Python/module/pexpect-4.6/pexpect/run.py (revision 061da546b983eb767bad15e67af1174fb0bcf31c)
1import sys
2import types
3
4from .exceptions import EOF, TIMEOUT
5from .pty_spawn import spawn
6
7def run(command, timeout=30, withexitstatus=False, events=None,
8        extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
9
10    '''
11    This function runs the given command; waits for it to finish; then
12    returns all output as a string. STDERR is included in output. If the full
13    path to the command is not given then the path is searched.
14
15    Note that lines are terminated by CR/LF (\\r\\n) combination even on
16    UNIX-like systems because this is the standard for pseudottys. If you set
17    'withexitstatus' to true, then run will return a tuple of (command_output,
18    exitstatus). If 'withexitstatus' is false then this returns just
19    command_output.
20
21    The run() function can often be used instead of creating a spawn instance.
22    For example, the following code uses spawn::
23
24        from pexpect import *
25        child = spawn('scp foo user@example.com:.')
26        child.expect('(?i)password')
27        child.sendline(mypassword)
28
29    The previous code can be replace with the following::
30
31        from pexpect import *
32        run('scp foo user@example.com:.', events={'(?i)password': mypassword})
33
34    **Examples**
35
36    Start the apache daemon on the local machine::
37
38        from pexpect import *
39        run("/usr/local/apache/bin/apachectl start")
40
41    Check in a file using SVN::
42
43        from pexpect import *
44        run("svn ci -m 'automatic commit' my_file.py")
45
46    Run a command and capture exit status::
47
48        from pexpect import *
49        (command_output, exitstatus) = run('ls -l /bin', withexitstatus=1)
50
51    The following will run SSH and execute 'ls -l' on the remote machine. The
52    password 'secret' will be sent if the '(?i)password' pattern is ever seen::
53
54        run("ssh username@machine.example.com 'ls -l'",
55            events={'(?i)password':'secret\\n'})
56
57    This will start mencoder to rip a video from DVD. This will also display
58    progress ticks every 5 seconds as it runs. For example::
59
60        from pexpect import *
61        def print_ticks(d):
62            print d['event_count'],
63        run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
64            events={TIMEOUT:print_ticks}, timeout=5)
65
66    The 'events' argument should be either a dictionary or a tuple list that
67    contains patterns and responses. Whenever one of the patterns is seen
68    in the command output, run() will send the associated response string.
69    So, run() in the above example can be also written as:
70
71        run("mencoder dvd://1 -o video.avi -oac copy -ovc copy",
72            events=[(TIMEOUT,print_ticks)], timeout=5)
73
74    Use a tuple list for events if the command output requires a delicate
75    control over what pattern should be matched, since the tuple list is passed
76    to pexpect() as its pattern list, with the order of patterns preserved.
77
78    Note that you should put newlines in your string if Enter is necessary.
79
80    Like the example above, the responses may also contain a callback, either
81    a function or method.  It should accept a dictionary value as an argument.
82    The dictionary contains all the locals from the run() function, so you can
83    access the child spawn object or any other variable defined in run()
84    (event_count, child, and extra_args are the most useful). A callback may
85    return True to stop the current run process.  Otherwise run() continues
86    until the next event. A callback may also return a string which will be
87    sent to the child. 'extra_args' is not used by directly run(). It provides
88    a way to pass data to a callback function through run() through the locals
89    dictionary passed to a callback.
90
91    Like :class:`spawn`, passing *encoding* will make it work with unicode
92    instead of bytes. You can pass *codec_errors* to control how errors in
93    encoding and decoding are handled.
94    '''
95    if timeout == -1:
96        child = spawn(command, maxread=2000, logfile=logfile, cwd=cwd, env=env,
97                        **kwargs)
98    else:
99        child = spawn(command, timeout=timeout, maxread=2000, logfile=logfile,
100                cwd=cwd, env=env, **kwargs)
101    if isinstance(events, list):
102        patterns= [x for x,y in events]
103        responses = [y for x,y in events]
104    elif isinstance(events, dict):
105        patterns = list(events.keys())
106        responses = list(events.values())
107    else:
108        # This assumes EOF or TIMEOUT will eventually cause run to terminate.
109        patterns = None
110        responses = None
111    child_result_list = []
112    event_count = 0
113    while True:
114        try:
115            index = child.expect(patterns)
116            if isinstance(child.after, child.allowed_string_types):
117                child_result_list.append(child.before + child.after)
118            else:
119                # child.after may have been a TIMEOUT or EOF,
120                # which we don't want appended to the list.
121                child_result_list.append(child.before)
122            if isinstance(responses[index], child.allowed_string_types):
123                child.send(responses[index])
124            elif (isinstance(responses[index], types.FunctionType) or
125                  isinstance(responses[index], types.MethodType)):
126                callback_result = responses[index](locals())
127                sys.stdout.flush()
128                if isinstance(callback_result, child.allowed_string_types):
129                    child.send(callback_result)
130                elif callback_result:
131                    break
132            else:
133                raise TypeError("parameter `event' at index {index} must be "
134                                "a string, method, or function: {value!r}"
135                                .format(index=index, value=responses[index]))
136            event_count = event_count + 1
137        except TIMEOUT:
138            child_result_list.append(child.before)
139            break
140        except EOF:
141            child_result_list.append(child.before)
142            break
143    child_result = child.string_type().join(child_result_list)
144    if withexitstatus:
145        child.close()
146        return (child_result, child.exitstatus)
147    else:
148        return child_result
149
150def runu(command, timeout=30, withexitstatus=False, events=None,
151        extra_args=None, logfile=None, cwd=None, env=None, **kwargs):
152    """Deprecated: pass encoding to run() instead.
153    """
154    kwargs.setdefault('encoding', 'utf-8')
155    return run(command, timeout=timeout, withexitstatus=withexitstatus,
156                events=events, extra_args=extra_args, logfile=logfile, cwd=cwd,
157                env=env, **kwargs)
158