author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 23 Jul 2009 15:28:41 +0200 | |
changeset 2456 | aa25d6b244c8 |
parent 2126 | a25859917ccc |
child 2476 | 1294a6bdf3bf |
permissions | -rw-r--r-- |
0 | 1 |
"""provides all lax instances management commands into a single utility script |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
11 |
import os |
|
12 |
import os.path as osp |
|
13 |
import time |
|
14 |
import re |
|
15 |
import urllib2 |
|
16 |
from urllib import urlencode |
|
17 |
from Cookie import SimpleCookie |
|
18 |
||
19 |
from logilab.common.clcommands import Command, register_commands, main_run |
|
20 |
||
21 |
from cubicweb.common.uilib import remove_html_tags |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
from cubicweb.web.views.schema import SKIP_TYPES |
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
23 |
|
0 | 24 |
APPLROOT = osp.abspath(osp.join(osp.dirname(osp.abspath(__file__)), '..')) |
25 |
||
26 |
||
27 |
def initialize_vregistry(applroot): |
|
28 |
# apply monkey patches first |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
29 |
from cubicweb.goa import do_monkey_patch |
0 | 30 |
do_monkey_patch() |
31 |
from cubicweb.goa.goavreg import GAERegistry |
|
32 |
from cubicweb.goa.goaconfig import GAEConfiguration |
|
33 |
#WebConfiguration.ext_resources['JAVASCRIPTS'].append('DATADIR/goa.js') |
|
34 |
config = GAEConfiguration('toto', applroot) |
|
35 |
vreg = GAERegistry(config) |
|
36 |
vreg.set_schema(config.load_schema()) |
|
37 |
return vreg |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
38 |
|
0 | 39 |
def alistdir(directory): |
40 |
return [osp.join(directory, f) for f in os.listdir(directory)] |
|
41 |
||
42 |
||
43 |
class LaxCommand(Command): |
|
44 |
"""base command class for all lax commands |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
45 |
creates vreg, schema and calls |
0 | 46 |
""" |
47 |
min_args = max_args = 0 |
|
48 |
||
49 |
def run(self, args): |
|
50 |
self.vreg = initialize_vregistry(APPLROOT) |
|
51 |
self._run(args) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
52 |
|
0 | 53 |
|
54 |
class GenerateSchemaCommand(LaxCommand): |
|
55 |
"""generates the schema's png file""" |
|
56 |
name = 'genschema' |
|
57 |
||
58 |
def _run(self, args): |
|
59 |
assert not args, 'no argument expected' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
60 |
from yams import schema2dot |
0 | 61 |
schema = self.vreg.schema |
62 |
path = osp.join(APPLROOT, 'data', 'schema.png') |
|
63 |
schema2dot.schema2dot(schema, path, #size=size, |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
64 |
skiptypes=SKIP_TYPES) |
0 | 65 |
print 'generated', path |
66 |
path = osp.join(APPLROOT, 'data', 'metaschema.png') |
|
2126
a25859917ccc
stop using meta attribute from yams schema. Use instead sets defining meta relations and another defining schema types. Refactor various schema view based on this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
67 |
schema2dot.schema2dot(schema, path) |
0 | 68 |
print 'generated', path |
69 |
||
70 |
||
71 |
class PopulateDataDirCommand(LaxCommand): |
|
72 |
"""populate application's data directory according to used cubes""" |
|
73 |
name = 'populatedata' |
|
74 |
||
75 |
def _run(self, args): |
|
76 |
assert not args, 'no argument expected' |
|
77 |
# first clean everything which is a symlink from the data directory |
|
78 |
datadir = osp.join(APPLROOT, 'data') |
|
79 |
if not osp.exists(datadir): |
|
80 |
print 'created data directory' |
|
81 |
os.mkdir(datadir) |
|
82 |
for filepath in alistdir(datadir): |
|
83 |
if osp.islink(filepath): |
|
84 |
print 'removing', filepath |
|
85 |
os.remove(filepath) |
|
86 |
cubes = list(self.vreg.config.cubes()) + ['shared'] |
|
87 |
for templ in cubes: |
|
88 |
templpath = self.vreg.config.cube_dir(templ) |
|
89 |
templdatadir = osp.join(templpath, 'data') |
|
90 |
if not osp.exists(templdatadir): |
|
91 |
print 'no data provided by', templ |
|
92 |
continue |
|
93 |
for resource in os.listdir(templdatadir): |
|
94 |
if resource == 'external_resources': |
|
95 |
continue |
|
96 |
if not osp.exists(osp.join(datadir, resource)): |
|
97 |
print 'symlinked %s from %s' % (resource, templ) |
|
98 |
os.symlink(osp.join(templdatadir, resource), |
|
99 |
osp.join(datadir, resource)) |
|
100 |
||
101 |
||
102 |
class NoRedirectHandler(urllib2.HTTPRedirectHandler): |
|
103 |
def http_error_302(self, req, fp, code, msg, headers): |
|
104 |
raise urllib2.HTTPError(req.get_full_url(), code, msg, headers, fp) |
|
105 |
http_error_301 = http_error_303 = http_error_307 = http_error_302 |
|
106 |
||
107 |
||
108 |
class GetSessionIdHandler(urllib2.HTTPRedirectHandler): |
|
109 |
def __init__(self, config): |
|
110 |
self.config = config |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
111 |
|
0 | 112 |
def http_error_303(self, req, fp, code, msg, headers): |
113 |
cookie = SimpleCookie(headers['Set-Cookie']) |
|
114 |
sessionid = cookie['__session'].value |
|
115 |
print 'session id', sessionid |
|
116 |
setattr(self.config, 'cookie', '__session=' + sessionid) |
|
117 |
return 1 # on exception should be raised |
|
118 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
119 |
|
0 | 120 |
class URLCommand(LaxCommand): |
121 |
"""abstract class for commands doing stuff by accessing the web application |
|
122 |
""" |
|
123 |
min_args = max_args = 1 |
|
124 |
arguments = '<site url>' |
|
125 |
||
126 |
options = ( |
|
127 |
('cookie', |
|
128 |
{'short': 'C', 'type' : 'string', 'metavar': 'key=value', |
|
129 |
'default': None, |
|
130 |
'help': 'session/authentication cookie.'}), |
|
131 |
('user', |
|
132 |
{'short': 'u', 'type' : 'string', 'metavar': 'login', |
|
133 |
'default': None, |
|
134 |
'help': 'user login instead of giving raw cookie string (require lax ' |
|
135 |
'based authentication).'}), |
|
136 |
('password', |
|
137 |
{'short': 'p', 'type' : 'string', 'metavar': 'password', |
|
138 |
'default': None, |
|
139 |
'help': 'user password instead of giving raw cookie string (require ' |
|
140 |
'lax based authentication).'}), |
|
141 |
) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
142 |
|
0 | 143 |
def _run(self, args): |
144 |
baseurl = args[0] |
|
145 |
if not baseurl.startswith('http'): |
|
146 |
baseurl = 'http://' + baseurl |
|
147 |
if not baseurl.endswith('/'): |
|
148 |
baseurl += '/' |
|
149 |
self.base_url = baseurl |
|
150 |
if not self.config.cookie and self.config.user: |
|
151 |
# no cookie specified but a user is. Try to open a session using |
|
152 |
# given authentication info |
|
153 |
print 'opening session for', self.config.user |
|
154 |
opener = urllib2.build_opener(GetSessionIdHandler(self.config)) |
|
155 |
urllib2.install_opener(opener) |
|
156 |
data = urlencode(dict(__login=self.config.user, |
|
157 |
__password=self.config.password)) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
158 |
self.open_url(urllib2.Request(baseurl, data)) |
0 | 159 |
opener = urllib2.build_opener(NoRedirectHandler()) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
160 |
urllib2.install_opener(opener) |
0 | 161 |
self.do_base_url(baseurl) |
162 |
||
163 |
def build_req(self, url): |
|
164 |
req = urllib2.Request(url) |
|
165 |
if self.config.cookie: |
|
166 |
req.headers['Cookie'] = self.config.cookie |
|
167 |
return req |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
168 |
|
0 | 169 |
def open_url(self, req): |
170 |
try: |
|
171 |
return urllib2.urlopen(req) |
|
172 |
except urllib2.HTTPError, ex: |
|
173 |
if ex.code == 302: |
|
174 |
self.error_302(req, ex) |
|
175 |
elif ex.code == 500: |
|
176 |
self.error_500(req, ex) |
|
177 |
else: |
|
178 |
raise |
|
179 |
||
180 |
def error_302(self, req, ex): |
|
181 |
print 'authentication required' |
|
182 |
print ('visit %s?vid=authinfo with your browser to get ' |
|
183 |
'authentication info' % self.base_url) |
|
184 |
sys.exit(1) |
|
185 |
||
186 |
def error_500(self, req, ex): |
|
187 |
print 'an unexpected error occured on the server' |
|
188 |
print ('you may get more information by visiting ' |
|
189 |
'%s' % req.get_full_url()) |
|
190 |
sys.exit(1) |
|
191 |
||
192 |
def extract_message(self, data): |
|
193 |
match = re.search(r'<div class="message">(.*?)</div>', data.read(), re.M|re.S) |
|
194 |
if match: |
|
195 |
msg = remove_html_tags(match.group(1)) |
|
196 |
print msg |
|
197 |
return msg |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
198 |
|
0 | 199 |
def do_base_url(self, baseurl): |
200 |
raise NotImplementedError() |
|
201 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
202 |
|
0 | 203 |
class DSInitCommand(URLCommand): |
204 |
"""initialize the datastore""" |
|
205 |
name = 'db-init' |
|
206 |
||
207 |
options = URLCommand.options + ( |
|
208 |
('sleep', |
|
209 |
{'short': 's', 'type' : 'int', 'metavar': 'nb seconds', |
|
210 |
'default': None, |
|
211 |
'help': 'number of seconds to wait between each request to avoid ' |
|
212 |
'going out of quota.'}), |
|
213 |
) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
214 |
|
0 | 215 |
def do_base_url(self, baseurl): |
216 |
req = self.build_req(baseurl + '?vid=contentinit') |
|
217 |
while True: |
|
218 |
try: |
|
219 |
data = self.open_url(req) |
|
220 |
except urllib2.HTTPError, ex: |
|
221 |
if ex.code == 303: # redirect |
|
222 |
print 'process completed' |
|
223 |
break |
|
224 |
raise |
|
225 |
msg = self.extract_message(data) |
|
226 |
if msg and msg.startswith('error: '): |
|
227 |
print ('you may to cleanup datastore by visiting ' |
|
228 |
'%s?vid=contentclear (ALL ENTITIES WILL BE DELETED)' |
|
229 |
% baseurl) |
|
230 |
break |
|
231 |
if self.config.sleep: |
|
232 |
time.sleep(self.config.sleep) |
|
233 |
||
234 |
||
235 |
class CleanSessionsCommand(URLCommand): |
|
236 |
"""cleanup sessions on the server. This command should usually be called |
|
237 |
regularly by a cron job or equivalent. |
|
238 |
""" |
|
239 |
name = "cleansessions" |
|
240 |
def do_base_url(self, baseurl): |
|
241 |
req = self.build_req(baseurl + '?vid=cleansessions') |
|
242 |
data = self.open_url(req) |
|
243 |
self.extract_message(data) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
244 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
245 |
|
1131
544609e83317
pylint cleanup, no more need for mx datetime conversion
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
246 |
register_commands([GenerateSchemaCommand, |
0 | 247 |
PopulateDataDirCommand, |
248 |
DSInitCommand, |
|
249 |
CleanSessionsCommand, |
|
250 |
]) |
|
251 |
||
252 |
def run(): |
|
253 |
main_run(sys.argv[1:]) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1131
diff
changeset
|
254 |
|
0 | 255 |
if __name__ == '__main__': |
256 |
run() |