Mercurial > kallithea
annotate rhodecode/bin/base.py @ 3893:7e9776f38c55 beta
fixed duplicated test in diff tests
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Wed, 22 May 2013 22:35:39 +0200 |
parents | 5a7d52cf084d |
children | ffd45b185016 |
rev | line source |
---|---|
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
1 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
2 Base utils for shell scripts |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
3 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
4 import os |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
5 import sys |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
6 import random |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
7 import urllib2 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
8 import pprint |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
9 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
10 try: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
11 from rhodecode.lib.ext_json import json |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
12 except ImportError: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
13 try: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
14 import simplejson as json |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
15 except ImportError: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
16 import json |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
17 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
18 CONFIG_NAME = '.rhodecode' |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
19 FORMAT_PRETTY = 'pretty' |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
20 FORMAT_JSON = 'json' |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
21 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
22 |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
23 def api_call(apikey, apihost, method=None, **kw): |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
24 """ |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
25 Api_call wrapper for RhodeCode. |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
26 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
27 :param apikey: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
28 :param apihost: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
29 :param format: formatting, pretty means prints and pprint of json |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
30 json returns unparsed json |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
31 :param method: |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
32 :returns: json response from server |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
33 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
34 def _build_data(random_id): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
35 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
36 Builds API data with given random ID |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
37 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
38 :param random_id: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
39 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
40 return { |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
41 "id": random_id, |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
42 "api_key": apikey, |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
43 "method": method, |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
44 "args": kw |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
45 } |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
46 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
47 if not method: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
48 raise Exception('please specify method name !') |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
49 |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
50 id_ = random.randrange(1, 9999) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
51 req = urllib2.Request('%s/_admin/api' % apihost, |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
52 data=json.dumps(_build_data(id_)), |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
53 headers={'content-type': 'text/plain'}) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
54 ret = urllib2.urlopen(req) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
55 raw_json = ret.read() |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
56 json_data = json.loads(raw_json) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
57 id_ret = json_data['id'] |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
58 if id_ret == id_: |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
59 return json_data |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
60 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
61 else: |
3875
5a7d52cf084d
Added --format into gist CLI
Marcin Kuzminski <marcin@python-works.com>
parents:
3840
diff
changeset
|
62 _formatted_json = pprint.pformat(json_data) |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
63 raise Exception('something went wrong. ' |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
64 'ID mismatch got %s, expected %s | %s' % ( |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
65 id_ret, id_, _formatted_json)) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
66 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
67 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
68 class RcConf(object): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
69 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
70 RhodeCode config for API |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
71 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
72 conf = RcConf() |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
73 conf['key'] |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
74 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
75 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
76 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
77 def __init__(self, config_location=None, autoload=True, autocreate=False, |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
78 config=None): |
3840
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3833
diff
changeset
|
79 HOME = os.getenv('HOME', os.getenv('USERPROFILE')) or '' |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3833
diff
changeset
|
80 HOME_CONF = os.path.abspath(os.path.join(HOME, CONFIG_NAME)) |
dc4644865e8b
Implemented simple gist functionality ref #530.
Marcin Kuzminski <marcin@python-works.com>
parents:
3833
diff
changeset
|
81 self._conf_name = HOME_CONF if not config_location else config_location |
3833
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
82 self._conf = {} |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
83 if autocreate: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
84 self.make_config(config) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
85 if autoload: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
86 self._conf = self.load_config() |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
87 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
88 def __getitem__(self, key): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
89 return self._conf[key] |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
90 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
91 def __nonzero__(self): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
92 if self._conf: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
93 return True |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
94 return False |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
95 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
96 def __eq__(self): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
97 return self._conf.__eq__() |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
98 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
99 def __repr__(self): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
100 return 'RcConf<%s>' % self._conf.__repr__() |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
101 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
102 def make_config(self, config): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
103 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
104 Saves given config as a JSON dump in the _conf_name location |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
105 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
106 :param config: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
107 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
108 update = False |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
109 if os.path.exists(self._conf_name): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
110 update = True |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
111 with open(self._conf_name, 'wb') as f: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
112 json.dump(config, f, indent=4) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
113 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
114 if update: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
115 sys.stdout.write('Updated config in %s\n' % self._conf_name) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
116 else: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
117 sys.stdout.write('Created new config in %s\n' % self._conf_name) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
118 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
119 def update_config(self, new_config): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
120 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
121 Reads the JSON config updates it's values with new_config and |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
122 saves it back as JSON dump |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
123 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
124 :param new_config: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
125 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
126 config = {} |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
127 try: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
128 with open(self._conf_name, 'rb') as conf: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
129 config = json.load(conf) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
130 except IOError, e: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
131 sys.stderr.write(str(e) + '\n') |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
132 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
133 config.update(new_config) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
134 self.make_config(config) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
135 |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
136 def load_config(self): |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
137 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
138 Loads config from file and returns loaded JSON object |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
139 """ |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
140 try: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
141 with open(self._conf_name, 'rb') as conf: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
142 return json.load(conf) |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
143 except IOError, e: |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
144 #sys.stderr.write(str(e) + '\n') |
5055dd385118
api: decouple some parts from api CLI script
Marcin Kuzminski <marcin@python-works.com>
parents:
diff
changeset
|
145 pass |