
{
    "name"  : "0PJ11热补丁-升级条件检查",
    "precondition" : ["isFix"],
    "problems" : {
        "patch_fail" : {
            "desc"      : "【问题】当前版本未检测到使用补丁进行过修复，工具尝试修复，但修复失败，请再次尝试。",
            "advice"    : "【建议】对当前版本进行热补丁升级，升级补丁过程不断网。"
        },
		"patch_ok" : {
            "desc"      : "【问题】当前版本未检测到使用补丁进行过修复，工具尝试修复，已经修复成功。",
            "advice"    : "【建议】再次检测看是否有热补丁问题。"
        }
    }
}

---------------------------code-------------------------------
#完全匹配，有补丁的大版本号
S_versionList = ['11.0(4)B4','11.0(4)B4T2']
precondition_result = 'pass'
precondition_message = ''
precondition_message_advice = ''

log_version = cli('show version detail')
log_versionList = log_version.split('\r\n')

#用户不同意检查
isFixed = precondition('isFix')
if !isFixed || isFixed == undefined :
	precondition_result = 'fail'
	precondition_message = '用户不同意检查，跳过检查！'
	precondition_message_advice = '若要检查此项，开始检测时请选择‘是’'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end
if !isRunningInToolkit():
	precondition_result = 'fail'
	precondition_message = '非工具端环境，不支持此项检测'
	precondition_message_advice = '非工具端环境，不支持此项检测'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

#版本日期大于xx的不用打补丁
if !isCheckVersion():
	precondition_result = 'fail'
	precondition_message = '版本无风险，无需进行升级！'
	precondition_message_advice = '版本无风险，无需进行升级！'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end
#版本一致性检查
if !isSameVer():
	precondition_result = 'fail'
	precondition_message = '设备板卡版本与系统版本不一致，不能打补丁，请版本升级一致后再重试！'
	precondition_message_advice = '请确保环境中板卡一致后再进行尝试'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end
#检查设备是否处于upgrading状态
if isUpgrading():
	precondition_result = 'fail'
	precondition_message = '设备正处在升级状态，请等待状态不是upgrading时再尝试！'
	precondition_message_advice = '等升级状态不是upgrading时再尝试'
	set_share_value('precondition_result',precondition_result)
	set_share_value('precondition_message',precondition_message)
	set_share_value('precondition_message_advice',precondition_message_advice)
	return 'skip'
end

set_share_value('precondition_result',precondition_result)
return 'skip'

def isCheckVersion():
	result = 1
	version = getVersion()
	if !isUpdate(version):
		result = 0
	end
	date = getDate()
	if date > 20160819 :
		result = 0
	end
	if log_version.indexOf('S78') != -1:
		result = 0
	end
	if log_version.indexOf('-DB') == -1:
		result = 0
	end
	
	return result
end

def getDate():
	result = ''
	#M04273008172016
	reg = /System\s+software\s+number\s+:\s+\w{7}(\d{2})(\d{2})(\d{4})/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			m = r[1]
			d = r[2]
			y = r[3]
			result = r[3]+r[1]+r[2]
		end
	end
	return result
end

#T版本另外给建议
def isT_version():
	result = 0
	version = getVersion()
	foreach data in T_versionList:
		if version == data :
			result = 1
			break
		end
	end
	return result
end
def isUpdate(version):
	result = 0
	foreach data in S_versionList:
		if data == version :
			result = 1
		end
	end
	return result
end

def getVersionDate():
	result = ''
	reg = /System\s+software\s+number\s+:\s+(\w{15})/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			result = r[1]
		end
	end
	return result
end

def isSameVer():
	result = 1
	soft_ver = getVersion()
	reg = /Software\s+version\s+:\s+[^ ]+\s+([^,^\r\n]+)/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			num = r[1]
			if num != soft_ver:
				result = 0
				break
			end
		end
	end
	return result
end

def getVersion():
	result = ''
	reg = /System\s+software\s+version\s+:\s+[^ ]+\s+([^,^\r\n]+)/
	foreach data in log_versionList:
		r = data.match(reg)
		if r != null:
			result = r[1]
		end
	end
	return result
end

def isUpgrading():
	result = 0
	log_upgrade = cli('show upgrade status')
	log_upgradeList = log_upgrade.split('\r\n')
	foreach data in log_upgradeList:
		if data.indexOf('upgrading') != -1:
			result = 1
		end
	end
	return result
end

def get_date_version(str):
	date = ''
	strList = str.split('\r\n')
	reg = /System software number\s+:\s+\w{1}\d{6}(\d{8})/
	foreach data in strList:
		r = data.match(reg)
		if r != null:
			date = r[1]
			break
		end
	end
	return date
end

def str_to_num(str):
	num = ''
	strList = str.split(",")
	foreach data in strList:
		num = num+data
	end
	return num
end
