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
|
#!/usr/bin/python
# vi: noexpandtab tabstop=4
# upstream-check - utility for checking upstream amazon source
#
# Copyright (C) 2010 Canonical Ltd.
#
# Authors: Scott Moser <smoser@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
download_url = "http://s3.amazonaws.com/ec2-downloads/"
Usage = """
Usage: upstream-check <string>
This will search the ec2 bucket xml, looking for files matching
<string> and print the newest.
Searches url: %s
Example:
upstream-check ec2-api-tools
""" % download_url
import urllib2
import sys
import xml.dom.minidom
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
def main(basename):
xmlstr = urllib2.urlopen(download_url).read()
dom = xml.dom.minidom.parseString(xmlstr)
# each 'Contents' looks like:
# <Contents>
# <Key>2006-06-26.ec2.wsdl</Key>
# <LastModified>2006-10-23T12:22:30.000Z</LastModified>
# <ETag>"d4fa76ef26b78d3905e009de9db8bf7d"</ETag>
# <Size>28344</Size>
# <StorageClass>STANDARD</StorageClass>
# </Contents>
flist = ( "Key", "LastModified", "ETag", "Size", "StorageClass" )
matches = [ ]
for content in dom.getElementsByTagName("Contents"):
fields = { }
for key in flist:
fields[key]=getText(content.getElementsByTagName(key)[0].childNodes)
try:
if not ( fields["Key"].startswith("%s-" % basename) and
fields["Key"].endswith(".zip")) :
continue
except KeyError:
continue
matches.append(fields)
matches.sort(cmp=lambda x,y: cmp(x["LastModified"], y["LastModified"]))
#for f in matches:
# print "%s\t%s\t%s" % (f["Key"], f["LastModified"], f["Size"])
ent = matches[len(matches)-1]
print "%s\t%s\t%s" % (ent["Key"], ent["LastModified"], ent["Size"])
if __name__ == '__main__':
if len(sys.argv) == 2:
swith=sys.argv[1]
else:
sys.stderr.write(Usage)
sys.exit(1)
main(swith)
|