# Copyright (c) 2013 - Network Box
#
# This file is part of Uptrack.
#
# Uptrack is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Uptrack 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Uptrack.  If not, see <http://www.gnu.org/licenses/>.


import logging

from sqlalchemy.sql import and_

import transaction

from uptrack.kojibase import KojiBase
from uptrack.models import DBSession, Package, Release


class Sync(object):
    def __init__(self, settings):
        self.log = logging.getLogger("uptrack")

        self.kojibase = KojiBase(settings["kojihub_url"])

    def get_latest_builds(self, release):
        return self.kojibase.get_latest_builds(release.koji_tag)

    def run(self):
        """Run the sync"""
        pkgs = DBSession.query(Package)
        releases = DBSession.query(Release)

        for release in releases:
            self.log.info("Synchronizing %s..." % release.name)
            builds = self.get_latest_builds(release)

            for build in builds:
                self.log.info(" Processing %s..." % build.name)
                pkg = pkgs.filter(and_(Package.name==build.name,
                                       Package.release==release)).first()

                if not pkg:
                    pkg = Package(name=build.name, release=release)
                    self.log.debug("  New package: %s" % pkg)

                elif pkg.released_evr != build.evr:
                    self.log.debug("  We updated %s to %s" % (pkg, build.evr))

                elif not pkg.upstream or not pkg.upstream_evr:
                    self.log.debug("  No update, but we don't know yet where it comes from")

                else:
                    # We already know where the package comes from, and it
                    # wasn't updated
                    self.log.debug("  No change, ignoring")
                    continue

                pkg.released_evr = build.evr
                DBSession.add(pkg)

                # TODO: get upstream version

        transaction.commit()

@@ -1486,17 +1486,16 @@
 g.info(%22
- 
 Processi
@@ -1778,48 +1778,85 @@
     
-self.log.debug(%22  New package: %25s%22 %25 pkg
+DBSession.add(pkg)%0A                    self.log.debug(%22This is a new package%22
 )%0A%0A 
@@ -1896,9 +1896,9 @@
 evr 
-!
+=
 = bu
@@ -1908,90 +1908,20 @@
 .evr
-:%0A                    self.log.debug(%22  We updated %25s to %25s%22 %25 (pkg, build.evr))%0A%0A
+ and %5C%0A     
     
@@ -1936,17 +1936,8 @@
     
-elif not 
 pkg.
@@ -1949,14 +1949,11 @@
 eam 
-or not
+and
  pkg
@@ -1971,124 +1971,8 @@
 vr:%0A
-                    self.log.debug(%22  No update, but we don't know yet where it comes from%22)%0A%0A                else:%0A
     
@@ -2115,18 +2115,16 @@
 .debug(%22
-  
 No chang
@@ -2179,24 +2179,27 @@
             
+if 
 pkg.released
@@ -2203,16 +2203,17 @@
 sed_evr 
+!
 = build.
@@ -2215,16 +2215,17 @@
 uild.evr
+:
 %0A       
@@ -2233,33 +2233,287 @@
         
-DBSession.add(pkg
+    self.log.debug(%22We updated %25s to %25s%22 %25 (pkg, build.evr))%0A                    pkg.released_evr = build.evr%0A%0A                elif pkg.upstream is None or pkg.upstream_evr is None:%0A                    self.log.debug(%22No update, but we don't know yet where it comes from%22
 )%0A%0A     

Move some code around