Fix source-install to accept package names and lookup their source package (bnc#785832)
authorMichael Andres <ma@suse.de>
Thu, 25 Oct 2012 16:08:36 +0000 (18:08 +0200)
committerMichael Andres <ma@suse.de>
Thu, 25 Oct 2012 16:08:36 +0000 (18:08 +0200)
doc/zypper.8
src/Zypper.h
src/misc.cc

index 8140043..48f3fda 100644 (file)
@@ -384,7 +384,9 @@ Install version 2.0.6 of virtualbox-ose package (any of the following):
 
 .TP
 .B source-install (si) <name> ...
-Install specified source packages and their build dependencies.
+Install specified source packages and their build dependencies. If the name of
+a binary package is given, the coresponding source package is looked up and
+installed instead.
 
 This command will try to find the newest available versions of the source
 packages and use rpm -i to install them and the packages that are required
index d3e70f3..20f6fd7 100644 (file)
@@ -120,7 +120,7 @@ struct RuntimeData
    */
   zypp::RepoInfo current_repo;
 
-  std::list<zypp::SrcPackage::constPtr> srcpkgs_to_install;
+  std::set<zypp::SrcPackage::constPtr> srcpkgs_to_install;
 
   // hack to enable media progress reporting in the commit phase in normal
   // output level
index ff1bf54..f61c90d 100644 (file)
@@ -35,7 +35,6 @@
 
 using namespace std;
 using namespace zypp;
-using namespace zypp::ui;
 using namespace boost;
 
 extern ZYpp::Ptr God;
@@ -121,7 +120,7 @@ bool confirm_licenses(Zypper & zypper)
   for (ResPool::const_iterator it = God->pool().begin(); it != God->pool().end(); ++it)
   {
     bool to_accept = true;
-    
+
     if (it->status().isToBeInstalled() &&
         !it->resolvable()->licenseToConfirm().empty())
     {
@@ -171,7 +170,7 @@ bool confirm_licenses(Zypper & zypper)
 
       if ( !it->resolvable()->needToAcceptLicense() )
         to_accept = false;
-      
+
       if (to_accept)
       {
         // introduction
@@ -318,50 +317,44 @@ void report_licenses(Zypper & zypper)
 }
 
 // ----------------------------------------------------------------------------
-
-static SrcPackage::constPtr source_find( const string & arg )
+namespace
 {
-   /*
-   * Workflow:
-   *
-   * 1. interate all SrcPackage resolvables with specified name
-   * 2. find the latest version or version satisfying specification.
-   */
-    SrcPackage::constPtr srcpkg;
-
-    ResPool pool(God->pool());
-    DBG << "looking source for : " << arg << endl;
-    for_( srcit, pool.byIdentBegin<SrcPackage>(arg),
-              pool.byIdentEnd<SrcPackage>(arg) )
+  SrcPackage::constPtr source_find( Zypper & zypper_r, const string & arg_r )
+  {
+    /*
+     * Workflow:
+     *
+     * 1. return srcpackage "arg_r" if available
+     * 2. else if package "arg_r" is available, return it's srcpackage if available
+     * 3; else return 0
+     */
+    DBG << "looking for source package: " << arg_r << endl;
+    ui::Selectable::Ptr p( ui::Selectable::get( ResKind::srcpackage, arg_r ) );
+    if ( p )
+      return asKind<SrcPackage>( p->theObj().resolvable() );
+
+    // else: try package and packages sourcepackage
+    p = zypp::ui::Selectable::get( zypp::ResKind::package, arg_r );
+    if ( p )
     {
-      DBG << *srcit << endl;
-      if ( ! srcit->status().isInstalled() ) // this will be true for all of the srcpackages, won't it?
-      {
-        SrcPackage::constPtr _srcpkg = asKind<SrcPackage>(srcit->resolvable());
+      std::string name( p->theObj()->asKind<Package>()->sourcePkgName() );
+      DBG << "looking for source package of package: " << name << endl;
+      zypper_r.out().info( boost::str( format(_("Package '%s' has source package '%s'.")) % arg_r % name ) );
 
-        DBG << "Considering srcpakcage " << _srcpkg->name() << "-" << _srcpkg->edition() << ": ";
-        if (srcpkg)
-        {
-          if (_srcpkg->edition() > srcpkg->edition())
-          {
-            DBG << "newer edition (" << srcpkg->edition() << " > " << _srcpkg->edition() << ")";
-            _srcpkg.swap(srcpkg);
-          }
-          else
-            DBG << "is older than the current candidate";
-        }
-        else
-        {
-          DBG << "first candindate";
-          _srcpkg.swap(srcpkg);
-        }
-        DBG << endl;
-      }
+      p = zypp::ui::Selectable::get( ResKind::srcpackage, p->theObj()->asKind<Package>()->sourcePkgName() );
+      if ( p )
+       return asKind<SrcPackage>( p->theObj().resolvable() );
+      else
+       zypper_r.out().error( boost::str( format(_("Source package '%s' for package '%s' not found.")) % name % arg_r ) );
     }
+    else
+      zypper_r.out().error( boost::str( format(_("Source package '%s' not found.")) % arg_r ) );
 
-    return srcpkg;
-}
 
+    DBG << "no source package found for: " << arg_r << endl;
+    return SrcPackage::constPtr();
+  }
+} // namespace
 // ----------------------------------------------------------------------------
 
 void build_deps_install(Zypper & zypper)
@@ -376,10 +369,11 @@ void build_deps_install(Zypper & zypper)
   for (vector<string>::const_iterator it = zypper.arguments().begin();
        it != zypper.arguments().end(); ++it)
   {
-    SrcPackage::constPtr srcpkg = source_find(*it);
+    SrcPackage::constPtr srcpkg = source_find(zypper, *it);
 
     if (srcpkg)
     {
+      zypper.runtimeData().srcpkgs_to_install.insert(srcpkg);
       DBG << format("Injecting build requieres for source package %s-%s")
           % srcpkg->name() % srcpkg->edition() << endl;
 
@@ -400,8 +394,6 @@ void build_deps_install(Zypper & zypper)
     }
     else
     {
-      zypper.out().error(boost::str(format(
-          _("Source package '%s' not found.")) % (*it)));
       zypper.setExitCode(ZYPPER_EXIT_INF_CAP_NOT_FOUND);
     }
   }
@@ -421,13 +413,10 @@ void mark_src_pkgs(Zypper & zypper)
   for (vector<string>::const_iterator it = zypper.arguments().begin();
        it != zypper.arguments().end(); ++it)
   {
-    SrcPackage::constPtr srcpkg = source_find(*it);
+    SrcPackage::constPtr srcpkg = source_find(zypper, *it);
 
-    if (srcpkg)
-      zypper.runtimeData().srcpkgs_to_install.push_back(srcpkg);
-    else
-      zypper.out().error(boost::str(format(
-          _("Source package '%s' not found.")) % (*it)));
+    if ( srcpkg )
+      zypper.runtimeData().srcpkgs_to_install.insert(srcpkg);
   }
 }