]> git.decadent.org.uk Git - dak.git/blobdiff - daklib/dbconn.py
enhance binary and source find routines
[dak.git] / daklib / dbconn.py
index 593b668f1889e54ec50910f80bef3af57619b518..66921b851a8efef6d5f530561d5376bc0400ac09 100755 (executable)
@@ -194,13 +194,19 @@ def get_binary_from_id(id, session=None):
 
 __all__.append('get_binary_from_id')
 
-def get_binaries_from_name(package, session=None):
+def get_binaries_from_name(package, version=None, architecture=None, session=None):
     """
     Returns list of DBBinary objects for given C{package} name
 
     @type package: str
     @param package: DBBinary package name to search for
 
+    @type version: str or None
+    @param version: Version to search for (or None)
+
+    @type package: str, list or None
+    @param package: Architectures to limit to (or None if no limit)
+
     @type session: Session
     @param session: Optional SQL session object (a temporary one will be
     generated if not supplied)
@@ -210,10 +216,42 @@ def get_binaries_from_name(package, session=None):
     """
     if session is None:
         session = DBConn().session()
-    return session.query(DBBinary).filter_by(package=package).all()
+
+    q = session.query(DBBinary).filter_by(package=package)
+
+    if version is not None:
+        q = q.filter_by(version=version)
+
+    if architecture is not None:
+        if not isinstance(architecture, list):
+            architecture = [architecture]
+        q = q.join(Architecture).filter(Architecture.arch_string.in_(architecture))
+
+    return q.all()
 
 __all__.append('get_binaries_from_name')
 
+def get_binaries_from_source_id(source_id, session=None):
+    """
+    Returns list of DBBinary objects for given C{source_id}
+
+    @type source_id: int
+    @param source_id: source_id to search for
+
+    @type session: Session
+    @param session: Optional SQL session object (a temporary one will be
+    generated if not supplied)
+
+    @rtype: list
+    @return: list of DBBinary objects for the given name (may be empty)
+    """
+    if session is None:
+        session = DBConn().session()
+    return session.query(DBBinary).filter_by(source_id=source_id).all()
+
+__all__.append('get_binaries_from_source_id')
+
+
 def get_binary_from_name_suite(package, suitename, session=None):
     ### For dak examine-package
     ### XXX: Doesn't use object API yet
@@ -252,6 +290,7 @@ def get_binary_components(package, suitename, arch, session=None):
     return session.execute(query, vals)
 
 __all__.append('get_binary_components')
+
 ################################################################################
 
 class Component(object):
@@ -669,6 +708,80 @@ __all__.append('Maintainer')
 
 ################################################################################
 
+class NewComment(object):
+    def __init__(self, *args, **kwargs):
+        pass
+
+    def __repr__(self):
+        return '''<NewComment for '%s %s' (%s)>''' % (self.package, self.version, self.comment_id)
+
+__all__.append('NewComment')
+
+def has_new_comment(package, version, session=None):
+    """
+    Returns true if the given combination of C{package}, C{version} has a comment.
+
+    @type package: string
+    @param package: name of the package
+
+    @type version: string
+    @param version: package version
+
+    @type session: Session
+    @param session: Optional SQLA session object (a temporary one will be
+    generated if not supplied)
+
+    @rtype: boolean
+    @return: true/false
+    """
+
+    if session is None:
+        session = DBConn().session()
+
+    q = session.query(NewComment)
+    q = q.filter_by(package=package)
+    q = q.filter_by(version=version)
+    return q.count() > 0
+
+__all__.append('has_new_comment')
+
+def get_new_comments(package=None, version=None, comment_id=None, session=None):
+    """
+    Returns (possibly empty) list of NewComment objects for the given
+    parameters
+
+    @type package: string (optional)
+    @param package: name of the package
+
+    @type version: string (optional)
+    @param version: package version
+
+    @type comment_id: int (optional)
+    @param comment_id: An id of a comment
+
+    @type session: Session
+    @param session: Optional SQLA session object (a temporary one will be
+    generated if not supplied)
+
+    @rtype: list
+    @return: A (possibly empty) list of NewComment objects will be returned
+
+    """
+
+    if session is None:
+        session = DBConn().session()
+
+    q = session.query(NewComment)
+    if package is not None: q = q.filter_by(package=package)
+    if version is not None: q = q.filter_by(version=version)
+    if comment_id is not None: q = q.filter_by(comment_id=comment_id)
+
+    return q.all()
+
+__all__.append('get_new_comments')
+
+################################################################################
+
 class Override(object):
     def __init__(self, *args, **kwargs):
         pass
@@ -1175,13 +1288,16 @@ def source_exists(source, source_version, suites = ["any"], session=None):
 
 __all__.append('source_exists')
 
-def get_sources_from_name(source, dm_upload_allowed=None, session=None):
+def get_sources_from_name(source, version=None, dm_upload_allowed=None, session=None):
     """
-    Returns list of DBSource objects for given C{source} name
+    Returns list of DBSource objects for given C{source} name and other parameters
 
     @type source: str
     @param source: DBSource package name to search for
 
+    @type source: str or None
+    @param source: DBSource version name to search for or None if not applicable
+
     @type dm_upload_allowed: bool
     @param dm_upload_allowed: If None, no effect.  If True or False, only
     return packages with that dm_upload_allowed setting
@@ -1197,6 +1313,10 @@ def get_sources_from_name(source, dm_upload_allowed=None, session=None):
         session = DBConn().session()
 
     q = session.query(DBSource).filter_by(source=source)
+
+    if version is not None:
+        q = q.filter_by(version=version)
+
     if dm_upload_allowed is not None:
         q = q.filter_by(dm_upload_allowed=dm_upload_allowed)
 
@@ -1526,6 +1646,7 @@ class DBConn(Singleton):
         self.tbl_keyrings = Table('keyrings', self.db_meta, autoload=True)
         self.tbl_location = Table('location', self.db_meta, autoload=True)
         self.tbl_maintainer = Table('maintainer', self.db_meta, autoload=True)
+        self.tbl_new_comments = Table('new_comments', self.db_meta, autoload=True)
         self.tbl_override = Table('override', self.db_meta, autoload=True)
         self.tbl_override_type = Table('override_type', self.db_meta, autoload=True)
         self.tbl_pending_content_associations = Table('pending_content_associations', self.db_meta, autoload=True)
@@ -1634,6 +1755,9 @@ class DBConn(Singleton):
         mapper(Maintainer, self.tbl_maintainer,
                properties = dict(maintainer_id = self.tbl_maintainer.c.id))
 
+        mapper(NewComment, self.tbl_new_comments,
+               properties = dict(comment_id = self.tbl_new_comments.c.id))
+
         mapper(Override, self.tbl_override,
                properties = dict(suite_id = self.tbl_override.c.suite,
                                  suite = relation(Suite),