summaryrefslogtreecommitdiff
path: root/bin/vv.py
blob: 4377ca8cfbf7ec8397fa060fdf5f07b7479f3653 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
import sys
import subprocess
from optparse import OptionParser
from ftplib import FTP
from pprint import pprint #del later

dic={}
contact={}
date={}
filename_default="version.conf"
ftpserver='10.0.6.235'
directory='./MESA/'

def getconf(confname):
	with open(confname) as f:
		for line in f.readlines():
			if line.startswith('#'):
				continue
			parts=line.split()
			if len(parts)>=3:
				name=parts[0].strip().replace('version','')
				name=name.replace('VERSION','')
				name=name.replace('_','')
				dic[name]=parts[1]
				contact[name]=parts[2]
				date[name]=parts[3]

def getfile(path):
	for root,dirs,files in os.walk(path):
		for f in files:
			if f.endswith('.so'):
				#print(f)
				fname=os.path.join(root,f)
				yield fname

def getversion(f):
	vers=subprocess.Popen('nm '+f+'|grep VERSION',shell=True,stdout=subprocess.PIPE)
	for line in vers.stdout.readlines():
		i=line.split()
		#print(i[-1])
		yield(i[-1])

def checkversion(v):
	name=v.strip().replace('version','')
	name=name.replace('VERSION','')
	name=name.replace('_','')
	name=filter(lambda ch:ch not in '0123456789',name)
	#newv=dic[name]
	newv=dic.get(name,None)
	if newv==None:
		print('\033[1;33m %s \033[1;m' % ('\t+++'+name+' is not found in your config file.'))
		return None
	if newv==v:
		print('\t\033[1;32m%-120s\033[1;32m%s \033[1;m' % (v,'[LASTEST]'))
	else:
		vnow=v.strip().split()
		vnew=newv.strip().split()
		if vnow[-1]<vnew[-1]:
			print('\t\033[1;31m%-120s\033[1;31m%s \033[1;m' % (v,'[OUTDATED]'))
			print('\t[+]The latest version is '+vnew[-1]+', build in '+ date[name]+'. Please contact '+contact[name]+' to verifiy the version.')
		elif vnow[-1]>vnew[-1]:
			print('\t\033[1;33m%-120s\033[1;33m%s \033[1;m' % (v,'[WARNING]'))
			print('\t[+]Newer than your latest config version!')
		else:
			print('\033[1;33m %s \033[1;m' % ('#####cannot check '+v))

def ftp_down(fname=filename_default):
	try:
		ftp=FTP(ftpserver)
		ftp.login()
		ftp.cwd(directory)
		#version.conf->version.conf.bak
		if os.path.exists(filename_default):
			#os.name(filename_default,filename_default+'.bak')
			if os.system('mv '+filename_default+' '+filename_default+'.bak')==0:
				print(filename_default+' has been renamed as '+filename_default+'.bak')
		file_handler=open(filename_default,'wb').write
		ftp.retrbinary("RETR %s" % os.path.basename(filename_default),file_handler,1024)
		ftp.close()
		print("get "+filename_default+" from "+ftpserver+" successfully.")
	except:
		print("get "+filename_default+" from "+ftpserver+" failed.")

'''def main(argv):
	getconf()
	if len(argv)==1:
		print("arg error")
		print("please input the dir path!")
		exit()
	for f in getfile(argv[1]):
		print(f)
		for i in getversion(f):
			#print('\t'+i)
			checkversion(i)
	#print('------')
	#pprint(dic)'''

def main():
	useage="usage:%prog [options arg]"
	parser=OptionParser(useage)
	parser.add_option("-f","--file",dest="filename",default=filename_default,help="FILENAME of your config. Default file is "+filename_default)
	parser.add_option("-p","--path",dest="path",default="./",help="lib PATH that you want to verifiy. Default path is ./")
	parser.add_option("-u","--update",dest="update",action="store_true",default=False,help="update config from ftp.")
	(options,args)=parser.parse_args()
	#print(options.filename)
	#print(options.path)
	if options.update:
		ftp_down()
	else:
		if not os.path.exists(options.filename):
			print(options.filename+" not exists")
			exit()
	getconf(options.filename)
	for f in getfile(options.path):
		print(f)
		for i in getversion(f):
			#print('\t'+i)
			checkversion(i)

if __name__=="__main__":
	#main(sys.argv)
	main()