38 import os.path as osp |
35 import os.path as osp |
39 from datetime import datetime |
36 from datetime import datetime |
40 from glob import glob |
37 from glob import glob |
41 from copy import copy |
38 from copy import copy |
42 from contextlib import contextmanager |
39 from contextlib import contextmanager |
43 |
|
44 from six import PY2, text_type |
|
45 |
40 |
46 from logilab.common.decorators import cached, clear_cache |
41 from logilab.common.decorators import cached, clear_cache |
47 |
42 |
48 from yams.buildobjs import EntityType |
43 from yams.buildobjs import EntityType |
49 from yams.constraints import SizeConstraint |
44 from yams.constraints import SizeConstraint |
1003 self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
998 self.rqlexec('DELETE CWEType ET WHERE ET name %(on)s', {'on': oldname}, |
1004 ask_confirm=False) |
999 ask_confirm=False) |
1005 # elif simply renaming an entity type |
1000 # elif simply renaming an entity type |
1006 else: |
1001 else: |
1007 self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s', |
1002 self.rqlexec('SET ET name %(newname)s WHERE ET is CWEType, ET name %(on)s', |
1008 {'newname': text_type(newname), 'on': oldname}, |
1003 {'newname': newname, 'on': oldname}, |
1009 ask_confirm=False) |
1004 ask_confirm=False) |
1010 if commit: |
1005 if commit: |
1011 self.commit() |
1006 self.commit() |
1012 |
1007 |
1013 def cmd_add_relation_type(self, rtype, addrdef=True, commit=True): |
1008 def cmd_add_relation_type(self, rtype, addrdef=True, commit=True): |
1215 restriction.append('X relation_type RT, RT name "%s"' % rtype) |
1210 restriction.append('X relation_type RT, RT name "%s"' % rtype) |
1216 assert restriction |
1211 assert restriction |
1217 values = [] |
1212 values = [] |
1218 for k, v in kwargs.items(): |
1213 for k, v in kwargs.items(): |
1219 values.append('X %s %%(%s)s' % (k, k)) |
1214 values.append('X %s %%(%s)s' % (k, k)) |
1220 if PY2 and isinstance(v, str): |
|
1221 kwargs[k] = unicode(v) |
|
1222 rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
1215 rql = 'SET %s WHERE %s' % (','.join(values), ','.join(restriction)) |
1223 self.rqlexec(rql, kwargs, ask_confirm=self.verbosity >= 2) |
1216 self.rqlexec(rql, kwargs, ask_confirm=self.verbosity >= 2) |
1224 if commit: |
1217 if commit: |
1225 self.commit() |
1218 self.commit() |
1226 |
1219 |
1248 elif oldvalue is not None: |
1241 elif oldvalue is not None: |
1249 if size is not None: |
1242 if size is not None: |
1250 self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
1243 self.rqlexec('SET C value %%(v)s WHERE X from_entity S, X relation_type R,' |
1251 'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
1244 'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
1252 'S name "%s", R name "%s"' % (etype, rtype), |
1245 'S name "%s", R name "%s"' % (etype, rtype), |
1253 {'v': text_type(SizeConstraint(size).serialize())}, |
1246 {'v': SizeConstraint(size).serialize()}, |
1254 ask_confirm=self.verbosity >= 2) |
1247 ask_confirm=self.verbosity >= 2) |
1255 else: |
1248 else: |
1256 self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
1249 self.rqlexec('DELETE X constrained_by C WHERE X from_entity S, X relation_type R,' |
1257 'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
1250 'X constrained_by C, C cstrtype CT, CT name "SizeConstraint",' |
1258 'S name "%s", R name "%s"' % (etype, rtype), |
1251 'S name "%s", R name "%s"' % (etype, rtype), |
1285 for the specified entity type(s); set it to false in |
1278 for the specified entity type(s); set it to false in |
1286 the case of a subworkflow |
1279 the case of a subworkflow |
1287 |
1280 |
1288 :rtype: `Workflow` |
1281 :rtype: `Workflow` |
1289 """ |
1282 """ |
1290 wf = self.cmd_create_entity('Workflow', name=text_type(name), |
1283 wf = self.cmd_create_entity('Workflow', name=name, |
1291 **kwargs) |
1284 **kwargs) |
1292 if not isinstance(wfof, (list, tuple)): |
1285 if not isinstance(wfof, (list, tuple)): |
1293 wfof = (wfof,) |
1286 wfof = (wfof,) |
1294 |
1287 |
1295 def _missing_wf_rel(etype): |
1288 def _missing_wf_rel(etype): |
1296 return 'missing workflow relations, see make_workflowable(%s)' % etype |
1289 return 'missing workflow relations, see make_workflowable(%s)' % etype |
1297 |
1290 |
1298 for etype in wfof: |
1291 for etype in wfof: |
1299 eschema = self.repo.schema[etype] |
1292 eschema = self.repo.schema[etype] |
1300 etype = text_type(etype) |
|
1301 if ensure_workflowable: |
1293 if ensure_workflowable: |
1302 assert 'in_state' in eschema.subjrels, _missing_wf_rel(etype) |
1294 assert 'in_state' in eschema.subjrels, _missing_wf_rel(etype) |
1303 assert 'custom_workflow' in eschema.subjrels, _missing_wf_rel(etype) |
1295 assert 'custom_workflow' in eschema.subjrels, _missing_wf_rel(etype) |
1304 assert 'wf_info_for' in eschema.objrels, _missing_wf_rel(etype) |
1296 assert 'wf_info_for' in eschema.objrels, _missing_wf_rel(etype) |
1305 rset = self.rqlexec( |
1297 rset = self.rqlexec( |
1306 'SET X workflow_of ET WHERE X eid %(x)s, ET name %(et)s', |
1298 'SET X workflow_of ET WHERE X eid %(x)s, ET name %(et)s', |
1307 {'x': wf.eid, 'et': text_type(etype)}, ask_confirm=False) |
1299 {'x': wf.eid, 'et': etype}, ask_confirm=False) |
1308 assert rset, 'unexistant entity type %s' % etype |
1300 assert rset, 'unexistant entity type %s' % etype |
1309 if default: |
1301 if default: |
1310 self.rqlexec( |
1302 self.rqlexec( |
1311 'SET ET default_workflow X WHERE X eid %(x)s, ET name %(et)s', |
1303 'SET ET default_workflow X WHERE X eid %(x)s, ET name %(et)s', |
1312 {'x': wf.eid, 'et': text_type(etype)}, ask_confirm=False) |
1304 {'x': wf.eid, 'et': etype}, ask_confirm=False) |
1313 if commit: |
1305 if commit: |
1314 self.commit() |
1306 self.commit() |
1315 return wf |
1307 return wf |
1316 |
1308 |
1317 def cmd_get_workflow_for(self, etype): |
1309 def cmd_get_workflow_for(self, etype): |
1338 given value. |
1330 given value. |
1339 |
1331 |
1340 To set a user specific property value, use appropriate method on CWUser |
1332 To set a user specific property value, use appropriate method on CWUser |
1341 instance. |
1333 instance. |
1342 """ |
1334 """ |
1343 value = text_type(value) |
1335 value = str(value) |
1344 try: |
1336 try: |
1345 prop = self.rqlexec( |
1337 prop = self.rqlexec( |
1346 'CWProperty X WHERE X pkey %(k)s, NOT X for_user U', |
1338 'CWProperty X WHERE X pkey %(k)s, NOT X for_user U', |
1347 {'k': text_type(pkey)}, ask_confirm=False).get_entity(0, 0) |
1339 {'k': str(pkey)}, ask_confirm=False).get_entity(0, 0) |
1348 except Exception: |
1340 except Exception: |
1349 self.cmd_create_entity('CWProperty', pkey=text_type(pkey), value=value) |
1341 self.cmd_create_entity('CWProperty', pkey=str(pkey), value=value) |
1350 else: |
1342 else: |
1351 prop.cw_set(value=value) |
1343 prop.cw_set(value=value) |
1352 |
1344 |
1353 # other data migration commands ########################################### |
1345 # other data migration commands ########################################### |
1354 |
1346 |