-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftToDate.py
More file actions
114 lines (94 loc) · 3.43 KB
/
softToDate.py
File metadata and controls
114 lines (94 loc) · 3.43 KB
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
# coding: utf-8
from bs4 import BeautifulSoup as bs
import urllib
import re
import argparse as ap
import pyodbc
import ConfigParser as cp
from distutils.version import LooseVersion as V
class Software:
def __init__(self, name, version):
self.name = name
self.version = version
self.upToDate = False
def getLatestVersion(self):
params = urllib.urlencode({'q': self.name})
f = urllib.urlopen("http://www.filehippo.com/search?%s" % params)
html = f.read()
parsed_html = bs(html)
results = parsed_html.find('div', attrs={'class': 'searchmiddle'})
if results.find('table'):
first_result = results.find('table').find(
'h2').find('a').contents[0].strip()
self.latestVersion = re.search(
'\d+\.\d*(\.\d+)*', first_result).group(0).strip()
self.compareVersions()
else:
self.latestVersion = 'Not found'
def compareVersions(self):
if V(self.version) >= V(self.latestVersion):
self.upToDate = True
def parseTextFile(file):
softs = []
for line in open(file, 'r'):
infos = line.rstrip('\r\n').split(',')
name = infos[0]
version = infos[1]
softs.append(Software(name, version))
return softs
def main():
# Arguments parsing
parser = ap.ArgumentParser(description='Software version checkker')
parser.add_argument('-f', '--file',
metavar='SOFTWARES_FILE',
default='softwares.txt',
help='path to text file containing software information')
parser.add_argument('--sql',
metavar='DB_CONFIG_FILE',
help='path to database config file')
args = parser.parse_args()
softwareFile = args.file
# Database mode
if args.sql != None:
try:
config = cp.RawConfigParser(allow_no_value=True)
config.read('.mr.developer.cfg')
server = config.get('SQL', 'server')
database = config.get('SQL', 'database')
user = config.get('SQL', 'user')
pwd = config.get('SQL', 'pwd')
query = config.get('SQL', 'query')
except Exception as detail:
print "ERROR: configuration file format (", detail, ")"
exit(1)
try:
conStr = 'DRIVER={SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (
server, database, user, pwd)
con = pyodbc.connect(conStr)
except Exception as detail:
print "ERROR: can't connect to database (", detail, ")"
exit(1)
try:
cursor = con.cursor()
cursor.execute(query)
rows = cursor.fetchall()
except Exception as detail:
print "ERROR: query failed (", detail, ")"
exit(1)
softwares = []
for row in rows:
softwares.append(Software(row.name, row.version))
print len(softwares), 'softwares found in database'
# Filetext mode
else:
softwares = parseTextFile(softwareFile)
print len(softwares), 'softwares found in file'
# WORK
for software in softwares:
software.getLatestVersion()
if software.upToDate:
print software.name, 'is up to date.'
else:
print software.name, '>', software.latestVersion
if __name__ == '__main__':
app = main()