1. 程式人生 > >【Python獲取軟體當前的配置資訊】實現info命令

【Python獲取軟體當前的配置資訊】實現info命令

原始碼:

# -*- coding: utf-8 -*-
#coding=utf-8
import os
import re
import sys
import datetime

M_name = open('.config')
for line in M_name:
	line_new = line.split(' ')[-1]
	
result = os.popen("svn info")  
res = result.read()
infile_new = open("test2.txt","w")
for line in res.splitlines():
	if "Revision" in line:									#獲取版本號	 以SVN_XXX的格式
		Strsvn = line.replace(' ','')			
		Strsvn = Strsvn.replace('Revision','')
		Strsvn = Strsvn.replace(':','')
os.remove("test2.txt")		
infile_new.close()

os.chdir('ModelSelect')
path = os.getcwd()
pattern = re.compile('"(.*)"')
for parent,dirnames,filenames in os.walk('.'):
	for dirname in dirnames:
		if os.path.isdir(dirname):
			dirname_1 = dirname.split('_')[0]
			if str(dirname_1) == str(int(line_new)):
				Model_Select = dirname
				os.chdir(dirname)
				for parent,dirnames,filenames in os.walk('.'):
					for filename in filenames:
						if filename == "customer.h":
							FileName = open('customer.h','r+')				#獲取命名所需的屏參、遙控等引數
							for line in FileName:
								if "#define CUSTOMER_PANEL				" in line:
									PANEL_NAME = pattern.findall(line)[0]
								if "#define CUSTOMER_IR					" in line:					
									IRName = pattern.findall(line)[0]
								if "#define CUSTOMER_PCB_NAME			" in line:					
									BOARD_NAME = pattern.findall(line)[0]
								if "#define	CUSTOMER_PCB_IC_TYPE 		" in line:					
									ChipID = pattern.findall(line)[0]
Time_str = datetime.datetime.now().strftime('%Y%m%d %H:%M:%S')									
print("\n-------------------- main code config -------------------")
print("**** Model_Select     	                      := %s"%(Model_Select))
print("**** CONFIG_MODEL_BOARD_NAME     	      := %s"%(BOARD_NAME))
print("**** CONFIG_PANEL_TYPE_NAME                   := %s"%(PANEL_NAME))
print("**** ChipID		       		      := %s"%(ChipID))
print("**** Software SVN version	    	      := %s"%(Strsvn))
print("**** Modified Date Time		      	      := %s"%(Time_str))
print("---------------------------------------------------------")
print("SVN version = %s"%(Strsvn))
print("Current Path = %s\n"%(path))

遇到的問題:

line_new = line.split(' ')[-1]  獲取的字串line_new 和 dirname_1 = dirname.split('_')[0]獲取到的dirname_1 比較的時候,打印出來是相等的,但是比較的時候顯示不相等。

解決:line_new 和 dirname_1兩個字串都先轉換成int然後再轉為str------>str(int(line_new))   str(int(dirname_1))   

在Makefile中,新增對應執行指令碼的命令,使用make info執行指令碼

        .
        .
        .
info:
    python info.py

zip:
    python zip.py
        .
        .
        .