1
# Copyright (C) 2009 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
def _script_to_commands(script, file_name=None):
21
"""Turn a script into a list of commands with their associated IOs.
23
Each command appears on a line by itself. It can be associated with an
24
input that will feed it and an expected output.
25
Comments starts with '#' until the end of line.
26
Empty lines are ignored.
27
Input and output are full lines terminated by a '\n'.
28
Input lines start with '<'.
29
Output lines start with '>'
37
for line in script.split('\n'):
39
# Keep a copy for error reporting
41
comment = line.find('#')
44
line = line[0:comment]
49
if line.startswith('<'):
52
raise SyntaxError('No command for that input',
53
(file_name, lineno, 1, orig))
55
input.append(line[1:] + '\n')
57
elif line.startswith('>'):
60
raise SyntaxError('No command for that output',
61
(file_name, lineno, 1, orig))
63
output.append(line[1:] + '\n')
66
# Time to output the current command
67
if cmd_cur is not None:
68
commands.append((cmd_cur, input, output))
70
cmd_cur = shlex.split(line)
74
# Add the last seen command
75
if cmd_cur is not None:
76
commands.append((cmd_cur, input, output))