/usr/lib/python2.7/dist-packages/gnatpython/optfileparser.py is in python-gnatpython 54-3build1.
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 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | ############################################################################
# #
# OPTFILEPARSER.PY #
# #
# Copyright (C) 2008 - 2010 Ada Core Technologies, Inc. #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/> #
# #
############################################################################
"""test.opt files processing
This package provides a single class called OptFileParse which process the
test.opt files as documented in AdaCore procedures.
"""
import re
import logging
import os.path
OPTLINE_REGEXPS = re.compile("^([^\s]+)(\s+([a-zA-Z0-9_-]+)(\s+(.*))?)?$")
# Regexp that matches valid lines in test.opt files
class BadFormattingError(Exception):
"""Raised when an input line is not correctly formatted"""
pass
TAGS = 0
ARG = 1
OVERIDABLE = 2
logger = logging.getLogger("gnatpython.optfileparser")
class OptFileParse(object):
"""
ATTRIBUTES
system_tags: the list of tags applied to the test.opt
is_dead : True if the test should be considered DEAD, False otherwise
"""
def __init__(self, system_tags, filename):
"""Parse a test.opt file
PARAMETERS
system_tags: either a list of tags or a string containing the list
of tags separated by commas
filename: the test.opt to be parsed. If this is a string then the
argument is a filename otherwise if this is a list we
consider it is the content of the .opt file
RETURN VALUE
an OptFileParse object
REMARKS
None
"""
if isinstance(system_tags, str):
self.system_tags = system_tags.lower().split(',')
else:
self.system_tags = []
for tag in system_tags:
self.system_tags.append(tag.lower())
# Append 'all' to system tags
if not 'all' in system_tags:
self.system_tags.append('all')
self.is_dead = False
self.__note = None
self.__enable_note = False
self.__matches = {}
self.__parse_file(filename)
def get_value(self, cmd, default_value=None):
"""Query on the parsing result
PARAMETERS
cmd: the command on which we do the query
(ex: dead, cmd, out, ...)
default_value: value returned by default
RETURN VALUE
a string
REMARKS
by default the query will return '' if there is no entry for the
selected command.
"""
cmd = cmd.lower()
if self.is_dead:
if cmd == 'dead':
return self.__matches[cmd][ARG]
else:
return default_value
if cmd in self.__matches and cmd != 'dead':
return self.__matches[cmd][ARG]
else:
return default_value
def get_values(self, default_values):
"""Query on the parsing result
PARAMETERS
default_value: a dictionnary for which keys are the commands on
which we do the query and the associated value
the default values.
RETURN VALUE
a dictionnary containing the resulting value for each command
REMARKS
Doing get_values ({'CMD' : 'test.cmd', 'OUT' : 'test.out'}) is
equivalent to do get_value ('CMD', 'test.cmd') and then
get_value ('OUT', 'test.out')
"""
result = {}
for key in default_values:
result[key] = self.get_value(key, default_values[key])
return result
def get_note(self, sep=None):
"""Get the note
PARAMETERS
sep: string used to join the activating tags. Default is ','. If
'' is specified then a list is returned.
RETURN VALUE
a string (list of tags responsible for the activation of the test)
is sep is not '' or a list.
REMARKS
If there is no note then '' or [] is returned depending on the sep
value
"""
if sep is None:
sep = ','
if len(sep) == 0:
if self.__note is not None and not self.is_dead:
return self.__note[TAGS]
else:
return []
else:
if self.__note is not None and not self.is_dead:
return ",".join(self.__note[TAGS])
else:
return ''
# INTERNAL FUNCTIONS
def __process_opt_line(self, line):
"""process one line of a test.opt type file
The format of each line is the following:
tag1,tag2,tag3,...,tagN [COMMAND [PARAMETERS]]
-if no COMMAND is given then we assume that the command is 'DEAD false'
"""
# Remove comments (Ada style) and trailing white characters
processed_line = re.sub("--.*$", "", line).rstrip()
# Line contains only comments and/or white characters so ignore it
if not processed_line:
return
m = OPTLINE_REGEXPS.match(processed_line)
if m is None:
raise BadFormattingError("Can not parse line: " + line)
# find command, tags and argument
tags = m.group(1).split(',')
cmd = ""
arg = ""
if m.group(3):
# Check for command
cmd = m.group(3).lower()
if m.group(4):
# Get optional argument
arg = m.group(5)
else:
# If no command is set then the implicit command is: dead="false"
cmd = "dead"
arg = "false"
if arg == '' and cmd == 'dead':
arg = 'true'
# Enable note only if a dead all is encountered
if arg != 'false' and cmd == 'dead' and self.__is_all(tags):
self.__enable_note = True
if cmd != 'required' and self.__match(tags):
logger.debug('match: ' + cmd + ', tags=' + '%s' % tags)
if self.__is_overidable(cmd):
self.__matches[cmd] = (tags, arg, self.__is_all(tags))
if not self.__is_dead_cmd(cmd) \
and (self.__note is None or self.__matches[cmd][OVERIDABLE]):
self.__note = self.__matches[cmd]
elif cmd == 'required' and not self.__match(tags):
self.__matches['required'] = (tags, arg, False)
def __is_overidable(self, cmd):
return not cmd in self.__matches \
or self.__matches[cmd][OVERIDABLE]
@classmethod
def __is_all(cls, tag_list):
return len(tag_list) == 1 and tag_list[0].lower() == 'all'
def __is_dead_cmd(self, cmd):
return cmd == 'dead' \
and 'dead' in self.__matches \
and self.__matches['dead'][ARG] != 'false'
def __match(self, tag_list):
"""Match tags against the system tags.
True if all non-negated tags and none of the negated tags in the given
list are present in system tags."""
for tag in tag_list:
if not tag.startswith("!"):
# If tag is non-negated, it must be present in system tags
if not (tag.lower() in self.system_tags):
return False
else:
# If tag is negated, it must be absent from system tags
if tag[1:].lower() in self.system_tags:
return False
return True
def __parse_file(self, filename):
have_opt_data = False
if isinstance(filename, list):
for line in filename:
self.__process_opt_line(line)
have_opt_data = True
elif os.path.isfile(filename):
optfile = open(filename, "r")
for line in optfile:
self.__process_opt_line(line)
optfile.close()
have_opt_data = True
if have_opt_data:
if 'required' in self.__matches:
self.__matches['dead'] = self.__matches['required']
self.is_dead = True
elif self.__note is not None:
self.is_dead = False
elif self.__is_dead_cmd('dead'):
self.is_dead = True
else:
self.is_dead = False
if (self.__note is not None and self.__note[OVERIDABLE]) \
or not self.__enable_note:
self.__note = None
def __str__(self):
result = ''
if self.is_dead:
result += 'dead="' + \
re.sub('"', '\\"', self.__matches['dead'][ARG]) + '"\n'
else:
for k in self.__matches:
if k != 'dead':
result += k + '="' + \
re.sub('"', '\\"', self.__matches[k][ARG]) + '"\n'
if self.__note is not None:
result += 'activating_tag="%s' % \
re.sub('"', '\\"', ",".join(self.__note[TAGS])) + '"\n'
result = result.rstrip()
return result
|