/usr/lib/gdesklets/scripting/Script.py is in gdesklets 0.36.1-5+b1.
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | from ElementWrapper import ElementWrapper
from ControlWrapper import ControlWrapper
from config.StateSaver import StateSaver, DefaultStateSaver
from Scriptlet import Scriptlet
from display.MenuItem import MenuItem
from utils.ErrorFormatter import ErrorFormatter
from utils import dialog
from layout import Unit
import exceptions
_AUTHORIZED_COMMANDS_KEY = "authorized_commands"
#
# Class for inline scripts together with their environment.
#
class Script:
def __init__(self, dsp_id, dsp_path):
self.__scriptlets = {}
# ID of the display
self.__dsp_id = dsp_id
# path of the display
self.__dsp_path = dsp_path
# the state saver
self.__state_saver = StateSaver(dsp_id)
# the environment for this script
self.__environment = {}
# the list of loaded controls
self.__loaded_controls = []
# flag indicating whether the display has been stopped
self.__is_stopped = False
#
# setup a sandbox environment
#
self.__environment["__builtins__"] = None
self.__environment["__name__"] = "inline" # required for classes
# unit constructor and units
self.__environment["Unit"] = Unit.Unit
self.__environment["PX"] = Unit.UNIT_PX
self.__environment["CM"] = Unit.UNIT_CM
self.__environment["IN"] = Unit.UNIT_IN
self.__environment["PT"] = Unit.UNIT_PT
self.__environment["PERCENT"] = Unit.UNIT_PERCENT
self.__environment["MenuItem"] = MenuItem
self.__environment["add_timer"] = self.__script_add_timer
self.__environment["get_config"] = self.__script_get_config
self.__environment["get_control"] = self.__script_get_control
self.__environment["set_config"] = self.__script_set_config
self.__environment["launch"] = self.__script_launch
# see end of file
# WTF do we need True/False there? (because they're not yet keywords)
self.__environment["False"] = False
self.__environment["True"] = True
self.__environment["abs"] = abs
self.__environment["bool"] = bool
self.__environment["callable"] = callable
self.__environment["chr"] = chr
self.__environment["classmethod"] = classmethod
self.__environment["cmp"] = cmp
self.__environment["complex"] = complex
self.__environment["delattr"] = delattr
self.__environment["dict"] = dict
self.__environment["divmod"] = divmod
self.__environment["enumerate"] = enumerate
self.__environment["float"] = float
self.__environment["getattr"] = getattr
self.__environment["hasattr"] = hasattr
self.__environment["hash"] = hash
self.__environment["hex"] = hex
self.__environment["id"] = id
self.__environment["int"] = int
self.__environment["isinstance"] = isinstance
self.__environment["issubclass"] = issubclass
self.__environment["iter"] = iter
self.__environment["len"] = len
self.__environment["list"] = list
self.__environment["locals"] = locals
self.__environment["long"] = long
self.__environment["max"] = max
self.__environment["min"] = min
self.__environment["object"] = object
self.__environment["oct"] = oct
self.__environment["ord"] = ord
self.__environment["property"] = property
self.__environment["range"] = range
self.__environment["reduce"] = reduce
self.__environment["repr"] = repr
self.__environment["round"] = round
self.__environment["setattr"] = setattr
self.__environment["staticmethod"] = staticmethod
self.__environment["str"] = str
self.__environment["sum"] = sum
self.__environment["super"] = super
self.__environment["tuple"] = tuple
self.__environment["type"] = type
self.__environment["unichr"] = unichr
self.__environment["unicode"] = unicode
self.__environment["vars"] = vars
self.__environment["xrange"] = xrange
self.__environment["zip"] = zip
# exceptions, we need the exceptions
for name in dir(exceptions):
if (not name.startswith("_")):
exc = getattr(exceptions, name)
self.__environment[name] = exc
#end for
#
# Handles errors in the script.
#
def __handle_error(self):
from utils.error import Error
Error().handle(self.__dsp_id)
#
# Runs a timer in the sandbox. The timer stops when the script is being
# stopped.
#
def __script_add_timer(self, interval, callback, *args):
def f():
try:
if (self.__is_stopped):
return False
else:
ret = callback(*args)
return ret
except:
self.__handle_error()
return False
if (type(interval) not in (type(1), type(1.0), type(1L))
or interval < 0):
raise UserError(_("Error in add_timer function"),
_("\"%s\" isn't a valid integer value!")
% `interval`)
#dialog.warning(_("Error in add_timer function"),
# _("\"%s\" isn't a valid integer value!")
# % `interval`)
#return
import gobject
gobject.timeout_add(interval, f)
#
# Retrieves a configuration value.
#
def __script_get_config(self, key, default = None):
return self.__state_saver.get_key(key, default)
#
# Stores a configuration value.
#
def __script_set_config(self, key, value):
self.__state_saver.set_key(key, value)
#
# Returns a control readily wrapped for the sandbox.
#
def __script_get_control(self, interface):
# FIXME: ensure that this does not break the sandbox
from factory.ControlFactory import ControlFactory
factory = ControlFactory()
ctrl = factory.get_control(interface)
if (ctrl):
self.__loaded_controls.append(ctrl)
wrapped = ControlWrapper(ctrl)
return wrapped
raise UserError(_("No Control could be found for interface %s") % \
(interface,),
_("This means that a functionality won't be available "
"during execution!"))
#dialog.warning(_("No Control could be found for interface %s") % \
# (interface,),
# _("This means that a functionality won't be available "
# "during execution!"))
#return ""
#
# Launches the given command if it's safe.
#
def __script_launch(self, command):
states = DefaultStateSaver()
permissions = states.get_key(_AUTHORIZED_COMMANDS_KEY, {})
def run_cmd():
import os
os.system(command + " &")
def run_and_permit():
permissions[(self.__dsp_id, command)] = True
states.set_key(_AUTHORIZED_COMMANDS_KEY, permissions)
run_cmd()
if ((self.__dsp_id, command) in permissions):
run_cmd()
else:
# FIXME: what is the correct way to escape '=' chars?
escaped_command = command.replace("=", "")
escaped_command = escaped_command.replace("\\", "\\\\")
escaped_command = escaped_command.replace("&", "&")
escaped_command = escaped_command.replace("<", "<")
escaped_command = escaped_command.replace(">", ">")
dialog.question(None,
_("Security Risk"),
_("The desklet %s wants to execute a system "
"command:\n"
"\n"
" <tt><b>%s</b></tt>\n"
"\n"
"To protect your system from malicious "
"programs, you can deny the execution of this "
"command.\n"
"\n"
"If you are sure that the command is harmless, "
"you may permanently allow this desklet "
"instance to run it.")
% (self.__dsp_path, escaped_command),
(_("Deny!"), None),
(_("Allow once"), run_cmd),
(_("Allow for this desklet"), run_and_permit))
#
# Stops this scripting object.
#
def stop(self):
self.__is_stopped = True
del self.__environment
for c in self.__loaded_controls:
try:
c.stop()
except StandardError, exc:
import traceback; traceback.print_exc()
log("Could not stop control %s" % c)
del c
del self.__loaded_controls
#
# Removes this scripting object and its state.
#
def remove(self):
states = DefaultStateSaver()
permissions = states.get_key(_AUTHORIZED_COMMANDS_KEY, {})
for ident, cmd in permissions.keys():
if (ident == self.__dsp_id): del permissions[(ident, cmd)]
states.set_key(_AUTHORIZED_COMMANDS_KEY, permissions)
self.__state_saver.remove()
#
# Creates the given namespace if it does not yet exist.
#
def __make_namespace(self, namespace):
class Namespace: pass
if (not namespace in self.__environment):
self.__environment[namespace] = Namespace()
#
# Adds the given element to the environment. The namespace has to exist.
# If no namespace is given, the element becomes a member of the global
# namespace.
#
def add_element(self, namespace, name, elem):
if (namespace): self.__make_namespace(namespace)
wrapped = ElementWrapper(elem)
if (not namespace):
self.__environment[name] = wrapped
else:
setattr(self.__environment[namespace], name, wrapped)
#
# Same as add_element() but puts elements into a structure of (nested)
# arrays.
#
def add_element_with_path(self, namespace, name, elem, indexpath):
if (namespace): self.__make_namespace(namespace)
try:
if (not namespace):
lst = self.__environment[name]
else:
lst = getattr(self.__environment[namespace], name)
except:
lst = []
if (not namespace):
self.__environment[name] = lst
else:
setattr(self.__environment[namespace], name, lst)
# build up the structure of (nested) arrays
for index in indexpath[:-1]:
while (len(lst) - 1 < index): lst.append([])
lst = lst[index]
index = indexpath[-1]
while (len(lst) - 1 < index): lst.append([])
lst[index] = ElementWrapper(elem)
#
# Fixes the indentation of Python code.
#
def __fix_indentation(self, code):
lines = code.splitlines()
min_indent = len(code)
# find the minimal indentation
for l in lines:
if (not l.strip()): continue
this_indent = len(l) - len(l.lstrip())
min_indent = min(min_indent, this_indent)
# apply the minimal indentation
out = ""
for l in lines:
out += l[min_indent:] + "\n"
return out
#
# Executes a block of script.
#
def execute(self, scriptlet, handle_error = True):
sid = scriptlet.script_id
# get the block into shape
code = self.__fix_indentation(scriptlet.script)
# remember scriptlet for later
self.__scriptlets[sid] = scriptlet
# compile and run
try:
from utils.error import Error
Error().register_code("<inline '%s'>" % sid, code)
pycode = compile(code, "<inline '%s'>" % sid, 'exec')
#pycode = compile(code, "%s" % scriptlet.filename, 'exec')
exec pycode in self.__environment
except:
#if (handle_error):
self.__handle_error()
#
# Retrieves the value of the given object from the scripting environment.
#
def get_value(self, name):
if (not name):
return
# TODO: if the type of the bound variable differs we have to check it
cmd = "__retrieve__ = %s" % (name,)
self.execute(Scriptlet(cmd, "<internal>"), handle_error = False)
# may raise an exception
return self.__environment.pop("__retrieve__")
#
# Sets the value of the given object in the scripting environment.
#
def set_value(self, name, value):
self.__environment["__inject__"] = value
cmd = "%s = __inject__" % name
self.execute(Scriptlet(cmd, "<internal>"), handle_error = False)
self.__environment.pop("__inject__")
#
# Calls the given function in the sandbox.
#
def call_function(self, name, *args):
func = self.get_value(name)
try:
func(*args)
except:
#log("A function call in the inline script failed.")
self.__handle_error()
#BEGIN
# + 2.3.3 http://python.org/doc/2.3.3/lib/built-in-funcs.html
# vs.
# - 2.2.3 http://python.org/doc/2.2.3/lib/built-in-funcs.html
# * dangerous
# * __import__
# abs
# - apply
# + basestring
# bool
# - buffer
# callable
# chr
# classmethod
# cmp
# - coerce
# * compile
# complex
# delattr
# dict
# dir # ?
# divmod
# + enumerate
# * eval
# * execfile
# * file
# filter # may be should print a warning and a link to list-comprehension
# float
# getattr
# globals # ?
# hasattr
# hash
# help # not very useful
# hex
# id
# - intern
# input # not very useful
# int
# isinstance
# issubclass
# iter
# len
# list
# locals
# long
# map # maybe should print a warning and a link to list-comprehension
# max
# min
# + object
# oct
# * open
# ord
# property
# range
# raw_input # not very usefull
# reduce
# * reload
# repr
# round
# setattr
# - slice
# staticmethod
# str
# + sum
# super
# tuple
# type
# unichr
# unicode
# vars
# xrange
# zip
# 2.3 Deprecated/Non-essentials functions
# http://python.org/doc/2.3.3/lib/non-essential-built-in-funcs.html
# - apply
# - buffer
# - coerce
# - intern
#END
|