def _create_debile_binaries(self, session, source, pkg):
        arch_all = session.query(Arch).filter(Arch.name == "all").one()
        arches = session.query(Arch).filter(Arch.name.in_(pkg.installed_archs)).all()

        if arch_all in source.arches and arch_all not in arches and source.affinity in arches:
            if not session.query(
                exists().where((Job.source == source) & (Job.arch == arch_all) & Job.check.has(Check.build == True))
            ).scalar():
                # We have the arch:affinity binary but is still lacking the arch:all binary
                # Make sure debile builds the arch:all binary separately
                check = session.query(Check).filter(Check.build == True).one()
                job = Job(check=check, arch=arch_all, source=source, binary=None)
                session.add(job)

        for arch in arches:
            if session.query(exists().where((Binary.source == source) & (Binary.arch == arch))).scalar():
                continue

            # Find the job for this binary
            job = (
                session.query(Job)
                .join(Job.check)
                .filter(Job.source == source, Job.arch == arch, Check.build == True)
                .first()
            )

            if not job and arch == arch_all and source.affinity in arches:
                # The arch:all binary might have been created by the arch:affinity build job.
                job = (
                    session.query(Job)
                    .join(Job.check)
                    .filter(Job.source == source, Job.arch == source.affinity, Check.build == True)
                    .first()
                )

            if job and (not job.finished_at or job.failed is True):
                # Dak accepted a binary upload that debile-master didn't ask for
                if arch != arch_all and not any(job.built_binaries):
                    session.delete(job)
                job = None

            if job:
                binary = job.new_binary(arch)
            else:
                binary = Binary(source=source, arch=arch, uploaded_at=datetime.utcnow())
            session.add(binary)

            for name, arch, filename in pkg.binaries:
                if arch == binary.arch.name:
                    directory, _, filename = filename.rpartition("/")
                    deb = Deb(binary=binary, directory=directory, filename=filename)
                    session.add(deb)

            print("Created binary for %s %s on %s" % (binary.name, binary.version, binary.arch))
            emit("accept", "binary", binary.debilize())
    def _create_debile_source(self, session, pkg):
        user = session.query(Person).filter_by(email="*****@*****.**").one()

        group_suite = (
            session.query(GroupSuite)
            .join(GroupSuite.group)
            .join(GroupSuite.suite)
            .filter(Group.name == "default", Suite.name == pkg.suite)
            .one()
        )
        component = session.query(Component).filter(Component.name == pkg.component).one()

        aroot = None
        if pkg.queue_name:
            # package is in a build-queue
            aroot = self._buildq_path
        else:
            aroot = self._archive_path

        dsc_fname = "{root}/{directory}/{filename}".format(root=aroot, directory=pkg.directory, filename=pkg.dsc)

        dsc = Dsc(open(dsc_fname))
        if "Build-Architecture-Indep" in dsc:
            valid_affinities = dsc["Build-Architecture-Indep"]
        elif "X-Build-Architecture-Indep" in dsc:
            valid_affinities = dsc["X-Build-Architecture-Indep"]
        elif "X-Arch-Indep-Build-Arch" in dsc:
            valid_affinities = dsc["X-Arch-Indep-Build-Arch"]
        else:
            valid_affinities = "any"

        source = create_source(dsc, group_suite, component, user, self._affinity_preference, valid_affinities)
        source.directory = pkg.directory
        source.dsc_filename = pkg.dsc
        session.add(source)

        for aname in pkg.installed_archs:
            arch = session.query(Arch).filter_by(name=aname).one()
            binary = Binary(source=source, arch=arch, uploaded_at=source.uploaded_at)
            session.add(binary)

            for name, arch, filename in pkg.binaries:
                if arch == binary.arch.name:
                    directory, _, filename = filename.rpartition("/")
                    deb = Deb(binary=binary, directory=directory, filename=filename)
                    session.add(deb)

        create_jobs(source, dose_report="No dose-builddebcheck report available yet.")

        oldsources = session.query(Source).filter(Source.group_suite == source.group_suite, Source.name == source.name)
        for oldsource in oldsources:
            if version_compare(oldsource.version, source.version) >= 0:
                continue

            # Drop any old jobs that are still pending.
            for job in oldsource.jobs:
                if (job.check.build and not any(job.built_binaries)) or not any(job.results):
                    session.delete(job)
                elif job.failed is None:
                    job.failed = any(result.failed for result in job.results)
                    job.builder = None
                    job.assigned_at = None
                    job.finished_at = None

            # Actually remove jobs marked for deletion above.
            session.commit()

            # If after cleanup there is no build jobs left, remove the source completely
            if not any(job.check.build for job in oldsource.jobs):
                session.delete(oldsource)

        print("Created source for %s %s" % (source.name, source.version))
        emit("accept", "source", source.debilize())
    def _create_debile_source(self, session, pkg):
        user = session.query(Person).filter_by(
            email="*****@*****.**").one()

        group_suite = session.query(GroupSuite).join(GroupSuite.group).join(
            GroupSuite.suite).filter(
                Group.name == "default",
                Suite.name == pkg.suite,
            ).one()
        component = session.query(Component).filter(
            Component.name == pkg.component).one()

        dsc_fname = "{root}/{directory}/{filename}".format(
            root=self._archive_path,
            directory=pkg.directory,
            filename=pkg.dsc,
        )

        dsc = Dsc(open(dsc_fname))
        if 'Build-Architecture-Indep' in dsc:
            valid_affinities = dsc['Build-Architecture-Indep']
        elif 'X-Build-Architecture-Indep' in dsc:
            valid_affinities = dsc['X-Build-Architecture-Indep']
        elif 'X-Arch-Indep-Build-Arch' in dsc:
            valid_affinities = dsc['X-Arch-Indep-Build-Arch']
        else:
            valid_affinities = "any"

        source = create_source(dsc, group_suite, component, user,
                               self._affinity_preference, valid_affinities)
        source.directory = pkg.directory
        source.dsc_filename = pkg.dsc
        session.add(source)

        for aname in pkg.installed_archs:
            arch = session.query(Arch).filter_by(name=aname).one()
            binary = Binary(source=source,
                            arch=arch,
                            uploaded_at=source.uploaded_at)
            session.add(binary)

            for name, arch, filename in pkg.binaries:
                if arch == binary.arch.name:
                    directory, _, filename = filename.rpartition('/')
                    deb = Deb(binary=binary,
                              directory=directory,
                              filename=filename)
                    session.add(deb)

        create_jobs(source,
                    dose_report="No dose-builddebcheck report available yet.")

        oldsources = session.query(Source).filter(
            Source.group_suite == source.group_suite,
            Source.name == source.name,
        )
        for oldsource in oldsources:
            if version_compare(oldsource.version, source.version) >= 0:
                continue

            # Drop any old jobs that are still pending.
            for job in oldsource.jobs:
                if (job.check.build and
                        not any(job.built_binaries)) or not any(job.results):
                    session.delete(job)
                elif job.failed is None:
                    job.failed = any(result.failed for result in job.results)
                    job.builder = None
                    job.assigned_at = None
                    job.finished_at = None

            # Actually remove jobs marked for deletion above.
            session.commit()

            # If after cleanup there is no build jobs left, remove the source completely
            if not any(job.check.build for job in oldsource.jobs):
                session.delete(oldsource)

        print("Created source for %s %s" % (source.name, source.version))
        emit('accept', 'source', source.debilize())
    def _create_debile_source(self, session, pkg):
        user = session.query(Person).filter_by(email="*****@*****.**").one()

        group_suite = session.query(GroupSuite).join(GroupSuite.group).join(GroupSuite.suite).filter(
            Group.name == "default",
            Suite.name == pkg.suite,
        ).one()
        component = session.query(Component).filter(
            Component.name == pkg.component
        ).one()

        dsc_fname = "{root}/{directory}/{filename}".format(
            root=self._archive_path,
            directory=pkg.directory,
            filename=pkg.dsc,
        )

        dsc = Dsc(open(dsc_fname))
        if 'Build-Architecture-Indep' in dsc:
            valid_affinities = dsc['Build-Architecture-Indep']
        elif 'X-Build-Architecture-Indep' in dsc:
            valid_affinities = dsc['X-Build-Architecture-Indep']
        elif 'X-Arch-Indep-Build-Arch' in dsc:
            valid_affinities = dsc['X-Arch-Indep-Build-Arch']
        else:
            valid_affinities = "any"

        source = create_source(dsc, group_suite, component, user,
                               self._affinity_preference, valid_affinities)
        source.directory = pkg.directory
        source.dsc_filename = pkg.dsc
        session.add(source)

        for aname in pkg.installed_archs:
            arch = session.query(Arch).filter_by(name=aname).one()
            binary = Binary(source=source, arch=arch, uploaded_at=source.uploaded_at)
            session.add(binary)

            for name, arch, filename in pkg.binaries:
                if arch == binary.arch.name:
                    directory, _, filename = filename.rpartition('/')
                    deb = Deb(binary=binary, directory=directory, filename=filename)
                    session.add(deb)

        create_jobs(source, dose_report="No dose-builddebcheck report available yet.")

        # Drop any old jobs that are still pending.
        oldsources = session.query(Source).filter(
            Source.group_suite == source.group_suite,
            Source.name == source.name,
        )
        for oldsource in oldsources:
            if version_compare(oldsource.version, source.version) >= 0:
                continue
            for job in oldsource.jobs:
                if (not any(job.results) and not any(job.built_binaries)):
                    session.delete(job)
                elif job.failed is None:
                    job.failed = True
            if not any(job.check.build for job in oldsource.jobs):
                session.delete(oldsource)

        print("Created source for %s %s" % (source.name, source.version))
        emit('accept', 'source', source.debilize())
    def _create_debile_binaries(self, session, source, pkg):
        arch_all = session.query(Arch).filter(Arch.name == "all").one()
        arches = session.query(Arch).filter(Arch.name.in_(
            pkg.installed_archs)).all()

        if arch_all in source.arches and arch_all not in arches and source.affinity in arches:
            if not session.query(exists().where(
                (Job.source == source) & (Job.arch == arch_all)
                    & Job.check.has(Check.build == True))).scalar():
                # We have the arch:affinity binary but is still lacking the arch:all binary
                # Make sure debile builds the arch:all binary separately
                check = session.query(Check).filter(Check.build == True).one()
                job = Job(check=check,
                          arch=arch_all,
                          source=source,
                          binary=None)
                session.add(job)

        for arch in arches:
            if session.query(exists().where((Binary.source == source)
                                            & (Binary.arch == arch))).scalar():
                continue

            # Find the job for this binary
            job = session.query(Job).join(Job.check).filter(
                Job.source == source,
                Job.arch == arch,
                Check.build == True,
            ).first()

            if not job and arch == arch_all and source.affinity in arches:
                # The arch:all binary might have been created by the arch:affinity build job.
                job = session.query(Job).join(Job.check).filter(
                    Job.source == source,
                    Job.arch == source.affinity,
                    Check.build == True,
                ).first()

            if job and (not job.finished_at or job.failed is True):
                # Dak accepted a binary upload that debile-master didn't ask for
                if arch != arch_all and not any(job.built_binaries):
                    session.delete(job)
                job = None

            if job:
                binary = job.new_binary(arch)
            else:
                binary = Binary(source=source,
                                arch=arch,
                                uploaded_at=datetime.utcnow())
            session.add(binary)

            for name, arch, filename in pkg.binaries:
                if arch == binary.arch.name:
                    directory, _, filename = filename.rpartition('/')
                    deb = Deb(binary=binary,
                              directory=directory,
                              filename=filename)
                    session.add(deb)

            print("Created binary for %s %s on %s" %
                  (binary.name, binary.version, binary.arch))
            emit('accept', 'binary', binary.debilize())