-@session_wrapper
-def get_or_set_contents_file_id(filename, session=None):
- """
- Returns database id for given filename.
-
- If no matching file is found, a row is inserted.
-
- @type filename: string
- @param filename: The filename
- @type session: SQLAlchemy
- @param session: Optional SQL session object (a temporary one will be
- generated if not supplied). If not passed, a commit will be performed at
- the end of the function, otherwise the caller is responsible for commiting.
-
- @rtype: int
- @return: the database id for the given component
- """
-
- q = session.query(ContentFilename).filter_by(filename=filename)
-
- try:
- ret = q.one().cafilename_id
- except NoResultFound:
- cf = ContentFilename()
- cf.filename = filename
- session.add(cf)
- session.commit_or_flush()
- ret = cf.cafilename_id
-
- return ret
-
-__all__.append('get_or_set_contents_file_id')
-
-@session_wrapper
-def get_contents(suite, overridetype, section=None, session=None):
- """
- Returns contents for a suite / overridetype combination, limiting
- to a section if not None.
-
- @type suite: Suite
- @param suite: Suite object
-
- @type overridetype: OverrideType
- @param overridetype: OverrideType object
-
- @type section: Section
- @param section: Optional section object to limit results to
-
- @type session: SQLAlchemy
- @param session: Optional SQL session object (a temporary one will be
- generated if not supplied)
-
- @rtype: ResultsProxy
- @return: ResultsProxy object set up to return tuples of (filename, section,
- package, arch_id)
- """
-
- # find me all of the contents for a given suite
- contents_q = """SELECT (p.path||'/'||n.file) AS fn,
- s.section,
- b.package,
- b.architecture
- FROM content_associations c join content_file_paths p ON (c.filepath=p.id)
- JOIN content_file_names n ON (c.filename=n.id)
- JOIN binaries b ON (b.id=c.binary_pkg)
- JOIN override o ON (o.package=b.package)
- JOIN section s ON (s.id=o.section)
- WHERE o.suite = :suiteid AND o.type = :overridetypeid
- AND b.type=:overridetypename"""
-
- vals = {'suiteid': suite.suite_id,
- 'overridetypeid': overridetype.overridetype_id,
- 'overridetypename': overridetype.overridetype}
-
- if section is not None:
- contents_q += " AND s.id = :sectionid"
- vals['sectionid'] = section.section_id
-
- contents_q += " ORDER BY fn"
-
- return session.execute(contents_q, vals)
-
-__all__.append('get_contents')
-
-################################################################################
-
-class ContentFilepath(object):
- def __init__(self, *args, **kwargs):
- pass
-
- def __repr__(self):
- return '<ContentFilepath %s>' % self.filepath
-
-__all__.append('ContentFilepath')
-
-@session_wrapper
-def get_or_set_contents_path_id(filepath, session=None):
- """
- Returns database id for given path.
-
- If no matching file is found, a row is inserted.
-
- @type filepath: string
- @param filepath: The filepath
-
- @type session: SQLAlchemy
- @param session: Optional SQL session object (a temporary one will be
- generated if not supplied). If not passed, a commit will be performed at
- the end of the function, otherwise the caller is responsible for commiting.
-
- @rtype: int
- @return: the database id for the given path
- """
-
- q = session.query(ContentFilepath).filter_by(filepath=filepath)
-
- try:
- ret = q.one().cafilepath_id
- except NoResultFound:
- cf = ContentFilepath()
- cf.filepath = filepath
- session.add(cf)
- session.commit_or_flush()
- ret = cf.cafilepath_id
-
- return ret
-
-__all__.append('get_or_set_contents_path_id')
-
-################################################################################
-
-class ContentAssociation(object):
- def __init__(self, *args, **kwargs):
- pass
-
- def __repr__(self):
- return '<ContentAssociation %s>' % self.ca_id
-
-__all__.append('ContentAssociation')
-
-def insert_content_paths(binary_id, fullpaths, session=None):
- """
- Make sure given path is associated with given binary id
-
- @type binary_id: int
- @param binary_id: the id of the binary
- @type fullpaths: list
- @param fullpaths: the list of paths of the file being associated with the binary
- @type session: SQLAlchemy session
- @param session: Optional SQLAlchemy session. If this is passed, the caller
- is responsible for ensuring a transaction has begun and committing the
- results or rolling back based on the result code. If not passed, a commit
- will be performed at the end of the function, otherwise the caller is
- responsible for commiting.
-
- @return: True upon success
- """
-
- privatetrans = False
- if session is None:
- session = DBConn().session()
- privatetrans = True
-
- try:
- # Insert paths
- def generate_path_dicts():
- for fullpath in fullpaths:
- if fullpath.startswith( './' ):
- fullpath = fullpath[2:]
-
- yield {'filename':fullpath, 'id': binary_id }
-
- for d in generate_path_dicts():
- session.execute( "INSERT INTO bin_contents ( file, binary_id ) VALUES ( :filename, :id )",
- d )
-
- session.commit()
- if privatetrans:
- session.close()
- return True
-
- except:
- traceback.print_exc()
-
- # Only rollback if we set up the session ourself
- if privatetrans:
- session.rollback()
- session.close()
-
- return False
-
-__all__.append('insert_content_paths')
-
-################################################################################
-