This file is indexed.

/usr/share/pyshared/twill/parse.py is in python-twill 0.9-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
Code parsing and evaluation for the twill mini-language.
"""

import sys
from cStringIO import StringIO

from errors import TwillAssertionError, TwillNameError
from pyparsing import OneOrMore, Word, printables, quotedString, Optional, \
     alphas, alphanums, ParseException, ZeroOrMore, restOfLine, Combine, \
     Literal, Group, removeQuotes, CharsNotIn

import twill.commands as commands
import namespaces
import re

### pyparsing stuff

# basically, a valid Python identifier:
command = Word(alphas + "_", alphanums + "_")
command = command.setResultsName('command')
command.setName("command")

# arguments to it.

# we need to reimplement all this junk from pyparsing because pcre's
# idea of escapable characters contains a lot more than the C-like
# thing pyparsing implements
_bslash = "\\"
_sglQuote = Literal("'")
_dblQuote = Literal('"')
_escapables = printables
_escapedChar = Word(_bslash, _escapables, exact=2)
dblQuotedString = Combine( _dblQuote + ZeroOrMore( CharsNotIn('\\"\n\r') | _escapedChar | '""' ) + _dblQuote ).streamline().setName("string enclosed in double quotes")
sglQuotedString = Combine( _sglQuote + ZeroOrMore( CharsNotIn("\\'\n\r") | _escapedChar | "''" ) + _sglQuote ).streamline().setName("string enclosed in single quotes")
quotedArg = ( dblQuotedString | sglQuotedString )
quotedArg.setParseAction(removeQuotes)
quotedArg.setName("quotedArg")

plainArgChars = printables.replace('#', '').replace('"', '').replace("'", "")
plainArg = Word(plainArgChars)
plainArg.setName("plainArg")

arguments = Group(ZeroOrMore(quotedArg | plainArg))
arguments = arguments.setResultsName('arguments')
arguments.setName("arguments")

# comment line.
comment = Literal('#') + restOfLine
comment = comment.suppress()
comment.setName('comment')

full_command = (
    comment
    | (command + arguments + Optional(comment))
    )
full_command.setName('full_command')

###

command_list = []           # filled in by namespaces.init_global_dict().

### command/argument handling.

def process_args(args, globals_dict, locals_dict):
    """
    Take a list of string arguments parsed via pyparsing and evaluate
    the special variables ('__*').

    Return a new list.
    """
    newargs = []
    for arg in args:
        # __variable substitution.
        if arg.startswith('__'):
            try:
                val = eval(arg, globals_dict, locals_dict)
            except NameError:           # not in dictionary; don't interpret.
                val = arg


            print '*** VAL IS', val, 'FOR', arg
            
            if isinstance(val, str):
                newargs.append(val)
            else:
                newargs.extend(val)
                
        # $variable substitution
        elif arg.startswith('$') and not arg.startswith('${'):
            try:
                val = eval(arg[1:], globals_dict, locals_dict)
            except NameError:           # not in dictionary; don't interpret.
                val = arg
            newargs.append(val)
        else:
            newargs.append(variable_substitution(arg, globals_dict, locals_dict))

    newargs = [ i.replace('\\n', '\n') for i in newargs ]

    return newargs

###

def execute_command(cmd, args, globals_dict, locals_dict, cmdinfo):
    """
    Actually execute the command.

    Side effects: __args__ is set to the argument tuple, __cmd__ is set to
    the command.
    """
    global command_list                 # all supported commands:
    # execute command.
    locals_dict['__cmd__'] = cmd
    locals_dict['__args__'] = args

    if cmd not in command_list:
        raise TwillNameError("unknown twill command: '%s'" % (cmd,))

    eval_str = "%s(*__args__)" % (cmd,)

    # compile the code object so that we can get 'cmdinfo' into the
    # error tracebacks.
    codeobj = compile(eval_str, cmdinfo, 'eval')

    # eval the codeobj in the appropriate dictionary.
    result = eval(codeobj, globals_dict, locals_dict)
    
    # set __url__
    locals_dict['__url__'] = commands.browser.get_url()

    return result

###

_print_commands = False

def parse_command(line, globals_dict, locals_dict):
    """
    Parse command.
    """
    res = full_command.parseString(line)
    if res:
        if _print_commands:
            print>>commands.OUT, "twill: executing cmd '%s'" % (line.strip(),)
            
        args = process_args(res.arguments.asList(), globals_dict, locals_dict)
        return (res.command, args)

    return None, None                   # e.g. a comment

###

def execute_string(buf, **kw):
    """
    Execute commands from a string buffer.
    """
    fp = StringIO(buf)
    
    kw['source'] = ['<string buffer>']
    if not kw.has_key('no_reset'):
       kw['no_reset'] = True
    
    _execute_script(fp, **kw)

def execute_file(filename, **kw):
    """
    Execute commands from a file.
    """
    # read the input lines
    if filename == "-":
        inp = sys.stdin
    else:
        inp = open(filename)

    kw['source'] = filename

    _execute_script(inp, **kw)
    
def _execute_script(inp, **kw):
    """
    Execute lines taken from a file-like iterator.
    """
    # initialize new local dictionary & get global + current local
    namespaces.new_local_dict()
    globals_dict, locals_dict = namespaces.get_twill_glocals()
    
    locals_dict['__url__'] = commands.browser.get_url()

    # reset browser
    if not kw.get('no_reset'):
        commands.reset_browser()

    # go to a specific URL?
    init_url = kw.get('initial_url')
    if init_url:
        commands.go(init_url)
        locals_dict['__url__'] = commands.browser.get_url()

    # should we catch exceptions on failure?
    catch_errors = False
    if kw.get('never_fail'):
        catch_errors = True

    # sourceinfo stuff
    sourceinfo = kw.get('source', "<input>")
    
    try:

        for n, line in enumerate(inp):
            if not line.strip():            # skip empty lines
                continue

            cmdinfo = "%s:%d" % (sourceinfo, n,)
            print 'AT LINE:', cmdinfo

            cmd, args = parse_command(line, globals_dict, locals_dict)
            if cmd is None:
                continue

            try:
                execute_command(cmd, args, globals_dict, locals_dict, cmdinfo)
            except SystemExit:
                # abort script execution, if a SystemExit is raised.
                return
            except TwillAssertionError, e:
                print>>commands.ERR, '''\
Oops!  Twill assertion error on line %d of '%s' while executing

  >> %s

%s
''' % (n, sourceinfo, line.strip(), e)
                if not catch_errors:
                    raise
            except Exception, e:
                print>>commands.ERR, '''\
EXCEPTION raised at line %d of '%s'

      %s

Error message: '%s'

''' % (n, sourceinfo, line.strip(),str(e).strip(),)

                if not catch_errors:
                    raise

    finally:
        namespaces.pop_local_dict()

###

def debug_print_commands(flag):
    """
    Turn on/off printing of commands as they are executed.  'flag' is bool.
    """
    global _print_commands
    _print_commands = bool(flag)
        

variable_expression = re.compile("\${(.*?)}")

def variable_substitution(raw_str, globals_dict, locals_dict):
    str=''
    pos = 0
    for m in variable_expression.finditer(raw_str):
        str = str+raw_str[pos:m.start()]
        try:
            str = str + eval(m.group(1), globals_dict, locals_dict)
        except NameError:
            str = str + m.group()
        pos = m.end()

    str = str+raw_str[pos:]

    return str