文章目录
1.模块导入
python3 -c ‘import sys;print(sys.path)’ 显示导入模块时会去哪些路径下查找
__init__
构造方法:
2.进/线程
2.1 多进程
# a.py
from multiprocessing import Process
from hal.hal_sensor import * # HalSensor
from hal.hal_temp import * # HalTemperature
sensor = HalSensor()
a=HalTemperature(sensor)
def pt():
while True:
b=a.get_inlet_temp()
print(b)
if __name__ == '__main__':
p1 = Process(target=pt)
p2 = Process(target=pt)
p3 = Process(target=pt)
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
# l = []
# for i in range(10):
# p = Process(target=pt)
# l.append(p)
# for x in l:
# x.start()
# for y in l:
# y.join()
如下可多执行几次。
2.2 多线程
绿框是主进程,没有等子线程结束就结束了
线程池
适合需要大量线程完成任务,但实际处理时间较短。
import time
from concurrent.futures import ThreadPoolExecutor
import random
# max_workers表示工人数量,也就是线程池里面的线程数量
pool = ThreadPoolExecutor(max_workers=2)
# 任务列表
task_list = ["任务1", "任务2", "任务3", "任务4", ]
def handler(task_name):
n = random.randrange(5) # 随机睡眠,模仿任务执行时间不确定性
time.sleep(n)
print(f"任务内容:{task_name}")
if __name__ == '__main__':
# 遍历任务,
for task in task_list:
"""
交给函数处理,submit会将所有任务都提交到一个地方,不会阻塞
然后线程池里面的每个线程会来取任务,
比如:线程池有3个线程,但是有5个任务
会先取走三个任务,每个线程去处理
其中一个线程处理完自己的任务之后,会再来提交过的任务区再拿走一个任务
"""
pool.submit(handler, task) # 线程取【任务task】并【执行handler】【任务task】
# pool.shutdown() # 让主线程等待所有子线程执行完之后,主线程再执行完
print("main执行完毕")
# main执行完毕
# 任务内容:任务1
# 任务内容:任务3
# 任务内容:任务2
# 任务内容:任务4
3.文件/xml
如下增:写入文件内容给文本文件。
def writeTextFile(filePath, fileContent, encoding='utf8'):
with open(filePath, 'w', encoding=encoding) as file:
file.write(fileContent)
如下改:批量修改图片大小。
import os
from PIL import Image
def getFilePathList(dirPath, partOfFileName=''):
allFileName_list = list(os.walk(dirPath))[0][2]
fileName_list = [k for k in allFileName_list if partOfFileName in k]
filePath_list = [os.path.join(dirPath, k) for k in fileName_list]
return filePath_list
def batchResizeImage(oldDirPath, newDirPath, height, width):
if not os.path.isdir(newDirPath):
os.mkdir(newDirPath)
jpgFilePath_list = getFilePathList(oldDirPath, '.jpg')
for jpgFilePath in jpgFilePath_list:
image = Image.open(jpgFilePath)
resized_image = image.resize((height, weight), Image.ANTIALIAS)
jpgFileName = os.path.split(jpgFilePath)[1]
saveFilePath = os.path.join(newDirPath, jpgFileName)
resized_image.save(saveFilePath)
oldDirPath = 'source_images'
newDirPath = 'train_images'
height = 640
width = 640
batchResizeImage(oldDirPath, newDirPath, height, width)
如下查:查询文件夹中的文件。
import os
def getFileNameList(dirPath, partOfFileName=''):
allFileName_list = list(os.walk(dirPath))[0][2]
fileName_list = [k for k in allFileName_list if partOfFileName in k]
return fileName_list
def getFilePathList(dirPath, partOfFileName=''):
allFileName_list = list(os.walk(dirPath))[0][2]
fileName_list = [k for k in allFileName_list if partOfFileName in k]
filePath_list = [os.path.join(dirPath, k) for k in fileName_list]
return filePath_list
如下查:读取文件。
import os
def getdir():
for root, dirs, files in os.walk("D:\soft\Dict\8.9.6.0\otherskins\simisent"):
#for dir in dirs:
print('root {}'.format(root))
print('dirs {}'.format(dirs))
print('files {}'.format(files))
a=getdir()
#11111111111111111111111111111111111111111111111111111111111111111111111
def show_files(path, all_files):
file_list = os.listdir(path)
for file in file_list:
cur_path = os.path.join(path, file)
if os.path.isdir(cur_path):
show_files(cur_path, all_files)
else:
all_files.append(file)
return all_files
contents = show_files("D:\soft\Dict\8.9.6.0\otherskins\simisent", [])
for content in contents: # 循环打印show_files函数返回的文件名列表
print(content)
def readTextFile(filePath, encoding='utf8'):
with open(filePath, encoding=encoding) as file:
return file.read()
如下查:搜索文件夹路径内含有指定内容的代码文件。
import os
# 传入3个参数:文件夹路径dirPath、指定内容partOfFileContent、代码文件后缀名suffixOfFileName
def searchFileContent(dirPath, partOfFileContent, suffixOfFileName=''):
dirPath = os.path.expanduser(dirPath)
walk_list = list(os.walk(dirPath))
result_list = []
for walk in walk_list:
filePath_list = [os.path.join(walk[0], k) for k in walk[2] \
if k.rsplit('.', maxsplit=1)[1]==suffixOfFileName.strip('.')]
for filePath in filePath_list:
with open(filePath, encoding='=utf8') as file:
fileContent = file.read()
if partOfFileContent in fileContent:W
print(filePath)
result_list.append(filePath)
return result_list
如下labelimg_yolo_txt转pascal voc_xml。
from PIL import Image
import os
#读取文件尺寸
def ImgSize(image):
img = Image.open(image)
w,h = img.width,img.height
return w,h
#labelimg中yolo转voc图位转换
#width,height就是原图的w,h #xmin指中心点占横比例,xmax指中心点占竖比例 #ymin指bbox占整图宽比例,ymax指bbox占整图高比例
def ScaleCovertor(width,height,xmin,xmax,ymin,ymax):
center_x = round(float(xmin* width))
center_y = round(float(xmax * height))
bbox_width = round(float(ymin * width))
bbox_height = round(float(ymax * height))
xmin = str(int(center_x - bbox_width / 2 ))
ymin = str(int(center_y - bbox_height / 2))
xmax = str(int(center_x + bbox_width / 2))
ymax = str(int(center_y + bbox_height / 2))
return xmin,ymin,xmax,ymax
def Main(filepath): #filepath是txt文件夹路径(里面全是需要转换的txt文件)
#设置xml内部格式
xml_head = '''
<annotation>
<folder>Desktop</folder>
<filename>{}</filename>
<path>unknonw</path>
<source>
<database>unknow</database>
</source>
<size>
<width>{}</width>
<height>{}</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
'''
xml_obj = '''
<object>
<name>{}</name>
<pose>no</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>{}</xmin>
<ymin>{}</ymin>
<xmax>{}</xmax>
<ymax>{}</ymax>
</bndbox>
</object>
'''
xml_end = '''
</annotation>'''
counter = 1 #计数器
for filename in os.listdir(filepath): #现在的filename是带后缀的
print ('Processing:->>',filename,'Number %s'%counter) #打印当前文件名 和 第几个文件
#原图:
content=[] #建立内容列表,class,中心点占比,bbox占比
with open(filepath+'/'+filename,'r') as readlines:
for linecontent in readlines.readlines(): #读取每一行内容
content.append(linecontent) #添加到列表中
w,h = ImgSize('C:/Users/lenovo/Desktop/yuantu'+'/'+filename.split('.')[0]+'.jpg') #调用文件尺寸读取函数
#xml:
obj = '' #这里创建xml,建立空字符串
head = xml_head.format(str(filename.split('.')[0]+'.jpg'),str(w),str(h)) #向xml head里添加文件名 文件w和h
for info in content: #读取每个文件里的内容
infodetail = info.split(' ') #以空格切割列表内的数据
#单独读取每个数据保存到变量里
Class,XMin,XMax,YMin,YMax = infodetail[0],infodetail[1],infodetail[2],infodetail[3],infodetail[4],
xmin,ymin,xmax,ymax = ScaleCovertor(w,h,float(XMin),float(XMax),float(YMin),float(YMax))
label= {1:'obstacle',0:'people'} #确定label和类的映射关系,下行用到
obj += xml_obj.format(label[int(Class)],xmin,ymin,xmax,ymax) #向主object里循环添加 一个图里的物体或类
#写入xml文件
with open('C:/Users/lenovo/Desktop/annotation2/xml'+filename.split('.')[0]+'.xml','w') as xmw:
#创建写入 合并 三个 xml主体部分
xmw.write(head+obj+xml_end)
counter+=1
Main('C:/Users/lenovo/Desktop/annotation2/txt') #txt文件夹
#验证转的对错
import matplotlib.pyplot as plt
import matplotlib.image as Image #这个读取库比较方便 不用把数据转来转去,plt可以直接使用
%matplotlib inline
img = Image.imread('/Users/Desktop/annotation2/test/yuantu/'+'20190721062948_000394_cc8cdaa5ee38.jpg') #读取
x1,y1,x2,y2 = 1344, 495, 1722, 1080 # 自己找验证
plt.gca().add_patch (
plt.Rectangle(xy=(x1,y1),width=x2-x1,height=y2-y1,fill=False,edgecolor='red',linewidth=2)
)
plt.imshow(img)
plt.show() #根据环境添加
如下删除 w label。
import re
import os
rawfolder='123' #存放三张xml的文件夹
newfolder='33333' #生成的新的xml文件夹
for i in os.listdir(rawfolder):
print (i) #输出#20190720073948_000258_cc8cdaa5ee49.xml
#20190720073950_000257_cc8cdaa64390.xml
#20190720073950_000258_cc8cdaa5ee3e.xml
with open(rawfolder+'/'+i,'r') as r:
content = r.readlines()
#print(content)
#输出['<annotation>\n', '\t<folder>img</folder>\n', '\t<filename>20190720073948_000258_cc8cdaa5ee49.JPG</filename>\n', ...]
c = 0
for j in content:
if '<name>w</name>' in j:
print (j,'下标-》',c) #c为14行<name>w</name>,从0行开始
start = 0
end = c-1 # c-1为上一行<object>
first_part = content[start:end]
second_part = content[end+12:] #整个一块为w的object
final = first_part+second_part
for x in final:
with open(newfolder+'/'+i,'a+') as w:
w.writelines(x)
print (x)
c+=1
# break
如下检查不是people和obstacle的label。
# 检查不是people和obstacle的label
import re
import os
rawfolder='123'
#newfolder='33333'
for i in os.listdir(rawfolder):
# print (i)
with open(rawfolder+'/'+i,'r') as r:
content = r.readlines()
# print(content)
for j in content:
if '<name>' in j and ('people' not in j and 'obstacle'not in j):
print (j)
print (i)
如下读取指定后缀。
import os
def get_filePathList(dirPath, partOfFileName=''):
all_fileName_list = next(os.walk(dirPath))[2] #['20190720072950_000256_cc8cdaa64390.JPG',
#'20190720073948_000258_cc8cdaa5ee49.JPG',
# '20190720073950_000257_cc8cdaa64390.JPG',
# '20190720074950_000259_cc8cdaa5ee3e .jpg',
#'20190720074950_000259_cc8cdaa5ee3e.JPG']
fileName_list = [k for k in all_fileName_list if partOfFileName in k] #去除除了'.JPG'文件,不含前面绝对路径
filePath_list = [os.path.join(dirPath, k) for k in fileName_list] #含全部路径,['',
# '']
#return fileName_list
return filePath_list
dirPath='C:/Users/lenovo/Desktop/lian'
a=get_filePathList(dirPath,'.JPG')
a
#print(len(a))
如下检查是否有图片漏标,并删除漏标图片。
def delete_file(filePath):
if not os.path.exists(filePath): #filePath指C:/Users/lenovo/Desktop/lianxi/img\\20190720072950_000256_cc8cdaa64390.JPG'
print('%s 这个文件路径不存在,请检查一下' %filePath)
else:
print('%s 这个路径的文件需手动删除' %filePath)
def check_1(dirPath, suffix):
xmlFilePath_list = get_filePathList(dirPath, '.xml') # 与suffix不同,自己指定'.xml'
xmlFilePathPrefix_list = [k[:-4] for k in xmlFilePath_list] # 不带.xml
xmlFilePathPrefix_set = set(xmlFilePathPrefix_list)
#print(xmlFilePathPrefix_set) #{'绝对路径不带后缀',
# ' ' }
imageFilePath_list = get_filePathList(dirPath, suffix)
imageFilePathPrefix_list = [k[:-4] for k in imageFilePath_list] # 不带后缀
imageFilePathPrefix_set = set(imageFilePathPrefix_list)
#print(imageFilePathPrefix_set)
redundant_imgFilePathPrefix_list = list(imageFilePathPrefix_set - xmlFilePathPrefix_set)
redundant_imgFilePath_list = [k+'.JPG' for k in redundant_imgFilePathPrefix_list]
#上行带.JPG后缀, 如果自定义.0JPG,显示这个文件路径不存在,请检查一下
for imgFilePath in redundant_imgFilePath_list:
delete_file(imgFilePath)
dirPath='C:/Users/lenovo/Desktop/lx'
check_1(dirPath,'.JPG')
检测标记的box是否超过图片的边界,若有则显示删除与box相关的xml文件和图片文件。
import xml.etree.ElementTree as ET
from PIL import Image
def check_2(dirPath, suffix):
xmlFilePath_list = get_filePathList(dirPath, '.xml')
#print(xmlFilePath_list) #['.xml全部路径',
# ' ']
allFileCorrect = True # 跳出for循环则执行 if allFileCorrect
for xmlFilePath in xmlFilePath_list:
imageFilePath = xmlFilePath[:-4] + '.' + suffix.strip('.')
#print(xmlFilePath)
#print(imageFilePath)
#C:/Users/lenovo/Desktop/lx\20190720072950_000256_cc8cdaa64390.xml
#C:/Users/lenovo/Desktop/lx\20190720072950_000256_cc8cdaa64390.JPG
#.....
image = Image.open(imageFilePath)
width, height = image.size
with open(xmlFilePath) as file:
fileContent = file.read()
#print(fileContent) #<annotation>...
root = ET.XML(fileContent) #根<annotation>...
object_list = root.findall('object') # <object>
for object_item in object_list:
bndbox = object_item.find('bndbox') #<bndbox>
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
if xmax>xmin and ymax>ymin and xmax<=width and ymax<=height:
continue
else:
delete_file(xmlFilePath)
delete_file(imageFilePath)
allFileCorrect = False
break
if allFileCorrect:
print('祝贺你! 已经通过检验,所有xml文件中的标注框都没有越界')
dirPath='C:/Users/lenovo/Desktop/lx' #lx文件夹里.xml和.JPG混在一起
check_2(dirPath,'.JPG')#''里必须.JPG或不填
如下检查xmin<0…,并修改xmin…
#coding=utf-8
import os
import shutil
import random
from xml.etree.ElementTree import ElementTree,Element
import cv2
def read_xml(in_path):
'''
读取并解析xml文件
in_path: xml路径
return: ElementTree
'''
tree = ElementTree()
tree.parse(in_path)
return tree
def check():
url = "C:/Users/lenovo/Desktop/source/xml_sum" # xml_sum只存放xml的文件夹
for item in os.listdir(url): # item为.xml文件
tree = read_xml(url + "/" + item) # read_xml函数上面定义
root = tree.getroot()
object = root.findall("object")
size = root.find("size")
width =int(size.find("width").text)
height = int(size.find("height").text)
if object == None:
print(item)
continue
for it in object:
bndbox = it.find("bndbox")
if bndbox == None:
print(item)
xmin = int(bndbox.find("xmin").text)
xmax = int(bndbox.find("xmax").text)
ymin = int(bndbox.find("ymin").text)
ymax = int(bndbox.find("ymax").text)
if xmin <= 0 or xmin >= xmax or ymin <=0 or ymin >= ymax:
print(item)
if xmax > width or ymax> height:
print(item)
if __name__ =='__main__':
check() # 不输出则表示全对。输出123111.xml,没有列表引号
import xml.etree.ElementTree as ET
def generateNewXmlFile(old_xmlFilePath, new_xmlFilePath):
with open(old_xmlFilePath) as file:
fileContent = file.read()
root = ET.XML(fileContent)
object_list = root.findall('object')
for object_item in object_list:
bndbox = object_item.find('bndbox')
xmin = bndbox.find('xmin')
xminValue = int(xmin.text)
xmin.text = str(int(xminValue + 1))
ymin = bndbox.find('ymin')
yminValue = int(ymin.text)
ymin.text = str(int(yminValue + 1))
xmax = bndbox.find('xmax')
xmaxValue = int(xmax.text)
xmax.text = str(int(xmaxValue + 1))
ymax = bndbox.find('ymax')
ymaxValue = int(ymax.text)
ymax.text = str(int(ymaxValue + 1))
tree = ET.ElementTree(root)
tree.write(new_xmlFilePath)
old_dirPath ='C:/Users/lenovo/Desktop/999/8'
new_dirPath ='C:/Users/lenovo/Desktop/999/9'
def batch_modify_xml(old_dirPath, new_dirPath): #修改文件夹中的若干xml文件
#以下4行将new_dirPath和xmlFileName名称结合,内容是调用generateNewXmlFile函数改写
xmlFilePath_list = get_filePathList(old_dirPath, '.xml')
for xmlFilePath in xmlFilePath_list:
xmlFileName = os.path.split(xmlFilePath)[1] #1后
#print(xmlFileName) #输出 20190720073950_000257_cc8cdaa64390.xml
new_xmlFilePath = os.path.join(new_dirPath, xmlFileName)
generateNewXmlFile(xmlFilePath, new_xmlFilePath)
batch_modify_xml(old_dirPath, new_dirPath)
如下读取classname。
def get_classNameList(txtFilePath):
with open(txtFilePath, 'r', encoding='utf8') as file:
fileContent = file.read()
line_list = [k.strip() for k in fileContent.split('\n') if k.strip()!='']
className_list= sorted(line_list, reverse=False)
return className_list
txtFilePath='C:/Users/lenovo/Desktop/labelImg/data/predefined_classes -outofstock.txt'
get_classNameList(txtFilePath)
# 添加环境变量
import sys
sys.path.append('')
import os
pathnoname,name=os.path.split("E:/lpthw/zedshaw/ex19.py")
print(pathnoname)
print(name)
如下检查trainval.txt。
import cv2
from os import listdir
from os.path import isfile,isdir,join
trainval_list = list()
with open('./trainval.txt','r') as f:
for line in f.readlines():
line = line.strip('\n')
a = line +'.jpg'
trainval_list.append(a)
print(trainval_list)
for i in trainval_list:
img_path = '{}{}'.format('./img3/',i)
img = cv2.imread(img_path)
try:
img.shape
print(img.shape) # 在img3文件夹中没有......11111.jpg图片
except:
print('fail read:' + img_path)
continue
4.百度人脸api
https://ai.baidu.com/docs#/Auth/top 获取Access Token。
如下点击发送后返回Body中access_token。
# encoding:utf-8
import os
import base64
import json
import xlwt
import urllib.request,urllib.parse
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect" # 在人脸识别API文档中人脸检测中
row = 0
col = 0
workbook = xlwt.Workbook('123.xls')
worksheet = workbook.add_sheet("gongzuobiao")
worksheet.write(row, col+0, "时间")
worksheet.write(row, col+1, "相机ID")
worksheet.write(row, col+2, "性别")
worksheet.write(row, col+3, "年龄")
worksheet.write(row, col+4, "种族")
worksheet.write(row, col+5, "表情")
for imgname in os.listdir("G:/img3"):
print("imgname:",imgname) # imgname:
data = imgname.split('_')[0]
print("data:", data) # data:
camera = imgname.split('_')[2].split(".")[0]
print("camera:",camera) # camera:
imgpath = "G:/img3"+imgname
f = open(imgpath, 'rb')
img = base64.b64encode(f.read())
params ={"image":img,
"image_type":"BASE64",
"face_field":"age,gender,race,emotion"}
params = urllib.parse.urlencode(params).encode(encoding='UTF8')
access_token = '24.0449cf2437689f550ca5f93dac53fce3.2592000.1574310334.282335-17084453' ###############
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(request)
content = response.read()
print("content:",content)
if content:
str = content.decode()
content = json.loads(str)
results = content['result']
if results:
row += 1
for item in results["face_list"]:
print("item:\n",item['gender']['type'])
worksheet.write(row, col + 0, data)
worksheet.write(row, col + 1, camera)
worksheet.write(row, col + 2, item['gender']['type'])
worksheet.write(row, col + 3, item['age'])
worksheet.write(row, col + 4, item['race']['type'])
worksheet.write(row, col + 5, item['emotion']['type'])
workbook.save('123.xls')
5.aiohttp
tomcat配置文件中监听的IP地址设置了0.0.0.0表示tomcat服务器监听在本机的所有IP地址,通任何一个IP都可访问到
localhost
:是一个域名,默认指向127.0.0.1这ip(本地回环地址,速度快,不会出现在主机外部网络),绑定了localhost的服务只能在本机访问。
# a.py:创建一个后端app应用
from aiohttp import web
# from application.routes import setup_routes
app = web.Application()
# setup_routes(app)
web.run_app(app, host='0.0.0.0', port=9000) #开启后端
python a.py:在浏览器中打开http://localhost:9000/
或者使用命令curl -X GET http://localhost:9000
,对于请求现在只会返回404: Not Found。如下创建一个视图(返回值)和路由
并在a.py中将注释的两行放开运行: curl -X GET localhost:9000/hello ,Hello Aiohttp!
# application文件夹/views.py
from aiohttp import web
async def hello(request):
return web.Response(text='Hello Aiohttp!')
# application文件夹/routes.py
from .view import hello
def setup_routes(app):
app.router.add_get('/hello', hello) # 第二个hello是调用views.py中函数,相当于response
# 第一个/hello可单独写一个.py文件里面写成如下,取board_routes[0]
# board_routes = [
# "/api/psu/info",
# "/api/psu/info/{name}",
# ]
如下问题是发送的curl中data的json数据格式不对。404 not found是curl中url路径错误。
Connection refused说明restapi服务没起,curl http://127.0.0.1:8080/api/v1.0/info。
# board_endpoint.py:视图即response,调用rest_bmc.py中函数接口
import re
import rest_help
import rest_psu
import rest_fan
from aiohttp import web
from rest_utils import dumps_bytestr
class boardApp_Handler:
async def rest_help_hdl(self, request):
return web.json_response( rest_help.get_help(), dumps=dumps_bytestr)
async def rest_psu_model_name_hdl(self, request):
return web.json_response( rest_psu.get_model_name(request.match_info["name"]), dumps=dumps_bytestr)
# boardroutes.py:路由即/
board_routes = []
board_routes.append("/api/help")
board_routes.append("/api/psu/{name}/model_name")
# board_setup_routes.py:连接作用
from board_endpoint import boardApp_Handler
from boardroutes import *
from aiohttp.web import Application
def setup_board_routes(app: Application, write_enabed: bool):
bhandler = boardApp_Handler()
# 下行第一个参数路径(boardroutes.py路由),第二个参数response(board_endpoint.py视图)
app.router.add_get(board_routes[0], bhandler.rest_help_hdl)
app.router.add_get(board_routes[3], bhandler.rest_psu_model_name_hdl)
6.json/walk/split/getattr/bin/lspci/ethtool/usb/serial
json.dumps(将字典转化为字符串,将json信息写进文件),json.loads(将字符串转化为字典),pip3 install --upgrade pip(#!/usr/bin/env python3)。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import json
# dict1 = {"age": "12"} # <type 'dict'>
# json_info = json.dumps(dict1)
# print("通过json.dumps()函数处理:json_info的类型:"+str(type(json_info))) # <type 'str'>
# print(json_info) # {"age": "12"}
json_info = '{"age": "12"}' # <type 'str'>
dict1 = json.loads(json_info)
print("通过json.loads()函数处理:dict1的类型:"+str(type(dict1))) # <type 'dict'>
print(dict1) # {u'age': u'12'} # u:unicode中文显示
# json_info = "{'age': '12'}"
# file = open('1.json','w')
# json.dump(json_info,file) #生成1.json里面内容为 "{'age': '12'}"
# coding=UTF-8<code>
import os
import json
def d():
info_list = ['a','b','c','d','e','f','g','h','i']
split_list = [info_list[i:i+4] for i in range(0, len(info_list), 4)]
print(split_list) # [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i']]
x = set('runoob')
print(x) # set(['b', 'r', 'u', 'o', 'n'])
print(len(x)) # 5
i2clist={
'2':'port 1 SFP',
'3':'port 2 SFP',
}
def e():
for i in i2clist:
print(i) # 3 2
def search_dir_by_name(name, dir):
result = []
try:
files = os.listdir(dir)
for file in files:
if name in file:
result.append(os.path.join(dir, file))
except Exception as e:
pass
return result
dir = "/sys/block/"
spect = "sd"
ssdpath = []
# /sys/block/sd* remove=1 sda fdisk | grep sda 有/dev/sda , 没有的话只有sda, 拼接/dev/sda
if __name__ == '__main__':
result = search_dir_by_name(spect, dir)
#print(result) #['/sys/block/sdd', '/sys/block/sdb', '/sys/block/sde', '/sys/block/sdc', '/sys/block/sda']
for item in result:
with open(os.path.join(item, "removable"), 'r') as fd:
#print(item) # /sys/block/sde
#print(os.path.join(item, "removable")) # /sys/block/sde/removable
#print(fd.read()) #0或1 # SyntaxError: Non-ASCII character '\xe6' 加#coding=UTF-8<code>
value = fd.read()
if value.strip() == "0": # found ssd #去除1
ssd_disk = "/dev/" + os.path.basename(item) #ssd_disk:/dev/sda .../dev/sdd
ssdpath.append(ssd_disk)
python中None,False,空字符串"",0,空列表[],空字典{},空元组()都相当于False。 []列表 , ()元组 , {}集合 , {a:b}字典。for else中break后不走else。
def stop_all(self, a) 。b=self.stop_all(a)默认第一个参数有self。
# a.py
# import json, os
# print(json.__file__) # /usr/lib/python3.8/json/__init__.py
# print(os.path.abspath(__file__)) # /home/y/utool/a.py
# print(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # /home/y
sys.path.append(dirname(dirname(abspath(__file__)))) #当前文件所在绝对路径的文件夹的上级文件夹
Project_path = dirname(abspath(__file__)) #获取所在路径文件夹
# a.py
import binascii
def crc2hex(crc):
crc_bytes = bytes(str(crc), 'utf-8')
print("11",crc_bytes) # b'a'
print("22",binascii.crc32(crc_bytes)) # 3904355907
return '0x{:0>8X}'.format(binascii.crc32(crc_bytes)) # 计算CRC32校验值(摘要算法),并将其转换为8位的十六进制字符串
a=crc2hex("a")
print(a) # 0xE8B7BE43
# b.py
import os
import json
import sys
def seek_file(path, name):
fullpath = None
for root, dirs, files in os.walk(path):
if name in files:
fullpath = '{0}/{1}'.format(root, name)
break
return fullpath
if len(sys.argv)==3:
print(sys.argv[1],sys.argv[2])
path=seek_file(sys.argv[1],sys.argv[2])
print(path)
else:
print(len(sys.argv),'err')
root@bmc-oob:~# python b.py /sys/bus/i2c/devices/26-0048 in3_input
/sys/bus/i2c/devices/26-0048 in3_input
/sys/bus/i2c/devices/26-0048/hwmon/hwmon4/in3_input
def get_baseboard_sku():
try:
proc = subprocess.Popen([IPMITOOL_PATH, "raw", "0x3a", "0x10", "9", "0x1a", "1", "0x12"],
stdout=subprocess.PIPE,
shell=False,
stderr=subprocess.STDOUT) # proc返回20
stdout = proc.communicate()[0] # b' 20\n'
proc.wait()
sku_str = stdout.decode() # 20
sku_bin = bin(int("0x" + sku_str.strip(), 16))[-5:] # 0x20(十六进制 )= 32(十进制 ),bin(32) = 0b100000
sku_dict = {
"00001" : "ns35x",
"00010" : "ns34x",
}
sku = sku_dict.get(sku_bin, "")
except OSError as e:
raise OSError("Cannot detect platform")
return sku
output = remote.command("sol.sh", time=10, expect_list=['-----------------------------------'])
output1 = remote.command('\x15\x12\x14\x33', current=True, expect_list=expect_list)
output2 = remote.command('\r\n', current=True, expect_list=expect_list)
if "bmc-oob" in output1 or "bmc-oob" in output2:
emmc_info_cmd = "mmc cid read /sys/block/mmcblk0/device",
emmc_size_cmd = "cat /sys/block/mmcblk0/size"
def bmc_ping_cpu_test(self, also_print_console=False):
count = 5
ping_cmd = "ping %s -c %d | grep received" % (
self.bmc_login["ip"], count)
status, output = run_command(ping_cmd)
self.logger.log_info(output, also_print_console)
if output.find(" 0% packet loss") > 0:
ret = E.OK
else:
self.fail_reason.append("cpu ping server lost packages")
ret = E.EMGMT11002
return ret
bmc_emmc_stress_test_cmd = "touch /var/log/emmc.test ; \
echo '==emmc read stress test==' ; \
(time dd if=/dev/mmcblk0 of=/dev/null bs=1M count=10 && sync) >> /var/log/emmc.log ; echo ' ' ; \
echo '==emmc write stress test==' ; \
(time dd if=/dev/zero of=/var/log/emmc.test bs=1M count=10 && sync) >> /var/log/emmc.log ; \
rm /var/log/emmc.test"
def str_to_hex(self, s): # 把字符串转化为对应的16进制串
return ' '.join([hex(ord(c)).replace('0x', '') for c in s])
def hex_to_str(self, s):
return ''.join([chr(i) for i in [int(b, 16) for b in s.split(' ')]])
# def str2bin(s): # bin二进制
# return ' '.join([bin(ord(c)).replace('0b', '') for c in s])
# def bin2str(s):
# return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])
def replace_char(self, string, char, index):
string = list(string)
string[index] = char
return ''.join(string)
def modify_profile(self, also_print_console=True):
ret, Before_mac = self.get_mac_address_from_tlv_eeprom(also_print_console)
if ret != E.OK:
return ret
After_mac = Before_mac # 00E0EC000000
for i in range(11, -1, -1): # 从11到0 (竖着打印) , 12个(相当于索引)
last = After_mac[i] # 从右向左单个遍历
if last == "F":
After_mac = self.replace_char(After_mac, "0", i)
continue
else:
last = self.str_to_hex(last) # 字符0转为ascii码48,再转为十六进制0x30即30
last = int(last) + int(self.offset["offset"]["bmc_offset"])
last = str(last)
last = self.hex_to_str(last) # 函数里有split,所以last必须为str类型
After_mac = self.replace_char(After_mac, last, i) # After_mac = "EFFFFFFFFFFF" , 因为下行break,只打印出一行F00000000000
break
sign = 0
os.chdir(self.hwsku_path)
with open('fru.ini', 'r') as f:
lines = []
for line in f.readlines():
if line != '\n':
lines.append(line)
f.close()
with open('fru.ini', 'w') as f:
for line in lines:
if "bmc_base_mac_address=" in line:
if sign == 1:
f.write('%s' % line)
continue
line = "bmc_base_mac_address="+After_mac
f.write('%s\n' %line)
sign = 1
else:
f.write('%s' %line)
return After_mac
# root@localhost:~# lspci -nnn -s 07:00.0 (07:00.0是fpga的bus号,fpga是pcie设备)
# 07:00.0 Memory controller [0580]: Xilinx Corporation Device [10ee:7021]
# root@localhost:~# lspci -vvv -s 07:00.0 (devmem 0xfb800000 8)
# Region 0: Memory at fb800000 (32-bit, non-prefetchable) [size=4M]
"pcie":{
"X552":{ ######### device
"width":"x1",
"speed":"2.5GT/s",
"num":4
}
},
def _get_bus_dev_func(self, device): ######### device就是X552
bus_dev_func = []
ret, out = run_command("lspci | grep {}".format(device))
for line in out.splitlines():
if re.search("[0-9a-f]{2}\:[0-9a-f]{2}\.[0-9a-f]{1}", line):
bus_dev_func.append(re.findall(
"[0-9a-f]{2}\:[0-9a-f]{2}\.[0-9a-f]{1}", line)[0])
else:
bus_dev_func.append(None)
return bus_dev_func
def _get_device_conf(self, busid): ######### 遍历bus_dev_func中busid
ret, output = run_command(
"lspci -s {} -vvv | grep -i LnkSta | grep Speed".format(busid))
if ret or output == '':
return '', ''
else:
speed = output.strip(" \t\n").split(',')[0].split('Speed')[1].strip()
width = output.strip(" \t\n").split(',')[1].split('Width')[1].strip()
return speed, width
# ethtool 网卡名 :查看网卡速率,全双工
# ethtool -s ethX [speed 10|100|1000] [duplex half|full] [autoneg on|off]
# 设置网口速率 10/100/1000M 设置网口半/全双工 设置网口是否自协商
# cat /sys/block/sda/queue/rotational , 1是HDD,反之是SSD
def get_usb_location(self):
dir = "/sys/block/"
spect = "sd"
usbpath = []
result = self.search_dir_by_name(spect, dir)
if len(result) <= 0:
self.fail_reason.append("no usb found")
return E.EUSB1000, usbpath
for item in result:
with open(os.path.join(item, "removable")) as fd:
value = fd.read()
if value.strip() == "1":
usbpath.append(item)
if not usbpath:
self.fail_reason.append("no usb found")
return E.EUSB1000, usbpath
return E.OK, usbpath
def get_usb_info(self, path): # path是get_usb_locationde得到
size_file = os.path.join(path, "size")
model_file = os.path.join(path, "device/model")
try:
with open(size_file) as f, open(model_file) as f1:
value = f.read()
value1 = f1.read()
if value.strip() == '0':
return E.EUSB1007,{}
return E.OK, {
"dev": os.path.basename(path), "size": round(
float(value) * 512 / 1024 / 1024 / 1024, 1), "model": value1}
except IOError as e:
return E.EIO, str(e)
def usb_stress(self, usb_dev, also_print_console=False):
ret = E.EFAIL
cmd = "fdisk -l |grep %s|grep 'Disk' -v|awk '{print $1}'" % usb_dev
fdisk_ret, fdisk_log = run_command(cmd)
usb_list = fdisk_log.splitlines()
if not usb_list:
usb_list.append("/dev/" + usb_dev)
for usb_disk in usb_list:
self.logger.log_info("[USB STRESS TEST]:", also_print_console)
ret, log = run_command("mount -v | grep /mnt/usb")
if log != '':
run_command("umount /mnt/usb/")
run_command("rm -rf /mnt/usb/")
mount_ret, mount_log = run_command("mkdir -p /mnt/usb && mount {} /mnt/usb/".format(usb_disk))
if mount_ret:
self.fail_reason.append("mount usb failed")
ret = E.EUSB1001
run_command("rm -rf /mnt/usb/")
return ret
else:
test_stress_cmd = "dd if={} of={}4G.txt bs=4k count=1048570".format(
usb_disk, "/mnt/usb/")
waiting = pit_util_common.waiting(self.logger,"usbstress testing...")
stress_ret, stress_log = run_command(test_stress_cmd)
waiting.stop("usbstress test stop!")
if stress_ret:
self.fail_reason.append("stress test failed!")
ret = E.EUSB1006
else:
self.logger.log_info("stress test success.", also_print_console)
ret = E.OK
run_command("rm -rf /mnt/usb/4G.txt")
run_command("umount /mnt/usb/")
run_command("rm -rf /mnt/usb/")
远程升级slave(PXE/XDPE)FW:解析test.xsf(硬件给,相当于FW,和bin文件一样)字段,写进NVM,再read from nvm to reg。