Gentoo Archives: gentoo-commits

From: Vikraman Choudhury <vikraman.choudhury@×××××.com>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/gentoostats:master commit in: client/gentoostats/, client/
Date: Mon, 01 Aug 2011 22:24:49
Message-Id: 9fc558e2719e01c5ad25e55124e45a662ebb5fc0.vikraman@gentoo
1 commit: 9fc558e2719e01c5ad25e55124e45a662ebb5fc0
2 Author: Vikraman Choudhury <vikraman.choudhury <AT> gmail <DOT> com>
3 AuthorDate: Mon Jul 25 22:22:55 2011 +0000
4 Commit: Vikraman Choudhury <vikraman.choudhury <AT> gmail <DOT> com>
5 CommitDate: Mon Jul 25 22:22:55 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/gentoostats.git;a=commit;h=9fc558e2
7
8 misc fixes
9
10 ---
11 client/gentoostats-cli | 9 ++-
12 client/gentoostats/list.py | 109 +++++++++++++++++++------------------------
13 client/gentoostats/utils.py | 22 +++++++++
14 3 files changed, 76 insertions(+), 64 deletions(-)
15
16 diff --git a/client/gentoostats-cli b/client/gentoostats-cli
17 index cee0da1..c8a5222 100755
18 --- a/client/gentoostats-cli
19 +++ b/client/gentoostats-cli
20 @@ -3,9 +3,12 @@
21 import sys
22 import importlib
23
24 -actions = {'list':'gentoostats.list', 'search':'gentooostats.search'}
25 +actions = {
26 + 'list': 'gentoostats.list',
27 + 'search': 'gentooostats.search'
28 + }
29
30 -def print_usage():
31 +def print_usage(actions):
32 print 'Usage:', sys.argv[0], '<action>'
33 print 'Available actions:'
34 for action in actions.keys():
35 @@ -14,7 +17,7 @@ def print_usage():
36 def main():
37 argc = len(sys.argv)
38 if argc == 1:
39 - print_usage()
40 + print_usage(actions)
41 sys.exit(1)
42 try:
43 loaded_module = importlib.import_module(actions[sys.argv[1]])
44
45 diff --git a/client/gentoostats/list.py b/client/gentoostats/list.py
46 index 480587c..3cf60cf 100644
47 --- a/client/gentoostats/list.py
48 +++ b/client/gentoostats/list.py
49 @@ -2,34 +2,38 @@
50 import sys
51 import json
52 import httplib
53 +import utils
54
55 -objects={
56 -'arch':'list_arch',
57 -'feature':'list_feature',
58 -'lang':'list_lang',
59 -'mirror':'list_mirror',
60 -'repo':'list_repo',
61 -'category':'list_cat',
62 -}
63 +objects = {
64 + 'arch': 'list_arch',
65 + 'feature': 'list_feature',
66 + 'lang': 'list_lang',
67 + 'mirror': 'list_mirror',
68 + 'repo': 'list_repo',
69 + 'cat': 'list_cat',
70 + 'cp': 'list_cp',
71 + 'cpv': 'list_cpv'
72 + }
73
74 server = 'soc.dev.gentoo.org'
75 url = '/gentoostats'
76 -headers = {'Accept':'application/json'}
77 +headers = {'Accept': 'application/json'}
78
79 -def print_usage():
80 +def print_usage(objects):
81 print 'Usage: list <object>'
82 print 'Available objects:'
83 for obj in objects.keys():
84 print obj
85
86 -def pprint(object):
87 +def pprint(title, object):
88 + print title
89 import pprint
90 pprint.pprint(object)
91
92 def main(opts):
93 l = len(opts)
94 if l == 0:
95 - print_usage()
96 + print_usage(objects)
97 sys.exit(1)
98
99 if opts[0] not in objects:
100 @@ -37,61 +41,44 @@ def main(opts):
101 sys.exit(1)
102
103 try:
104 - globals()[objects[opts[0]]]()
105 + globals()[objects[opts[0]]](server, url, headers)
106 except KeyError:
107 sys.stderr.write('Unimplemented')
108 sys.exit(1)
109
110 -def list_arch():
111 - conn = httplib.HTTPSConnection(server)
112 - conn.request('GET', url=url+'/arch', headers=headers)
113 - try:
114 - arch_data = json.JSONDecoder().decode(conn.getresponse().read())
115 - except ValueError:
116 - sys.exit(1)
117 - pprint(arch_data)
118 +def list(server, url_base, url_extra, headers):
119 + get_data = utils.GET(server=server, url=url_base+url_extra, headers=headers)
120 + data = utils.deserialize(get_data)
121 + return data
122
123 -def list_feature():
124 - conn = httplib.HTTPSConnection(server)
125 - conn.request('GET', url=url+'/feature', headers=headers)
126 - try:
127 - feature_data = json.JSONDecoder().decode(conn.getresponse().read())
128 - except ValueError:
129 - sys.exit(1)
130 - pprint(feature_data)
131 +def list_arch(server, url, headers):
132 + data = list(server, url, '/arch', headers)
133 + pprint('Arch', data)
134
135 -def list_lang():
136 - conn = httplib.HTTPSConnection(server)
137 - conn.request('GET', url=url+'/lang', headers=headers)
138 - try:
139 - lang_data = json.JSONDecoder().decode(conn.getresponse().read())
140 - except ValueError:
141 - sys.exit(1)
142 - pprint(lang_data)
143 +def list_feature(server, url, headers):
144 + data = list(server, url, '/feature', headers)
145 + pprint('Feature', data)
146
147 -def list_mirror():
148 - conn = httplib.HTTPSConnection(server)
149 - conn.request('GET', url=url+'/mirror', headers=headers)
150 - try:
151 - mirror_data = json.JSONDecoder().decode(conn.getresponse().read())
152 - except ValueError:
153 - sys.exit(1)
154 - pprint(mirror_data)
155 +def list_lang(server, url, headers):
156 + data = list(server, url, '/lang', headers)
157 + pprint('Lang', data)
158
159 -def list_repo():
160 - conn = httplib.HTTPSConnection(server)
161 - conn.request('GET', url=url+'/mirror', headers=headers)
162 - try:
163 - repo_data = json.JSONDecoder().decode(conn.getresponse().read())
164 - except ValueError:
165 - sys.exit(1)
166 - pprint(repo_data)
167 +def list_mirror(server, url, headers):
168 + data = list(server, url, '/mirror', headers)
169 + pprint('Mirror', data)
170
171 -def list_cat():
172 - conn = httplib.HTTPSConnection(server)
173 - conn.request('GET', url=url+'/package', headers=headers)
174 - try:
175 - cat_data = json.JSONDecoder().decode(conn.getresponse().read())
176 - except ValueError:
177 - sys.exit(1)
178 - pprint(cat_data)
179 +def list_repo(server, url, headers):
180 + data = list(server, url, '/repo', headers)
181 + pprint('Repo', data)
182 +
183 +def list_cat(server, url, headers):
184 + data = list(server, url, '/package', headers)
185 + pprint('Category', data)
186 +
187 +def list_cp(server, url, headers):
188 + data = list(server, url, '/package', headers)
189 + pprint('Category/Package', data)
190 +
191 +def list_cpv(server, url, headers):
192 + data = list(server, url, '/package', headers)
193 + pprint('Category/Package-Version', data)
194
195 diff --git a/client/gentoostats/utils.py b/client/gentoostats/utils.py
196 new file mode 100644
197 index 0000000..ae520f2
198 --- /dev/null
199 +++ b/client/gentoostats/utils.py
200 @@ -0,0 +1,22 @@
201 +
202 +import json
203 +import httplib
204 +
205 +def GET(server, url, headers, https=True):
206 + if https:
207 + conn = httplib.HTTPSConnection(server)
208 + else:
209 + conn = httplib.HTTPConnection(server)
210 + try:
211 + conn.request('GET', url=url, headers=headers)
212 + data = conn.getresponse().read()
213 + except httplib.HTTPException:
214 + return None
215 + return data
216 +
217 +def deserialize(object):
218 + try:
219 + decoded = json.JSONDecoder().decode(object)
220 + except (ValueError, TypeError):
221 + return None
222 + return decoded