5 @contact: Debian FTPMaster <ftpmaster@debian.org>
6 @copyright: 2000, 2001, 2002, 2003, 2004, 2006 James Troup <james@nocrew.org>
7 @copyright: 2008-2009 Mark Hymers <mhy@debian.org>
8 @copyright: 2009 Joerg Jaspert <joerg@debian.org>
9 @copyright: 2009 Mike O'Connor <stew@debian.org>
10 @license: GNU General Public License version 2 or later
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ################################################################################
29 # < mhy> I need a funny comment
30 # < sgran> two peanuts were walking down a dark street
31 # < sgran> one was a-salted
32 # * mhy looks up the definition of "funny"
34 ################################################################################
40 from datetime import datetime, timedelta
41 from errno import ENOENT
42 from tempfile import mkstemp, mkdtemp
44 from inspect import getargspec
47 from sqlalchemy import create_engine, Table, MetaData
48 from sqlalchemy.orm import sessionmaker, mapper, relation
49 from sqlalchemy import types as sqltypes
51 # Don't remove this, we re-export the exceptions to scripts which import us
52 from sqlalchemy.exc import *
53 from sqlalchemy.orm.exc import NoResultFound
55 from config import Config
56 from textutils import fix_maintainer
58 ################################################################################
60 # Patch in support for the debversion field type so that it works during
63 class DebVersion(sqltypes.Text):
64 def get_col_spec(self):
67 sa_major_version = sqlalchemy.__version__[0:3]
68 if sa_major_version == "0.5":
69 from sqlalchemy.databases import postgres
70 postgres.ischema_names['debversion'] = DebVersion
72 raise Exception("dak isn't ported to SQLA versions != 0.5 yet. See daklib/dbconn.py")
74 ################################################################################
76 __all__ = ['IntegrityError', 'SQLAlchemyError']
78 ################################################################################
80 def session_wrapper(fn):
82 Wrapper around common ".., session=None):" handling. If the wrapped
83 function is called without passing 'session', we create a local one
84 and destroy it when the function ends.
86 Also attaches a commit_or_flush method to the session; if we created a
87 local session, this is a synonym for session.commit(), otherwise it is a
88 synonym for session.flush().
91 def wrapped(*args, **kwargs):
92 private_transaction = False
94 # Find the session object
95 session = kwargs.get('session')
98 if len(args) <= len(getargspec(fn)[0]) - 1:
99 # No session specified as last argument or in kwargs
100 private_transaction = True
101 session = kwargs['session'] = DBConn().session()
103 # Session is last argument in args
107 session = args[-1] = DBConn().session()
108 private_transaction = True
110 if private_transaction:
111 session.commit_or_flush = session.commit
113 session.commit_or_flush = session.flush
116 return fn(*args, **kwargs)
118 if private_transaction:
119 # We created a session; close it.
122 wrapped.__doc__ = fn.__doc__
123 wrapped.func_name = fn.func_name
127 __all__.append('session_wrapper')
129 ################################################################################
131 class Architecture(object):
132 def __init__(self, *args, **kwargs):
135 def __eq__(self, val):
136 if isinstance(val, str):
137 return (self.arch_string== val)
138 # This signals to use the normal comparison operator
139 return NotImplemented
141 def __ne__(self, val):
142 if isinstance(val, str):
143 return (self.arch_string != val)
144 # This signals to use the normal comparison operator
145 return NotImplemented
148 return '<Architecture %s>' % self.arch_string
150 __all__.append('Architecture')
153 def get_architecture(architecture, session=None):
155 Returns database id for given C{architecture}.
157 @type architecture: string
158 @param architecture: The name of the architecture
160 @type session: Session
161 @param session: Optional SQLA session object (a temporary one will be
162 generated if not supplied)
165 @return: Architecture object for the given arch (None if not present)
168 q = session.query(Architecture).filter_by(arch_string=architecture)
172 except NoResultFound:
175 __all__.append('get_architecture')
178 def get_architecture_suites(architecture, session=None):
180 Returns list of Suite objects for given C{architecture} name
183 @param source: Architecture name to search for
185 @type session: Session
186 @param session: Optional SQL session object (a temporary one will be
187 generated if not supplied)
190 @return: list of Suite objects for the given name (may be empty)
193 q = session.query(Suite)
194 q = q.join(SuiteArchitecture)
195 q = q.join(Architecture).filter_by(arch_string=architecture).order_by('suite_name')
201 __all__.append('get_architecture_suites')
203 ################################################################################
205 class Archive(object):
206 def __init__(self, *args, **kwargs):
210 return '<Archive %s>' % self.archive_name
212 __all__.append('Archive')
215 def get_archive(archive, session=None):
217 returns database id for given C{archive}.
219 @type archive: string
220 @param archive: the name of the arhive
222 @type session: Session
223 @param session: Optional SQLA session object (a temporary one will be
224 generated if not supplied)
227 @return: Archive object for the given name (None if not present)
230 archive = archive.lower()
232 q = session.query(Archive).filter_by(archive_name=archive)
236 except NoResultFound:
239 __all__.append('get_archive')
241 ################################################################################
243 class BinAssociation(object):
244 def __init__(self, *args, **kwargs):
248 return '<BinAssociation %s (%s, %s)>' % (self.ba_id, self.binary, self.suite)
250 __all__.append('BinAssociation')
252 ################################################################################
254 class BinContents(object):
255 def __init__(self, *args, **kwargs):
259 return '<BinContents (%s, %s)>' % (self.binary, self.filename)
261 __all__.append('BinContents')
263 ################################################################################
265 class DBBinary(object):
266 def __init__(self, *args, **kwargs):
270 return '<DBBinary %s (%s, %s)>' % (self.package, self.version, self.architecture)
272 __all__.append('DBBinary')
275 def get_suites_binary_in(package, session=None):
277 Returns list of Suite objects which given C{package} name is in
280 @param source: DBBinary package name to search for
283 @return: list of Suite objects for the given package
286 return session.query(Suite).join(BinAssociation).join(DBBinary).filter_by(package=package).all()
288 __all__.append('get_suites_binary_in')
291 def get_binary_from_id(binary_id, session=None):
293 Returns DBBinary object for given C{id}
296 @param binary_id: Id of the required binary
298 @type session: Session
299 @param session: Optional SQLA session object (a temporary one will be
300 generated if not supplied)
303 @return: DBBinary object for the given binary (None if not present)
306 q = session.query(DBBinary).filter_by(binary_id=binary_id)
310 except NoResultFound:
313 __all__.append('get_binary_from_id')
316 def get_binaries_from_name(package, version=None, architecture=None, session=None):
318 Returns list of DBBinary objects for given C{package} name
321 @param package: DBBinary package name to search for
323 @type version: str or None
324 @param version: Version to search for (or None)
326 @type package: str, list or None
327 @param package: Architectures to limit to (or None if no limit)
329 @type session: Session
330 @param session: Optional SQL session object (a temporary one will be
331 generated if not supplied)
334 @return: list of DBBinary objects for the given name (may be empty)
337 q = session.query(DBBinary).filter_by(package=package)
339 if version is not None:
340 q = q.filter_by(version=version)
342 if architecture is not None:
343 if not isinstance(architecture, list):
344 architecture = [architecture]
345 q = q.join(Architecture).filter(Architecture.arch_string.in_(architecture))
351 __all__.append('get_binaries_from_name')
354 def get_binaries_from_source_id(source_id, session=None):
356 Returns list of DBBinary objects for given C{source_id}
359 @param source_id: source_id to search for
361 @type session: Session
362 @param session: Optional SQL session object (a temporary one will be
363 generated if not supplied)
366 @return: list of DBBinary objects for the given name (may be empty)
369 return session.query(DBBinary).filter_by(source_id=source_id).all()
371 __all__.append('get_binaries_from_source_id')
374 def get_binary_from_name_suite(package, suitename, session=None):
375 ### For dak examine-package
376 ### XXX: Doesn't use object API yet
378 sql = """SELECT DISTINCT(b.package), b.version, c.name, su.suite_name
379 FROM binaries b, files fi, location l, component c, bin_associations ba, suite su
380 WHERE b.package=:package
382 AND fi.location = l.id
383 AND l.component = c.id
386 AND su.suite_name=:suitename
387 ORDER BY b.version DESC"""
389 return session.execute(sql, {'package': package, 'suitename': suitename})
391 __all__.append('get_binary_from_name_suite')
394 def get_binary_components(package, suitename, arch, session=None):
395 # Check for packages that have moved from one component to another
396 query = """SELECT c.name FROM binaries b, bin_associations ba, suite s, location l, component c, architecture a, files f
397 WHERE b.package=:package AND s.suite_name=:suitename
398 AND (a.arch_string = :arch OR a.arch_string = 'all')
399 AND ba.bin = b.id AND ba.suite = s.id AND b.architecture = a.id
400 AND f.location = l.id
401 AND l.component = c.id
404 vals = {'package': package, 'suitename': suitename, 'arch': arch}
406 return session.execute(query, vals)
408 __all__.append('get_binary_components')
410 ################################################################################
412 class BinaryACL(object):
413 def __init__(self, *args, **kwargs):
417 return '<BinaryACL %s>' % self.binary_acl_id
419 __all__.append('BinaryACL')
421 ################################################################################
423 class BinaryACLMap(object):
424 def __init__(self, *args, **kwargs):
428 return '<BinaryACLMap %s>' % self.binary_acl_map_id
430 __all__.append('BinaryACLMap')
432 ################################################################################
437 ArchiveDir "%(archivepath)s";
438 OverrideDir "/srv/ftp.debian.org/scripts/override/";
439 CacheDir "/srv/ftp.debian.org/database/";
444 Packages::Compress ". bzip2 gzip";
445 Sources::Compress ". bzip2 gzip";
450 bindirectory "incoming"
455 BinOverride "override.sid.all3";
456 BinCacheDB "packages-accepted.db";
458 FileList "%(filelist)s";
461 Packages::Extensions ".deb .udeb";
464 bindirectory "incoming/"
467 BinOverride "override.sid.all3";
468 SrcOverride "override.sid.all3.src";
469 FileList "%(filelist)s";
473 class BuildQueue(object):
474 def __init__(self, *args, **kwargs):
478 return '<BuildQueue %s>' % self.queue_name
480 def write_metadata(self, starttime, force=False):
481 # Do we write out metafiles?
482 if not (force or self.generate_metadata):
485 session = DBConn().session().object_session(self)
487 fl_fd = fl_name = ac_fd = ac_name = None
489 arches = " ".join([ a.arch_string for a in session.query(Architecture).all() if a.arch_string != 'source' ])
490 startdir = os.getcwd()
493 # Grab files we want to include
494 newer = session.query(BuildQueueFile).filter_by(build_queue_id = self.queue_id).filter(BuildQueueFile.lastused + timedelta(seconds=self.stay_of_execution) > starttime).all()
495 # Write file list with newer files
496 (fl_fd, fl_name) = mkstemp()
498 os.write(fl_fd, '%s\n' % n.fullpath)
501 # Write minimal apt.conf
502 # TODO: Remove hardcoding from template
503 (ac_fd, ac_name) = mkstemp()
504 os.write(ac_fd, MINIMAL_APT_CONF % {'archivepath': self.path,
505 'filelist': fl_name})
508 # Run apt-ftparchive generate
509 os.chdir(os.path.dirname(ac_name))
510 os.system('apt-ftparchive -qq -o APT::FTPArchive::Contents=off generate %s' % os.path.basename(ac_name))
512 # Run apt-ftparchive release
513 # TODO: Eww - fix this
514 bname = os.path.basename(self.path)
518 # We have to remove the Release file otherwise it'll be included in the
521 os.unlink(os.path.join(bname, 'Release'))
525 os.system("""apt-ftparchive -qq -o APT::FTPArchive::Release::Origin="%s" -o APT::FTPArchive::Release::Label="%s" -o APT::FTPArchive::Release::Description="%s" -o APT::FTPArchive::Release::Architectures="%s" release %s > Release""" % (self.origin, self.label, self.releasedescription, arches, bname))
530 keyring = "--secret-keyring \"%s\"" % cnf["Dinstall::SigningKeyring"]
531 if cnf.has_key("Dinstall::SigningPubKeyring"):
532 keyring += " --keyring \"%s\"" % cnf["Dinstall::SigningPubKeyring"]
534 os.system("gpg %s --no-options --batch --no-tty --armour --default-key %s --detach-sign -o Release.gpg Release""" % (keyring, self.signingkey))
536 # Move the files if we got this far
537 os.rename('Release', os.path.join(bname, 'Release'))
539 os.rename('Release.gpg', os.path.join(bname, 'Release.gpg'))
541 # Clean up any left behind files
568 def clean_and_update(self, starttime, Logger, dryrun=False):
569 """WARNING: This routine commits for you"""
570 session = DBConn().session().object_session(self)
572 if self.generate_metadata and not dryrun:
573 self.write_metadata(starttime)
575 # Grab files older than our execution time
576 older = session.query(BuildQueueFile).filter_by(build_queue_id = self.queue_id).filter(BuildQueueFile.lastused + timedelta(seconds=self.stay_of_execution) <= starttime).all()
582 Logger.log(["I: Would have removed %s from the queue" % o.fullpath])
584 Logger.log(["I: Removing %s from the queue" % o.fullpath])
585 os.unlink(o.fullpath)
588 # If it wasn't there, don't worry
589 if e.errno == ENOENT:
592 # TODO: Replace with proper logging call
593 Logger.log(["E: Could not remove %s" % o.fullpath])
600 for f in os.listdir(self.path):
601 if f.startswith('Packages') or f.startswith('Source') or f.startswith('Release'):
605 r = session.query(BuildQueueFile).filter_by(build_queue_id = self.queue_id).filter_by(filename = f).one()
606 except NoResultFound:
607 fp = os.path.join(self.path, f)
609 Logger.log(["I: Would remove unused link %s" % fp])
611 Logger.log(["I: Removing unused link %s" % fp])
615 Logger.log(["E: Failed to unlink unreferenced file %s" % r.fullpath])
617 def add_file_from_pool(self, poolfile):
618 """Copies a file into the pool. Assumes that the PoolFile object is
619 attached to the same SQLAlchemy session as the Queue object is.
621 The caller is responsible for committing after calling this function."""
622 poolfile_basename = poolfile.filename[poolfile.filename.rindex(os.sep)+1:]
624 # Check if we have a file of this name or this ID already
625 for f in self.queuefiles:
626 if f.fileid is not None and f.fileid == poolfile.file_id or \
627 f.poolfile.filename == poolfile_basename:
628 # In this case, update the BuildQueueFile entry so we
629 # don't remove it too early
630 f.lastused = datetime.now()
631 DBConn().session().object_session(poolfile).add(f)
634 # Prepare BuildQueueFile object
635 qf = BuildQueueFile()
636 qf.build_queue_id = self.queue_id
637 qf.lastused = datetime.now()
638 qf.filename = poolfile_basename
640 targetpath = poolfile.fullpath
641 queuepath = os.path.join(self.path, poolfile_basename)
645 # We need to copy instead of symlink
647 utils.copy(targetpath, queuepath)
648 # NULL in the fileid field implies a copy
651 os.symlink(targetpath, queuepath)
652 qf.fileid = poolfile.file_id
656 # Get the same session as the PoolFile is using and add the qf to it
657 DBConn().session().object_session(poolfile).add(qf)
662 __all__.append('BuildQueue')
665 def get_build_queue(queuename, session=None):
667 Returns BuildQueue object for given C{queue name}, creating it if it does not
670 @type queuename: string
671 @param queuename: The name of the queue
673 @type session: Session
674 @param session: Optional SQLA session object (a temporary one will be
675 generated if not supplied)
678 @return: BuildQueue object for the given queue
681 q = session.query(BuildQueue).filter_by(queue_name=queuename)
685 except NoResultFound:
688 __all__.append('get_build_queue')
690 ################################################################################
692 class BuildQueueFile(object):
693 def __init__(self, *args, **kwargs):
697 return '<BuildQueueFile %s (%s)>' % (self.filename, self.build_queue_id)
701 return os.path.join(self.buildqueue.path, self.filename)
704 __all__.append('BuildQueueFile')
706 ################################################################################
708 class ChangePendingBinary(object):
709 def __init__(self, *args, **kwargs):
713 return '<ChangePendingBinary %s>' % self.change_pending_binary_id
715 __all__.append('ChangePendingBinary')
717 ################################################################################
719 class ChangePendingFile(object):
720 def __init__(self, *args, **kwargs):
724 return '<ChangePendingFile %s>' % self.change_pending_file_id
726 __all__.append('ChangePendingFile')
728 ################################################################################
730 class ChangePendingSource(object):
731 def __init__(self, *args, **kwargs):
735 return '<ChangePendingSource %s>' % self.change_pending_source_id
737 __all__.append('ChangePendingSource')
739 ################################################################################
741 class Component(object):
742 def __init__(self, *args, **kwargs):
745 def __eq__(self, val):
746 if isinstance(val, str):
747 return (self.component_name == val)
748 # This signals to use the normal comparison operator
749 return NotImplemented
751 def __ne__(self, val):
752 if isinstance(val, str):
753 return (self.component_name != val)
754 # This signals to use the normal comparison operator
755 return NotImplemented
758 return '<Component %s>' % self.component_name
761 __all__.append('Component')
764 def get_component(component, session=None):
766 Returns database id for given C{component}.
768 @type component: string
769 @param component: The name of the override type
772 @return: the database id for the given component
775 component = component.lower()
777 q = session.query(Component).filter_by(component_name=component)
781 except NoResultFound:
784 __all__.append('get_component')
786 ################################################################################
788 class DBConfig(object):
789 def __init__(self, *args, **kwargs):
793 return '<DBConfig %s>' % self.name
795 __all__.append('DBConfig')
797 ################################################################################
800 def get_or_set_contents_file_id(filename, session=None):
802 Returns database id for given filename.
804 If no matching file is found, a row is inserted.
806 @type filename: string
807 @param filename: The filename
808 @type session: SQLAlchemy
809 @param session: Optional SQL session object (a temporary one will be
810 generated if not supplied). If not passed, a commit will be performed at
811 the end of the function, otherwise the caller is responsible for commiting.
814 @return: the database id for the given component
817 q = session.query(ContentFilename).filter_by(filename=filename)
820 ret = q.one().cafilename_id
821 except NoResultFound:
822 cf = ContentFilename()
823 cf.filename = filename
825 session.commit_or_flush()
826 ret = cf.cafilename_id
830 __all__.append('get_or_set_contents_file_id')
833 def get_contents(suite, overridetype, section=None, session=None):
835 Returns contents for a suite / overridetype combination, limiting
836 to a section if not None.
839 @param suite: Suite object
841 @type overridetype: OverrideType
842 @param overridetype: OverrideType object
844 @type section: Section
845 @param section: Optional section object to limit results to
847 @type session: SQLAlchemy
848 @param session: Optional SQL session object (a temporary one will be
849 generated if not supplied)
852 @return: ResultsProxy object set up to return tuples of (filename, section,
856 # find me all of the contents for a given suite
857 contents_q = """SELECT (p.path||'/'||n.file) AS fn,
861 FROM content_associations c join content_file_paths p ON (c.filepath=p.id)
862 JOIN content_file_names n ON (c.filename=n.id)
863 JOIN binaries b ON (b.id=c.binary_pkg)
864 JOIN override o ON (o.package=b.package)
865 JOIN section s ON (s.id=o.section)
866 WHERE o.suite = :suiteid AND o.type = :overridetypeid
867 AND b.type=:overridetypename"""
869 vals = {'suiteid': suite.suite_id,
870 'overridetypeid': overridetype.overridetype_id,
871 'overridetypename': overridetype.overridetype}
873 if section is not None:
874 contents_q += " AND s.id = :sectionid"
875 vals['sectionid'] = section.section_id
877 contents_q += " ORDER BY fn"
879 return session.execute(contents_q, vals)
881 __all__.append('get_contents')
883 ################################################################################
885 class ContentFilepath(object):
886 def __init__(self, *args, **kwargs):
890 return '<ContentFilepath %s>' % self.filepath
892 __all__.append('ContentFilepath')
895 def get_or_set_contents_path_id(filepath, session=None):
897 Returns database id for given path.
899 If no matching file is found, a row is inserted.
901 @type filename: string
902 @param filename: The filepath
903 @type session: SQLAlchemy
904 @param session: Optional SQL session object (a temporary one will be
905 generated if not supplied). If not passed, a commit will be performed at
906 the end of the function, otherwise the caller is responsible for commiting.
909 @return: the database id for the given path
912 q = session.query(ContentFilepath).filter_by(filepath=filepath)
915 ret = q.one().cafilepath_id
916 except NoResultFound:
917 cf = ContentFilepath()
918 cf.filepath = filepath
920 session.commit_or_flush()
921 ret = cf.cafilepath_id
925 __all__.append('get_or_set_contents_path_id')
927 ################################################################################
929 class ContentAssociation(object):
930 def __init__(self, *args, **kwargs):
934 return '<ContentAssociation %s>' % self.ca_id
936 __all__.append('ContentAssociation')
938 def insert_content_paths(binary_id, fullpaths, session=None):
940 Make sure given path is associated with given binary id
943 @param binary_id: the id of the binary
944 @type fullpaths: list
945 @param fullpaths: the list of paths of the file being associated with the binary
946 @type session: SQLAlchemy session
947 @param session: Optional SQLAlchemy session. If this is passed, the caller
948 is responsible for ensuring a transaction has begun and committing the
949 results or rolling back based on the result code. If not passed, a commit
950 will be performed at the end of the function, otherwise the caller is
951 responsible for commiting.
953 @return: True upon success
958 session = DBConn().session()
964 for fullpath in fullpaths:
965 if fullpath.startswith( './' ):
966 fullpath = fullpath[2:]
968 session.execute( "INSERT INTO bin_contents ( file, binary_id ) VALUES ( :filename, :id )", { 'filename': fullpath, 'id': binary_id} )
976 traceback.print_exc()
978 # Only rollback if we set up the session ourself
985 __all__.append('insert_content_paths')
987 ################################################################################
989 class DSCFile(object):
990 def __init__(self, *args, **kwargs):
994 return '<DSCFile %s>' % self.dscfile_id
996 __all__.append('DSCFile')
999 def get_dscfiles(dscfile_id=None, source_id=None, poolfile_id=None, session=None):
1001 Returns a list of DSCFiles which may be empty
1003 @type dscfile_id: int (optional)
1004 @param dscfile_id: the dscfile_id of the DSCFiles to find
1006 @type source_id: int (optional)
1007 @param source_id: the source id related to the DSCFiles to find
1009 @type poolfile_id: int (optional)
1010 @param poolfile_id: the poolfile id related to the DSCFiles to find
1013 @return: Possibly empty list of DSCFiles
1016 q = session.query(DSCFile)
1018 if dscfile_id is not None:
1019 q = q.filter_by(dscfile_id=dscfile_id)
1021 if source_id is not None:
1022 q = q.filter_by(source_id=source_id)
1024 if poolfile_id is not None:
1025 q = q.filter_by(poolfile_id=poolfile_id)
1029 __all__.append('get_dscfiles')
1031 ################################################################################
1033 class PoolFile(object):
1034 def __init__(self, *args, **kwargs):
1038 return '<PoolFile %s>' % self.filename
1042 return os.path.join(self.location.path, self.filename)
1044 __all__.append('PoolFile')
1047 def check_poolfile(filename, filesize, md5sum, location_id, session=None):
1050 (ValidFileFound [boolean or None], PoolFile object or None)
1052 @type filename: string
1053 @param filename: the filename of the file to check against the DB
1056 @param filesize: the size of the file to check against the DB
1058 @type md5sum: string
1059 @param md5sum: the md5sum of the file to check against the DB
1061 @type location_id: int
1062 @param location_id: the id of the location to look in
1065 @return: Tuple of length 2.
1066 If more than one file found with that name:
1068 If valid pool file found: (True, PoolFile object)
1069 If valid pool file not found:
1070 (False, None) if no file found
1071 (False, PoolFile object) if file found with size/md5sum mismatch
1074 q = session.query(PoolFile).filter_by(filename=filename)
1075 q = q.join(Location).filter_by(location_id=location_id)
1085 if obj.md5sum != md5sum or obj.filesize != int(filesize):
1093 __all__.append('check_poolfile')
1096 def get_poolfile_by_id(file_id, session=None):
1098 Returns a PoolFile objects or None for the given id
1101 @param file_id: the id of the file to look for
1103 @rtype: PoolFile or None
1104 @return: either the PoolFile object or None
1107 q = session.query(PoolFile).filter_by(file_id=file_id)
1111 except NoResultFound:
1114 __all__.append('get_poolfile_by_id')
1118 def get_poolfile_by_name(filename, location_id=None, session=None):
1120 Returns an array of PoolFile objects for the given filename and
1121 (optionally) location_id
1123 @type filename: string
1124 @param filename: the filename of the file to check against the DB
1126 @type location_id: int
1127 @param location_id: the id of the location to look in (optional)
1130 @return: array of PoolFile objects
1133 q = session.query(PoolFile).filter_by(filename=filename)
1135 if location_id is not None:
1136 q = q.join(Location).filter_by(location_id=location_id)
1140 __all__.append('get_poolfile_by_name')
1143 def get_poolfile_like_name(filename, session=None):
1145 Returns an array of PoolFile objects which are like the given name
1147 @type filename: string
1148 @param filename: the filename of the file to check against the DB
1151 @return: array of PoolFile objects
1154 # TODO: There must be a way of properly using bind parameters with %FOO%
1155 q = session.query(PoolFile).filter(PoolFile.filename.like('%%/%s' % filename))
1159 __all__.append('get_poolfile_like_name')
1162 def add_poolfile(filename, datadict, location_id, session=None):
1164 Add a new file to the pool
1166 @type filename: string
1167 @param filename: filename
1169 @type datadict: dict
1170 @param datadict: dict with needed data
1172 @type location_id: int
1173 @param location_id: database id of the location
1176 @return: the PoolFile object created
1178 poolfile = PoolFile()
1179 poolfile.filename = filename
1180 poolfile.filesize = datadict["size"]
1181 poolfile.md5sum = datadict["md5sum"]
1182 poolfile.sha1sum = datadict["sha1sum"]
1183 poolfile.sha256sum = datadict["sha256sum"]
1184 poolfile.location_id = location_id
1186 session.add(poolfile)
1187 # Flush to get a file id (NB: This is not a commit)
1192 __all__.append('add_poolfile')
1194 ################################################################################
1196 class Fingerprint(object):
1197 def __init__(self, *args, **kwargs):
1201 return '<Fingerprint %s>' % self.fingerprint
1203 __all__.append('Fingerprint')
1206 def get_fingerprint(fpr, session=None):
1208 Returns Fingerprint object for given fpr.
1211 @param fpr: The fpr to find / add
1213 @type session: SQLAlchemy
1214 @param session: Optional SQL session object (a temporary one will be
1215 generated if not supplied).
1218 @return: the Fingerprint object for the given fpr or None
1221 q = session.query(Fingerprint).filter_by(fingerprint=fpr)
1225 except NoResultFound:
1230 __all__.append('get_fingerprint')
1233 def get_or_set_fingerprint(fpr, session=None):
1235 Returns Fingerprint object for given fpr.
1237 If no matching fpr is found, a row is inserted.
1240 @param fpr: The fpr to find / add
1242 @type session: SQLAlchemy
1243 @param session: Optional SQL session object (a temporary one will be
1244 generated if not supplied). If not passed, a commit will be performed at
1245 the end of the function, otherwise the caller is responsible for commiting.
1246 A flush will be performed either way.
1249 @return: the Fingerprint object for the given fpr
1252 q = session.query(Fingerprint).filter_by(fingerprint=fpr)
1256 except NoResultFound:
1257 fingerprint = Fingerprint()
1258 fingerprint.fingerprint = fpr
1259 session.add(fingerprint)
1260 session.commit_or_flush()
1265 __all__.append('get_or_set_fingerprint')
1267 ################################################################################
1269 # Helper routine for Keyring class
1270 def get_ldap_name(entry):
1272 for k in ["cn", "mn", "sn"]:
1274 if ret and ret[0] != "" and ret[0] != "-":
1276 return " ".join(name)
1278 ################################################################################
1280 class Keyring(object):
1281 gpg_invocation = "gpg --no-default-keyring --keyring %s" +\
1282 " --with-colons --fingerprint --fingerprint"
1287 def __init__(self, *args, **kwargs):
1291 return '<Keyring %s>' % self.keyring_name
1293 def de_escape_gpg_str(self, txt):
1294 esclist = re.split(r'(\\x..)', txt)
1295 for x in range(1,len(esclist),2):
1296 esclist[x] = "%c" % (int(esclist[x][2:],16))
1297 return "".join(esclist)
1299 def load_keys(self, keyring):
1302 if not self.keyring_id:
1303 raise Exception('Must be initialized with database information')
1305 k = os.popen(self.gpg_invocation % keyring, "r")
1309 for line in k.xreadlines():
1310 field = line.split(":")
1311 if field[0] == "pub":
1313 (name, addr) = email.Utils.parseaddr(field[9])
1314 name = re.sub(r"\s*[(].*[)]", "", name)
1315 if name == "" or addr == "" or "@" not in addr:
1317 addr = "invalid-uid"
1318 name = self.de_escape_gpg_str(name)
1319 self.keys[key] = {"email": addr}
1321 self.keys[key]["name"] = name
1322 self.keys[key]["aliases"] = [name]
1323 self.keys[key]["fingerprints"] = []
1325 elif key and field[0] == "sub" and len(field) >= 12:
1326 signingkey = ("s" in field[11])
1327 elif key and field[0] == "uid":
1328 (name, addr) = email.Utils.parseaddr(field[9])
1329 if name and name not in self.keys[key]["aliases"]:
1330 self.keys[key]["aliases"].append(name)
1331 elif signingkey and field[0] == "fpr":
1332 self.keys[key]["fingerprints"].append(field[9])
1333 self.fpr_lookup[field[9]] = key
1335 def import_users_from_ldap(self, session):
1339 LDAPDn = cnf["Import-LDAP-Fingerprints::LDAPDn"]
1340 LDAPServer = cnf["Import-LDAP-Fingerprints::LDAPServer"]
1342 l = ldap.open(LDAPServer)
1343 l.simple_bind_s("","")
1344 Attrs = l.search_s(LDAPDn, ldap.SCOPE_ONELEVEL,
1345 "(&(keyfingerprint=*)(gidnumber=%s))" % (cnf["Import-Users-From-Passwd::ValidGID"]),
1346 ["uid", "keyfingerprint", "cn", "mn", "sn"])
1348 ldap_fin_uid_id = {}
1355 uid = entry["uid"][0]
1356 name = get_ldap_name(entry)
1357 fingerprints = entry["keyFingerPrint"]
1359 for f in fingerprints:
1360 key = self.fpr_lookup.get(f, None)
1361 if key not in self.keys:
1363 self.keys[key]["uid"] = uid
1367 keyid = get_or_set_uid(uid, session).uid_id
1368 byuid[keyid] = (uid, name)
1369 byname[uid] = (keyid, name)
1371 return (byname, byuid)
1373 def generate_users_from_keyring(self, format, session):
1377 for x in self.keys.keys():
1378 if self.keys[x]["email"] == "invalid-uid":
1380 self.keys[x]["uid"] = format % "invalid-uid"
1382 uid = format % self.keys[x]["email"]
1383 keyid = get_or_set_uid(uid, session).uid_id
1384 byuid[keyid] = (uid, self.keys[x]["name"])
1385 byname[uid] = (keyid, self.keys[x]["name"])
1386 self.keys[x]["uid"] = uid
1389 uid = format % "invalid-uid"
1390 keyid = get_or_set_uid(uid, session).uid_id
1391 byuid[keyid] = (uid, "ungeneratable user id")
1392 byname[uid] = (keyid, "ungeneratable user id")
1394 return (byname, byuid)
1396 __all__.append('Keyring')
1399 def get_keyring(keyring, session=None):
1401 If C{keyring} does not have an entry in the C{keyrings} table yet, return None
1402 If C{keyring} already has an entry, simply return the existing Keyring
1404 @type keyring: string
1405 @param keyring: the keyring name
1408 @return: the Keyring object for this keyring
1411 q = session.query(Keyring).filter_by(keyring_name=keyring)
1415 except NoResultFound:
1418 __all__.append('get_keyring')
1420 ################################################################################
1422 class KeyringACLMap(object):
1423 def __init__(self, *args, **kwargs):
1427 return '<KeyringACLMap %s>' % self.keyring_acl_map_id
1429 __all__.append('KeyringACLMap')
1431 ################################################################################
1433 class DBChange(object):
1434 def __init__(self, *args, **kwargs):
1438 return '<DBChange %s>' % self.changesname
1440 __all__.append('DBChange')
1443 def get_dbchange(filename, session=None):
1445 returns DBChange object for given C{filename}.
1447 @type archive: string
1448 @param archive: the name of the arhive
1450 @type session: Session
1451 @param session: Optional SQLA session object (a temporary one will be
1452 generated if not supplied)
1455 @return: Archive object for the given name (None if not present)
1458 q = session.query(DBChange).filter_by(changesname=filename)
1462 except NoResultFound:
1465 __all__.append('get_dbchange')
1467 ################################################################################
1469 class Location(object):
1470 def __init__(self, *args, **kwargs):
1474 return '<Location %s (%s)>' % (self.path, self.location_id)
1476 __all__.append('Location')
1479 def get_location(location, component=None, archive=None, session=None):
1481 Returns Location object for the given combination of location, component
1484 @type location: string
1485 @param location: the path of the location, e.g. I{/srv/ftp.debian.org/ftp/pool/}
1487 @type component: string
1488 @param component: the component name (if None, no restriction applied)
1490 @type archive: string
1491 @param archive_id: the archive name (if None, no restriction applied)
1493 @rtype: Location / None
1494 @return: Either a Location object or None if one can't be found
1497 q = session.query(Location).filter_by(path=location)
1499 if archive is not None:
1500 q = q.join(Archive).filter_by(archive_name=archive)
1502 if component is not None:
1503 q = q.join(Component).filter_by(component_name=component)
1507 except NoResultFound:
1510 __all__.append('get_location')
1512 ################################################################################
1514 class Maintainer(object):
1515 def __init__(self, *args, **kwargs):
1519 return '''<Maintainer '%s' (%s)>''' % (self.name, self.maintainer_id)
1521 def get_split_maintainer(self):
1522 if not hasattr(self, 'name') or self.name is None:
1523 return ('', '', '', '')
1525 return fix_maintainer(self.name.strip())
1527 __all__.append('Maintainer')
1530 def get_or_set_maintainer(name, session=None):
1532 Returns Maintainer object for given maintainer name.
1534 If no matching maintainer name is found, a row is inserted.
1537 @param name: The maintainer name to add
1539 @type session: SQLAlchemy
1540 @param session: Optional SQL session object (a temporary one will be
1541 generated if not supplied). If not passed, a commit will be performed at
1542 the end of the function, otherwise the caller is responsible for commiting.
1543 A flush will be performed either way.
1546 @return: the Maintainer object for the given maintainer
1549 q = session.query(Maintainer).filter_by(name=name)
1552 except NoResultFound:
1553 maintainer = Maintainer()
1554 maintainer.name = name
1555 session.add(maintainer)
1556 session.commit_or_flush()
1561 __all__.append('get_or_set_maintainer')
1564 def get_maintainer(maintainer_id, session=None):
1566 Return the name of the maintainer behind C{maintainer_id} or None if that
1567 maintainer_id is invalid.
1569 @type maintainer_id: int
1570 @param maintainer_id: the id of the maintainer
1573 @return: the Maintainer with this C{maintainer_id}
1576 return session.query(Maintainer).get(maintainer_id)
1578 __all__.append('get_maintainer')
1580 ################################################################################
1582 class NewComment(object):
1583 def __init__(self, *args, **kwargs):
1587 return '''<NewComment for '%s %s' (%s)>''' % (self.package, self.version, self.comment_id)
1589 __all__.append('NewComment')
1592 def has_new_comment(package, version, session=None):
1594 Returns true if the given combination of C{package}, C{version} has a comment.
1596 @type package: string
1597 @param package: name of the package
1599 @type version: string
1600 @param version: package version
1602 @type session: Session
1603 @param session: Optional SQLA session object (a temporary one will be
1604 generated if not supplied)
1610 q = session.query(NewComment)
1611 q = q.filter_by(package=package)
1612 q = q.filter_by(version=version)
1614 return bool(q.count() > 0)
1616 __all__.append('has_new_comment')
1619 def get_new_comments(package=None, version=None, comment_id=None, session=None):
1621 Returns (possibly empty) list of NewComment objects for the given
1624 @type package: string (optional)
1625 @param package: name of the package
1627 @type version: string (optional)
1628 @param version: package version
1630 @type comment_id: int (optional)
1631 @param comment_id: An id of a comment
1633 @type session: Session
1634 @param session: Optional SQLA session object (a temporary one will be
1635 generated if not supplied)
1638 @return: A (possibly empty) list of NewComment objects will be returned
1641 q = session.query(NewComment)
1642 if package is not None: q = q.filter_by(package=package)
1643 if version is not None: q = q.filter_by(version=version)
1644 if comment_id is not None: q = q.filter_by(comment_id=comment_id)
1648 __all__.append('get_new_comments')
1650 ################################################################################
1652 class Override(object):
1653 def __init__(self, *args, **kwargs):
1657 return '<Override %s (%s)>' % (self.package, self.suite_id)
1659 __all__.append('Override')
1662 def get_override(package, suite=None, component=None, overridetype=None, session=None):
1664 Returns Override object for the given parameters
1666 @type package: string
1667 @param package: The name of the package
1669 @type suite: string, list or None
1670 @param suite: The name of the suite (or suites if a list) to limit to. If
1671 None, don't limit. Defaults to None.
1673 @type component: string, list or None
1674 @param component: The name of the component (or components if a list) to
1675 limit to. If None, don't limit. Defaults to None.
1677 @type overridetype: string, list or None
1678 @param overridetype: The name of the overridetype (or overridetypes if a list) to
1679 limit to. If None, don't limit. Defaults to None.
1681 @type session: Session
1682 @param session: Optional SQLA session object (a temporary one will be
1683 generated if not supplied)
1686 @return: A (possibly empty) list of Override objects will be returned
1689 q = session.query(Override)
1690 q = q.filter_by(package=package)
1692 if suite is not None:
1693 if not isinstance(suite, list): suite = [suite]
1694 q = q.join(Suite).filter(Suite.suite_name.in_(suite))
1696 if component is not None:
1697 if not isinstance(component, list): component = [component]
1698 q = q.join(Component).filter(Component.component_name.in_(component))
1700 if overridetype is not None:
1701 if not isinstance(overridetype, list): overridetype = [overridetype]
1702 q = q.join(OverrideType).filter(OverrideType.overridetype.in_(overridetype))
1706 __all__.append('get_override')
1709 ################################################################################
1711 class OverrideType(object):
1712 def __init__(self, *args, **kwargs):
1716 return '<OverrideType %s>' % self.overridetype
1718 __all__.append('OverrideType')
1721 def get_override_type(override_type, session=None):
1723 Returns OverrideType object for given C{override type}.
1725 @type override_type: string
1726 @param override_type: The name of the override type
1728 @type session: Session
1729 @param session: Optional SQLA session object (a temporary one will be
1730 generated if not supplied)
1733 @return: the database id for the given override type
1736 q = session.query(OverrideType).filter_by(overridetype=override_type)
1740 except NoResultFound:
1743 __all__.append('get_override_type')
1745 ################################################################################
1747 class PendingContentAssociation(object):
1748 def __init__(self, *args, **kwargs):
1752 return '<PendingContentAssociation %s>' % self.pca_id
1754 __all__.append('PendingContentAssociation')
1756 def insert_pending_content_paths(package, fullpaths, session=None):
1758 Make sure given paths are temporarily associated with given
1762 @param package: the package to associate with should have been read in from the binary control file
1763 @type fullpaths: list
1764 @param fullpaths: the list of paths of the file being associated with the binary
1765 @type session: SQLAlchemy session
1766 @param session: Optional SQLAlchemy session. If this is passed, the caller
1767 is responsible for ensuring a transaction has begun and committing the
1768 results or rolling back based on the result code. If not passed, a commit
1769 will be performed at the end of the function
1771 @return: True upon success, False if there is a problem
1774 privatetrans = False
1777 session = DBConn().session()
1781 arch = get_architecture(package['Architecture'], session)
1782 arch_id = arch.arch_id
1784 # Remove any already existing recorded files for this package
1785 q = session.query(PendingContentAssociation)
1786 q = q.filter_by(package=package['Package'])
1787 q = q.filter_by(version=package['Version'])
1788 q = q.filter_by(architecture=arch_id)
1793 for fullpath in fullpaths:
1794 (path, filename) = os.path.split(fullpath)
1796 if path.startswith( "./" ):
1799 filepath_id = get_or_set_contents_path_id(path, session)
1800 filename_id = get_or_set_contents_file_id(filename, session)
1802 pathcache[fullpath] = (filepath_id, filename_id)
1804 for fullpath, dat in pathcache.items():
1805 pca = PendingContentAssociation()
1806 pca.package = package['Package']
1807 pca.version = package['Version']
1808 pca.filepath_id = dat[0]
1809 pca.filename_id = dat[1]
1810 pca.architecture = arch_id
1813 # Only commit if we set up the session ourself
1821 except Exception, e:
1822 traceback.print_exc()
1824 # Only rollback if we set up the session ourself
1831 __all__.append('insert_pending_content_paths')
1833 ################################################################################
1835 class PolicyQueue(object):
1836 def __init__(self, *args, **kwargs):
1840 return '<PolicyQueue %s>' % self.queue_name
1842 __all__.append('PolicyQueue')
1845 def get_policy_queue(queuename, session=None):
1847 Returns PolicyQueue object for given C{queue name}
1849 @type queuename: string
1850 @param queuename: The name of the queue
1852 @type session: Session
1853 @param session: Optional SQLA session object (a temporary one will be
1854 generated if not supplied)
1857 @return: PolicyQueue object for the given queue
1860 q = session.query(PolicyQueue).filter_by(queue_name=queuename)
1864 except NoResultFound:
1867 __all__.append('get_policy_queue')
1869 ################################################################################
1871 class Priority(object):
1872 def __init__(self, *args, **kwargs):
1875 def __eq__(self, val):
1876 if isinstance(val, str):
1877 return (self.priority == val)
1878 # This signals to use the normal comparison operator
1879 return NotImplemented
1881 def __ne__(self, val):
1882 if isinstance(val, str):
1883 return (self.priority != val)
1884 # This signals to use the normal comparison operator
1885 return NotImplemented
1888 return '<Priority %s (%s)>' % (self.priority, self.priority_id)
1890 __all__.append('Priority')
1893 def get_priority(priority, session=None):
1895 Returns Priority object for given C{priority name}.
1897 @type priority: string
1898 @param priority: The name of the priority
1900 @type session: Session
1901 @param session: Optional SQLA session object (a temporary one will be
1902 generated if not supplied)
1905 @return: Priority object for the given priority
1908 q = session.query(Priority).filter_by(priority=priority)
1912 except NoResultFound:
1915 __all__.append('get_priority')
1918 def get_priorities(session=None):
1920 Returns dictionary of priority names -> id mappings
1922 @type session: Session
1923 @param session: Optional SQL session object (a temporary one will be
1924 generated if not supplied)
1927 @return: dictionary of priority names -> id mappings
1931 q = session.query(Priority)
1933 ret[x.priority] = x.priority_id
1937 __all__.append('get_priorities')
1939 ################################################################################
1941 class Section(object):
1942 def __init__(self, *args, **kwargs):
1945 def __eq__(self, val):
1946 if isinstance(val, str):
1947 return (self.section == val)
1948 # This signals to use the normal comparison operator
1949 return NotImplemented
1951 def __ne__(self, val):
1952 if isinstance(val, str):
1953 return (self.section != val)
1954 # This signals to use the normal comparison operator
1955 return NotImplemented
1958 return '<Section %s>' % self.section
1960 __all__.append('Section')
1963 def get_section(section, session=None):
1965 Returns Section object for given C{section name}.
1967 @type section: string
1968 @param section: The name of the section
1970 @type session: Session
1971 @param session: Optional SQLA session object (a temporary one will be
1972 generated if not supplied)
1975 @return: Section object for the given section name
1978 q = session.query(Section).filter_by(section=section)
1982 except NoResultFound:
1985 __all__.append('get_section')
1988 def get_sections(session=None):
1990 Returns dictionary of section names -> id mappings
1992 @type session: Session
1993 @param session: Optional SQL session object (a temporary one will be
1994 generated if not supplied)
1997 @return: dictionary of section names -> id mappings
2001 q = session.query(Section)
2003 ret[x.section] = x.section_id
2007 __all__.append('get_sections')
2009 ################################################################################
2011 class DBSource(object):
2012 def __init__(self, *args, **kwargs):
2016 return '<DBSource %s (%s)>' % (self.source, self.version)
2018 __all__.append('DBSource')
2021 def source_exists(source, source_version, suites = ["any"], session=None):
2023 Ensure that source exists somewhere in the archive for the binary
2024 upload being processed.
2025 1. exact match => 1.0-3
2026 2. bin-only NMU => 1.0-3+b1 , 1.0-3.1+b1
2028 @type package: string
2029 @param package: package source name
2031 @type source_version: string
2032 @param source_version: expected source version
2035 @param suites: list of suites to check in, default I{any}
2037 @type session: Session
2038 @param session: Optional SQLA session object (a temporary one will be
2039 generated if not supplied)
2042 @return: returns 1 if a source with expected version is found, otherwise 0
2049 for suite in suites:
2050 q = session.query(DBSource).filter_by(source=source)
2052 # source must exist in suite X, or in some other suite that's
2053 # mapped to X, recursively... silent-maps are counted too,
2054 # unreleased-maps aren't.
2055 maps = cnf.ValueList("SuiteMappings")[:]
2057 maps = [ m.split() for m in maps ]
2058 maps = [ (x[1], x[2]) for x in maps
2059 if x[0] == "map" or x[0] == "silent-map" ]
2062 if x[1] in s and x[0] not in s:
2065 q = q.join(SrcAssociation).join(Suite)
2066 q = q.filter(Suite.suite_name.in_(s))
2068 # Reduce the query results to a list of version numbers
2069 ql = [ j.version for j in q.all() ]
2072 if source_version in ql:
2076 from daklib.regexes import re_bin_only_nmu
2077 orig_source_version = re_bin_only_nmu.sub('', source_version)
2078 if orig_source_version in ql:
2081 # No source found so return not ok
2086 __all__.append('source_exists')
2089 def get_suites_source_in(source, session=None):
2091 Returns list of Suite objects which given C{source} name is in
2094 @param source: DBSource package name to search for
2097 @return: list of Suite objects for the given source
2100 return session.query(Suite).join(SrcAssociation).join(DBSource).filter_by(source=source).all()
2102 __all__.append('get_suites_source_in')
2105 def get_sources_from_name(source, version=None, dm_upload_allowed=None, session=None):
2107 Returns list of DBSource objects for given C{source} name and other parameters
2110 @param source: DBSource package name to search for
2112 @type source: str or None
2113 @param source: DBSource version name to search for or None if not applicable
2115 @type dm_upload_allowed: bool
2116 @param dm_upload_allowed: If None, no effect. If True or False, only
2117 return packages with that dm_upload_allowed setting
2119 @type session: Session
2120 @param session: Optional SQL session object (a temporary one will be
2121 generated if not supplied)
2124 @return: list of DBSource objects for the given name (may be empty)
2127 q = session.query(DBSource).filter_by(source=source)
2129 if version is not None:
2130 q = q.filter_by(version=version)
2132 if dm_upload_allowed is not None:
2133 q = q.filter_by(dm_upload_allowed=dm_upload_allowed)
2137 __all__.append('get_sources_from_name')
2140 def get_source_in_suite(source, suite, session=None):
2142 Returns list of DBSource objects for a combination of C{source} and C{suite}.
2144 - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
2145 - B{suite} - a suite name, eg. I{unstable}
2147 @type source: string
2148 @param source: source package name
2151 @param suite: the suite name
2154 @return: the version for I{source} in I{suite}
2158 q = session.query(SrcAssociation)
2159 q = q.join('source').filter_by(source=source)
2160 q = q.join('suite').filter_by(suite_name=suite)
2163 return q.one().source
2164 except NoResultFound:
2167 __all__.append('get_source_in_suite')
2169 ################################################################################
2172 def add_dsc_to_db(u, filename, session=None):
2173 entry = u.pkg.files[filename]
2177 source.source = u.pkg.dsc["source"]
2178 source.version = u.pkg.dsc["version"] # NB: not files[file]["version"], that has no epoch
2179 source.maintainer_id = get_or_set_maintainer(u.pkg.dsc["maintainer"], session).maintainer_id
2180 source.changedby_id = get_or_set_maintainer(u.pkg.changes["changed-by"], session).maintainer_id
2181 source.fingerprint_id = get_or_set_fingerprint(u.pkg.changes["fingerprint"], session).fingerprint_id
2182 source.install_date = datetime.now().date()
2184 dsc_component = entry["component"]
2185 dsc_location_id = entry["location id"]
2187 source.dm_upload_allowed = (u.pkg.dsc.get("dm-upload-allowed", '') == "yes")
2189 # Set up a new poolfile if necessary
2190 if not entry.has_key("files id") or not entry["files id"]:
2191 filename = entry["pool name"] + filename
2192 poolfile = add_poolfile(filename, entry, dsc_location_id, session)
2194 pfs.append(poolfile)
2195 entry["files id"] = poolfile.file_id
2197 source.poolfile_id = entry["files id"]
2201 for suite_name in u.pkg.changes["distribution"].keys():
2202 sa = SrcAssociation()
2203 sa.source_id = source.source_id
2204 sa.suite_id = get_suite(suite_name).suite_id
2209 # Add the source files to the DB (files and dsc_files)
2211 dscfile.source_id = source.source_id
2212 dscfile.poolfile_id = entry["files id"]
2213 session.add(dscfile)
2215 for dsc_file, dentry in u.pkg.dsc_files.items():
2217 df.source_id = source.source_id
2219 # If the .orig tarball is already in the pool, it's
2220 # files id is stored in dsc_files by check_dsc().
2221 files_id = dentry.get("files id", None)
2223 # Find the entry in the files hash
2224 # TODO: Bail out here properly
2226 for f, e in u.pkg.files.items():
2231 if files_id is None:
2232 filename = dfentry["pool name"] + dsc_file
2234 (found, obj) = check_poolfile(filename, dentry["size"], dentry["md5sum"], dsc_location_id)
2235 # FIXME: needs to check for -1/-2 and or handle exception
2236 if found and obj is not None:
2237 files_id = obj.file_id
2240 # If still not found, add it
2241 if files_id is None:
2242 # HACK: Force sha1sum etc into dentry
2243 dentry["sha1sum"] = dfentry["sha1sum"]
2244 dentry["sha256sum"] = dfentry["sha256sum"]
2245 poolfile = add_poolfile(filename, dentry, dsc_location_id, session)
2246 pfs.append(poolfile)
2247 files_id = poolfile.file_id
2249 poolfile = get_poolfile_by_id(files_id, session)
2250 if poolfile is None:
2251 utils.fubar("INTERNAL ERROR. Found no poolfile with id %d" % files_id)
2252 pfs.append(poolfile)
2254 df.poolfile_id = files_id
2259 # Add the src_uploaders to the DB
2260 uploader_ids = [source.maintainer_id]
2261 if u.pkg.dsc.has_key("uploaders"):
2262 for up in u.pkg.dsc["uploaders"].split(","):
2264 uploader_ids.append(get_or_set_maintainer(up, session).maintainer_id)
2267 for up in uploader_ids:
2268 if added_ids.has_key(up):
2269 utils.warn("Already saw uploader %s for source %s" % (up, source.source))
2275 su.maintainer_id = up
2276 su.source_id = source.source_id
2281 return dsc_component, dsc_location_id, pfs
2283 __all__.append('add_dsc_to_db')
2286 def add_deb_to_db(u, filename, session=None):
2288 Contrary to what you might expect, this routine deals with both
2289 debs and udebs. That info is in 'dbtype', whilst 'type' is
2290 'deb' for both of them
2293 entry = u.pkg.files[filename]
2296 bin.package = entry["package"]
2297 bin.version = entry["version"]
2298 bin.maintainer_id = get_or_set_maintainer(entry["maintainer"], session).maintainer_id
2299 bin.fingerprint_id = get_or_set_fingerprint(u.pkg.changes["fingerprint"], session).fingerprint_id
2300 bin.arch_id = get_architecture(entry["architecture"], session).arch_id
2301 bin.binarytype = entry["dbtype"]
2304 filename = entry["pool name"] + filename
2305 fullpath = os.path.join(cnf["Dir::Pool"], filename)
2306 if not entry.get("location id", None):
2307 entry["location id"] = get_location(cnf["Dir::Pool"], entry["component"], session=session).location_id
2309 if entry.get("files id", None):
2310 poolfile = get_poolfile_by_id(bin.poolfile_id)
2311 bin.poolfile_id = entry["files id"]
2313 poolfile = add_poolfile(filename, entry, entry["location id"], session)
2314 bin.poolfile_id = entry["files id"] = poolfile.file_id
2317 bin_sources = get_sources_from_name(entry["source package"], entry["source version"], session=session)
2318 if len(bin_sources) != 1:
2319 raise NoSourceFieldError, "Unable to find a unique source id for %s (%s), %s, file %s, type %s, signed by %s" % \
2320 (bin.package, bin.version, bin.architecture.arch_string,
2321 filename, bin.binarytype, u.pkg.changes["fingerprint"])
2323 bin.source_id = bin_sources[0].source_id
2325 # Add and flush object so it has an ID
2329 # Add BinAssociations
2330 for suite_name in u.pkg.changes["distribution"].keys():
2331 ba = BinAssociation()
2332 ba.binary_id = bin.binary_id
2333 ba.suite_id = get_suite(suite_name).suite_id
2338 # Deal with contents - disabled for now
2339 #contents = copy_temporary_contents(bin.package, bin.version, bin.architecture.arch_string, os.path.basename(filename), None, session)
2341 # print "REJECT\nCould not determine contents of package %s" % bin.package
2342 # session.rollback()
2343 # raise MissingContents, "No contents stored for package %s, and couldn't determine contents of %s" % (bin.package, filename)
2347 __all__.append('add_deb_to_db')
2349 ################################################################################
2351 class SourceACL(object):
2352 def __init__(self, *args, **kwargs):
2356 return '<SourceACL %s>' % self.source_acl_id
2358 __all__.append('SourceACL')
2360 ################################################################################
2362 class SrcAssociation(object):
2363 def __init__(self, *args, **kwargs):
2367 return '<SrcAssociation %s (%s, %s)>' % (self.sa_id, self.source, self.suite)
2369 __all__.append('SrcAssociation')
2371 ################################################################################
2373 class SrcFormat(object):
2374 def __init__(self, *args, **kwargs):
2378 return '<SrcFormat %s>' % (self.format_name)
2380 __all__.append('SrcFormat')
2382 ################################################################################
2384 class SrcUploader(object):
2385 def __init__(self, *args, **kwargs):
2389 return '<SrcUploader %s>' % self.uploader_id
2391 __all__.append('SrcUploader')
2393 ################################################################################
2395 SUITE_FIELDS = [ ('SuiteName', 'suite_name'),
2396 ('SuiteID', 'suite_id'),
2397 ('Version', 'version'),
2398 ('Origin', 'origin'),
2400 ('Description', 'description'),
2401 ('Untouchable', 'untouchable'),
2402 ('Announce', 'announce'),
2403 ('Codename', 'codename'),
2404 ('OverrideCodename', 'overridecodename'),
2405 ('ValidTime', 'validtime'),
2406 ('Priority', 'priority'),
2407 ('NotAutomatic', 'notautomatic'),
2408 ('CopyChanges', 'copychanges'),
2409 ('CopyDotDak', 'copydotdak'),
2410 ('CommentsDir', 'commentsdir'),
2411 ('OverrideSuite', 'overridesuite'),
2412 ('ChangelogBase', 'changelogbase')]
2415 class Suite(object):
2416 def __init__(self, *args, **kwargs):
2420 return '<Suite %s>' % self.suite_name
2422 def __eq__(self, val):
2423 if isinstance(val, str):
2424 return (self.suite_name == val)
2425 # This signals to use the normal comparison operator
2426 return NotImplemented
2428 def __ne__(self, val):
2429 if isinstance(val, str):
2430 return (self.suite_name != val)
2431 # This signals to use the normal comparison operator
2432 return NotImplemented
2436 for disp, field in SUITE_FIELDS:
2437 val = getattr(self, field, None)
2439 ret.append("%s: %s" % (disp, val))
2441 return "\n".join(ret)
2443 __all__.append('Suite')
2446 def get_suite_architecture(suite, architecture, session=None):
2448 Returns a SuiteArchitecture object given C{suite} and ${arch} or None if it
2452 @param suite: Suite name to search for
2454 @type architecture: str
2455 @param architecture: Architecture name to search for
2457 @type session: Session
2458 @param session: Optional SQL session object (a temporary one will be
2459 generated if not supplied)
2461 @rtype: SuiteArchitecture
2462 @return: the SuiteArchitecture object or None
2465 q = session.query(SuiteArchitecture)
2466 q = q.join(Architecture).filter_by(arch_string=architecture)
2467 q = q.join(Suite).filter_by(suite_name=suite)
2471 except NoResultFound:
2474 __all__.append('get_suite_architecture')
2477 def get_suite(suite, session=None):
2479 Returns Suite object for given C{suite name}.
2482 @param suite: The name of the suite
2484 @type session: Session
2485 @param session: Optional SQLA session object (a temporary one will be
2486 generated if not supplied)
2489 @return: Suite object for the requested suite name (None if not present)
2492 q = session.query(Suite).filter_by(suite_name=suite)
2496 except NoResultFound:
2499 __all__.append('get_suite')
2501 ################################################################################
2503 class SuiteArchitecture(object):
2504 def __init__(self, *args, **kwargs):
2508 return '<SuiteArchitecture (%s, %s)>' % (self.suite_id, self.arch_id)
2510 __all__.append('SuiteArchitecture')
2513 def get_suite_architectures(suite, skipsrc=False, skipall=False, session=None):
2515 Returns list of Architecture objects for given C{suite} name
2518 @param source: Suite name to search for
2520 @type skipsrc: boolean
2521 @param skipsrc: Whether to skip returning the 'source' architecture entry
2524 @type skipall: boolean
2525 @param skipall: Whether to skip returning the 'all' architecture entry
2528 @type session: Session
2529 @param session: Optional SQL session object (a temporary one will be
2530 generated if not supplied)
2533 @return: list of Architecture objects for the given name (may be empty)
2536 q = session.query(Architecture)
2537 q = q.join(SuiteArchitecture)
2538 q = q.join(Suite).filter_by(suite_name=suite)
2541 q = q.filter(Architecture.arch_string != 'source')
2544 q = q.filter(Architecture.arch_string != 'all')
2546 q = q.order_by('arch_string')
2550 __all__.append('get_suite_architectures')
2552 ################################################################################
2554 class SuiteSrcFormat(object):
2555 def __init__(self, *args, **kwargs):
2559 return '<SuiteSrcFormat (%s, %s)>' % (self.suite_id, self.src_format_id)
2561 __all__.append('SuiteSrcFormat')
2564 def get_suite_src_formats(suite, session=None):
2566 Returns list of allowed SrcFormat for C{suite}.
2569 @param suite: Suite name to search for
2571 @type session: Session
2572 @param session: Optional SQL session object (a temporary one will be
2573 generated if not supplied)
2576 @return: the list of allowed source formats for I{suite}
2579 q = session.query(SrcFormat)
2580 q = q.join(SuiteSrcFormat)
2581 q = q.join(Suite).filter_by(suite_name=suite)
2582 q = q.order_by('format_name')
2586 __all__.append('get_suite_src_formats')
2588 ################################################################################
2591 def __init__(self, *args, **kwargs):
2594 def __eq__(self, val):
2595 if isinstance(val, str):
2596 return (self.uid == val)
2597 # This signals to use the normal comparison operator
2598 return NotImplemented
2600 def __ne__(self, val):
2601 if isinstance(val, str):
2602 return (self.uid != val)
2603 # This signals to use the normal comparison operator
2604 return NotImplemented
2607 return '<Uid %s (%s)>' % (self.uid, self.name)
2609 __all__.append('Uid')
2612 def add_database_user(uidname, session=None):
2614 Adds a database user
2616 @type uidname: string
2617 @param uidname: The uid of the user to add
2619 @type session: SQLAlchemy
2620 @param session: Optional SQL session object (a temporary one will be
2621 generated if not supplied). If not passed, a commit will be performed at
2622 the end of the function, otherwise the caller is responsible for commiting.
2625 @return: the uid object for the given uidname
2628 session.execute("CREATE USER :uid", {'uid': uidname})
2629 session.commit_or_flush()
2631 __all__.append('add_database_user')
2634 def get_or_set_uid(uidname, session=None):
2636 Returns uid object for given uidname.
2638 If no matching uidname is found, a row is inserted.
2640 @type uidname: string
2641 @param uidname: The uid to add
2643 @type session: SQLAlchemy
2644 @param session: Optional SQL session object (a temporary one will be
2645 generated if not supplied). If not passed, a commit will be performed at
2646 the end of the function, otherwise the caller is responsible for commiting.
2649 @return: the uid object for the given uidname
2652 q = session.query(Uid).filter_by(uid=uidname)
2656 except NoResultFound:
2660 session.commit_or_flush()
2665 __all__.append('get_or_set_uid')
2668 def get_uid_from_fingerprint(fpr, session=None):
2669 q = session.query(Uid)
2670 q = q.join(Fingerprint).filter_by(fingerprint=fpr)
2674 except NoResultFound:
2677 __all__.append('get_uid_from_fingerprint')
2679 ################################################################################
2681 class UploadBlock(object):
2682 def __init__(self, *args, **kwargs):
2686 return '<UploadBlock %s (%s)>' % (self.source, self.upload_block_id)
2688 __all__.append('UploadBlock')
2690 ################################################################################
2692 class DBConn(object):
2694 database module init.
2698 def __init__(self, *args, **kwargs):
2699 self.__dict__ = self.__shared_state
2701 if not getattr(self, 'initialised', False):
2702 self.initialised = True
2703 self.debug = kwargs.has_key('debug')
2706 def __setuptables(self):
2715 'build_queue_files',
2718 'content_associations',
2719 'content_file_names',
2720 'content_file_paths',
2721 'changes_pending_binaries',
2722 'changes_pending_files',
2723 'changes_pending_files_map',
2724 'changes_pending_source',
2725 'changes_pending_source_files',
2726 'changes_pool_files',
2738 'pending_content_associations',
2748 'suite_architectures',
2749 'suite_src_formats',
2750 'suite_build_queue_copy',
2755 for table_name in tables:
2756 table = Table(table_name, self.db_meta, autoload=True)
2757 setattr(self, 'tbl_%s' % table_name, table)
2759 def __setupmappers(self):
2760 mapper(Architecture, self.tbl_architecture,
2761 properties = dict(arch_id = self.tbl_architecture.c.id))
2763 mapper(Archive, self.tbl_archive,
2764 properties = dict(archive_id = self.tbl_archive.c.id,
2765 archive_name = self.tbl_archive.c.name))
2767 mapper(BinAssociation, self.tbl_bin_associations,
2768 properties = dict(ba_id = self.tbl_bin_associations.c.id,
2769 suite_id = self.tbl_bin_associations.c.suite,
2770 suite = relation(Suite),
2771 binary_id = self.tbl_bin_associations.c.bin,
2772 binary = relation(DBBinary)))
2774 mapper(BuildQueue, self.tbl_build_queue,
2775 properties = dict(queue_id = self.tbl_build_queue.c.id))
2777 mapper(BuildQueueFile, self.tbl_build_queue_files,
2778 properties = dict(buildqueue = relation(BuildQueue, backref='queuefiles'),
2779 poolfile = relation(PoolFile, backref='buildqueueinstances')))
2781 mapper(DBBinary, self.tbl_binaries,
2782 properties = dict(binary_id = self.tbl_binaries.c.id,
2783 package = self.tbl_binaries.c.package,
2784 version = self.tbl_binaries.c.version,
2785 maintainer_id = self.tbl_binaries.c.maintainer,
2786 maintainer = relation(Maintainer),
2787 source_id = self.tbl_binaries.c.source,
2788 source = relation(DBSource),
2789 arch_id = self.tbl_binaries.c.architecture,
2790 architecture = relation(Architecture),
2791 poolfile_id = self.tbl_binaries.c.file,
2792 poolfile = relation(PoolFile),
2793 binarytype = self.tbl_binaries.c.type,
2794 fingerprint_id = self.tbl_binaries.c.sig_fpr,
2795 fingerprint = relation(Fingerprint),
2796 install_date = self.tbl_binaries.c.install_date,
2797 binassociations = relation(BinAssociation,
2798 primaryjoin=(self.tbl_binaries.c.id==self.tbl_bin_associations.c.bin))))
2800 mapper(BinaryACL, self.tbl_binary_acl,
2801 properties = dict(binary_acl_id = self.tbl_binary_acl.c.id))
2803 mapper(BinaryACLMap, self.tbl_binary_acl_map,
2804 properties = dict(binary_acl_map_id = self.tbl_binary_acl_map.c.id,
2805 fingerprint = relation(Fingerprint, backref="binary_acl_map"),
2806 architecture = relation(Architecture)))
2808 mapper(Component, self.tbl_component,
2809 properties = dict(component_id = self.tbl_component.c.id,
2810 component_name = self.tbl_component.c.name))
2812 mapper(DBConfig, self.tbl_config,
2813 properties = dict(config_id = self.tbl_config.c.id))
2815 mapper(DSCFile, self.tbl_dsc_files,
2816 properties = dict(dscfile_id = self.tbl_dsc_files.c.id,
2817 source_id = self.tbl_dsc_files.c.source,
2818 source = relation(DBSource),
2819 poolfile_id = self.tbl_dsc_files.c.file,
2820 poolfile = relation(PoolFile)))
2822 mapper(PoolFile, self.tbl_files,
2823 properties = dict(file_id = self.tbl_files.c.id,
2824 filesize = self.tbl_files.c.size,
2825 location_id = self.tbl_files.c.location,
2826 location = relation(Location)))
2828 mapper(Fingerprint, self.tbl_fingerprint,
2829 properties = dict(fingerprint_id = self.tbl_fingerprint.c.id,
2830 uid_id = self.tbl_fingerprint.c.uid,
2831 uid = relation(Uid),
2832 keyring_id = self.tbl_fingerprint.c.keyring,
2833 keyring = relation(Keyring),
2834 source_acl = relation(SourceACL),
2835 binary_acl = relation(BinaryACL)))
2837 mapper(Keyring, self.tbl_keyrings,
2838 properties = dict(keyring_name = self.tbl_keyrings.c.name,
2839 keyring_id = self.tbl_keyrings.c.id))
2841 mapper(DBChange, self.tbl_changes,
2842 properties = dict(change_id = self.tbl_changes.c.id,
2843 poolfiles = relation(PoolFile,
2844 secondary=self.tbl_changes_pool_files,
2845 backref="changeslinks"),
2846 seen = self.tbl_changes.c.seen,
2847 source = self.tbl_changes.c.source,
2848 binaries = self.tbl_changes.c.binaries,
2849 architecture = self.tbl_changes.c.architecture,
2850 distribution = self.tbl_changes.c.distribution,
2851 urgency = self.tbl_changes.c.urgency,
2852 maintainer = self.tbl_changes.c.maintainer,
2853 changedby = self.tbl_changes.c.changedby,
2854 date = self.tbl_changes.c.date,
2855 version = self.tbl_changes.c.version,
2856 files = relation(ChangePendingFile,
2857 secondary=self.tbl_changes_pending_files_map,
2858 backref="changesfile"),
2859 in_queue_id = self.tbl_changes.c.in_queue,
2860 in_queue = relation(PolicyQueue,
2861 primaryjoin=(self.tbl_changes.c.in_queue==self.tbl_policy_queue.c.id)),
2862 approved_for_id = self.tbl_changes.c.approved_for))
2864 mapper(ChangePendingBinary, self.tbl_changes_pending_binaries,
2865 properties = dict(change_pending_binary_id = self.tbl_changes_pending_binaries.c.id))
2867 mapper(ChangePendingFile, self.tbl_changes_pending_files,
2868 properties = dict(change_pending_file_id = self.tbl_changes_pending_files.c.id,
2869 filename = self.tbl_changes_pending_files.c.filename,
2870 size = self.tbl_changes_pending_files.c.size,
2871 md5sum = self.tbl_changes_pending_files.c.md5sum,
2872 sha1sum = self.tbl_changes_pending_files.c.sha1sum,
2873 sha256sum = self.tbl_changes_pending_files.c.sha256sum))
2875 mapper(ChangePendingSource, self.tbl_changes_pending_source,
2876 properties = dict(change_pending_source_id = self.tbl_changes_pending_source.c.id,
2877 change = relation(DBChange),
2878 maintainer = relation(Maintainer,
2879 primaryjoin=(self.tbl_changes_pending_source.c.maintainer_id==self.tbl_maintainer.c.id)),
2880 changedby = relation(Maintainer,
2881 primaryjoin=(self.tbl_changes_pending_source.c.changedby_id==self.tbl_maintainer.c.id)),
2882 fingerprint = relation(Fingerprint),
2883 source_files = relation(ChangePendingFile,
2884 secondary=self.tbl_changes_pending_source_files,
2885 backref="pending_sources")))
2886 mapper(KeyringACLMap, self.tbl_keyring_acl_map,
2887 properties = dict(keyring_acl_map_id = self.tbl_keyring_acl_map.c.id,
2888 keyring = relation(Keyring, backref="keyring_acl_map"),
2889 architecture = relation(Architecture)))
2891 mapper(Location, self.tbl_location,
2892 properties = dict(location_id = self.tbl_location.c.id,
2893 component_id = self.tbl_location.c.component,
2894 component = relation(Component),
2895 archive_id = self.tbl_location.c.archive,
2896 archive = relation(Archive),
2897 archive_type = self.tbl_location.c.type))
2899 mapper(Maintainer, self.tbl_maintainer,
2900 properties = dict(maintainer_id = self.tbl_maintainer.c.id))
2902 mapper(NewComment, self.tbl_new_comments,
2903 properties = dict(comment_id = self.tbl_new_comments.c.id))
2905 mapper(Override, self.tbl_override,
2906 properties = dict(suite_id = self.tbl_override.c.suite,
2907 suite = relation(Suite),
2908 component_id = self.tbl_override.c.component,
2909 component = relation(Component),
2910 priority_id = self.tbl_override.c.priority,
2911 priority = relation(Priority),
2912 section_id = self.tbl_override.c.section,
2913 section = relation(Section),
2914 overridetype_id = self.tbl_override.c.type,
2915 overridetype = relation(OverrideType)))
2917 mapper(OverrideType, self.tbl_override_type,
2918 properties = dict(overridetype = self.tbl_override_type.c.type,
2919 overridetype_id = self.tbl_override_type.c.id))
2921 mapper(PolicyQueue, self.tbl_policy_queue,
2922 properties = dict(policy_queue_id = self.tbl_policy_queue.c.id))
2924 mapper(Priority, self.tbl_priority,
2925 properties = dict(priority_id = self.tbl_priority.c.id))
2927 mapper(Section, self.tbl_section,
2928 properties = dict(section_id = self.tbl_section.c.id))
2930 mapper(DBSource, self.tbl_source,
2931 properties = dict(source_id = self.tbl_source.c.id,
2932 version = self.tbl_source.c.version,
2933 maintainer_id = self.tbl_source.c.maintainer,
2934 maintainer = relation(Maintainer,
2935 primaryjoin=(self.tbl_source.c.maintainer==self.tbl_maintainer.c.id)),
2936 poolfile_id = self.tbl_source.c.file,
2937 poolfile = relation(PoolFile),
2938 fingerprint_id = self.tbl_source.c.sig_fpr,
2939 fingerprint = relation(Fingerprint),
2940 changedby_id = self.tbl_source.c.changedby,
2941 changedby = relation(Maintainer,
2942 primaryjoin=(self.tbl_source.c.changedby==self.tbl_maintainer.c.id)),
2943 srcfiles = relation(DSCFile,
2944 primaryjoin=(self.tbl_source.c.id==self.tbl_dsc_files.c.source)),
2945 srcassociations = relation(SrcAssociation,
2946 primaryjoin=(self.tbl_source.c.id==self.tbl_src_associations.c.source)),
2947 srcuploaders = relation(SrcUploader)))
2949 mapper(SourceACL, self.tbl_source_acl,
2950 properties = dict(source_acl_id = self.tbl_source_acl.c.id))
2952 mapper(SrcAssociation, self.tbl_src_associations,
2953 properties = dict(sa_id = self.tbl_src_associations.c.id,
2954 suite_id = self.tbl_src_associations.c.suite,
2955 suite = relation(Suite),
2956 source_id = self.tbl_src_associations.c.source,
2957 source = relation(DBSource)))
2959 mapper(SrcFormat, self.tbl_src_format,
2960 properties = dict(src_format_id = self.tbl_src_format.c.id,
2961 format_name = self.tbl_src_format.c.format_name))
2963 mapper(SrcUploader, self.tbl_src_uploaders,
2964 properties = dict(uploader_id = self.tbl_src_uploaders.c.id,
2965 source_id = self.tbl_src_uploaders.c.source,
2966 source = relation(DBSource,
2967 primaryjoin=(self.tbl_src_uploaders.c.source==self.tbl_source.c.id)),
2968 maintainer_id = self.tbl_src_uploaders.c.maintainer,
2969 maintainer = relation(Maintainer,
2970 primaryjoin=(self.tbl_src_uploaders.c.maintainer==self.tbl_maintainer.c.id))))
2972 mapper(Suite, self.tbl_suite,
2973 properties = dict(suite_id = self.tbl_suite.c.id,
2974 policy_queue = relation(PolicyQueue),
2975 copy_queues = relation(BuildQueue, secondary=self.tbl_suite_build_queue_copy)))
2977 mapper(SuiteArchitecture, self.tbl_suite_architectures,
2978 properties = dict(suite_id = self.tbl_suite_architectures.c.suite,
2979 suite = relation(Suite, backref='suitearchitectures'),
2980 arch_id = self.tbl_suite_architectures.c.architecture,
2981 architecture = relation(Architecture)))
2983 mapper(SuiteSrcFormat, self.tbl_suite_src_formats,
2984 properties = dict(suite_id = self.tbl_suite_src_formats.c.suite,
2985 suite = relation(Suite, backref='suitesrcformats'),
2986 src_format_id = self.tbl_suite_src_formats.c.src_format,
2987 src_format = relation(SrcFormat)))
2989 mapper(Uid, self.tbl_uid,
2990 properties = dict(uid_id = self.tbl_uid.c.id,
2991 fingerprint = relation(Fingerprint)))
2993 mapper(UploadBlock, self.tbl_upload_blocks,
2994 properties = dict(upload_block_id = self.tbl_upload_blocks.c.id,
2995 fingerprint = relation(Fingerprint, backref="uploadblocks"),
2996 uid = relation(Uid, backref="uploadblocks")))
2998 ## Connection functions
2999 def __createconn(self):
3000 from config import Config
3004 connstr = "postgres://%s" % cnf["DB::Host"]
3005 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
3006 connstr += ":%s" % cnf["DB::Port"]
3007 connstr += "/%s" % cnf["DB::Name"]
3010 connstr = "postgres:///%s" % cnf["DB::Name"]
3011 if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
3012 connstr += "?port=%s" % cnf["DB::Port"]
3014 self.db_pg = create_engine(connstr, echo=self.debug)
3015 self.db_meta = MetaData()
3016 self.db_meta.bind = self.db_pg
3017 self.db_smaker = sessionmaker(bind=self.db_pg,
3021 self.__setuptables()
3022 self.__setupmappers()
3025 return self.db_smaker()
3027 __all__.append('DBConn')