-
Notifications
You must be signed in to change notification settings - Fork 193
Home
mythmgn edited this page Feb 6, 2017
·
8 revisions
Welcome to the CUP wiki!
#1.基础库介绍
目前基础库包含如下大的Package[每类包含若干小的package及module].
排序按照常用->较常用->专用
from cup import mail
mailer = mail.SmtpMailer(
'[email protected]', # sender
'xxxx.smtp.xxx.com', # smtp server
is_html=True # use html or not for email contents
)
mailer.sendmail(
[
'[email protected]',
'[email protected]',
'[email protected]'
],
'test_img',
(
'testset <img src="cid:screenshot.png"></img>'
),
# attachment
[
'/home/work/screenshot.png',
'../abc.zip'
]
)
去除复杂log设置,即开即用
import logging
from cup import log
log.init_comlog(
'test',
log.DEBUG,
'/home/work/test/test.log',
log.ROTATION,
1024,
False
)
log.info('test xxx')
log.error('test critical')
# 支持重新设置日志level、日志文件位置、参数等,具体参数设置见文档
# Level: Datetime * [pid:threadid] [source.code:lineno] content
INFO: 2017-02-06 19:35:54,242 * [16561:140543940048640] [log.py:278] --------------------Log Initialized Successfully--------------------
INFO: 2017-02-06 19:35:54,242 * [16561:140543940048640] [cup_log_test.py:36] info
DEBUG: 2017-02-06 19:35:54,243 * [16561:140543940048640] [cup_log_test.py:40] debug
##1.1 cup.jenkinslib
能够与JenkinsLib进行交互,获取、操作jenkins job
import cup
import cup.jenkinslib
############### quick start ###############
jenkins = cup.jenkinslib.Jenkins('cup.jenkins.baidu.com')
job = jenkins['cup_quick']
print job.name, job.last_stable_build_number, job.description
print job[5], job["lastSuccessBuild"], job.last_stable_build
qi = job.invoke()
build = qi.block_until_building()
print build.name, build.number, build.timestamp
try:
build.block_until_complete(timeout=20)
except cup.jenkinslib.RunTimeout as err:
print "timeout:", err
build.stop()
print build.duration, build.result, build.description
build.description = "new description"
jenkins.enable_ftp('ftp.baidu.com', 'cup', 'password', 22)
with build.ftp_artifacts as af:
af['artifacts_path'].download('./local_path')
网络操作相关的package. 两个重要部分:
- a. 网卡信息获取、路由信息获取、socket参数设置
- b. 异步消息通信协议支持
# 1. 获取路由信息
from cup.net import route
ri = route.RouteInfo()
print json.dumps(ri.get_route_by_ip('10.32.19.92'), indent=1)
print json.dumps(ri.get_routes(), indent=1)
{
"Use": "0",
"Iface": "eth1",
"Metric": "0",
"Destination": "10.0.0.0",
"Mask": "255.0.0.0",
"RefCnt": "0",
"MTU": "0",
"Window": "0",
"Gateway": "10.226.71.1",
"Flags": "0003",
"IRTT": "0"
}
[
{
"Use": "0",
"Iface": "eth1",
"Metric": "0",
"Destination": "10.226.71.0",
"Mask": "255.255.255.0",
"RefCnt": "0",
"MTU": "0",
"Window": "0",
"Gateway": "0.0.0.0",
"Flags": "0001",
"IRTT": "0"
},
# 2. 获取ip && 设置socket参数等
from cup import net
# set up socket params
net.set_sock_keepalive_linux(sock, 1, 3, 3)
net.set_sock_linger(sock)
net.set_sock_quickack(sock)
net.set_sock_reusable(sock, True) # port resuable
# get ipaddr of a network adapter/interface
print net.getip_byinterface('eth0')
print net.getip_byinterface('eth1')
print net.getip_byinterface('xgbe0') # 万兆网卡
# get ipaddr of a hostname
print net.get_hostip('abc.test.com')
机器资源相关package. 举例, 获得当前机器cpu/mem/net统计信息。 进程资源信息(所有/proc/pid能拿到的资源信息)
操作shell命令相关的package. 举例,支持超时kill的shell命令执行
各类util类pacakge。举例,线程池,conf(对应c++ Configure及hdfs xxx.xml),生成器、自动等待触发器等。
2. 如下Module:
cup.decorators 进行函数、类修饰的module. 举例,一键变类为Singleton类。
cup.err cup基础库相关的异常module.
cup.const 静态不可变变量支持
cup.oper 命令操作类相关module. 举例,cup.oper.rmrf、进程存在性检查、pid获取等等.
cup.timeplus 时间相关module. 简化time操作函数。
cup.unittest unit-test相关操作module. 实现了市面上常见的assert类型并提供cup独有的UT类
3. Installation
- run # python setup.py install
4. Reference
- CUP use pexpect and httplib.
* Pexpect http://pexpect.sourceforge.net/ (under MIT license)
* Httplib2 http://code.google.com/p/httplib2/ (under MIT license)