This file is indexed.

/usr/share/pyshared/Pyro/util.py is in pyro 1:3.14-1.1.

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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#############################################################################
#
#	Pyro Utilities
#
#	This is part of "Pyro" - Python Remote Objects
#	which is (c) Irmen de Jong - irmen@razorvine.net
#
#############################################################################

from __future__ import with_statement
import os, sys, traceback
import time, random, linecache
import socket, binascii
import Pyro.constants
from Pyro.util2 import *	# bring in 'missing' util functions


# bogus lock class, for systems that don't have threads.
class BogusLock(object):
	def __enter__(self):
		return self
	def __exit__(self, exc_type, exc_val, exc_tb):
		pass
	def acquire(self): pass
	def release(self): pass

def getLockObject():	
	if supports_multithreading(): # XXX
		from threading import Lock
		return Lock()
	else:
		return BogusLock()
def getRLockObject():	
	if supports_multithreading():
		from threading import RLock
		return RLock()
	else:
		return BogusLock()


# bogus event class, for systems that don't have threads
class BogusEvent(object):
	def __init__(self):
		self.flag=0
	def isSet(self): return self.flag==1
	def set(self): self.flag=1
	def clear(self): self.flag=0
	def wait(self,timeout=None):
		raise RuntimeError("cannot wait in non-threaded environment")

def getEventObject():
	if supports_multithreading():
		from threading import Event
		return Event()
	else:
		return BogusEvent()	
	

# Logging stuff.

# Select the logging implementation to use!
if Pyro.config.PYRO_STDLOGGING:
	# new-style logging using logging module, python 2.3+
	import logging, logging.config
	cfgfile=Pyro.config.PYRO_STDLOGGING_CFGFILE
	if not os.path.isabs(cfgfile):
		Pyro.config.PYRO_STDLOGGING_CFGFILE=os.path.join(Pyro.config.PYRO_STORAGE, cfgfile)
		cfgfile=Pyro.config.PYRO_STDLOGGING_CFGFILE
	externalConfig=0
	try:
		open(cfgfile).close()
		logging.config.fileConfig(cfgfile)
		externalConfig=1
	except IOError,x:
		# Config file couldn't be read! Use builtin config.
		# First make the logfiles absolute paths:
		if not os.path.isabs(Pyro.config.PYRO_LOGFILE):
			Pyro.config.PYRO_LOGFILE=os.path.join(Pyro.config.PYRO_STORAGE, Pyro.config.PYRO_LOGFILE)
		if not os.path.isabs(Pyro.config.PYRO_USER_LOGFILE):
			Pyro.config.PYRO_USER_LOGFILE=os.path.join(Pyro.config.PYRO_STORAGE, Pyro.config.PYRO_USER_LOGFILE)

	class LoggerBase(object):
		if externalConfig:
			def __init__(self):
				self.logger=logging.getLogger(self._getLoggerName())
		else:
			def __init__(self):
				self.logger=logging.getLogger("Pyro."+str(id(self)))		# each time a different logger ...
				self.setLevel(self._getPyroLevel())
				handler=logging.FileHandler(self._logfile())
				handler.setFormatter(logging.Formatter("%(asctime)s [%(process)d:%(thread)d] ** %(levelname)s ** %(message)s"))
				self.logger.addHandler(handler)
		def setLevel(self, pyroLevel):
			if pyroLevel>=3:
				self.logger.setLevel(logging.DEBUG)
			elif pyroLevel>=2:
				self.logger.setLevel(logging.WARN)
			elif pyroLevel>=1:
				self.logger.setLevel(logging.ERROR)
			else:
				self.logger.setLevel(999)
		def msg(self,source,*args):
			self.setLevel(self._getPyroLevel())
			if not args:
				(args, source) = ([source], "N/A")
			self.logger.info("%s ** %s", source, reduce(lambda x,y: str(x)+' '+str(y),args))
		def warn(self,source,*args):
			self.setLevel(self._getPyroLevel())
			if not args:
				(args, source) = ([source], "N/A")
			self.logger.warn("%s ** %s", source, reduce(lambda x,y: str(x)+' '+str(y),args))
		def error(self,source,*args):
			self.setLevel(self._getPyroLevel())
			if not args:
				(args, source) = ([source], "N/A")
			self.logger.error("%s ** %s", source, reduce(lambda x,y: str(x)+' '+str(y),args))
		def raw(self,ztr):
			self.logger.log(999,ztr.rstrip())
		def _logfile(self):
			raise NotImplementedError,'must override'
		def _getlevel(self):
			raise NotImplementedError,'must override'


	class SystemLogger(LoggerBase):
		def _getLoggerName(self):
			return "Pyro.system"
		def _getPyroLevel(self):
			return Pyro.config.PYRO_TRACELEVEL
		def _logfile(self):
			return Pyro.config.PYRO_LOGFILE
			
	class UserLogger(LoggerBase):
		def _getLoggerName(self):
			return "Pyro.user"
		def _getPyroLevel(self):
			return Pyro.config.PYRO_USER_TRACELEVEL
		def _logfile(self):
			return Pyro.config.PYRO_USER_LOGFILE
	
else:
	# classic Pyro logging. 
	
	class LoggerBase(object):
		# Logger base class. Subclasses must override _logfile and  _checkTraceLevel.
		def __init__(self):
			self.lock=getLockObject()
		def msg(self,source,*args):
			if self._checkTraceLevel(3): self._trace('NOTE',source, args)
		def warn(self,source,*args):
			if self._checkTraceLevel(2): self._trace('WARN',source, args)
		def error(self,source,*args):
			if self._checkTraceLevel(1): self._trace('ERR!',source, args)
		def raw(self,str):
			with self.lock:
				f=open(self._logfile(),'a')
				f.write(str)
				f.close()
		def _trace(self,typ,source, arglist):
			with self.lock:
				if not arglist:
					(arglist, source) = ([source], "N/A")
				try:
					tf=open(self._logfile(),'a')
					try:
						pid=os.getpid()
						pidinfo=" ["+str(os.getpid())
					except:
						pidinfo=" ["   # at least jython has no getpid()
					if supports_multithreading():
						pidinfo+=":"+threading.currentThread().getName()
					pidinfo+="] "	
					tf.write(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+
					  pidinfo+'** '+typ+' ** '+str(source)+' ** '+reduce(lambda x,y: str(x)+' '+str(y),arglist)+'\n')
					tf.close()
				except Exception,x:
					pass
		def _logfile(self):
			raise NotImplementedError,'must override'
		def _checkTraceLevel(self,level):
			raise NotImplementedError,'must override'
	
	class SystemLogger(LoggerBase):
		def _checkTraceLevel(self, level):
			return Pyro.config.PYRO_TRACELEVEL >= level
		def _logfile(self):
			filename=Pyro.config.PYRO_LOGFILE
			if not os.path.isabs(filename):
				Pyro.config.PYRO_LOGFILE=os.path.join(Pyro.config.PYRO_STORAGE, filename)
			return Pyro.config.PYRO_LOGFILE
			
	class UserLogger(LoggerBase):
		def _checkTraceLevel(self, level):
			return Pyro.config.PYRO_USER_TRACELEVEL >= level
		def _logfile(self):
			filename=Pyro.config.PYRO_USER_LOGFILE
			if not os.path.isabs(filename):
				Pyro.config.PYRO_USER_LOGFILE=os.path.join(Pyro.config.PYRO_STORAGE, filename)
			return Pyro.config.PYRO_USER_LOGFILE


# The logger object 'Log'.
Log = SystemLogger()


# Caching directory lister, outputs (filelist,dirlist) tuple
# Based upon dircache.py, but implemented in a callable object
# that has a thread-safe cache.
class DirLister(object):
	def __init__(self):
		self.lock=getLockObject()
		self.__listdir_cache = {}

	def __call__(self,path):
		with self.lock:
			try:
				cached_mtime, files, directories = self.__listdir_cache[path]
				del self.__listdir_cache[path]
			except KeyError:
				cached_mtime, files, directories = -1, [], []
		mtime = os.stat(path)[8]
		if mtime <> cached_mtime:
			files=[]
			directories=[]
			for e in os.listdir(path):
				if os.path.isdir(os.path.join(path,e)):
					directories.append(e)
				else:
					files.append(e)
		with self.lock:
			self.__listdir_cache[path] = mtime, files, directories
			return files,directories

listdir = DirLister()		# callable object		


# Fairly simple argument options parser. Like getopt(3).
class ArgParser(object):
	def __init__(self):
		pass
	def parse(self, args, optionlist):
		# optionlist is a string such as "ab:c" which means
		# we search for 3 options (-a, -b, -c) of which -b has an argument.
		self.options={}		# public, the option->value dictionary
		self.args=[]		# public, the rest of the arguments
		self.ignored=[]		# public, ignored options
		optionlist+=' '		# add sentinel
		if type(args)==type(''):
			args=args.split()
		while args:
			arg=args[0]
			del args[0]
			if arg[0]=='-':
				if len(arg)>=2:   # arg is an option. Check our list
					idx = optionlist.find(arg[1])
					if idx>=0:
						if optionlist[idx+1]==':':   # option requires argument.
							if len(arg)>=3:   # argument is appended. Use this.
								self.options[arg[1]]=arg[2:]
								continue
							# fetch argument from next string
							if len(args)>=1:
								self.options[arg[1]]=args[0]
								del args[0]
								continue
							else:   # missing arg, substitute None
								self.options[arg[1]]=None
						else:   # option requires no argument, use None
							self.options[arg[1]]=None
					else:   # didn't find this option, skip it
						self.ignored.append(arg[1])
				else:   # arg is a single '-'. Stop parsing.
					for a in args:
						self.args.append(a)
					args=None
			else:   # arg is no option, add it to the residu list and continue
				self.args.append(arg)
	def hasOpt(self, option):
		return self.options.has_key(option)
	def getOpt(self, option, default=Exception()):
		try:
			return self.options[option]
		except KeyError:
			if not isinstance(default,Exception):
				return default
			raise KeyError('no such option')
	def printIgnored(self):
		if self.ignored:
			print 'Ignored options:',
			for o in self.ignored:
				print '-'+o,
			print


_getGUID_counter=0		# extra safeguard against double numbers
_getGUID_lock=getLockObject()

if os.name=='java':
	# define jython specific stuff
	# first, the guid stuff. try java5 uuid first.
	try:
		from java.util import UUID
		def getGUID():
			return str(UUID.randomUUID())
	except ImportError:
		# older java, use rmi's vmid instead
		from java.rmi.dgc import VMID
		def getGUID():
			return str(VMID().toString().replace(':','-').replace('--','-'))
	import imp
	if not hasattr(imp,"acquire_lock"):
		# simulate missing imp.acquire_lock() from jython 2.2 (fixed in jython 2.5)
		imp_lock=getLockObject()
		def imp_acquire_lock():
			return imp_lock.acquire()
		def imp_release_lock():
			return imp_lock.release()
		imp.acquire_lock=imp_acquire_lock
		imp.release_lock=imp_release_lock
		
elif sys.platform=='cli':
	import System
	def getGUID():
		# IronPython uses .NET guid call
		return System.Guid.NewGuid().ToString()
else:	
	def getGUID():
		# Generate readable GUID string.
		# The GUID is constructed as follows: hexlified string of
		# AAAAAAAA-AAAABBBB-BBBBBBBB-BBCCCCCC  (a 128-bit number in hex)
		# where A=network address, B=timestamp, C=random. 
		# The 128 bit number is returned as a string of 16 8-bits characters.
		# For A: should use the machine's MAC ethernet address, but there is no
		# portable way to get it... use the IP address + 2 bytes process id.
		try:
			ip=socket.gethostbyname(socket.gethostname())
			networkAddrStr=binascii.hexlify(socket.inet_aton(ip))+"%04x" % os.getpid()
		except socket.error:
			# can't get IP address... use another value, like our Python id() and PID
			Log.warn('getGUID','Can\'t get IP address')
			try:
				ip=os.getpid()
			except:
				ip=0
			ip += id(getGUID)
			networkAddrStr = "%08lx%04x" % (ip, os.getpid())
	
		with _getGUID_lock:  # cannot generate multiple GUIDs at once
			global _getGUID_counter
			t1=time.time()*100 +_getGUID_counter
			_getGUID_counter+=1 
		t2=int((t1*time.clock())%sys.maxint) & 0xffffff
		t1=int(t1%sys.maxint) 
		timestamp = (long(t1) << 24) | t2 
		r2=(random.randint(0,sys.maxint//2)>>4) & 0xffff
		r3=(random.randint(0,sys.maxint//2)>>5) & 0xff
		return networkAddrStr+'%014x%06x' % (timestamp, (r2<<8)|r3 )

def genguid_scripthelper(argv):
	p=ArgParser()
	p.parse(argv,'')
	if p.args or p.ignored:
		print 'Usage: genguid  (no arguments)'
		print 'This tool generates Pyro UIDs.'
		raise SystemExit
	print getGUID()



# Get the configured pickling module. 
# Currently supported: cPickle, pickle, gnosis.xml.pickle (@paranoia 0 or -1).
def getPickle():
	if Pyro.config.PYRO_XML_PICKLE:
		# user requires xml pickle. Fails if that is not available!
		return getXMLPickle()
	else:	
		try: 
			import cPickle
			return cPickle
		except ImportError:
			# Fall back on pickle if cPickle isn't available
			import pickle
			return pickle

_xmlpickle={}
def getXMLPickle(impl=None):		
	# load & config the required xml pickle.
	# Currently supported: Gnosis Utils' gnosis.xml.pickle.
	global _xmlpickle
	if not impl:
		impl=Pyro.config.PYRO_XML_PICKLE
	if impl in _xmlpickle:
		return _xmlpickle[impl]
	try:	
		if impl=='gnosis':
			import gnosis.xml.pickle
			import gnosis.version
			gnosisVer=(gnosis.version.MAJOR, gnosis.version.MINOR)
			if gnosisVer==(1,2):
				# gnosis 1.2 style pickling, with paranoia setting
				_xmlpickle[impl]=gnosis.xml.pickle
				gnosis.xml.pickle.setParanoia(Pyro.config.PYRO_GNOSIS_PARANOIA)		# default paranoia level is too strict for Pyro
				gnosis.xml.pickle.setParser('SAX')		# use fastest parser (cEXPAT?)
				return gnosis.xml.pickle
			elif gnosisVer>=(1,3):
				from gnosis.xml.pickle import SEARCH_ALL, SEARCH_STORE, SEARCH_NO_IMPORT, SEARCH_NONE
				if Pyro.config.PYRO_GNOSIS_PARANOIA<0:
					class_search_flag = SEARCH_ALL		# allow import of needed modules
				elif Pyro.config.PYRO_GNOSIS_PARANOIA==0:
					class_search_flag = SEARCH_NO_IMPORT  # dont import new modules, only use known
				else:
					class_search_flag = SEARCH_STORE   # only use class store
				# create a wrapper class to be able to pass additional args into gnosis methods
				class GnosisPickle:
					def dumps(data, *args,**kwargs):
						return gnosis.xml.pickle.dumps(data, allow_rawpickles=0)
					dumps=staticmethod(dumps)
					def loads(xml, *args, **kwargs):
						return gnosis.xml.pickle.loads(xml, allow_rawpickles=0, class_search=class_search_flag)
					loads=staticmethod(loads)
					def dump(data, file, *args,**kwargs):
						return gnosis.xml.pickle.dump(data, file, allow_rawpickles=0)
					dump=staticmethod(dump)
					def load(file, *args, **kwargs):
						return gnosis.xml.pickle.load(file, allow_rawpickles=0, class_search=class_search_flag)
					load=staticmethod(load)
				_xmlpickle[impl]=GnosisPickle
				return GnosisPickle
			else:
				raise NotImplementedError('no supported Gnosis tools version found (need at least 1.2). Found '+gnosis.version.VSTRING)
		else:
			raise ImportError('unsupported xml pickle implementation requested: %s' % impl)
	except ImportError:
		Log.error('xml pickling implementation (%s) is not available' % impl)
		raise NotImplementedError('xml pickling implementation (%s) is not available' % impl)


# Pyro traceback printing
def getPyroTraceback(exc_obj):
	def formatRemoteTraceback(remote_tb_lines) :
		result=[]
		result.append(" +--- This exception occured remotely (Pyro) - Remote traceback:")
		for line in remote_tb_lines :
			if line.endswith("\n"):
				line=line[:-1]
			lines = line.split("\n")
			for line in lines :
				result.append("\n | ")
				result.append(line)
		result.append("\n +--- End of remote traceback")
		return result
	try:
		exc_type, exc_value, exc_trb=sys.exc_info()
		remote_tb=getattr(exc_obj,Pyro.constants.TRACEBACK_ATTRIBUTE,None)
		local_tb=formatTraceback(exc_type, exc_value, exc_trb)
		if remote_tb:
			remote_tb=formatRemoteTraceback(remote_tb)
			return local_tb + remote_tb
		else:
			# hmm. no remote tb info, return just the local tb.
			return local_tb
	finally:
		# clean up cycle to traceback, to allow proper GC
		del exc_type, exc_value, exc_trb


def formatTraceback(ex_type=None, ex_value=None, tb=None):
	if ex_type is None and tb is None:
		ex_type,ex_value,tb=sys.exc_info()
	if Pyro.config.PYRO_DETAILED_TRACEBACK:
		get_line_number = traceback.tb_lineno
	
		res = ['-'*50+ "\n",
			   " <%s> RAISED : %s\n" % (str(ex_type), str(ex_value)),
			   " Extended Stacktrace follows (most recent call last)\n",
			   '-'*50+'\n' ]
	 
		try:
			# Do some manipulation shit of stack
			if tb != None:
				frame_stack = []
				line_number_stack = []
	 
				#tb = sys.exc_info()[2]
				while 1:
					line_num = get_line_number(tb)
					line_number_stack.append(line_num)
					if not tb.tb_next:
						break
					tb = tb.tb_next
	 
				f = tb.tb_frame
				for x in line_number_stack:
					frame_stack.append(f)
					f = f.f_back
	 
				frame_stack.reverse()
	 
				lines = iter(line_number_stack)
				seen_crap = 0
				for frame in frame_stack:
					# Get items
					flocals = frame.f_locals.items()[:]
	 
					line_num = lines.next()
					filename = frame.f_code.co_filename
	 
					name = None
					for key, value, in flocals:
						if key == "self":
							name = "%s::%s" % (value.__class__.__name__, frame.f_code.co_name)
					if name == None:
						name = frame.f_code.co_name
	 
					res.append('File "%s", line (%s), in %s\n' % (filename, line_num, name))
					res.append("Source code:\n")
					
					code_line = linecache.getline(filename, line_num)
					if code_line:
						res.append('    %s\n' % code_line.strip())
	  
					if not seen_crap:
						seen_crap = 1
						continue
	  
					res.append("Local values:\n")
					flocals.sort()
					fcode=frame.f_code
					for key, value, in flocals:
						if key in fcode.co_names or key in fcode.co_varnames or key in fcode.co_cellvars:
							local_res="  %20s = " % key
							try:
								local_res += repr(value)
							except:
								try:
									local_res += str(value)
								except:
									local_res += "<ERROR>"
									
							res.append(local_res+"\n")
							
					res.append('-'*50 + '\n')
			res.append(" <%s> RAISED : %s\n" % (str(ex_type), str(ex_value)))
			res.append('-'*50+'\n')
			return res
			
		except:
			return ['-'*50+"\nError building extended traceback!!! :\n",
				  ''.join(traceback.format_exception(* sys.exc_info() ) ) + '-'*50 + '\n',
				  'Original Exception follows:\n',
				  ''.join(traceback.format_exception(ex_type, ex_value, tb)) ]

	else:
		# default traceback format.
		return traceback.format_exception(ex_type, ex_value, tb)