def append_agent(parser, agent_type, desc_keywords=dict(), initiate_keywords=dict(), name=None): desc_factory = serialization.lookup(agent_type) if not desc_factory: raise ConfigParser.Error("Unknown agent_type: %s" % (agent_type, )) desc = desc_factory(**desc_keywords) parser.values.agents.append((desc, initiate_keywords, name))
def migration_script(connection): log.info("script", "Running the migration script.") index = yield connection.query_view(view.DocumentByType, group_level=2, parse_result=False) try: for (type_name, version), count in index: restorator = serialization.lookup(type_name) if not restorator: log.error( 'script', "Failed to lookup the restorator for the " "type name: %s. There is %d objects like this in the" " database. They will not be migrated.", type_name, count) if (IVersionAdapter.providedBy(restorator) and ((version is None and restorator.version > 1) or (version is not None and version < restorator.version))): log.info('script', "I will migrate %d documents of the " "type: %s from version %s to %d", count, type_name, version, restorator.version) migrated = 0 while migrated < count: fetched = yield connection.query_view( view.DocumentByType, key=(type_name, version), limit=15, reduce=False, include_docs=True) migrated += len(fetched) if not fetched: break log.info("script", "Migrated %d documents of the type %s " "from %s version to %s", migrated, type_name, version, restorator.version) except Exception: error.handle_exception("script", None, "Failed running migration script") raise
def clean_all_descriptors(connection, dry_run=False): rows = yield connection.query_view(view.DocumentByType, group_level=1, parse_results=False) to_delete = list() for row in rows: type_name = row[0][0] restorator = serialization.lookup(type_name) if not restorator: log.info('cleanup', 'Could not lookup restorator for type name: %s. ' 'There is %s documents of this type.', type_name, row[1]) continue if IDescriptor.implementedBy(restorator): log.info('cleanup', 'I will delete %s documents of type name: %s', row[1], type_name) to_delete.append(type_name) if dry_run: log.info("cleanup", "Not deleting anything, this is just a dry run.") return for type_name in to_delete: keys = view.DocumentByType.fetch(type_name) keys['include_docs'] = False rows = yield connection.query_view( view.DocumentByType, parse_results=False, **keys) for (key, value, doc_id) in rows: try: yield connection.update_document(doc_id, update.delete) except Exception as e: log.error("cleanup", "Cannot delete the documents of type %s with ID: %s." " Reason: %s", type_name, doc_id, e)
def lookup_descriptor(name): r = serialization.lookup(name) if r is not None and not IDescriptor.implementedBy(r): raise TypeError("lookup_descriptor() tried to return %r" % (r,)) return r