annotate rhodecode/bin/rhodecode_api.py @ 4147:1c8f818787b3 rhodecode-2.2.5-gpl

old style: show the full link box on summary page - no overlap or truncation
author Mads Kiilerich <madski@unity3d.com>
date Wed, 02 Jul 2014 19:03:23 -0400
parents ffd45b185016
children 05cabd91f7c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 # 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
3 # 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
4 # 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
5 # (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
6 #
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # 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
11 #
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 # 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
13 # along with this program. If not, see <http://www.gnu.org/licenses/>.
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
14 """
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
15 rhodecode.bin.api
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
16 ~~~~~~~~~~~~~~~~~
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
17
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
18 Api CLI client for RhodeCode
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
19
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
20 :created_on: Jun 3, 2012
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
21 :author: marcink
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
22 :copyright: (c) 2013 RhodeCode GmbH.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
23 :license: GPLv3, see LICENSE for more details.
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
24 """
2379
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"
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
38 "Create config file: rhodecode-api --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)
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
109 error_prefix = ''
3896
8dae2a288339 API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents: 3875
diff changeset
110 if json_resp['error']:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
111 error_prefix = 'ERROR:'
3896
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['error']
8dae2a288339 API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents: 3875
diff changeset
113 else:
8dae2a288339 API cli should prefer to display errors instead of responses
Marcin Kuzminski <marcin@python-works.com>
parents: 3875
diff changeset
114 json_data = json_resp['result']
3875
5a7d52cf084d Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
115 if args.format == FORMAT_JSON:
5a7d52cf084d Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
116 print json.dumps(json_data)
5a7d52cf084d Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents: 3840
diff changeset
117 elif args.format == FORMAT_PRETTY:
4116
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
118 print 'Server response \n%s%s' % (
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
119 error_prefix, json.dumps(json_data, indent=4, sort_keys=True)
ffd45b185016 Imported some of the GPLv3'd changes from RhodeCode v2.2.5.
Bradley M. Kuhn <bkuhn@sfconservancy.org>
parents: 3896
diff changeset
120 )
2379
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
121 return 0
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
122
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
123 if __name__ == '__main__':
7ac09514a178 created rhodecode-api binary script for working with api via cli
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
124 sys.exit(main(sys.argv))