Mercurial > kallithea
annotate rhodecode/bin/rhodecode_api.py @ 4086:4ec370789b3e
added larger buffers for example uwsgi setup
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 09 Jul 2013 19:48:42 +0200 |
parents | 8dae2a288339 |
children | ffd45b185016 |
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 """ |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
3 rhodecode.bin.api |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
4 ~~~~~~~~~~~~~~~~~ |
2379
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 sys |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 import argparse |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
30 from rhodecode.bin.base import json, api_call, RcConf, FORMAT_JSON, FORMAT_PRETTY |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
32 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 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
|
34 usage = ( |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
35 "rhodecode-api [-h] [--format=FORMAT] [--apikey=APIKEY] [--apihost=APIHOST] " |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
36 "[--config=CONFIG] [--save-config] " |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
37 "METHOD <key:val> <key2:val> ...\n" |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
38 "Create config file: rhodecode-gist --apikey=<key> --apihost=http://rhodecode.server --save-config" |
2571
94a92bd832c5
added --config option into rhodecode-api script for optional path for
Marcin Kuzminski <marcin@python-works.com>
parents:
2509
diff
changeset
|
39 ) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 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
|
42 usage=usage) |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 ## config |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 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
|
46 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
|
47 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
|
48 group.add_argument('--config', help='config file') |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
49 group.add_argument('--save-config', action='store_true', help='save the given config into a file') |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 group = parser.add_argument_group('API') |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
52 group.add_argument('method', metavar='METHOD', nargs='?', type=str, default=None, |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 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
|
54 ) |
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
|
55 group.add_argument('--format', dest='format', type=str, |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
56 help='output format default: `%s` can ' |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
57 'be also `%s`' % (FORMAT_PRETTY, FORMAT_JSON), |
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
|
58 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
|
59 ) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 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
|
61 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
|
62 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 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
|
65 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 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
|
67 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 :param argv: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 """ |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 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
|
71 argv = sys.argv |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 conf = None |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 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
|
75 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 api_credentials_given = (args.apikey and args.apihost) |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
77 if args.save_config: |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 if not api_credentials_given: |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
79 raise parser.error('--save-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
|
80 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
|
81 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
|
82 'apihost': args.apihost}) |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
3631
diff
changeset
|
83 sys.exit() |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 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
|
86 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
|
87 if not conf: |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 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
|
89 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
|
90 '--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
|
91 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 apikey = args.apikey or conf['apikey'] |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
93 apihost = args.apihost or conf['apihost'] |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 method = args.method |
2509
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
95 |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
96 # if we don't have method here it's an error |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
97 if not method: |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
98 parser.error('Please specify method name') |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
99 |
2509
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
100 try: |
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
101 margs = dict(map(lambda s: s.split(':', 1), other)) |
3631
10b4e34841a4
Don't catch all exceptions
Marcin Kuzminski <marcin@python-works.com>
parents:
2571
diff
changeset
|
102 except Exception: |
2509
955956f4bdda
Show proper error on argument parse when using api-cli
Marcin Kuzminski <marcin@python-works.com>
parents:
2505
diff
changeset
|
103 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
|
104 sys.exit() |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
105 if args.format == FORMAT_PRETTY: |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
106 print 'Calling method %s => %s' % (method, apihost) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 |
3896
8dae2a288339
API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents:
3875
diff
changeset
|
108 json_resp = api_call(apikey, apihost, method, **margs) |
8dae2a288339
API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents:
3875
diff
changeset
|
109 if json_resp['error']: |
8dae2a288339
API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents:
3875
diff
changeset
|
110 json_data = json_resp['error'] |
8dae2a288339
API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents:
3875
diff
changeset
|
111 else: |
8dae2a288339
API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents:
3875
diff
changeset
|
112 json_data = json_resp['result'] |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
113 if args.format == FORMAT_JSON: |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
114 print json.dumps(json_data) |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
115 elif args.format == FORMAT_PRETTY: |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
116 print 'Server response \n%s' % ( |
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
117 json.dumps(json_data, indent=4, sort_keys=True)) |
2379
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 return 0 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 if __name__ == '__main__': |
7ac09514a178
created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 sys.exit(main(sys.argv)) |