Example #1
0
 def create_test_map(tot_size_bytes, num_elts):
     """Create a map containing num_elts with a sum of all elements being tot_size_bytes (excluding keys)"""
     size_per_elt_bytes = int(tot_size_bytes / num_elts)
     test_map = {}
     for elt_no in range(num_elts):
         test_map[_compat.unicode('elt_%06d' % elt_no)] = \
             _compat.unicode(AmqpLargeContentTestSender.create_test_string(size_per_elt_bytes))
     return test_map
 def encode_amqp_type(amqp_type, test_value):
     """Encode an AMQP type from a stringified test_value"""
     if amqp_type == 'null':
         return None
     if amqp_type == 'boolean':
         return True if test_value == 'True' else False
     if amqp_type == 'ubyte':
         return proton.ubyte(int(test_value, 16))
     if amqp_type == 'ushort':
         return proton.ushort(int(test_value, 16))
     if amqp_type == 'uint':
         return proton.uint(int(test_value, 16))
     if amqp_type == 'ulong':
         return proton.ulong(int(test_value, 16))
     if amqp_type == 'byte':
         return proton.byte(int(test_value, 16))
     if amqp_type == 'short':
         return proton.short(int(test_value, 16))
     if amqp_type == 'int':
         return proton.int32(int(test_value, 16))
     if amqp_type == 'long':
         return _compat.str2long(test_value, 16)
     if amqp_type == 'float':
         return proton.float32(
             struct.unpack('!f', _compat.decode_hex(test_value[2:]))[0])
     if amqp_type == 'double':
         return struct.unpack('!d', _compat.decode_hex(test_value[2:]))[0]
     if amqp_type == 'decimal32':
         return proton.decimal32(int(test_value[2:], 16))
     if amqp_type == 'decimal64':
         return proton.decimal64(_compat.str2long(test_value[2:], 16))
     if amqp_type == 'decimal128':
         return proton.decimal128(_compat.decode_hex(test_value[2:]))
     if amqp_type == 'char':
         if len(test_value) == 1:  # Format 'a'
             return proton.char(test_value)
         return proton.char(_compat.unichr(int(test_value, 16)))
     if amqp_type == 'timestamp':
         return proton.timestamp(int(test_value, 16))
     if amqp_type == 'uuid':
         return uuid.UUID(test_value)
     if amqp_type == 'binary':
         return base64.b64decode(test_value)
     if amqp_type == 'binarystr':
         return _compat.unicode(test_value)
     if amqp_type == 'string':
         return _compat.unicode(test_value)
     if amqp_type == 'symbol':
         return proton.symbol(test_value)
     if amqp_type in ['array', 'list', 'map']:
         print(
             'send: Complex AMQP type "%s" unsupported, see amqp_complex_types_test'
             % amqp_type)
         return None
     print('send: Unknown AMQP type "%s"' % amqp_type)
     return None
Example #3
0
 def format(self,record):
     try: record.message=record.getMessage()
     except Exception: err=sys.exc_info()[1]; record.message="Bad message (%r): %r" % (err,record.__dict__)
     record.asctime=time.strftime(TIME_FORMAT,self.converter(record.created)); prefix=PREFIX % record.__dict__
     if self._coloured: prefix=(self._colors.get(record.levelno,self._normal)+prefix+self._normal)
     # Encoding notes:  The logging module prefers to work with character
     # strings, but only enforces that log messages are instances of
     # basestring.  In python 2, non-ascii bytestrings will make
     # their way through the logging framework until they blow up with
     # an unhelpful decoding error (with this formatter it happens
     # when we attach the prefix, but there are other opportunities for
     # exceptions further along in the framework).
     #
     # If a byte string makes it this far, convert it to unicode to
     # ensure it will make it out to the logs.  Use repr() as a fallback
     # to ensure that all byte strings can be converted successfully,
     # but don't do it by default so we don't add extra quotes to ascii
     # bytestrings.  This is a bit of a hacky place to do this, but
     # it's worth it since the encoding errors that would otherwise
     # result are so useless (and tornado is fond of using utf8-encoded
     # byte strings wherever possible).
     try: message=unicode(record.message)
     except UnicodeDecodeError: message=repr(record.message)
     formatted=prefix+" "+message
     if record.exc_info:
     	if not record.exc_text: record.exc_text=self.formatException(record.exc_info)
     if record.exc_text: formatted=formatted.rstrip()+"\n"+record.exc_text
     return formatted.replace("\n","\n    ")
 def _create_jms_textmessage(self, test_value_text, hdr_kwargs, hdr_annotations):
     """Create a JMS text message"""
     return proton.Message(id=(self.sent+1),
                           body=_compat.unicode(test_value_text),
                           annotations=JmsHdrsPropsTestSender.merge_dicts(create_annotation('JMS_TEXTMESSAGE_TYPE'),
                                                                          hdr_annotations),
                           **hdr_kwargs)
Example #5
0
 def create_test_list(tot_size_bytes, num_elts):
     """Create a list containing num_elts with a sum of all elements being tot_size_bytes"""
     size_per_elt_bytes = int(tot_size_bytes / num_elts)
     test_list = []
     for _ in range(num_elts):
         test_list.append(
             _compat.unicode(
                 AmqpLargeContentTestSender.create_test_string(
                     size_per_elt_bytes)))
     return test_list
Example #6
0
 def format_list(self,basedir,listing,ignore_err=True):
     """Return an iterator object that yields the entries of given directory emulating the "/bin/ls -lA" UNIX command output.
      - (str) basedir: the absolute dirname.
      - (list) listing: the names of the entries in basedir
      - (bool) ignore_err: when False raise exception if os.lstat() call fails.
     On platforms which do not support the pwd and grp modules (such
     as Windows), ownership is printed as "owner" and "group" as a
     default, and number of hard links is always "1". On UNIX
     systems, the actual owner, group, and number of links are
     printed.
     This is how output appears to client:
     -rw-rw-rw-   1 owner   group    7045120 Sep 02  3:47 music.mp3
     drwxrwxrwx   1 owner   group          0 Aug 31 18:50 e-books
     -rw-rw-rw-   1 owner   group        380 Sep 02  3:40 module.py """
     assert isinstance(basedir,unicode),basedir
     if listing: assert isinstance(listing[0],unicode)
     if self.cmd_channel.use_gmt_times: timefunc=time.gmtime
     else: timefunc=time.localtime
     SIX_MONTHS=180 * 24 * 60 * 60; readlink=getattr(self,'readlink',None); now=time.time()
     for basename in listing:
         if not PY3:
         	try: file=os.path.join(basedir,basename)
         	except UnicodeDecodeError: # (Python 2 only) might happen on filesystem not # supporting UTF8 meaning os.listdir() returned a list # of mixed bytes and unicode strings: # http://goo.gl/6DLHD # http://bugs.python.org/issue683592
         		file=os.path.join(bytes(basedir),bytes(basename))
         		if not isinstance(basename,unicode): basename=unicode(basename,'utf8')
         else: file=os.path.join(basedir,basename)
         try: st=self.lstat(file)
         except (OSError,FilesystemError):
         	if ignore_err: continue
         	raise
         perms=_filemode(st.st_mode)  # permissions
         nlinks=st.st_nlink  # number of links to inode
         if not nlinks: nlinks=1 # non-posix system, let's use a bogus value
         size=st.st_size  # file size
         uname=self.get_user_by_uid(st.st_uid); gname=self.get_group_by_gid(st.st_gid); mtime=timefunc(st.st_mtime)
         # if modification time > 6 months shows "month year"
         # else "month hh:mm";  this matches proftpd format, see: # http://code.google.com/p/pyftpdlib/issues/detail?id=187
         if (now - st.st_mtime) > SIX_MONTHS: fmtstr="%d  %Y"
         else: fmtstr="%d %H:%M"
         try: mtimestr="%s %s" % (_months_map[mtime.tm_mon],time.strftime(fmtstr,mtime))
         except ValueError: mtime=timefunc(); mtimestr="%s %s" % (_months_map[mtime.tm_mon],time.strftime("%d %H:%M",mtime)) # It could be raised if last mtime happens to be too # old (prior to year 1900) in which case we return # the current time as last mtime.
         islink=(st.st_mode & 61440)==stat.S_IFLNK # same as stat.S_ISLNK(st.st_mode) but slighlty faster
         if islink and readlink is not None:
         	# if the file is a symlink, resolve it, e.g. # "symlink -> realfile"
         	try: basename=basename+" -> "+readlink(file)
         	except (OSError,FilesystemError):
         		if not ignore_err: raise
         line="%s %3s %-8s %-8s %8s %s %s\r\n" % (perms,nlinks,uname,gname,size,mtimestr,basename) # formatting is matched with proftpd ls output
         yield line.encode('utf8',self.cmd_channel.unicode_errors)
Example #7
0
 def encode(self, value):
     "Return a bytestring representation of the value"
     if isinstance(value, Token):
         return value.encoded_value
     elif isinstance(value, bytes):
         return value
     elif isinstance(value, (int, long)):
         value = b(str(value))
     elif isinstance(value, float):
         value = b(repr(value))
     elif not isinstance(value, basestring):
         # an object we don't know how to deal with. default to unicode()
         value = unicode(value)
     if isinstance(value, unicode):
         value = value.encode(self.encoding, self.encoding_errors)
     return value
Example #8
0
 def create_message(self, tot_size_bytes, num_elts):
     """
     Creates a single message with the test value translated from its string representation to the appropriate
     AMQP value.
     """
     if self.amqp_type == 'binary':
         return proton.Message(
             body=AmqpLargeContentTestSender.create_test_string(
                 tot_size_bytes).encode('utf-8'))
     if self.amqp_type == 'string':
         return proton.Message(body=_compat.unicode(
             AmqpLargeContentTestSender.create_test_string(tot_size_bytes)))
     if self.amqp_type == 'symbol':
         return proton.Message(body=proton.symbol(
             AmqpLargeContentTestSender.create_test_string(tot_size_bytes)))
     if self.amqp_type == 'list':
         return proton.Message(
             body=AmqpLargeContentTestSender.create_test_list(
                 tot_size_bytes, num_elts))
     if self.amqp_type == 'map':
         return proton.Message(
             body=AmqpLargeContentTestSender.create_test_map(
                 tot_size_bytes, num_elts))
     return None
Example #9
0
 def __init__(self,*args,**kwargs):
     logging.Formatter.__init__(self,*args,**kwargs)
     self._coloured=COLOURED and _stderr_supports_color()
     if self._coloured:
         curses.setupterm()
         # The curses module has some str/bytes confusion in
         # python3.  Until version 3.2.3, most methods return
         # bytes, but only accept strings.  In addition, we want to
         # output these strings with the logging module, which
         # works with unicode strings.  The explicit calls to
         # unicode() below are harmless in python2 but will do the
         # right conversion in python 3.
         fg_color=(curses.tigetstr("setaf") or curses.tigetstr("setf") or "")
         if (3,0) < sys.version_info < (3,2,3): fg_color=unicode(fg_color,"ascii")
         self._colors={
             logging.DEBUG: 		unicode(curses.tparm(fg_color, 4), "ascii"), # blue
             logging.INFO: 		unicode(curses.tparm(fg_color, 2), "ascii"), # green
             logging.WARNING: 	unicode(curses.tparm(fg_color, 3), "ascii"), # yellow
             logging.ERROR: 		unicode(curses.tparm(fg_color, 1), "ascii")  # red
         }
         self._normal=unicode(curses.tigetstr("sgr0"),"ascii")
Example #10
0
 def format_mlsx(self, basedir, listing, perms, facts, ignore_err=True):
     """Return an iterator object that yields the entries of a given directory or of a single file in a form suitable with MLSD and MLST commands.
     Every entry includes a list of "facts" referring the listed element.  See RFC-3659, chapter 7, to see what every single fact stands for.
      - (str) basedir: the absolute dirname.
      - (list) listing: the names of the entries in basedir
      - (str) perms: the string referencing the user permissions.
      - (str) facts: the list of "facts" to be returned.
      - (bool) ignore_err: when False raise exception if os.stat() call fails.
     Note that "facts" returned may change depending on the platform and on what user specified by using the OPTS command.
     This is how output could appear to the client issuing a MLSD request:
     type=file;size=156;perm=r;modify=20071029155301;unique=801cd2; music.mp3
     type=dir;size=0;perm=el;modify=20071127230206;unique=801e33; ebooks
     type=file;size=211;perm=r;modify=20071103093626;unique=801e32; module.py """
     assert isinstance(basedir,unicode),basedir
     if listing: assert isinstance(listing[0],unicode)
     if self.cmd_channel.use_gmt_times: timefunc=time.gmtime
     else: timefunc=time.localtime
     permdir=''.join([x for x in perms if x not in 'arw']); permfile=''.join([x for x in perms if x not in 'celmp'])
     if ('w' in perms) or ('a' in perms) or ('f' in perms): permdir += 'c'
     if 'd' in perms: permdir += 'p'
     show_type='type' in facts
     show_perm='perm' in facts
     show_size='size' in facts
     show_modify='modify' in facts
     show_create='create' in facts
     show_mode='unix.mode' in facts
     show_uid='unix.uid' in facts
     show_gid='unix.gid' in facts
     show_unique='unique' in facts
     for basename in listing:
         retfacts=dict()
         if not PY3:
         	try: file=os.path.join(basedir,basename)
         	except UnicodeDecodeError: # (Python 2 only) might happen on filesystem not # supporting UTF8 meaning os.listdir() returned a list # of mixed bytes and unicode strings: # http://goo.gl/6DLHD # http://bugs.python.org/issue683592
         		file=os.path.join(bytes(basedir),bytes(basename))
         		if not isinstance(basename,unicode): basename=unicode(basename,'utf8')
         else: file=os.path.join(basedir,basename)
         try: st=self.stat(file) # in order to properly implement 'unique' fact (RFC-3659, # chapter 7.5.2) we are supposed to follow symlinks, hence # use os.stat() instead of os.lstat()
         except (OSError,FilesystemError):
         	if ignore_err: continue
         	raise
         isdir=(st.st_mode & 61440)==stat.S_IFDIR # type + perm # same as stat.S_ISDIR(st.st_mode) but slightly faster
         if isdir:
         	if show_type:
         		if basename=='.': retfacts['type']='cdir'
         		elif basename=='..': retfacts['type']='pdir'
         		else: retfacts['type']='dir'
         	if show_perm: retfacts['perm']=permdir
         else:
         	if show_type: retfacts['type']='file'
         	if show_perm: retfacts['perm']=permfile
         if show_size: retfacts['size']=st.st_size  # file size
         if show_modify: # last modification time
         	try: retfacts['modify']=time.strftime("%Y%m%d%H%M%S",timefunc(st.st_mtime))
         	except ValueError: pass # it could be raised if last mtime happens to be too old # (prior to year 1900)
         if show_create:
         	# on Windows we can provide also the creation time
         	try: retfacts['create'] = time.strftime("%Y%m%d%H%M%S",timefunc(st.st_ctime))
         	except ValueError: pass
         # UNIX only
         if show_mode: retfacts['unix.mode']=oct(st.st_mode & 511)
         if show_uid: retfacts['unix.uid']=st.st_uid
         if show_gid: retfacts['unix.gid']=st.st_gid
         # We provide unique fact (see RFC-3659, chapter 7.5.2) on # posix platforms only; we get it by mixing st_dev and
         # st_ino values which should be enough for granting an # uniqueness for the file listed.
         # The same approach is used by pure-ftpd. # Implementors who want to provide unique fact on other
         # platforms should use some platform-specific method (e.g. # on Windows NTFS filesystems MTF records could be used).
         if show_unique: retfacts['unique']="%xg%x" % (st.st_dev,st.st_ino)
         # facts can be in any order but we sort them by name
         factstring="".join(["%s=%s;" % (x,retfacts[x]) \
                               for x in sorted(retfacts.keys())])
         line="%s %s\r\n" % (factstring,basename)
         yield line.encode('utf8',self.cmd_channel.unicode_errors)
Example #11
0
 def _create_jms_textmessage(self, test_value_text):
     """Create a JMS text message"""
     return proton.Message(
         id=(self.sent + 1),
         body=_compat.unicode(test_value_text),
         annotations=create_annotation('JMS_TEXTMESSAGE_TYPE'))
Example #12
0
 def __unicode__(self):
     if isinstance(self.args[0], unicode):
         return self.args[0]
     return unicode(self.args[0])