| author: | Guannan Ma |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | cache related module |
Bases: object
Key-Value Cache object
Get your cache with key. If the cache is expired, it will return None. If the key does not exist, it will return None.
}
| Return type: | dict |
|---|
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | decorators related module |
Bases: object
Singleton你的类. 用法如下:
import cup
@cup.decorator.Singleton
class YourClass(object):
def __init__(self):
pass
| Platform: | Linux |
|---|
只支持linux的python修饰符, 用来表明这个函数只能运行在linux系统上. 如果函数运行在非linux平台, raise cup.err.DecoratorException
import cup @cup.decorator.needlinux def your_func():
pass
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)
)(_test_trace_time_map) traced_test_trace_time_map(sleep_time=5)
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| last_modify_date: | |
| 2014 | |
| descrition: | error related module |
Bases: cup.err.BaseCupException
Cup Decorator修饰符相关的异常Exception
Bases: cup.err.BaseCupException
cup.log相关的Exception
Bases: cup.err.BaseCupException
cup.res相关的Exception
Bases: cup.err.ResException
通用Exception, 找不到这个进程
Bases: cup.err.ResException
通用Exception, 权限相关的异常Exception类
Bases: cup.err.BaseCupException
通用网络相关Exception
Bases: cup.err.NetException
cup.net.async异步消息相关的异常Exception类
Bases: cup.err.BaseCupException
结束线程相关的err
| author: | Guannan Ma |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | common log related module |
| Parameters: |
|
|---|
初始化日志函数。用法如下:
:param loggername:
这个logger的名字.
| Parameters: |
|
|---|
请注意,打印日志时要么打印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')
| author: | Guannan Ma |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | mail related modules. Recommand using SmtpMailer |
请注意。mutt_sendmail不推荐被使用,除非无法使用此module的SmtpMailer.
| Parameters: |
|
|---|---|
| Returns: | 执行成功返回True, 否则返回False, 并打印错误到stderr |
Bases: object
| Parameters: |
|
|---|
如下有个支持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']
)
| author: | Zhao Minghao |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | operations related module |
rm the file if no exception happens. Will not raise exception if it fails
通过name找到该进程的pid. 之后通过传入的path匹配/proc进程目录底下的cwd文件, 如果cwd也包含该path目录。 则认为proc进程存在, return True, 否则False :param path:
源程序运行启动的路径
| Parameters: | name – 源程序名称 |
|---|---|
| Returns: | 存在返回True, 不存在返回False |
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | cross-platform functions related module |
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| last_date: | 2014 |
| descrition: | Null |
如果val is not True, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件
如果val is not False, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件
assert_eq, 如果val!=exp, assert并打印到stdout. errmsg参数为assert后提示到stderr的调用者错误信息 如果开启过leo.log.init_comlog的log, 同时打印critical log到log文件
assert_eq_one, 如果val!=array(可遍历类型)里面的任何item, assert
Bases: object
leo库拥有的测试class. 支持nosetests. 可派生此类来实现测试class. 其中set_result函数会在nosetests执行case后设置,case成功则设置True, case fail设置False. 在teardown阶段可调用get_result函数 来获得case是否执行成功。
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())
| author: | Guannan Ma maguannan@baidu.com @mythmgn |
|---|---|
| create_date: | 2014 |
| modify_date: | 2014 |
@author Guannan Ma @brief @note