Mercurial > kallithea
annotate rhodecode/bin/rhodecode_api.py @ 2729:e9e7c40b4f1a beta
added deque with maxlen for py2.5 compat
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 22 Aug 2012 12:09:49 +0200 |
parents | 94a92bd832c5 |
children | 10b4e34841a4 |
rev | line source |
---|---|
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 rhodecode.bin.backup_manager |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 Api CLI client for RhodeCode |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 :created_on: Jun 3, 2012 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 :author: marcink |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 :copyright: (C) 2010-2012 Marcin Kuzminski <marcin@python-works.com> |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 :license: GPLv3, see COPYING for more details. |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 # This program is free software: you can redistribute it and/or modify |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 # it under the terms of the GNU General Public License as published by |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 # the Free Software Foundation, either version 3 of the License, or |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 # (at your option) any later version. |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 # |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 # This program is distributed in the hope that it will be useful, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 # GNU General Public License for more details. |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 # |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
23 # You should have received a copy of the GNU General Public License |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
25 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 from __future__ import with_statement |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 import os |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import sys |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 import random |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 import urllib2 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 import pprint |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 import argparse |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 try: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 from rhodecode.lib.ext_json import json |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 except ImportError: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 try: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 import simplejson as json |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 except ImportError: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 import json |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 CONFIG_NAME = '.rhodecode' |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
44 FORMAT_PRETTY = 'pretty' |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
45 FORMAT_JSON = 'json' |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 class RcConf(object): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
49 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 RhodeCode config for API |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 conf = RcConf() |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 conf['key'] |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
57 def __init__(self, config_location=None, autoload=True, autocreate=False, |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
58 config=None): |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
59 self._conf_name = CONFIG_NAME if not config_location else config_location |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 self._conf = {} |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 if autocreate: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
62 self.make_config(config) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 if autoload: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 self._conf = self.load_config() |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 def __getitem__(self, key): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 return self._conf[key] |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 def __nonzero__(self): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 if self._conf: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 return True |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 return False |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 def __eq__(self): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 return self._conf.__eq__() |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 def __repr__(self): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 return 'RcConf<%s>' % self._conf.__repr__() |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
79 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
80 def make_config(self, config): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
81 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 Saves given config as a JSON dump in the _conf_name location |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 :param config: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 :type config: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 """ |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
87 update = False |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
88 if os.path.exists(self._conf_name): |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
89 update = True |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 with open(self._conf_name, 'wb') as f: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 json.dump(config, f, indent=4) |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
92 |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
93 if update: |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
94 sys.stdout.write('Updated config in %s\n' % self._conf_name) |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
95 else: |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
96 sys.stdout.write('Created new config in %s\n' % self._conf_name) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 def update_config(self, new_config): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 Reads the JSON config updates it's values with new_config and |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 saves it back as JSON dump |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 :param new_config: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 config = {} |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 try: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 with open(self._conf_name, 'rb') as conf: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 config = json.load(conf) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 except IOError, e: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 sys.stderr.write(str(e) + '\n') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 config.update(new_config) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 self.make_config(config) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 def load_config(self): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 Loads config from file and returns loaded JSON object |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 try: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 with open(self._conf_name, 'rb') as conf: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 return json.load(conf) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 except IOError, e: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 #sys.stderr.write(str(e) + '\n') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 pass |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
125 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
127 def api_call(apikey, apihost, format, method=None, **kw): |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 Api_call wrapper for RhodeCode |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 :param apikey: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 :param apihost: |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
133 :param format: formatting, pretty means prints and pprint of json |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
134 json returns unparsed json |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 :param method: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 def _build_data(random_id): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 Builds API data with given random ID |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 :param random_id: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 :type random_id: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 return { |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 "id": random_id, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
146 "api_key": apikey, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
147 "method": method, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
148 "args": kw |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
149 } |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
150 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
151 if not method: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
152 raise Exception('please specify method name !') |
2505
02fc6ce0cd44
don't make a api call after doing create_config
Marcin Kuzminski <marcin@python-works.com>
parents:
2381
diff
changeset
|
153 id_ = random.randrange(1, 9999) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
154 req = urllib2.Request('%s/_admin/api' % apihost, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
155 data=json.dumps(_build_data(id_)), |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
156 headers={'content-type': 'text/plain'}) |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
157 if format == FORMAT_PRETTY: |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
158 sys.stdout.write('calling %s to %s \n' % (req.get_data(), apihost)) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
159 ret = urllib2.urlopen(req) |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
160 raw_json = ret.read() |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
161 json_data = json.loads(raw_json) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
162 id_ret = json_data['id'] |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
163 _formatted_json = pprint.pformat(json_data) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
164 if id_ret == id_: |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
165 if format == FORMAT_JSON: |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
166 sys.stdout.write(str(raw_json)) |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
167 else: |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
168 sys.stdout.write('rhodecode returned:\n%s\n' % (_formatted_json)) |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
169 |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
170 else: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
171 raise Exception('something went wrong. ' |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
172 'ID mismatch got %s, expected %s | %s' % ( |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
173 id_ret, id_, _formatted_json)) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
174 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
175 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
176 def argparser(argv): |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
177 usage = ( |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
178 "rhodecode_api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] " |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
179 " [--config=CONFIG] " |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
180 "_create_config or METHOD <key:val> <key2:val> ..." |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
181 ) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
182 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
183 parser = argparse.ArgumentParser(description='RhodeCode API cli', |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
184 usage=usage) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
185 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
186 ## config |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
187 group = parser.add_argument_group('config') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
188 group.add_argument('--apikey', help='api access key') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
189 group.add_argument('--apihost', help='api host') |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
190 group.add_argument('--config', help='config file') |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
191 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
192 group = parser.add_argument_group('API') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
193 group.add_argument('method', metavar='METHOD', type=str, |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
194 help='API method name to call followed by key:value attributes', |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
195 ) |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
196 group.add_argument('--format', dest='format', type=str, |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
197 help='output format default: `pretty` can ' |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
198 'be also `%s`' % FORMAT_JSON, |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
199 default=FORMAT_PRETTY |
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
200 ) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
201 args, other = parser.parse_known_args() |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
202 return parser, args, other |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
203 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
204 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
205 def main(argv=None): |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
206 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
207 Main execution function for cli |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
208 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
209 :param argv: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
210 :type argv: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
211 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
212 if argv is None: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
213 argv = sys.argv |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
214 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
215 conf = None |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
216 parser, args, other = argparser(argv) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
217 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
218 api_credentials_given = (args.apikey and args.apihost) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
219 if args.method == '_create_config': |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
220 if not api_credentials_given: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
221 raise parser.error('_create_config requires --apikey and --apihost') |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
222 conf = RcConf(config_location=args.config, |
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
223 autocreate=True, config={'apikey': args.apikey, |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
224 'apihost': args.apihost}) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
225 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
226 if not conf: |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
227 conf = RcConf(config_location=args.config, autoload=True) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
228 if not conf: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
229 if not api_credentials_given: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
230 parser.error('Could not find config file and missing ' |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
231 '--apikey or --apihost in params') |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
232 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
233 apikey = args.apikey or conf['apikey'] |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
234 host = args.apihost or conf['apihost'] |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
235 method = args.method |
2505
02fc6ce0cd44
don't make a api call after doing create_config
Marcin Kuzminski <marcin@python-works.com>
parents:
2381
diff
changeset
|
236 if method == '_create_config': |
02fc6ce0cd44
don't make a api call after doing create_config
Marcin Kuzminski <marcin@python-works.com>
parents:
2381
diff
changeset
|
237 sys.exit() |
2509
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
238 |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
239 try: |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
240 margs = dict(map(lambda s: s.split(':', 1), other)) |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
241 except: |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
242 sys.stderr.write('Error parsing arguments \n') |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
243 sys.exit() |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
244 |
2381
e487d2a6aa38
Added optional --format=json into api cli. That will return pure JSON data from server
Marcin Kuzminski <marcin@python-works.com>
parents:
2379
diff
changeset
|
245 api_call(apikey, host, args.format, method, **margs) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
246 return 0 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
247 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
248 if __name__ == '__main__': |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
249 sys.exit(main(sys.argv)) |