以下shell脚本是什么意思,转换成python是什么样的。ComNvmeDevNameInfoGet() { local slot_info="$1" local ret_file="$2" local ctrl_str="$3" local slot_id="" local slot_temp="" local slot_id_get="" local contrl_num="" local test_scene="" local ctrl_id=0 ls /dev/ | grep "nvme[0-9]\{1,\}" | grep -v "nvme[0-9]\{1,\}n" >nvme_info.txt slot_id=$(echo "${slot_info}" | sed 's/^0*//g') slot_id=$[slot_id+0] slot_id=$[slot_id-1] set +x while read contrl_num || [[ ! -z ${contrl_num} ]] do if [ "0" == "${ctrl_id}" ];then find /sys/devices -name "${contrl_num}" > ${contrl_num}_bus_info.txt else rm -f ${contrl_num}_bus_info.txt dos2unix ${contrl_num}_bus_info.txt fi #兼容服务器的PCIE链路 if grep -iw "FT2-MP1" /root/TestPlat/common/test_scene.ini ;then slot_temp=$(cat ${contrl_num}_bus_info.txt | awk -F / '{print $5}' | awk -F "." '{print $2}') else slot_temp=$(cat ${contrl_num}_bus_info.txt | awk -F "/" '{print $7}' | cut -b 9-10) fi slot_id_get=$[0x${slot_temp}+0] if [ "${slot_id_get}" = "${slot_id}" ];then set -x echo "${slot_id}:/dev/${contrl_num}:Ctrl${ctrl_id}" echo "/dev/${contrl_num}" >"${ret_file}" return 0 fi done < nvme_info.txt set -x echo "$(date +"%y-%m-%d %H:%M:%S") can not get nvme controller ctrl${ctrl_id} info,slot${slot_info}." | tee -a error.txt return 1
时间: 2024-01-22 20:19:50 浏览: 235
C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结
这个shell脚本的作用是根据输入的slot_info(slot id)来获取对应的NVMe控制器的设备名,并将其输出到指定的文件中。具体流程如下:
1. 将所有/dev/目录下的名字类似于“nvme[0-9]”但不包含“nvme[0-9]n”的文件名存储在nvme_info.txt文件中。
2. 将输入的slot_info转换为十进制,并减去1,得到slot_id。
3. 遍历nvme_info.txt中的每个文件名,获取其对应的控制器编号contrl_num。
4. 根据contrl_num在/sys/devices目录下查找对应的总线信息文件,并将其存储在contrl_num_bus_info.txt文件中。
5. 根据服务器型号的不同,从contrl_num_bus_info.txt中提取出对应的PCIe插槽编号slot_temp。
6. 将slot_temp转换为十进制,得到slot_id_get。
7. 如果slot_id_get与slot_id相等,则将控制器设备名输出到ret_file文件中,并返回0表示成功。
8. 如果遍历完所有的文件名仍然没有找到对应的控制器设备名,则将错误信息输出到error.txt文件中,并返回1表示失败。
将其转换成Python代码大致如下:
```python
import os
import re
import datetime
def ComNvmeDevNameInfoGet(slot_info, ret_file, ctrl_str):
with open("nvme_info.txt", "w") as f:
for file in os.listdir("/dev/"):
if re.match("nvme[0-9]+", file) and not re.match("nvme[0-9]+n", file):
f.write(file + "\n")
slot_id = int(slot_info.lstrip("0"))
slot_id -= 1
ctrl_id = 0
with open("nvme_info.txt", "r") as f:
for contrl_num in f:
contrl_num = contrl_num.strip()
if ctrl_id == 0:
with open(f"/sys/devices/*/pci*/{contrl_num}/", "w") as bus_info_file:
bus_info_file.write(contrl_num)
else:
os.remove(f"{contrl_num}_bus_info.txt")
os.system(f"dos2unix {contrl_num}_bus_info.txt")
if "FT2-MP1" in open("/root/TestPlat/common/test_scene.ini").read():
with open(f"{contrl_num}_bus_info.txt", "r") as bus_info_file:
slot_temp = re.search(r"/pci([0-9a-fA-F]{2}).*/", bus_info_file.read()).group(1)
else:
with open(f"{contrl_num}_bus_info.txt", "r") as bus_info_file:
slot_temp = re.search(r"/pci.*/(.*):.*", bus_info_file.read()).group(1)[8:10]
slot_id_get = int(slot_temp, 16)
if slot_id_get == slot_id:
with open(ret_file, "w") as ret_file:
ret_file.write(f"/dev/{contrl_num}")
return f"{slot_id}:/dev/{contrl_num}:Ctrl{ctrl_id}"
ctrl_id += 1
with open("error.txt", "a") as error_file:
error_file.write(f"{datetime.datetime.now().strftime('%y-%m-%d %H:%M:%S')} can not get nvme controller ctrl{ctrl_id} info,slot{slot_info}.\n")
return 1
```
阅读全文