6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
7 """ |
7 """ |
8 __docformat__ = "restructuredtext en" |
8 __docformat__ = "restructuredtext en" |
9 |
9 |
10 import sys |
10 import sys |
|
11 import os |
11 from itertools import chain |
12 from itertools import chain |
12 |
13 |
13 from logilab.common.shellutils import ProgressBar |
14 from logilab.common.shellutils import ProgressBar |
14 |
15 |
15 from yams import schema as schemamod, buildobjs as ybo |
16 from yams import schema as schemamod, buildobjs as ybo |
274 |
275 |
275 def serialize_schema(cursor, schema, verbose=False): |
276 def serialize_schema(cursor, schema, verbose=False): |
276 """synchronize schema and permissions in the database according to |
277 """synchronize schema and permissions in the database according to |
277 current schema |
278 current schema |
278 """ |
279 """ |
279 _title = '-> storing the schema in the database ' |
280 quiet = not os.environ('APYCOT_ROOT') |
280 print _title, |
281 if not quiet: |
|
282 _title = '-> storing the schema in the database ' |
|
283 print _title, |
281 eschemas = schema.entities() |
284 eschemas = schema.entities() |
282 aller = eschemas + schema.relations() |
285 aller = eschemas + schema.relations() |
283 if not verbose: |
286 if not verbose and not quiet: |
284 pb_size = len(aller) + len(CONSTRAINTS) + len([x for x in eschemas if x.specializes()]) |
287 pb_size = len(aller) + len(CONSTRAINTS) + len([x for x in eschemas if x.specializes()]) |
285 pb = ProgressBar(pb_size, title=_title) |
288 pb = ProgressBar(pb_size, title=_title) |
|
289 else: |
|
290 pb = None |
286 rql = 'INSERT CWConstraintType X: X name %(ct)s' |
291 rql = 'INSERT CWConstraintType X: X name %(ct)s' |
287 for cstrtype in CONSTRAINTS: |
292 for cstrtype in CONSTRAINTS: |
288 if verbose: |
293 if verbose: |
289 print rql |
294 print rql |
290 cursor.execute(rql, {'ct': unicode(cstrtype)}) |
295 cursor.execute(rql, {'ct': unicode(cstrtype)}) |
291 if not verbose: |
296 if pb is not None: |
292 pb.update() |
297 pb.update() |
293 groupmap = group_mapping(cursor, interactive=False) |
298 groupmap = group_mapping(cursor, interactive=False) |
294 for ertype in aller: |
299 for ertype in aller: |
295 # skip eid and has_text relations |
300 # skip eid and has_text relations |
296 if ertype in VIRTUAL_RTYPES: |
301 if ertype in VIRTUAL_RTYPES: |
302 cursor.execute(rql, kwargs) |
307 cursor.execute(rql, kwargs) |
303 for rql, kwargs in erperms2rql(schema[ertype], groupmap): |
308 for rql, kwargs in erperms2rql(schema[ertype], groupmap): |
304 if verbose: |
309 if verbose: |
305 print rql |
310 print rql |
306 cursor.execute(rql, kwargs) |
311 cursor.execute(rql, kwargs) |
307 if not verbose: |
312 if pb is not None: |
308 pb.update() |
313 pb.update() |
309 for rql, kwargs in specialize2rql(schema): |
314 for rql, kwargs in specialize2rql(schema): |
310 if verbose: |
315 if verbose: |
311 print rql % kwargs |
316 print rql % kwargs |
312 cursor.execute(rql, kwargs) |
317 cursor.execute(rql, kwargs) |
313 if not verbose: |
318 if pb is not None: |
314 pb.update() |
319 pb.update() |
315 print |
320 if not quiet: |
|
321 print |
316 |
322 |
317 |
323 |
318 def _ervalues(erschema): |
324 def _ervalues(erschema): |
319 try: |
325 try: |
320 type_ = unicode(erschema.type) |
326 type_ = unicode(erschema.type) |