dbconn.py 39.8 KB
Newer Older
1
#!/usr/bin/python
M
Mark Hymers 已提交
2

3 4 5 6 7 8
""" DB access class

@contact: Debian FTPMaster <ftpmaster@debian.org>
@copyright: 2000, 2001, 2002, 2003, 2004, 2006  James Troup <james@nocrew.org>
@copyright: 2008-2009  Mark Hymers <mhy@debian.org>
@copyright: 2009  Joerg Jaspert <joerg@debian.org>
9
@copyright: 2009  Mike O'Connor <stew@debian.org>
10 11
@license: GNU General Public License version 2 or later
"""
M
Mark Hymers 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

################################################################################

# < mhy> I need a funny comment
# < sgran> two peanuts were walking down a dark street
# < sgran> one was a-salted
#  * mhy looks up the definition of "funny"

################################################################################

36
import os
M
Mark Hymers 已提交
37
import psycopg2
38
import traceback
M
Mark Hymers 已提交
39

M
Mark Hymers 已提交
40
from sqlalchemy import create_engine, Table, MetaData, select
M
Mark Hymers 已提交
41
from sqlalchemy.orm import sessionmaker, mapper, relation
M
Mark Hymers 已提交
42

M
Mark Hymers 已提交
43 44 45
# Don't remove this, we re-export the exceptions to scripts which import us
from sqlalchemy.exc import *

46
from singleton import Singleton
M
Mark Hymers 已提交
47 48 49

################################################################################

M
Mark Hymers 已提交
50
__all__ = ['IntegrityError', 'SQLAlchemyError']
51 52 53

################################################################################

M
Mark Hymers 已提交
54
class Architecture(object):
M
Mark Hymers 已提交
55 56
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
57 58 59 60

    def __repr__(self):
        return '<Architecture %s>' % self.arch_string

61 62
__all__.append('Architecture')

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
def get_architecture(architecture, session=None):
    """
    Returns database id for given C{architecture}.

    @type architecture: string
    @param architecture: The name of the architecture

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: Architecture
    @return: Architecture object for the given arch (None if not present)

    """
    if session is None:
        session = DBConn().session()
    q = session.query(Architecture).filter_by(arch_string=architecture)
    if q.count() == 0:
        return None
    return q.one()

85 86
__all__.append('get_architecture')

M
Mark Hymers 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
def get_architecture_suites(architecture, session=None):
    """
    Returns list of Suite objects for given C{architecture} name

    @type source: str
    @param source: Architecture name 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 Suite objects for the given name (may be empty)
    """

    if session is None:
        session = DBConn().session()

    q = session.query(Suite)
    q = q.join(SuiteArchitecture)
    q = q.join(Architecture).filter_by(arch_string=architecture).order_by('suite_name')
    return q.all()

110 111
__all__.append('get_architecture_suites')

M
Mark Hymers 已提交
112 113
################################################################################

M
Mark Hymers 已提交
114
class Archive(object):
M
Mark Hymers 已提交
115 116
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
117 118 119 120

    def __repr__(self):
        return '<Archive %s>' % self.name

121 122
__all__.append('Archive')

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
def get_archive(archive, session=None):
    """
    returns database id for given c{archive}.

    @type archive: string
    @param archive: the name of the arhive

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: Archive
    @return: Archive object for the given name (None if not present)

    """
    archive = archive.lower()
    if session is None:
        session = DBConn().session()
    q = session.query(Archive).filter_by(archive_name=archive)
    if q.count() == 0:
        return None
    return q.one()

146
__all__.append('get_archive')
147

M
Mark Hymers 已提交
148 149
################################################################################

M
Mark Hymers 已提交
150
class BinAssociation(object):
M
Mark Hymers 已提交
151 152
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
153 154

    def __repr__(self):
M
Mark Hymers 已提交
155
        return '<BinAssociation %s (%s, %s)>' % (self.ba_id, self.binary, self.suite)
M
Mark Hymers 已提交
156

157 158
__all__.append('BinAssociation')

M
Mark Hymers 已提交
159 160
################################################################################

161
class DBBinary(object):
M
Mark Hymers 已提交
162 163
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
164 165

    def __repr__(self):
166
        return '<DBBinary %s (%s, %s)>' % (self.package, self.version, self.architecture)
M
Mark Hymers 已提交
167

168
__all__.append('DBBinary')
169

170 171
def get_binary_from_id(id, session=None):
    """
172
    Returns DBBinary object for given C{id}
173 174 175 176 177 178 179 180

    @type id: int
    @param id: Id of the required binary

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

181 182
    @rtype: DBBinary
    @return: DBBinary object for the given binary (None if not present)
183 184 185
    """
    if session is None:
        session = DBConn().session()
186
    q = session.query(DBBinary).filter_by(binary_id=id)
187 188 189
    if q.count() == 0:
        return None
    return q.one()
M
Mark Hymers 已提交
190

191 192
__all__.append('get_binary_from_id')

M
Mark Hymers 已提交
193 194
def get_binaries_from_name(package, session=None):
    """
195
    Returns list of DBBinary objects for given C{package} name
M
Mark Hymers 已提交
196 197

    @type package: str
198
    @param package: DBBinary package name to search for
M
Mark Hymers 已提交
199 200 201 202 203 204

    @type session: Session
    @param session: Optional SQL session object (a temporary one will be
    generated if not supplied)

    @rtype: list
205
    @return: list of DBBinary objects for the given name (may be empty)
M
Mark Hymers 已提交
206 207 208
    """
    if session is None:
        session = DBConn().session()
209
    return session.query(DBBinary).filter_by(package=package).all()
M
Mark Hymers 已提交
210

211 212
__all__.append('get_binaries_from_name')

M
Mark Hymers 已提交
213 214
################################################################################

M
Mark Hymers 已提交
215
class Component(object):
M
Mark Hymers 已提交
216 217
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
218 219 220 221

    def __repr__(self):
        return '<Component %s>' % self.component_name

222 223 224

__all__.append('Component')

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
def get_component(component, session=None):
    """
    Returns database id for given C{component}.

    @type component: string
    @param component: The name of the override type

    @rtype: int
    @return: the database id for the given component

    """
    component = component.lower()
    if session is None:
        session = DBConn().session()
    q = session.query(Component).filter_by(component_name=component)
    if q.count() == 0:
        return None
    return q.one()

244 245
__all__.append('get_component')

M
Mark Hymers 已提交
246 247
################################################################################

M
Mark Hymers 已提交
248
class DBConfig(object):
M
Mark Hymers 已提交
249 250
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
251 252 253 254

    def __repr__(self):
        return '<DBConfig %s>' % self.name

255 256
__all__.append('DBConfig')

M
Mark Hymers 已提交
257 258
################################################################################

M
Mark Hymers 已提交
259
class ContentFilename(object):
M
Mark Hymers 已提交
260 261
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
262 263 264 265

    def __repr__(self):
        return '<ContentFilename %s>' % self.filename

266 267
__all__.append('ContentFilename')

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
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)

    @rtype: int
    @return: the database id for the given component
    """
    if session is None:
        session = DBConn().session()

    try:
        q = session.query(ContentFilename).filter_by(filename=filename)
        if q.count() < 1:
            cf = ContentFilename()
            cf.filename = filename
            session.add(cf)
            return cf.cafilename_id
        else:
            return q.one().cafilename_id

    except:
        traceback.print_exc()
        raise

__all__.append('get_or_set_contents_file_id')

M
Mark Hymers 已提交
302 303
################################################################################

M
Mark Hymers 已提交
304
class ContentFilepath(object):
M
Mark Hymers 已提交
305 306
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
307 308 309 310

    def __repr__(self):
        return '<ContentFilepath %s>' % self.filepath

311 312
__all__.append('ContentFilepath')

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
def get_or_set_contents_path_id(filepath, session):
    """
    Returns database id for given path.

    If no matching file is found, a row is inserted.

    @type filename: string
    @param filename: The filepath
    @type session: SQLAlchemy
    @param session: Optional SQL session object (a temporary one will be
    generated if not supplied)

    @rtype: int
    @return: the database id for the given path
    """
    if session is None:
        session = DBConn().session()

    try:
        q = session.query(ContentFilepath).filter_by(filepath=filepath)
        if q.count() < 1:
            cf = ContentFilepath()
            cf.filepath = filepath
            session.add(cf)
            return cf.cafilepath_id
        else:
            return q.one().cafilepath_id

    except:
        traceback.print_exc()
        raise

__all__.append('get_or_set_contents_path_id')

M
Mark Hymers 已提交
347 348
################################################################################

349
class ContentAssociation(object):
M
Mark Hymers 已提交
350 351
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
352 353 354 355

    def __repr__(self):
        return '<ContentAssociation %s>' % self.ca_id

356 357
__all__.append('ContentAssociation')

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
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

    @return: True upon success
    """

    privatetrans = False

    if session is None:
        session = DBConn().session()
        privatetrans = True

    try:
        for fullpath in fullpaths:
            (path, file) = os.path.split(fullpath)

            # Get the necessary IDs ...
            ca = ContentAssociation()
            ca.binary_id = binary_id
            ca.filename_id = get_or_set_contents_file_id(file)
            ca.filepath_id = get_or_set_contents_path_id(path)
            session.add(ca)

        # Only commit if we set up the session ourself
        if privatetrans:
            session.commit()

        return True
    except:
        traceback.print_exc()

        # Only rollback if we set up the session ourself
        if privatetrans:
            session.rollback()

        return False

__all__.append('insert_content_paths')

M
Mark Hymers 已提交
408 409
################################################################################

M
Mark Hymers 已提交
410
class DSCFile(object):
M
Mark Hymers 已提交
411 412
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
413 414 415 416

    def __repr__(self):
        return '<DSCFile %s>' % self.dscfile_id

417 418
__all__.append('DSCFile')

M
Mark Hymers 已提交
419 420
################################################################################

M
Mark Hymers 已提交
421
class PoolFile(object):
M
Mark Hymers 已提交
422 423
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
424 425 426 427

    def __repr__(self):
        return '<PoolFile %s>' % self.filename

428 429
__all__.append('PoolFile')

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
def get_poolfile_by_name(filename, location_id=None, session=None):
    """
    Returns an array of PoolFile objects for the given filename and
    (optionally) location_id

    @type filename: string
    @param filename: the filename of the file to check against the DB

    @type location_id: int
    @param location_id: the id of the location to look in (optional)

    @rtype: array
    @return: array of PoolFile objects
    """

    if session is not None:
        session = DBConn().session()

    q = session.query(PoolFile).filter_by(filename=filename)

    if location_id is not None:
        q = q.join(Location).filter_by(location_id=location_id)

    return q.all()

__all__.append('get_poolfile_by_name')

M
Mark Hymers 已提交
457 458
################################################################################

M
Mark Hymers 已提交
459
class Fingerprint(object):
M
Mark Hymers 已提交
460 461
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
462 463 464 465

    def __repr__(self):
        return '<Fingerprint %s>' % self.fingerprint

466 467
__all__.append('Fingerprint')

M
Mark Hymers 已提交
468 469
################################################################################

M
Mark Hymers 已提交
470
class Keyring(object):
M
Mark Hymers 已提交
471 472
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
473 474 475 476

    def __repr__(self):
        return '<Keyring %s>' % self.keyring_name

477 478
__all__.append('Keyring')

M
Mark Hymers 已提交
479 480
################################################################################

M
Mark Hymers 已提交
481
class Location(object):
M
Mark Hymers 已提交
482 483
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
484 485 486 487

    def __repr__(self):
        return '<Location %s (%s)>' % (self.path, self.location_id)

488 489
__all__.append('Location')

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
def get_location(location, component=None, archive=None, session=None):
    """
    Returns Location object for the given combination of location, component
    and archive

    @type location: string
    @param location: the path of the location, e.g. I{/srv/ftp.debian.org/ftp/pool/}

    @type component: string
    @param component: the component name (if None, no restriction applied)

    @type archive: string
    @param archive_id: the archive name (if None, no restriction applied)

    @rtype: Location / None
    @return: Either a Location object or None if one can't be found
    """

    if session is None:
        session = DBConn().session()

    q = session.query(Location).filter_by(path=location)

    if archive is not None:
        q = q.join(Archive).filter_by(archive_name=archive)

    if component is not None:
        q = q.join(Component).filter_by(component_name=component)

    if q.count() < 1:
        return None
    else:
        return q.one()

__all__.append('get_location')

M
Mark Hymers 已提交
526 527
################################################################################

M
Mark Hymers 已提交
528
class Maintainer(object):
M
Mark Hymers 已提交
529 530
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
531 532 533 534

    def __repr__(self):
        return '''<Maintainer '%s' (%s)>''' % (self.name, self.maintainer_id)

535 536
__all__.append('Maintainer')

M
Mark Hymers 已提交
537 538
################################################################################

M
Mark Hymers 已提交
539
class Override(object):
M
Mark Hymers 已提交
540 541
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
542 543 544 545

    def __repr__(self):
        return '<Override %s (%s)>' % (self.package, self.suite_id)

546 547
__all__.append('Override')

M
Mark Hymers 已提交
548 549
################################################################################

M
Mark Hymers 已提交
550
class OverrideType(object):
M
Mark Hymers 已提交
551 552
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
553 554 555 556

    def __repr__(self):
        return '<OverrideType %s>' % self.overridetype

557 558
__all__.append('OverrideType')

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
def get_override_type(override_type, session=None):
    """
    Returns OverrideType object for given C{override type}.

    @type override_type: string
    @param override_type: The name of the override type

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: int
    @return: the database id for the given override type

    """
    if session is None:
        session = DBConn().session()
    q = session.query(Priority).filter_by(priority=priority)
    if q.count() == 0:
        return None
    return q.one()

581 582
__all__.append('get_override_type')

M
Mark Hymers 已提交
583 584
################################################################################

M
Mark Hymers 已提交
585
class PendingContentAssociation(object):
M
Mark Hymers 已提交
586 587
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
588 589 590 591

    def __repr__(self):
        return '<PendingContentAssociation %s>' % self.pca_id

592 593
__all__.append('PendingContentAssociation')

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
def insert_pending_content_paths(package, fullpaths, session=None):
    """
    Make sure given paths are temporarily associated with given
    package

    @type package: dict
    @param package: the package to associate with should have been read in from the binary control file
    @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

    @return: True upon success, False if there is a problem
    """

    privatetrans = False

    if session is None:
        session = DBConn().session()
        privatetrans = True

    try:
        arch = get_architecture(package['Architecture'], session)
        arch_id = arch.arch_id

        # Remove any already existing recorded files for this package
        q = session.query(PendingContentAssociation)
        q = q.filter_by(package=package['Package'])
        q = q.filter_by(version=package['Version'])
        q = q.filter_by(architecture=arch_id)
        q.delete()

        # Insert paths
        for fullpath in fullpaths:
            (path, file) = os.path.split(fullpath)

            if path.startswith( "./" ):
                path = path[2:]

            pca = PendingContentAssociation()
            pca.package = package['Package']
            pca.version = package['Version']
            pca.filename_id = get_or_set_contents_file_id(file, session)
            pca.filepath_id = get_or_set_contents_path_id(path, session)
            pca.architecture = arch_id
            session.add(pca)

        # Only commit if we set up the session ourself
        if privatetrans:
            session.commit()

        return True
    except:
        traceback.print_exc()

        # Only rollback if we set up the session ourself
        if privatetrans:
            session.rollback()

        return False

__all__.append('insert_pending_content_paths')

M
Mark Hymers 已提交
660 661
################################################################################

M
Mark Hymers 已提交
662
class Priority(object):
M
Mark Hymers 已提交
663 664
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
665 666 667 668

    def __repr__(self):
        return '<Priority %s (%s)>' % (self.priority, self.priority_id)

669 670
__all__.append('Priority')

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
def get_priority(priority, session=None):
    """
    Returns Priority object for given C{priority name}.

    @type priority: string
    @param priority: The name of the priority

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: Priority
    @return: Priority object for the given priority

    """
    if session is None:
        session = DBConn().session()
    q = session.query(Priority).filter_by(priority=priority)
    if q.count() == 0:
        return None
    return q.one()

693 694
__all__.append('get_priority')

M
Mark Hymers 已提交
695 696
################################################################################

M
Mark Hymers 已提交
697
class Queue(object):
M
Mark Hymers 已提交
698 699
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
700 701 702 703

    def __repr__(self):
        return '<Queue %s>' % self.queue_name

704 705
__all__.append('Queue')

M
Mark Hymers 已提交
706 707
################################################################################

M
Mark Hymers 已提交
708
class QueueBuild(object):
M
Mark Hymers 已提交
709 710
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
711 712 713 714

    def __repr__(self):
        return '<QueueBuild %s (%s)>' % (self.filename, self.queue_id)

715 716
__all__.append('QueueBuild')

M
Mark Hymers 已提交
717 718
################################################################################

M
Mark Hymers 已提交
719
class Section(object):
M
Mark Hymers 已提交
720 721
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
722 723 724 725

    def __repr__(self):
        return '<Section %s>' % self.section

726 727
__all__.append('Section')

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
def get_section(section, session=None):
    """
    Returns Section object for given C{section name}.

    @type section: string
    @param section: The name of the section

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: Section
    @return: Section object for the given section name

    """
    if session is None:
        session = DBConn().session()
    q = session.query(Section).filter_by(section=section)
    if q.count() == 0:
        return None
    return q.one()

750 751
__all__.append('get_section')

M
Mark Hymers 已提交
752 753
################################################################################

754
class DBSource(object):
M
Mark Hymers 已提交
755 756
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
757 758

    def __repr__(self):
759
        return '<DBSource %s (%s)>' % (self.source, self.version)
M
Mark Hymers 已提交
760

761
__all__.append('DBSource')
762

M
Mark Hymers 已提交
763 764
def get_sources_from_name(source, session=None):
    """
765
    Returns list of DBSource objects for given C{source} name
M
Mark Hymers 已提交
766 767

    @type source: str
768
    @param source: DBSource package name to search for
M
Mark Hymers 已提交
769 770 771 772 773 774

    @type session: Session
    @param session: Optional SQL session object (a temporary one will be
    generated if not supplied)

    @rtype: list
775
    @return: list of DBSource objects for the given name (may be empty)
M
Mark Hymers 已提交
776 777 778
    """
    if session is None:
        session = DBConn().session()
779
    return session.query(DBSource).filter_by(source=source).all()
M
Mark Hymers 已提交
780

781 782
__all__.append('get_sources_from_name')

783 784
def get_source_in_suite(source, suite, session=None):
    """
785
    Returns list of DBSource objects for a combination of C{source} and C{suite}.
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801

      - B{source} - source package name, eg. I{mailfilter}, I{bbdb}, I{glibc}
      - B{suite} - a suite name, eg. I{unstable}

    @type source: string
    @param source: source package name

    @type suite: string
    @param suite: the suite name

    @rtype: string
    @return: the version for I{source} in I{suite}

    """
    if session is None:
        session = DBConn().session()
M
updates  
Mark Hymers 已提交
802 803 804
    q = session.query(SrcAssociation)
    q = q.join('source').filter_by(source=source)
    q = q.join('suite').filter_by(suite_name=suite)
805 806
    if q.count() == 0:
        return None
M
updates  
Mark Hymers 已提交
807 808
    # ???: Maybe we should just return the SrcAssociation object instead
    return q.one().source
809

810 811
__all__.append('get_source_in_suite')

M
Mark Hymers 已提交
812 813
################################################################################

M
Mark Hymers 已提交
814
class SrcAssociation(object):
M
Mark Hymers 已提交
815 816
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
817 818

    def __repr__(self):
M
Mark Hymers 已提交
819
        return '<SrcAssociation %s (%s, %s)>' % (self.sa_id, self.source, self.suite)
M
Mark Hymers 已提交
820

821 822
__all__.append('SrcAssociation')

M
Mark Hymers 已提交
823 824
################################################################################

M
Mark Hymers 已提交
825
class SrcUploader(object):
M
Mark Hymers 已提交
826 827
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
828 829 830 831

    def __repr__(self):
        return '<SrcUploader %s>' % self.uploader_id

832 833
__all__.append('SrcUploader')

M
Mark Hymers 已提交
834 835
################################################################################

M
Mark Hymers 已提交
836
class Suite(object):
M
Mark Hymers 已提交
837 838
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
839 840 841 842

    def __repr__(self):
        return '<Suite %s>' % self.suite_name

843 844
__all__.append('Suite')

M
Mark Hymers 已提交
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
def get_suite_architecture(suite, architecture, session=None):
    """
    Returns a SuiteArchitecture object given C{suite} and ${arch} or None if it
    doesn't exist

    @type suite: str
    @param suite: Suite name to search for

    @type architecture: str
    @param architecture: Architecture name to search for

    @type session: Session
    @param session: Optional SQL session object (a temporary one will be
    generated if not supplied)

    @rtype: SuiteArchitecture
    @return: the SuiteArchitecture object or None
    """

    if session is None:
        session = DBConn().session()

    q = session.query(SuiteArchitecture)
    q = q.join(Architecture).filter_by(arch_string=architecture)
    q = q.join(Suite).filter_by(suite_name=suite)
    if q.count() == 0:
        return None
    return q.one()

874
__all__.append('get_suite_architecture')
M
Mark Hymers 已提交
875

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
def get_suite(suite, session=None):
    """
    Returns Suite object for given C{suite name}.

    @type suite: string
    @param suite: The name of the suite

    @type session: Session
    @param session: Optional SQLA session object (a temporary one will be
    generated if not supplied)

    @rtype: Suite
    @return: Suite object for the requested suite name (None if not presenT)

    """
    if session is None:
        session = DBConn().session()
    q = session.query(Suite).filter_by(suite_name=suite)
    if q.count() == 0:
        return None
    return q.one()

898 899
__all__.append('get_suite')

M
Mark Hymers 已提交
900 901
################################################################################

M
Mark Hymers 已提交
902
class SuiteArchitecture(object):
M
Mark Hymers 已提交
903 904
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
905 906 907 908

    def __repr__(self):
        return '<SuiteArchitecture (%s, %s)>' % (self.suite_id, self.arch_id)

909 910
__all__.append('SuiteArchitecture')

911
def get_suite_architectures(suite, skipsrc=False, skipall=False, session=None):
M
Mark Hymers 已提交
912 913 914 915 916 917
    """
    Returns list of Architecture objects for given C{suite} name

    @type source: str
    @param source: Suite name to search for

918 919 920 921 922 923 924 925
    @type skipsrc: boolean
    @param skipsrc: Whether to skip returning the 'source' architecture entry
    (Default False)

    @type skipall: boolean
    @param skipall: Whether to skip returning the 'all' architecture entry
    (Default False)

M
Mark Hymers 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938
    @type session: Session
    @param session: Optional SQL session object (a temporary one will be
    generated if not supplied)

    @rtype: list
    @return: list of Architecture objects for the given name (may be empty)
    """

    if session is None:
        session = DBConn().session()

    q = session.query(Architecture)
    q = q.join(SuiteArchitecture)
939 940 941 942 943 944
    q = q.join(Suite).filter_by(suite_name=suite)
    if skipsrc:
        q = q.filter(Architecture.arch_string != 'source')
    if skipall:
        q = q.filter(Architecture.arch_string != 'all')
    q = q.order_by('arch_string')
M
Mark Hymers 已提交
945 946
    return q.all()

947
__all__.append('get_suite_architectures')
M
Mark Hymers 已提交
948

M
Mark Hymers 已提交
949 950
################################################################################

M
Mark Hymers 已提交
951
class Uid(object):
M
Mark Hymers 已提交
952 953
    def __init__(self, *args, **kwargs):
        pass
M
Mark Hymers 已提交
954 955 956 957

    def __repr__(self):
        return '<Uid %s (%s)>' % (self.uid, self.name)

958 959
__all__.append('Uid')

M
Mark Hymers 已提交
960 961
################################################################################

M
Mark Hymers 已提交
962 963
class DBConn(Singleton):
    """
964
    database module init.
M
Mark Hymers 已提交
965 966 967 968 969
    """
    def __init__(self, *args, **kwargs):
        super(DBConn, self).__init__(*args, **kwargs)

    def _startup(self, *args, **kwargs):
M
updates  
Mark Hymers 已提交
970 971 972
        self.debug = False
        if kwargs.has_key('debug'):
            self.debug = True
M
Mark Hymers 已提交
973 974
        self.__createconn()

M
Mark Hymers 已提交
975 976
    def __setuptables(self):
        self.tbl_architecture = Table('architecture', self.db_meta, autoload=True)
M
Mark Hymers 已提交
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
        self.tbl_archive = Table('archive', self.db_meta, autoload=True)
        self.tbl_bin_associations = Table('bin_associations', self.db_meta, autoload=True)
        self.tbl_binaries = Table('binaries', self.db_meta, autoload=True)
        self.tbl_component = Table('component', self.db_meta, autoload=True)
        self.tbl_config = Table('config', self.db_meta, autoload=True)
        self.tbl_content_associations = Table('content_associations', self.db_meta, autoload=True)
        self.tbl_content_file_names = Table('content_file_names', self.db_meta, autoload=True)
        self.tbl_content_file_paths = Table('content_file_paths', self.db_meta, autoload=True)
        self.tbl_dsc_files = Table('dsc_files', self.db_meta, autoload=True)
        self.tbl_files = Table('files', self.db_meta, autoload=True)
        self.tbl_fingerprint = Table('fingerprint', self.db_meta, autoload=True)
        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_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)
        self.tbl_priority = Table('priority', self.db_meta, autoload=True)
        self.tbl_queue = Table('queue', self.db_meta, autoload=True)
        self.tbl_queue_build = Table('queue_build', self.db_meta, autoload=True)
        self.tbl_section = Table('section', self.db_meta, autoload=True)
        self.tbl_source = Table('source', self.db_meta, autoload=True)
        self.tbl_src_associations = Table('src_associations', self.db_meta, autoload=True)
        self.tbl_src_uploaders = Table('src_uploaders', self.db_meta, autoload=True)
        self.tbl_suite = Table('suite', self.db_meta, autoload=True)
        self.tbl_suite_architectures = Table('suite_architectures', self.db_meta, autoload=True)
        self.tbl_uid = Table('uid', self.db_meta, autoload=True)

    def __setupmappers(self):
M
Mark Hymers 已提交
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
        mapper(Architecture, self.tbl_architecture,
               properties = dict(arch_id = self.tbl_architecture.c.id))

        mapper(Archive, self.tbl_archive,
               properties = dict(archive_id = self.tbl_archive.c.id,
                                 archive_name = self.tbl_archive.c.name))

        mapper(BinAssociation, self.tbl_bin_associations,
               properties = dict(ba_id = self.tbl_bin_associations.c.id,
                                 suite_id = self.tbl_bin_associations.c.suite,
M
Mark Hymers 已提交
1016 1017
                                 suite = relation(Suite),
                                 binary_id = self.tbl_bin_associations.c.bin,
1018
                                 binary = relation(DBBinary)))
M
Mark Hymers 已提交
1019

1020
        mapper(DBBinary, self.tbl_binaries,
M
Mark Hymers 已提交
1021
               properties = dict(binary_id = self.tbl_binaries.c.id,
M
Mark Hymers 已提交
1022 1023
                                 package = self.tbl_binaries.c.package,
                                 version = self.tbl_binaries.c.version,
M
Mark Hymers 已提交
1024
                                 maintainer_id = self.tbl_binaries.c.maintainer,
M
Mark Hymers 已提交
1025
                                 maintainer = relation(Maintainer),
M
Mark Hymers 已提交
1026
                                 source_id = self.tbl_binaries.c.source,
1027
                                 source = relation(DBSource),
M
Mark Hymers 已提交
1028
                                 arch_id = self.tbl_binaries.c.architecture,
M
Mark Hymers 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037
                                 architecture = relation(Architecture),
                                 poolfile_id = self.tbl_binaries.c.file,
                                 poolfile = relation(PoolFile),
                                 binarytype = self.tbl_binaries.c.type,
                                 fingerprint_id = self.tbl_binaries.c.sig_fpr,
                                 fingerprint = relation(Fingerprint),
                                 install_date = self.tbl_binaries.c.install_date,
                                 binassociations = relation(BinAssociation,
                                                            primaryjoin=(self.tbl_binaries.c.id==self.tbl_bin_associations.c.bin))))
M
Mark Hymers 已提交
1038 1039 1040 1041 1042 1043 1044 1045

        mapper(Component, self.tbl_component,
               properties = dict(component_id = self.tbl_component.c.id,
                                 component_name = self.tbl_component.c.name))

        mapper(DBConfig, self.tbl_config,
               properties = dict(config_id = self.tbl_config.c.id))

1046
        mapper(ContentAssociation, self.tbl_content_associations,
M
Mark Hymers 已提交
1047 1048
               properties = dict(ca_id = self.tbl_content_associations.c.id,
                                 filename_id = self.tbl_content_associations.c.filename,
M
Mark Hymers 已提交
1049
                                 filename    = relation(ContentFilename),
M
Mark Hymers 已提交
1050
                                 filepath_id = self.tbl_content_associations.c.filepath,
M
Mark Hymers 已提交
1051 1052
                                 filepath    = relation(ContentFilepath),
                                 binary_id   = self.tbl_content_associations.c.binary_pkg,
1053
                                 binary      = relation(DBBinary)))
M
Mark Hymers 已提交
1054

M
Mark Hymers 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

        mapper(ContentFilename, self.tbl_content_file_names,
               properties = dict(cafilename_id = self.tbl_content_file_names.c.id,
                                 filename = self.tbl_content_file_names.c.file))

        mapper(ContentFilepath, self.tbl_content_file_paths,
               properties = dict(cafilepath_id = self.tbl_content_file_paths.c.id,
                                 filepath = self.tbl_content_file_paths.c.path))

        mapper(DSCFile, self.tbl_dsc_files,
               properties = dict(dscfile_id = self.tbl_dsc_files.c.id,
                                 source_id = self.tbl_dsc_files.c.source,
1067
                                 source = relation(DBSource),
M
Mark Hymers 已提交
1068 1069
                                 poolfile_id = self.tbl_dsc_files.c.file,
                                 poolfile = relation(PoolFile)))
M
Mark Hymers 已提交
1070 1071 1072 1073

        mapper(PoolFile, self.tbl_files,
               properties = dict(file_id = self.tbl_files.c.id,
                                 filesize = self.tbl_files.c.size,
M
Mark Hymers 已提交
1074 1075
                                 location_id = self.tbl_files.c.location,
                                 location = relation(Location)))
M
Mark Hymers 已提交
1076 1077 1078 1079

        mapper(Fingerprint, self.tbl_fingerprint,
               properties = dict(fingerprint_id = self.tbl_fingerprint.c.id,
                                 uid_id = self.tbl_fingerprint.c.uid,
M
Mark Hymers 已提交
1080 1081 1082
                                 uid = relation(Uid),
                                 keyring_id = self.tbl_fingerprint.c.keyring,
                                 keyring = relation(Keyring)))
M
Mark Hymers 已提交
1083 1084 1085 1086 1087 1088 1089 1090

        mapper(Keyring, self.tbl_keyrings,
               properties = dict(keyring_name = self.tbl_keyrings.c.name,
                                 keyring_id = self.tbl_keyrings.c.id))

        mapper(Location, self.tbl_location,
               properties = dict(location_id = self.tbl_location.c.id,
                                 component_id = self.tbl_location.c.component,
M
Mark Hymers 已提交
1091
                                 component = relation(Component),
M
Mark Hymers 已提交
1092
                                 archive_id = self.tbl_location.c.archive,
M
Mark Hymers 已提交
1093
                                 archive = relation(Archive),
M
Mark Hymers 已提交
1094 1095 1096 1097 1098 1099 1100
                                 archive_type = self.tbl_location.c.type))

        mapper(Maintainer, self.tbl_maintainer,
               properties = dict(maintainer_id = self.tbl_maintainer.c.id))

        mapper(Override, self.tbl_override,
               properties = dict(suite_id = self.tbl_override.c.suite,
M
Mark Hymers 已提交
1101
                                 suite = relation(Suite),
M
Mark Hymers 已提交
1102
                                 component_id = self.tbl_override.c.component,
M
Mark Hymers 已提交
1103
                                 component = relation(Component),
M
Mark Hymers 已提交
1104
                                 priority_id = self.tbl_override.c.priority,
M
Mark Hymers 已提交
1105
                                 priority = relation(Priority),
M
Mark Hymers 已提交
1106
                                 section_id = self.tbl_override.c.section,
M
Mark Hymers 已提交
1107 1108 1109
                                 section = relation(Section),
                                 overridetype_id = self.tbl_override.c.type,
                                 overridetype = relation(OverrideType)))
M
Mark Hymers 已提交
1110 1111 1112 1113 1114 1115 1116 1117

        mapper(OverrideType, self.tbl_override_type,
               properties = dict(overridetype = self.tbl_override_type.c.type,
                                 overridetype_id = self.tbl_override_type.c.id))

        mapper(PendingContentAssociation, self.tbl_pending_content_associations,
               properties = dict(pca_id = self.tbl_pending_content_associations.c.id,
                                 filepath_id = self.tbl_pending_content_associations.c.filepath,
M
Mark Hymers 已提交
1118 1119 1120
                                 filepath = relation(ContentFilepath),
                                 filename_id = self.tbl_pending_content_associations.c.filename,
                                 filename = relation(ContentFilename)))
M
Mark Hymers 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129

        mapper(Priority, self.tbl_priority,
               properties = dict(priority_id = self.tbl_priority.c.id))

        mapper(Queue, self.tbl_queue,
               properties = dict(queue_id = self.tbl_queue.c.id))

        mapper(QueueBuild, self.tbl_queue_build,
               properties = dict(suite_id = self.tbl_queue_build.c.suite,
M
Mark Hymers 已提交
1130 1131
                                 queue_id = self.tbl_queue_build.c.queue,
                                 queue = relation(Queue)))
M
Mark Hymers 已提交
1132 1133 1134 1135

        mapper(Section, self.tbl_section,
               properties = dict(section_id = self.tbl_section.c.id))

1136
        mapper(DBSource, self.tbl_source,
M
Mark Hymers 已提交
1137
               properties = dict(source_id = self.tbl_source.c.id,
M
Mark Hymers 已提交
1138
                                 version = self.tbl_source.c.version,
M
Mark Hymers 已提交
1139
                                 maintainer_id = self.tbl_source.c.maintainer,
M
Mark Hymers 已提交
1140 1141 1142 1143
                                 maintainer = relation(Maintainer,
                                                       primaryjoin=(self.tbl_source.c.maintainer==self.tbl_maintainer.c.id)),
                                 poolfile_id = self.tbl_source.c.file,
                                 poolfile = relation(PoolFile),
M
Mark Hymers 已提交
1144
                                 fingerprint_id = self.tbl_source.c.sig_fpr,
M
Mark Hymers 已提交
1145 1146 1147 1148 1149 1150 1151 1152
                                 fingerprint = relation(Fingerprint),
                                 changedby_id = self.tbl_source.c.changedby,
                                 changedby = relation(Maintainer,
                                                      primaryjoin=(self.tbl_source.c.changedby==self.tbl_maintainer.c.id)),
                                 srcfiles = relation(DSCFile,
                                                     primaryjoin=(self.tbl_source.c.id==self.tbl_dsc_files.c.source)),
                                 srcassociations = relation(SrcAssociation,
                                                            primaryjoin=(self.tbl_source.c.id==self.tbl_src_associations.c.source))))
M
Mark Hymers 已提交
1153 1154

        mapper(SrcAssociation, self.tbl_src_associations,
M
Mark Hymers 已提交
1155 1156 1157 1158
               properties = dict(sa_id = self.tbl_src_associations.c.id,
                                 suite_id = self.tbl_src_associations.c.suite,
                                 suite = relation(Suite),
                                 source_id = self.tbl_src_associations.c.source,
1159
                                 source = relation(DBSource)))
M
Mark Hymers 已提交
1160 1161 1162 1163

        mapper(SrcUploader, self.tbl_src_uploaders,
               properties = dict(uploader_id = self.tbl_src_uploaders.c.id,
                                 source_id = self.tbl_src_uploaders.c.source,
1164
                                 source = relation(DBSource,
M
Mark Hymers 已提交
1165 1166 1167 1168
                                                   primaryjoin=(self.tbl_src_uploaders.c.source==self.tbl_source.c.id)),
                                 maintainer_id = self.tbl_src_uploaders.c.maintainer,
                                 maintainer = relation(Maintainer,
                                                       primaryjoin=(self.tbl_src_uploaders.c.maintainer==self.tbl_maintainer.c.id))))
M
Mark Hymers 已提交
1169 1170 1171 1172 1173 1174

        mapper(Suite, self.tbl_suite,
               properties = dict(suite_id = self.tbl_suite.c.id))

        mapper(SuiteArchitecture, self.tbl_suite_architectures,
               properties = dict(suite_id = self.tbl_suite_architectures.c.suite,
1175
                                 suite = relation(Suite, backref='suitearchitectures'),
M
Mark Hymers 已提交
1176 1177
                                 arch_id = self.tbl_suite_architectures.c.architecture,
                                 architecture = relation(Architecture)))
M
Mark Hymers 已提交
1178 1179 1180

        mapper(Uid, self.tbl_uid,
               properties = dict(uid_id = self.tbl_uid.c.id))
M
Mark Hymers 已提交
1181

M
Mark Hymers 已提交
1182 1183
    ## Connection functions
    def __createconn(self):
M
Mark Hymers 已提交
1184
        from config import Config
1185 1186
        cnf = Config()
        if cnf["DB::Host"]:
M
Mark Hymers 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
            # TCP/IP
            connstr = "postgres://%s" % cnf["DB::Host"]
            if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
                connstr += ":%s" % cnf["DB::Port"]
            connstr += "/%s" % cnf["DB::Name"]
        else:
            # Unix Socket
            connstr = "postgres:///%s" % cnf["DB::Name"]
            if cnf["DB::Port"] and cnf["DB::Port"] != "-1":
                connstr += "?port=%s" % cnf["DB::Port"]
1197

M
updates  
Mark Hymers 已提交
1198
        self.db_pg   = create_engine(connstr, echo=self.debug)
M
Mark Hymers 已提交
1199 1200 1201 1202
        self.db_meta = MetaData()
        self.db_meta.bind = self.db_pg
        self.db_smaker = sessionmaker(bind=self.db_pg,
                                      autoflush=True,
1203
                                      autocommit=False)
M
Mark Hymers 已提交
1204

M
Mark Hymers 已提交
1205
        self.__setuptables()
M
Mark Hymers 已提交
1206
        self.__setupmappers()
M
Mark Hymers 已提交
1207

M
Mark Hymers 已提交
1208 1209
    def session(self):
        return self.db_smaker()
M
Mark Hymers 已提交
1210

M
Mark Hymers 已提交
1211
__all__.append('DBConn')
M
Mike O'Connor 已提交
1212