Skip to content
mythmgn edited this page Feb 8, 2017 · 8 revisions

Welcome to the CUP wiki!

#1.基础库介绍

目前基础库包含如下大的Package[每类包含若干小的package及module].

排序按照常用->较常用->专用

1.1 cup.mail 发送邮件类

具体文档介绍

http://docs.iobusy.com/docs/cup/cup.html#module-cup.mail

代码示例:

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'
    ]
)

1.2 cup.log 简单好用的cup log函数

去除复杂log设置,即开即用

具体文档介绍

http://docs.iobusy.com/docs/cup/cup.html#module-cup.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.3 cup.res 获取linux机器资源类相关信息

举例, 获得当前机器cpu/mem/net统计信息。 进程资源信息(所有/proc/pid能拿到的资源信息)

具体文档介绍

http://docs.iobusy.com/docs/cup/cup.res.html

代码示例:

# 1. 获取系统信息, 可以获取cpu mem network_speed等等
import cup
# 计算60内cpu的使用情况。
from cup.res import linux
cpuinfo = linux.get_cpu_usage(intvl_in_sec=60)
print cpuinfo.usr

# get_meminfo函数取得系统Mem信息的回返信息。 是个namedtuple
# total, available, percent, used, free, active, inactive, buffers, cached是她的属性

from cup.res import linux
meminfo = linux.get_meminfo()
print meminfo.total
print meminfo.available

# 获取系统其他信息, 如启动时间、cpu情况、网卡情况等
cup.res.linux.get_boottime_since_epoch()# Returns:	回返自从epoch以来的时间。 以秒记.
cup.res.linux.get_cpu_nums()[source] # 回返机器的CPU个数信息

cup.res.linux.get_cpu_usage(intvl_in_sec=1) # 取得下个intvl_in_sec时间内的CPU使用率信息 回返CPUInfo

cup.res.linux.get_kernel_version() # 拿到Linux系统的kernel信息, 回返是一个三元组 e.g.(‘2’, ‘6’, ‘32’):

cup.res.linux.get_meminfo() # 获得当前系统的内存信息, 回返MemInfo数据结构。

cup.res.linux.get_net_recv_speed(str_interface, intvl_in_sec) # 获得某个网卡在intvl_in_sec时间内的收包速度

cup.res.linux.get_net_through(str_interface) # 获取网卡的收发包的统计信息。 回返结果为(rx_bytes, tx_bytes)

cup.res.linux.get_net_transmit_speed(str_interface, intvl_in_sec=1) # 获得网卡在intvl_in_sec时间内的网络发包速度

print cup.res.linux.get_net_transmit_speed('eth1', 5)

cup.res.linux.get_swapinfo() # 获得当前系统的swap信息

cup.res.linux.net_io_counters() # net io encounters
# 2. 获取进程相关信息, 移植自psutil,可以获取/proc/xxxpid进程内的所有信息
# 也可以获取该进程下所有子进程相关的信息, 详见代码
get_connections(*args, **kwargs)

get_cpu_times(*args, **kwargs)

get_ext_memory_info(*args, **kwargs)

get_memory_info(*args, **kwargs)

get_memory_maps()

get_num_ctx_switches(*args, **kwargs)[source]

get_num_fds(*args, **kwargs)[source]

get_open_files(*args, **kwargs)[source]

get_process_cmdline(*args, **kwargs)[source]

get_process_create_time(*args, **kwargs)[source]

get_process_cwd(*args, **kwargs)[source]

get_process_exe()[source]
获得进程的Exe信息如果这个进程是个daemon请使用get_process_name

get_process_gids(*args, **kwargs)[source]

get_process_io_counters(*args, **kwargs)[source]

get_process_name(*args, **kwargs)[source]

get_process_nice(*args, **kwargs)[source]

get_process_num_threads(*args, **kwargs)[source]

get_process_ppid(*args, **kwargs)[source]

get_process_status(*args, **kwargs)[source]

get_process_threads(*args, **kwargs)[source]

get_process_uids(*args, **kwargs)[source]

nt_mmap_ext alias of mmap

nt_mmap_grouped alias of mmap

1.4 cup.shell 操作shell命令相关的package.

举例,支持超时kill的shell命令执行

具体文档介绍

http://docs.iobusy.com/docs/cup/cup.shell.html

代码示例

# 特色示例
# 1. 拥有超时控制的shell执行. 超时shell命令会被kill (SIGTERM)
import cup
shelltool = cup.shell.ShellExec()
print shelltool.run('/bin/ls', timeout=1)

# 2. 其他类,比如使用rm -rf删除某路径,获取文件MD5等
cup.shell.md5file(filename) # 计算一个文件的md5值。返回32位长的hex字符串。

cup.shell.kill9_byname(strname) # kill -9 process by name

cup.shell.del_if_exist(path) # 如果文件/目录/symlink存在则删除他们

# 3. 进行远程shell操作 (内部调用了pexpect)

# 具体参照 http://docs.iobusy.com/docs/cup/cup.shell.html#module-cup.shell.expect
# 其中exit_status为本地ssh命令退出码, remote_status为远程的退出码

1.5 cup.util util类package - 线程池、唯一码||自增线程安全生成器、自动等待&超时触发器等

各类util类pacakge。举例,线程池,conf(对应c++ Configure及hdfs xxx.xml),生成器、自动等待触发器

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')

1.2 cup.net

网络操作相关的package. 两个重要部分:

  • a. 网卡信息获取、路由信息获取、socket参数设置
  • b. 异步消息通信协议支持

1.2.1 网卡信息获取、路由信息获取、socket设置

具体文档介绍

http://docs.iobusy.com/docs/cup/cup.net.html

代码示例:

# 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')

cup.thirdp 第三方的库依赖支持

cup.services 延时及queue执行器、内存池、心跳service等。

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)                                

欢迎查看文档 http://docs.iobusy.com/docs/cup/

Clone this wiki locally