def __getitem__(self, name): """See `ISourcePackageNameSet`.""" name = ensure_unicode(name) try: return SourcePackageName.byName(name) except SQLObjectNotFound: raise NoSuchSourcePackageName(name)
def search(self, query, vocab_filter=None): """Return terms where query is a subtring of the name.""" if query: clause = CONTAINSSTRING(self._table.q.name, ensure_unicode(query)) if self._filter: clause = AND(clause, self._filter) return self._table.select(clause, orderBy=self._orderBy) return self.emptySelectResults()
def bulkAddActivity(self, references, result=BugWatchActivityStatus.SYNC_SUCCEEDED, oops_id=None): """See `IBugWatchSet`.""" bulk.create((BugWatchActivity.bug_watch_id, BugWatchActivity.result, BugWatchActivity.oops_id), [(bug_watch_id, result, ensure_unicode(oops_id)) for bug_watch_id in set(get_bug_watch_ids(references))])
def bulkAddActivity(self, references, result=BugWatchActivityStatus.SYNC_SUCCEEDED, oops_id=None): """See `IBugWatchSet`.""" bulk.create( (BugWatchActivity.bug_watch_id, BugWatchActivity.result, BugWatchActivity.oops_id), [(bug_watch_id, result, ensure_unicode(oops_id)) for bug_watch_id in set(get_bug_watch_ids(references))])
def searchForTerms(self, query=None, vocab_filter=None): if not query: return self.emptySelectResults() query = ensure_unicode(query).lower() clause = CONTAINSSTRING(self._table.q.name, query) if self._filter: clause = AND(clause, self._filter) results = self._table.select(clause, orderBy=self._orderBy) return self.iterator(results.count(), results, self.toTerm)
def searchForTerms(self, query=None, vocab_filter=None): if not query: return self.emptySelectResults() query = ensure_unicode(query).lower() results = IStore(self._table).find( self._table, self._table.name.contains_string(query), *self._clauses).order_by(self._order_by) return self.iterator(results.count(), results, self.toTerm)
def ensure(self, name): """Ensure that the given BinaryPackageName exists, creating it if necessary. Returns the BinaryPackageName """ name = ensure_unicode(name) try: return self[name] except NotFoundError: return self.new(name)
def __setitem__(self, key, value): key = ensure_unicode(key) pickled_value = pickle.dumps(value, pickle.HIGHEST_PROTOCOL) self.session_data._ensureClientId() self.store.execute( "SELECT set_session_pkg_data(?, ?, ?, ?)", (self.session_data.client_id, self.product_id, key, pickled_value), noresult=True) # Store the value in the cache too self._data_cache[key] = value
def __contains__(self, name): """See `IPillarNameSet`.""" name = ensure_unicode(name) result = IStore(PillarName).execute(""" SELECT TRUE FROM PillarName WHERE (id IN (SELECT alias_for FROM PillarName WHERE name=?) OR name=?) AND alias_for IS NULL AND active IS TRUE """, [name, name]) return result.get_one() is not None
def __contains__(self, name): """See `IPillarNameSet`.""" name = ensure_unicode(name) result = IStore(PillarName).execute( """ SELECT TRUE FROM PillarName WHERE (id IN (SELECT alias_for FROM PillarName WHERE name=?) OR name=?) AND alias_for IS NULL AND active IS TRUE """, [name, name]) return result.get_one() is not None
def search(self, text): """See `ILanguageSet`.""" if text: text = ensure_unicode(text).lower() results = ISlaveStore(Language).find( Language, Or(Language.code.lower().contains_string(text), Language.englishname.lower().contains_string( text))).order_by(Language.englishname) else: results = None return results
def search(self, query, vocab_filter=None): """Search for web bug trackers.""" query = ensure_unicode(query).lower() results = IStore(self._table).find( self._table, And( self._filter, BugTracker.active == True, Or(CONTAINSSTRING(BugTracker.name, query), CONTAINSSTRING(BugTracker.title, query), CONTAINSSTRING(BugTracker.summary, query), CONTAINSSTRING(BugTracker.baseurl, query)))) results = results.order_by(self._order_by) return results
def __init__(self, session_data_container, client_id): self.session_data_container = session_data_container self.client_id = ensure_unicode(client_id) self.lastAccessTime = time.time() # Update the last access time in the db if it is out of date table_name = session_data_container.session_data_table_name query = """ UPDATE %s SET last_accessed = CURRENT_TIMESTAMP WHERE client_id = ? AND last_accessed < CURRENT_TIMESTAMP - '%d seconds'::interval """ % (table_name, session_data_container.resolution) self.store.execute(query, (self.client_id,), noresult=True)
def search(self, text): """See `ILanguageSet`.""" if text: text = ensure_unicode(text).lower() results = ISlaveStore(Language).find( Language, Or( Language.code.lower().contains_string(text), Language.englishname.lower().contains_string( text))).order_by(Language.englishname) else: results = None return results
def __init__(self, session_data_container, client_id): self.session_data_container = session_data_container self.client_id = ensure_unicode(client_id) self.lastAccessTime = time.time() # Update the last access time in the db if it is out of date table_name = session_data_container.session_data_table_name query = """ UPDATE %s SET last_accessed = CURRENT_TIMESTAMP WHERE client_id = ? AND last_accessed < CURRENT_TIMESTAMP - '%d seconds'::interval """ % (table_name, session_data_container.resolution) self.store.execute(query, (self.client_id, ), noresult=True)
def __init__(self, distroseries, parents=(), arches=(), archindep_archtag=None, packagesets=None, rebuild=False, overlays=(), overlay_pockets=(), overlay_components=()): self.distroseries = distroseries self.parent_ids = [int(id) for id in parents] # Load parent objects in bulk... parents_bulk = bulk.load(DistroSeries, self.parent_ids) # ... sort the parents to match the order in the 'parents' parameter. self.parents = sorted( parents_bulk, key=lambda parent: self.parent_ids.index(parent.id)) self.arches = arches self.archindep_archtag = archindep_archtag if packagesets is None: self.packagesets_ids = None self.packagesets = None else: self.packagesets_ids = [ ensure_unicode(packageset) for packageset in packagesets ] self.packagesets = bulk.load( Packageset, [int(packageset) for packageset in packagesets]) self.rebuild = rebuild self.overlays = overlays self.overlay_pockets = overlay_pockets self.overlay_components = overlay_components self._store = IMasterStore(DistroSeries) self.first_derivation = ( not self.distroseries.distribution.has_published_sources) if self.first_derivation: # Use-case #1. self.derivation_parents = self.parents self.derivation_parent_ids = self.parent_ids else: # Use-case #2. self.derivation_parents = [self.distroseries.previous_series] self.derivation_parent_ids = [ p.id for p in self.derivation_parents if p is not None ] if self.parent_ids == []: self.parents = ( self.distroseries.previous_series.getParentSeries()) self._create_source_names_by_parent()
def search(self, query, vocab_filter=None): """Search for web bug trackers.""" query = ensure_unicode(query).lower() results = IStore(self._table).find( self._table, And( self._filter, BugTracker.active == True, Or( CONTAINSSTRING(BugTracker.name, query), CONTAINSSTRING(BugTracker.title, query), CONTAINSSTRING(BugTracker.summary, query), CONTAINSSTRING(BugTracker.baseurl, query)))) results = results.order_by(self._order_by) return results
def getByName(self, name, ignore_inactive=False): """Return the pillar with the given name. If ignore_inactive is True, then only active pillars are considered. If no pillar is found, None is returned. """ # We could attempt to do this in a single database query, but I # expect that doing two queries will be faster that OUTER JOINing # the Project, Product and Distribution tables (and this approach # works better with SQLObject too. # Retrieve information out of the PillarName table. query = """ SELECT id, product, project, distribution FROM PillarName WHERE (id = (SELECT alias_for FROM PillarName WHERE name=?) OR name=?) AND alias_for IS NULL%s LIMIT 1 """ if ignore_inactive: query %= " AND active IS TRUE" else: query %= "" name = ensure_unicode(name) result = IStore(PillarName).execute(query, [name, name]) row = result.get_one() if row is None: return None assert len([column for column in row[1:] if column is None]) == 2, ( "One (and only one) of product, project or distribution may be " "NOT NULL: %s" % row[1:]) id, product, project, distribution = row if product is not None: return getUtility(IProductSet).get(product) elif project is not None: return getUtility(IProjectGroupSet).get(project) else: return getUtility(IDistributionSet).get(distribution)
def __delitem__(self, key): """Delete an item. Note that this will never fail in order to avoid race conditions in code using the session machinery """ try: del self._data_cache[key] except KeyError: # Not in the cache, then it won't be in the DB. Or if it is, # another process has inserted it and we should keep our grubby # fingers out of it. return query = """ DELETE FROM %s WHERE client_id = ? AND product_id = ? AND key = ? """ % self.table_name self.store.execute(query, (self.session_data.client_id, self.product_id, ensure_unicode(key)), noresult=True)
def __delitem__(self, key): """Delete an item. Note that this will never fail in order to avoid race conditions in code using the session machinery """ try: del self._data_cache[key] except KeyError: # Not in the cache, then it won't be in the DB. Or if it is, # another process has inserted it and we should keep our grubby # fingers out of it. return query = """ DELETE FROM %s WHERE client_id = ? AND product_id = ? AND key = ? """ % self.table_name self.store.execute( query, (self.session_data.client_id, self.product_id, ensure_unicode(key)), noresult=True)
def __init__( self, distroseries, parents=(), arches=(), archindep_archtag=None, packagesets=(), rebuild=False, overlays=(), overlay_pockets=(), overlay_components=()): self.distroseries = distroseries self.parent_ids = [int(id) for id in parents] # Load parent objects in bulk... parents_bulk = bulk.load(DistroSeries, self.parent_ids) # ... sort the parents to match the order in the 'parents' parameter. self.parents = sorted( parents_bulk, key=lambda parent: self.parent_ids.index(parent.id)) self.arches = arches self.archindep_archtag = archindep_archtag self.packagesets_ids = [ ensure_unicode(packageset) for packageset in packagesets] self.packagesets = bulk.load( Packageset, [int(packageset) for packageset in packagesets]) self.rebuild = rebuild self.overlays = overlays self.overlay_pockets = overlay_pockets self.overlay_components = overlay_components self._store = IMasterStore(DistroSeries) self.first_derivation = ( not self.distroseries.distribution.has_published_sources) if self.first_derivation: # Use-case #1. self.derivation_parents = self.parents self.derivation_parent_ids = self.parent_ids else: # Use-case #2. self.derivation_parents = [self.distroseries.previous_series] self.derivation_parent_ids = [ p.id for p in self.derivation_parents if p is not None] if self.parent_ids == []: self.parents = ( self.distroseries.previous_series.getParentSeries()) self._create_source_names_by_parent()
def build_search_query(self, text, extra_columns=()): """Query parameters shared by search() and count_search_matches(). :returns: Storm ResultSet object """ # These classes are imported in this method to prevent an import loop. from lp.registry.model.product import Product from lp.registry.model.projectgroup import ProjectGroup from lp.registry.model.distribution import Distribution OtherPillarName = ClassAlias(PillarName) origin = [ PillarName, LeftJoin(OtherPillarName, PillarName.alias_for == OtherPillarName.id), LeftJoin(Product, PillarName.product == Product.id), LeftJoin(ProjectGroup, PillarName.project == ProjectGroup.id), LeftJoin(Distribution, PillarName.distribution == Distribution.id), ] conditions = SQL(''' PillarName.active = TRUE AND (PillarName.name = lower(%(text)s) OR Product.fti @@ ftq(%(text)s) OR lower(Product.title) = lower(%(text)s) OR Project.fti @@ ftq(%(text)s) OR lower(Project.title) = lower(%(text)s) OR Distribution.fti @@ ftq(%(text)s) OR lower(Distribution.title) = lower(%(text)s) ) ''' % sqlvalues(text=ensure_unicode(text))) columns = [ PillarName, OtherPillarName, Product, ProjectGroup, Distribution ] for column in extra_columns: columns.append(column) return IStore(PillarName).using(*origin).find(tuple(columns), conditions)
def build_search_query(self, text, extra_columns=()): """Query parameters shared by search() and count_search_matches(). :returns: Storm ResultSet object """ # These classes are imported in this method to prevent an import loop. from lp.registry.model.product import Product from lp.registry.model.projectgroup import ProjectGroup from lp.registry.model.distribution import Distribution OtherPillarName = ClassAlias(PillarName) origin = [ PillarName, LeftJoin( OtherPillarName, PillarName.alias_for == OtherPillarName.id), LeftJoin(Product, PillarName.product == Product.id), LeftJoin(ProjectGroup, PillarName.project == ProjectGroup.id), LeftJoin( Distribution, PillarName.distribution == Distribution.id), ] conditions = SQL(''' PillarName.active = TRUE AND (PillarName.name = lower(%(text)s) OR Product.fti @@ ftq(%(text)s) OR lower(Product.title) = lower(%(text)s) OR Project.fti @@ ftq(%(text)s) OR lower(Project.title) = lower(%(text)s) OR Distribution.fti @@ ftq(%(text)s) OR lower(Distribution.title) = lower(%(text)s) ) ''' % sqlvalues(text=ensure_unicode(text))) columns = [ PillarName, OtherPillarName, Product, ProjectGroup, Distribution] for column in extra_columns: columns.append(column) return IStore(PillarName).using(*origin).find( tuple(columns), conditions)
def __init__(self, session_data, product_id): self.session_data = session_data self.product_id = ensure_unicode(product_id) self.table_name = ( session_data.session_data_container.session_pkg_data_table_name) self._populate()
def addSources(self, names): """See `IPackageset`.""" if isinstance(names, basestring): names = [ensure_unicode(names)] clauses = (SourcePackageName, SourcePackageName.name.is_in(names)) self._api_add_or_remove(clauses, self._addSourcePackageNames)
def queryByName(self, name): return IStore(BinaryPackageName).find( BinaryPackageName, name=ensure_unicode(name)).one()
def queryByName(self, name): return IStore(BinaryPackageName).find(BinaryPackageName, name=ensure_unicode(name)).one()
def new(self, name): return BinaryPackageName(name=ensure_unicode(name))