-
Notifications
You must be signed in to change notification settings - Fork 683
/
check.py
108 lines (90 loc) · 3.22 KB
/
check.py
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
import os, os.path
import shutil
import urllib, urllib2
import simplejson as json
from play.utils import *
COMMANDS = ['check']
HELP = {
'check': 'Check for a release newer than the current one'
}
TAGS_URL = "http://github.com/api/v2/json/repos/show/playframework/play/tags"
def execute(**kargs):
args = kargs.get("args")
play_env = kargs.get("env")
if len(sys.argv) == 3:
version = sys.argv[2]
else:
version = playVersion(play_env)
current = Release(version)
releases = allreleases()
if len(releases) == 0:
print "~ No release found."
elif current == max(releases):
print "~ You are using the latest version."
else:
print "~ ***** Latest release: " + str(max(releases))
print "~ ***** Your version : " + str(current)
print "~"
print "~ Latest release download: " + max(releases).url()
print "~"
def allreleases():
try:
req = urllib2.Request(TAGS_URL)
req.add_header('Accept', 'application/json')
opener = urllib2.build_opener()
result = opener.open(req)
return map(lambda x: Release(x), json.loads(result.read())["tags"])
except urllib2.HTTPError, e:
print "~ Oops,"
print "~ Cannot contact github..."
print "~"
sys.exit(-1)
except urllib2.URLError, e:
print "~ Oops,"
print "~ Cannot contact github..."
print "~"
sys.exit(-1)
class Release:
# TODO: Be smarter at analysing the rest (ex: RC1 vs RC2)
def __init__(self, strversion):
self.strversion = strversion
self.numpart = re.findall(r"\d+[\.\d+]+", strversion)[0]
self.rest = strversion.replace(self.numpart, "")
self.versions = map(lambda x: int(x), self.numpart.split("."))
if not self.rest: self.rest = "Z" # Hack for final release
def url(self):
return "http://download.playframework.org/releases/play-" + self.strversion + ".zip"
def __eq__(self, other):
return self.strversion == other.strversion
def __lt__(self, other):
if self == other:
return False
for i in range(len(self.versions)):
if self.versions[i] < other.versions[i]:
return True # ex: 1.1 vs 1.2
if self.versions[i] > other.versions[i]:
return False
if len(self.versions) < len(other.versions):
return True
if len(self.versions) > len(other.versions):
return False
# From here, numeric part is the same - now having a rest means older version
if len(other.numpart) > 0 and len(self.numpart) == 0:
return False
if len(self.numpart) > 0 and len(other.numpart) == 0:
return True
# Both have a rest, use a string comparison
# alpha1 < beta1 < rc1 < rc2...
return self.rest < other.rest
def __le__(self, other):
return self == other or self < other
def __gt__(self, other):
return not (self <= other)
def __ge__(self, other):
return not (self < other)
def __ne__(self, other):
return not (self == other)
def __repr__(self):
return self.strversion
def __str__(self):
return self.strversion