cup package

Submodules

cup.cache module

author:Guannan Ma
create_date:2014
last_date:2014
descrition:cache related module
class cup.cache.KvCache[source]

Bases: object

Key-Value Cache object

cleanup_expired()[source]

Delete all expired items

clear()[source]

remove all kv cache inside.

get(key)[source]

Get your cache with key. If the cache is expired, it will return None. If the key does not exist, it will return None.

get_expired()[source]
return expired items. Return type is a dict (
{
‘key’ : (value, expire_time)

}

Return type:dict
set(kvlist, expire_sec=None)[source]
Parameters:
  • kvlist – kvlist is a dict that contains your cache.
  • expire_sec – if expire_sec is None, the cache will never expire.
stat()[source]
Returns:a tuple with (item_num, expired_num)

cup.decorators module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
last_date:2014
descrition:decorators related module
class cup.decorators.Singleton(cls)[source]

Bases: object

Singleton你的类. 用法如下:

import cup

@cup.decorator.Singleton
class YourClass(object):
    def __init__(self):
    pass
cup.decorators.needlinux(function)[source]
Platform:Linux

只支持linux的python修饰符, 用来表明这个函数只能运行在linux系统上. 如果函数运行在非linux平台, raise cup.err.DecoratorException

用法如下::

import cup @cup.decorator.needlinux def your_func():

pass
class cup.decorators.TraceUsedTime(b_print_stdout=False, enter_msg='', leave_msg='')[source]

Bases: object

追踪函数的耗时情况 如果init过cup.log.init_comlog, 会打印到log文件。 example:

::

import time

from cup import decorators

@decorators.TraceUsedTime(True) def test():

print ‘test’ time.sleep(4)

# trace something with context. E.g. event_id def _test_trace_time_map(sleep_time):

print “ready to work” time.sleep(sleep_time)
traced_test_trace_time_map = decorators.TraceUsedTime(
b_print_stdout=False, enter_msg=’event_id: 0x12345’, leave_msg=’event_id: 0x12345’

)(_test_trace_time_map) traced_test_trace_time_map(sleep_time=5)

cup.err module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
last_modify_date:
 2014
descrition:error related module
exception cup.err.BaseCupException(msg)[source]

Bases: exceptions.Exception

所有cup库Exception的基类.

exception cup.err.DecoratorException(msg)[source]

Bases: cup.err.BaseCupException

Cup Decorator修饰符相关的异常Exception

exception cup.err.LoggerException(msg)[source]

Bases: cup.err.BaseCupException

cup.log相关的Exception

exception cup.err.ResException(msg)[source]

Bases: cup.err.BaseCupException

cup.res相关的Exception

exception cup.err.NoSuchProcess(pid, str_process_name)[source]

Bases: cup.err.ResException

通用Exception, 找不到这个进程

exception cup.err.AccessDenied(str_resouce)[source]

Bases: cup.err.ResException

通用Exception, 权限相关的异常Exception类

exception cup.err.NetException(msg='')[source]

Bases: cup.err.BaseCupException

通用网络相关Exception

exception cup.err.AsyncMsgError(msg='')[source]

Bases: cup.err.NetException

cup.net.async异步消息相关的异常Exception类

exception cup.err.ThreadTermException(msg='')[source]

Bases: cup.err.BaseCupException

结束线程相关的err

cup.log module

author:Guannan Ma
create_date:2014
last_date:2014
descrition:common log related module
cup.log.debug(msg, back_trace_len=0)[source]
Parameters:
  • msg – logging.DEBUG级别的日志打印。
  • back_trace_len – 为扩展预留的参数, 正常使用可忽略。
cup.log.info(msg, back_trace_len=0)[source]

logging.INFO的日志打印

cup.log.warn(msg, back_trace_len=0)[source]

logging.WARN级别的日志打印

cup.log.critical(msg, back_trace_len=0)[source]

logging.CRITICAL级别的日志打印

cup.log.init_comlog(loggername, loglevel=20, logfile='cup.log', logtype=0, maxlogsize=1073741824, bprint_console=False)[source]

初始化日志函数。用法如下:

:param loggername:
这个logger的名字.
Parameters:
  • loglevel – 一共四种 logging.DEBUG logging.INFO logging.WARN logging.CRITICAL
  • logfile – log文件的位置,如不存在,会尝试创建该文件
  • logtype – 支持两种leo.log.ROTATION cup.log.INFINITE ROTATION会在文件大小达到maxlogsize的时候进行switch. 一共会switch 30个文件, 然后在这30个文件里面ROTATION的写 INFINITE会一直写log文件
  • maxlogsize – logfile的最大文件大小(单位byte).超过会进行覆盖写或者switch.
  • b_printcmd – 打印日志到logfile的同时,是否打印到stdout.

请注意,打印日志时要么打印unicode字符,要么打印Python默认的UTF8的字符

E.g.

import logging
from cup import log
log.init_comlog(
    'test',
    logging.DEBUG,
    '/home/work/test/test.log',
    log.ROTATION,
    1024,
    False
)
log.info('test xxx')
log.critical('test critical')
cup.log.setloglevel(logginglevel)[source]

进程运行时更改loglevel

Parameters:logginglevel – 四种, 同init_comlog的loglevel

cup.mail module

author:Guannan Ma
create_date:2014
last_date:2014
descrition:mail related modules. Recommand using SmtpMailer
cup.mail.mutt_sendmail(tostr, subject, body, attach, content_is_html=False)[source]

请注意。mutt_sendmail不推荐被使用,除非无法使用此module的SmtpMailer.

Parameters:
  • exec_cwd – 切换到exec_cwd目录,然后发送邮件。 发送之后会回到原来的workdir. 如果不期望切换目录, 请传入’‘
  • tostr – 收件人列表, 可多人。 用,分隔
  • subject – 邮件主题
  • body – 邮件内容
  • attach – 邮件附件,可多项,用,分割。
  • content_is_html – 邮件内容是否是html格式的
Returns:

执行成功返回True, 否则返回False, 并打印错误到stderr

class cup.mail.SmtpMailer(sender, server='mail2-in.baidu.com', port=25, is_html=False)[source]

Bases: object

Parameters:
  • sender – 设置发送人邮箱
  • server – smtp的mailserver
  • port – port
  • is_html – body是否是html.

如下有个支持html嵌入图片以及附件的例子 我厂的smtp server hostname请自行babel搜索smtp服务器

import cup
mailer = cup.mail.SmtpMailer(
    'xxx@xxx.com',
    'xxxx.smtp.xxx.com',
    is_html=True
)
mailer.sendmail(
    [
        'maguannan',
        'liuxuan05',
        'zhaominghao'
    ],
    'test_img',
    (
        'testset <img src="http://sagit.baidu.com/main/'
        'wp-content/uploads/2013/06/monkeyc.jpg"></img>'
    ),
    ['/home/work/test.txt']
)
sendmail(recipients, subject='', body='', attachments=None)[source]

发送邮件.

Parameters:
  • recipients – 支持传入一个邮件接收者(string), 或者邮件接受者list
  • subject – 邮件主题
  • body – 邮件正文
  • attachments – 支持传入一个附件(string类型,邮件路径)或者附件list路径列表. 请注意, 需要传入绝对路径!
Returns:

发送成功返回(True, None)的tuple, 失败返回(False, error_msg)的tuple

setup(sender, server, port=25, is_html=False)[source]

可在运行过程中更改发送设置

cup.oper module

author:Zhao Minghao
create_date:2014
last_date:2014
descrition:operations related module
cup.oper.rm(name)[source]

rm the file if no exception happens. Will not raise exception if it fails

cup.oper.rmrf(fpath)[source]

可使用pythn自带的shutil.rmtree 不推荐使用这个函数 :param fpath:

删除的路径
cup.oper.is_proc_exist(path, name)[source]

通过name找到该进程的pid. 之后通过传入的path匹配/proc进程目录底下的cwd文件, 如果cwd也包含该path目录。 则认为proc进程存在, return True, 否则False :param path:

源程序运行启动的路径
Parameters:name – 源程序名称
Returns:存在返回True, 不存在返回False
cup.oper.kill(path, name, sign='', b_kill_child=False)[source]

通过和is_proc_exist一样的检查顺序找到这个进程, 然后根据向该进程发送sign. sign不赋值, 默认 kill pid, 赋值 kill -sign pid. b_kill_child 是否递归删除子进程 :param path:

源程序运行启动的路径
Parameters:
  • name – 源程序名称
  • sign – kill 程序发送的信号量, 支持空、9,、18、19
B_kill_child:

杀死当前进程的时候是否递归删除子进程

cup.oper.backup_file(srcpath, filename, dstpath, label=None)[source]

把srcpath目录下的filename文件备份到dstpath目录下。 备份名字变为 dstpath/filename.label label默认是(None), 可不传入,函数自动 把label变成当前时间. 如果dstpath不存在, 函数会尝试创建目录。如果创建失败, raise IOError

cup.platforms module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
last_date:2014
descrition:cross-platform functions related module
cup.platforms.is_linux()[source]
@return:
True or False

Check if you are running on Linux.

cup.platforms.is_windows()[source]
@return:
True or False

Check if you are running on Windows.

cup.timeplus module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
last_date:2014
cup.timeplus.get_str_now(fmt='%Y-%m-%d-%H-%M-%S')[source]
Parameters:fmt – 默认不需要填写,默认=’%Y-%m-%d-%H-%M-%S’. 可以更改成自己想用的string 格式. 比如 ‘%Y.%m.%d.%H.%M.%S’

cup.unittest module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
last_date:2014
descrition:Null
cup.unittest.assert_true(val, errmsg='')[source]

如果val is not True, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件

cup.unittest.assert_false(val, errmsg='')[source]

如果val is not False, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件

cup.unittest.assert_eq(val, exp, errmsg='')[source]

assert_eq, 如果val!=exp, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件

cup.unittest.assert_not_eq(val, exp, errmsg='')[source]

assert_not_eq. val不能等于exp, 如果等于则assert

cup.unittest.assert_eq_one(val, array, errmsg='')[source]

assert_eq_one, 如果val!=array(可遍历类型)里面的任何item, assert

cup.unittest.assert_lt(val, exp, errmsg='')[source]

assert_lt, expect val < exp

cup.unittest.assert_gt(val, exp, errmsg='')[source]

assert_gt, expect val > exp

cup.unittest.assert_ge(val, exp, errmsg='')[source]

expect val >= exp

cup.unittest.assert_le(val, exp, errmsg='')[source]

expect val <= exp

cup.unittest.assert_ne(val, exp, errmsg='')[source]

expect val != exp

class cup.unittest.CUTCase(logfile='./test.log', b_logstd=False, b_debug=False)[source]

Bases: object

leo库拥有的测试class. 支持nosetests. 可派生此类来实现测试class. 其中set_result函数会在nosetests执行case后设置,case成功则设置True, case fail设置False. 在teardown阶段可调用get_result函数 来获得case是否执行成功。

get_result()[source]

在teardown或者fail_teardown阶段获得是否case执行成功

set_result(b_result)[source]

cup ut 类用来设置case是否失败的函数。 一般不需要显示调用, 内部自动处理。

setup()[source]

Case的setup虚函数, 实际case需事先该函数

teardown()[source]

Case的setup虚函数, 实际case需实现该函数

test_run()[source]

Case的setup虚函数, 实际case需事先该函数

class cup.unittest.CCaseExecutor[source]

Bases: object

可调用CCaseExecutor类来执行leo.unittest.CUTCase的派生类case. 代码示例, 可nosetests执行, 也可python test_xxx.py执行的例子

#!/usr/bin/env python

import sys
import os
import logging

import cup
import sb_global

from nfsinit import CClearMan
from nfs import CNfs
from cli import CCli

class TestMyCase(cup.unittest.CUTCase):
    def __init__(self):
        super(self.__class__, self).__init__(
            logfile=sb_global.G_CASE_RUN_LOG,
            b_logstd=False
        )
        cup.log.info( 'Start to run ' + str(__file__ ) )
        self._cli = CCli()
        self._nfs = CNfs()
        self._uniq_strman = cup.util.CGeneratorMan()
        self._clearman = CClearMan()

    def setup(self):
        str_uniq = self._uniq_strman.get_uniqname()
        self._workfolder = os.path.abspath(os.getcwd())                     + '/' + str_uniq
        self._work_remote_folder =                     self._nfs.translate_path_into_remote_under_rw_folder(
            str_uniq
            )
        os.mkdir(self._workfolder)
        self._clearman.addfolder(self._workfolder)

    def _check_filemd5(self, src, dst):
        ret = os.system('/usr/bin/diff --brief %s %s' % (src, dst) )
        cup.unittest.assert_eq( 0, ret )

    def test_run(self):
        #
        # @author: maguannan
        #
        inited_bigfile = sb_global.G_NFS_DISK_RD +                     sb_global.G_TEST_BIGFILE
        bigfile = self._workfolder +                     '/test_big_file_offsite_write_random_write'
        self.tmpfile = sb_global.G_TMP4USE +                     '/test_big_file_offsite_write_random_write'
        os.system( 'cp %s %s' % (inited_bigfile, bigfile) )
        os.system( 'cp %s %s' % (bigfile, self.tmpfile) )
        times = 50
        baseOffsite = 1000
        fp0 = open(bigfile, 'r+b')
        fp1 = open(self.tmpfile, 'rb+')
        for i in xrange( 0, times ):
            fp0.seek(baseOffsite)
            fp1.seek(baseOffsite)
            fp0.write( 'a' * 100 )
            fp1.write( 'a' * 100 )
            baseOffsite += 1000
        fp0.close()
        fp1.close()

        self._check_filemd5(bigfile, self.tmpfile)

    def teardown(self):
        if self.get_result():
            # if case succeeded, do sth
            os.unlink(self.tmpfile)
            self._clearman.teardown()
        else:
            # if case failed, do sth else.
            print 'failed'
        cup.log.info( 'End running ' + str(__file__ ) )

if __name__ == '__main__':
    cup.unittest.CCaseExecutor().runcase(TestMyCase())
classmethod runcase(case)[source]

run the case

cup.version module

author:Guannan Ma maguannan@baidu.com @mythmgn
create_date:2014
modify_date:2014

Module contents

@author Guannan Ma @brief @note