Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def handle(self, msg):
msg_type = msg.get_msg_type()
if msg_type == self._type_man.getnumber_bytype('ACK'):
cup.log.info(
'get msg_type:ACK, msg_len:%d, msg_flag:%d, msg_src:%s, '
'msg_dest:%s, uniqid:%d' %
(
msg.get_msg_len(),
msg.get_flag(),
str(msg.get_from_addr()),
str(msg.get_to_addr()),
msg.get_uniq_id()
)
)
elif msg_type == self._type_man.getnumber_bytype('KILL'):
print 'KILL'
else:
self.default_handle(msg)
"""
@author
@brief
@note
"""
import time
import random
import cup
import logging
import threading
import test_msg_center
@cup.decorators.Singleton
class MsgGenerator(object):
def __init__(self, is_ack=False):
self._flagman = cup.net.async.CMsgFlag()
self._flagman.register_flags({
'URGENT': 0,
'NORMAL': 1,
})
self._type_man = cup.net.async.CMsgType()
self._is_ack = is_ack
def get_msg(self, self_ipport, to_ipport, i):
msg = cup.net.async.CNetMsg(is_postmsg=True)
msg.set_flag(self._flagman.getnumber_byflag('NORMAL'))
msg.set_from_addr(self_ipport, (1, 2))
msg.set_to_addr(to_ipport, (3, 4))
msg.set_msg_type(self._type_man.getnumber_bytype('ACK'))
def _recover_from_lastwriting(self, truncate_last_failure=True):
"""
recovery from last log writing
:raise Exception:
IOError, if any error happened
"""
folder = self._get_storage_dir()
files = self._get_ordered_logfiles(folder)
need_finish_file = False
if len(files) < 1:
log.info('no need recovery. Does not contain any files')
return
file_start_logid = -1
seek_file = None
if files[-1].find('writing') < 0:
# does not need recovery
log.info('does not have unfinished logfile, return')
file_start_logid = int(files[-1].split('.')[-1]) + 1
seek_file = files[-1]
log.info('next logid will be {0}'.format(self._logid))
else:
# need recovery, checkup, must <= 0 writing log file
count = 0
for fname in files:
if fname.find('writing') >= 0:
count += 1
if count > 1:
:raise:
cup.err.ConfigError if there's any config item missing
"""
ObjectInterface.__init__(self, config)
required_keys = ['uri', 'user', 'passwords']
if not self._validate_config(self._config, required_keys):
raise err.ConfigError(str(required_keys))
self._uri = self._config['uri']
self._user = self._config['user']
self._passwd = self._config['passwords']
self._extra = self._config['extra']
self._dufault_timeout = 30
if self._extra is not None and isinstance(self._config['extra'], int):
self._dufault_timeout = self._extra
log.info('to connect to ftp server')
self._ftp_con = ftplib.FTP()
self._host = self._uri.split(':')[1][2:]
self._port = ftplib.FTP_PORT
if len(self._uri.split(':')[2]) > 0:
self._port = int(self._uri.split(':')[2])
self._ftp_con.connect(self._host, self._port, self._dufault_timeout)
self._ftp_con.login(self._user, self._passwd)
self._last_optime = time.time()
self._timeout = 15 # idle time for ftp
def _get_ordered_logfiles(self, folder):
"""get log files in order"""
try:
files = sorted(os.listdir(folder), cmp=self.__cmp_logfile_id)
except TypeError:
import functools
files = sorted(os.listdir(folder), key=functools.cmp_to_key(
self.__cmp_logfile_id)
)
retfiles = []
for item in files:
if any([
len(item.split('.')) != 2,
item.find('done.') < 0 and item.find('writing.') < 0
]):
log.info('file name {0} invalid, will skip'.format(item))
continue
retfiles.append(item)
return retfiles
@classmethod
def _handle_attachments(cls, outer, attachments):
if type(attachments) == str:
attrs = [attachments]
elif type(attachments) == list:
attrs = attachments
else:
attrs = []
for attached in attrs:
if not os.path.isfile(attached):
log.warn('attached is not a file:%s' % attached)
continue
# Guess the content type based on the file's extension. Encoding
# will be ignored, although we should check for simple things like
# gzip'd or compressed files.
ctype, encoding = mimetypes.guess_type(attached)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed)
# use a generic bag-of-bits type.
ctype = 'application/octet-stream'
maintype, subtype = ctype.split('/', 1)
try:
if maintype == 'text':
with open(attached, 'rb') as fhandle:
# Note: we should handle calculating the charset
msg = text.MIMEText(
fhandle.read(), _subtype=subtype
def _main(argv):
"""main function"""
log.init_comlog('arrow_master', log.INFO,
_TOP_PATH + '/log/arrow_master.log',
log.ROTATION,
1024000000,
False
)
signal.signal(signal.SIGTERM, signal_handler)
if len(argv) < 2:
sys.stderr.write('should specify conf path')
sys.exit(-1)
master = Master(argv[1])
master.loop()
def _main(argv):
"""main function"""
log.init_comlog('arrow_master', log.INFO,
_TOP_PATH + '/log/arrow_master.log',
log.ROTATION,
1024000000,
False
)
signal.signal(signal.SIGTERM, signal_handler)
if len(argv) < 2:
sys.stderr.write('should specify conf path')
sys.exit(-1)
master = Master(argv[1])
master.loop()
def _cleanup_context(send_queue, peerinfo):
"""cleanup context"""
log.debug('to cleanup socket, peer:{0}'.format(peerinfo))
log.debug(
'cleanup: send_queue of socket size:{0}'.format(
send_queue.qsize()
)
)
while True:
try:
item = send_queue.get_nowait()
msg = item[2]
del msg
except queue.Empty:
break
if context is None:
:param exec_cwd:
exec working directory. Plz use
:param tostr:
recipt list, separated by ,
:param subject:
subject
:param body:
email content
:param attach:
email attachment
:param content_is_html:
is htm mode opened
:return:
return True on success, False otherwise
"""
decorators.needlinux(mutt_sendmail)
shellobj = shell.ShellExec()
temp_cwd = os.getcwd()
str_att = ''
cmdstr = ''
if attach == '':
if content_is_html is True:
cmdstr = 'echo "%s"|/usr/bin/mutt -e "my_hdr Content-Type:'\
'text/html" -s "%s" %s' \
% (body, subject, tostr)
else:
cmdstr = 'echo "%s"|/usr/bin/mutt -s "%s" %s' % (
body, subject, tostr
)
else:
attlist = attach.strip().split(',')