From 14d4e906cae44701d0fa967d7c4334957bfbf619 Mon Sep 17 00:00:00 2001
From: Pavel Kopyl <p.kopyl@samsung.com>
Date: Mon, 26 Jun 2017 16:59:16 +0300
Subject: [PATCH 1/2] [backend] The different names support for OBS and rpm
 packages in the scheduler

---
 src/backend/BSSched/BuildJob/Package.pm | 39 +++++++++++++++++++++++++++++++--
 src/backend/BSSched/Checker.pm          | 28 ++++++++++++++++++++++-
 2 files changed, 64 insertions(+), 3 deletions(-)

diff --git a/src/backend/BSSched/BuildJob/Package.pm b/src/backend/BSSched/BuildJob/Package.pm
index aa0ac8a..cefae40 100644
--- a/src/backend/BSSched/BuildJob/Package.pm
+++ b/src/backend/BSSched/BuildJob/Package.pm
@@ -78,6 +78,8 @@ sub check {
   my $prp = $ctx->{'prp'};
   my $notready = $ctx->{'notready'};
   my $dep2src = $ctx->{'dep2src'};
+  my $src2pkg = $ctx->{'src2pkg'};
+  my $pkg2src = $ctx->{'pkg2src'};
   my $edeps = $info->{'edeps'} || $ctx->{'edeps'}->{$packid} || [];
   my $depislocal = $ctx->{'depislocal'};
   my $gdst = $ctx->{'gdst'};
@@ -98,7 +100,26 @@ sub check {
   }
 
   # calculate if we're blocked
-  my @blocked = grep {$notready->{$dep2src->{$_}}} @$edeps;
+  # @blocked contain rpm-packages
+  my @blocked;
+  
+  # The original code. It's can be restored for upstream logic
+#  @blocked = grep {$notready->{$dep2src->{$_}}} @$edeps;
+  # iterate for rpm-packages
+  for my $dep (@$edeps) {
+    my $depsrc = $dep2src->{$dep};
+    print "        no depsrc for rpm-package '$dep'\n" unless (defined $depsrc);
+    my $obs_package = $src2pkg->{$depsrc};
+    # obs package may be undefined if it is present at the parent project
+    #print "        no obs_package for rpm-name '$depsrc'\n" unless (defined $obs_package);
+
+    # notready contain rpm-names or obs-packages
+    if ($notready->{$depsrc} or (defined $obs_package && $notready->{$obs_package})) {
+      # need to push rpm-package
+      push @blocked, $dep unless (grep ($dep eq $_, @blocked));
+    }
+  }
+
   @blocked = () if $repo->{'block'} && $repo->{'block'} eq 'never';
   if ($ctx->{'cychash'}->{$packid}) {
     # package belongs to a cycle, prune blocked list
@@ -115,7 +136,21 @@ sub check {
     my %cycs = map {$_ => 1} @{$ctx->{'cychash'}->{$packid}};
     # prune building cycle packages from blocked
     my $building = $ctx->{'building'};
-    @blocked = grep {!$cycs{$dep2src->{$_}} || !$building->{$dep2src->{$_}}} @blocked;
+
+    # @blocked = grep {!$cycs{$src2pkg->{$dep2src->{$_}}} || !$building->{$dep2src->{$_}}} @blocked;
+    my @blocked_prune;
+    for my $bpkg (@blocked) {           # rpm-packages
+      my $pkgsrc = $dep2src->{$bpkg};   # rpm-name
+      my $pkg = $src2pkg->{$pkgsrc};    # obs-package
+      # %cycs and %building contains obs-packages
+      next if ($cycs{$pkg} && $building->{$pkg});
+
+      push @blocked_prune, $bpkg unless (grep ($bpkg eq $_, @blocked_prune));
+    }
+    if ($#blocked_prune != $#blocked && $ctx->{'verbose'}) {
+      print "        prune blocked ($blocked[0]...)\n";
+    }
+    @blocked = @blocked_prune;
     if (@blocked) {
       if ($ctx->{'verbose'}) {
         print "      - $packid ($buildtype)\n";
diff --git a/src/backend/BSSched/Checker.pm b/src/backend/BSSched/Checker.pm
index c30ad0f..28298c2 100644
--- a/src/backend/BSSched/Checker.pm
+++ b/src/backend/BSSched/Checker.pm
@@ -399,6 +399,10 @@ sub expandandsort {
   my %havepatchinfos;
   my %pkg2buildtype;
 
+  my %pkgid2name;
+  my %name2pkgid;
+  my %pkgiddeps;
+
   my $subpacks = $ctx->{'subpacks'};
 
   $ctx->{'experrors'} = \%experrors;
@@ -411,6 +415,9 @@ sub expandandsort {
       next;
     }
     my $info = (grep {$_->{'repository'} eq $repoid} @{$pdata->{'info'} || []})[0];
+    $pkgid2name{$packid} = $info->{'name'} || $packid;
+    $name2pkgid{$pkgid2name{$packid}} = $packid;
+
     # calculate package type
     my $buildtype;
     if ($pdata->{'aggregatelist'}) {
@@ -470,21 +477,40 @@ sub expandandsort {
       @edeps = @deps;
     }
     $pdeps{$packid} = \@edeps;
+    $pkgiddeps{$pkgid2name{$packid}} = \@edeps;
+  }
+
+  my %src2pkg;
+  for my $packid (sort keys %pkg2src) {
+    my $n = $pkg2src{$packid} || $packid;
+    $src2pkg{$n} = $packid;
   }
 
   $ctx->{'edeps'} = \%pdeps;
   $ctx->{'experrors'} = \%experrors;
   $ctx->{'pkg2buildtype'} = \%pkg2buildtype;
+  $ctx->{'src2pkg'} = \%src2pkg;
+  $ctx->{'pkg2src'} = \%pkg2src;
 
+  my @packnames = map {$pkgid2name{$_}} @$packs;
   # now sort
   print "    sorting ".@$packs." packages\n";
   my @cycles;
   if (@$packs > 1) {
-    @$packs = BSSolv::depsort(\%pdeps, $ctx->{'dep2src'}, \@cycles, @$packs);
+    @$packs = BSSolv::depsort(\%pkgiddeps, $ctx->{'dep2src'}, \@cycles, @packnames);
     if (@cycles) {
       print "cycle: ".join(' -> ', @$_)."\n" for @cycles;
     }
   }
+  @$packs = map {$name2pkgid{$_}} @$packs;
+  for my $cyc (@cycles) {
+    if (ref($cyc) eq 'ARRAY') {
+      $_ = $name2pkgid{$_} for @{$cyc};
+    } else {
+      $cyc = $name2pkgid{$cyc};
+    }
+  }
+
   if (%havepatchinfos) {
     # bring patchinfos to back
     my @packs_patchinfos = grep {$havepatchinfos{$_}} @$packs;
-- 
2.7.4

