- Add method Product::replacedProducts to identify installed
authorMichael Andres <ma@suse.de>
Fri, 15 Aug 2008 16:50:22 +0000 (16:50 +0000)
committerMichael Andres <ma@suse.de>
Fri, 15 Aug 2008 16:50:22 +0000 (16:50 +0000)
  Products that would be replaced by installing a new Product.

devel/devel.ma/NewPool.cc
package/libzypp.changes
zypp/CMakeLists.txt
zypp/Product.cc
zypp/Product.h
zypp/sat/WhatObsoletes.cc [new file with mode: 0644]
zypp/sat/WhatObsoletes.h [new file with mode: 0644]
zypp/sat/WhatProvides.cc
zypp/sat/WhatProvides.h
zypp/ui/Selectable.h
zypp/ui/SelectableTraits.h

index 09bde91..0efd2db 100644 (file)
@@ -41,6 +41,7 @@
 #include "zypp/sat/SolvableSet.h"
 #include "zypp/sat/SolvIterMixin.h"
 #include "zypp/sat/detail/PoolImpl.h"
+#include "zypp/sat/WhatObsoletes.h"
 #include "zypp/PoolQuery.h"
 
 #include "zypp/parser/ProductConfReader.h"
@@ -528,10 +529,42 @@ try {
   ///////////////////////////////////////////////////////////////////
   ///////////////////////////////////////////////////////////////////
 
-  testcase();
-  MIL << "FOOO" << endl;
+  // Pool of Selectables
+  ResPoolProxy selectablePool( ResPool::instance().proxy() );
 
-   ///////////////////////////////////////////////////////////////////
+  // Iterate it's Products...
+  for_( it, selectablePool.byKindBegin<Product>(), selectablePool.byKindEnd<Product>() )
+  {
+    // current Product Selectable
+    ui::Selectable::Ptr prodSel( *it );
+    MIL << dump( prodSel ) << endl;
+
+    // It's candiate as Product pointer
+    Product::constPtr prod( prodSel->candidateAsKind<Product>() );
+    if ( prod )
+    {
+      // Not NULL, so there is an available Product.
+      // Get the installed Products it would replace.
+      Product::ReplacedProducts prodReplaces( prod->replacedProducts() );
+
+      // Iterate the replaced Products...
+      for_( it, prodReplaces.begin(), prodReplaces.end() )
+      {
+        // current replaced Product
+        Product::constPtr replacedProduct( *it );
+        DBG << replacedProduct << endl;
+
+        // and this is how you would get the Selectable that contains
+        // this replacedProduct, in case you need it ..
+        ui::Selectable::Ptr replacedProductsSelectable( ui::Selectable::get( replacedProduct ) );
+        DBG << replacedProductsSelectable << endl;
+      }
+    }
+
+  }
+
+
+  ///////////////////////////////////////////////////////////////////
   INT << "===[END]============================================" << endl << endl;
   zypp::base::LogControl::instance().logNothing();
   return 0;
index f478b7e..81d8fd6 100644 (file)
@@ -1,4 +1,12 @@
 -------------------------------------------------------------------
+Fri Aug 15 17:42:58 CEST 2008 - ma@suse.de
+
+- Add method Product::replacedProducts to identify installed 
+  Products that would be replaced by installing a new Product.
+  (for Fate #301997)
+- revision 10876
+
+-------------------------------------------------------------------
 Fri Aug 15 15:30:32 CEST 2008 - ma@suse.de
 
 - Fixes to Selectable doing staus manipulation on non-USER level.
index f49ca79..f1b317f 100644 (file)
@@ -483,6 +483,7 @@ SET( zypp_sat_SRCS
   sat/SolvableSet.cc
   sat/SolvIterMixin.cc
   sat/WhatProvides.cc
+  sat/WhatObsoletes.cc
   sat/LocaleSupport.cc
   sat/LookupAttr.cc
   sat/SolvAttr.cc
@@ -494,6 +495,7 @@ SET( zypp_sat_HEADERS
   sat/SolvableSet.h
   sat/SolvIterMixin.h
   sat/WhatProvides.h
+  sat/WhatObsoletes.h
   sat/LocaleSupport.h
   sat/LookupAttr.h
   sat/SolvAttr.h
index e540cd9..e7e98ed 100644 (file)
@@ -17,6 +17,8 @@
 
 #include "zypp/sat/LookupAttr.h"
 #include "zypp/sat/WhatProvides.h"
+#include "zypp/sat/WhatObsoletes.h"
+#include "zypp/PoolItem.h"
 
 using std::endl;
 
@@ -70,10 +72,6 @@ namespace zypp
   {}
 
   ///////////////////////////////////////////////////////////////////
-  //
-  //   Package interface forwarded to implementation
-  //
-  ///////////////////////////////////////////////////////////////////
 
   sat::Solvable Product::referencePackage() const
   {
@@ -104,6 +102,35 @@ namespace zypp
     return sat::Solvable::noSolvable;
   }
 
+  Product::ReplacedProducts Product::replacedProducts() const
+  {
+    std::vector<constPtr> ret;
+    // By now we simply collect what is obsoleted by the Product,
+    // or by the products buddy (release-package).
+
+    // Check our own dependencies. We should not have any,
+    // but just to be shure.
+    sat::WhatObsoletes obsoleting( satSolvable() );
+    for_( it, obsoleting.begin(), obsoleting.end() )
+    {
+      if ( it->isKind( ResKind::product ) )
+        ret.push_back( make<Product>( *it ) );
+    }
+
+    // If we have a buddy, we check what product buddies the
+    // buddy replaces.
+    obsoleting = sat::WhatObsoletes( poolItem().buddy() );
+    for_( it, obsoleting.poolItemBegin(), obsoleting.poolItemEnd() )
+    {
+      if ( (*it).buddy().isKind( ResKind::product ) )
+        ret.push_back( make<Product>( (*it).buddy() ) );
+    }
+
+    return ret;
+  }
+
+  ///////////////////////////////////////////////////////////////////
+
   std::string Product::shortName() const
   { return lookupStrAttribute( sat::SolvAttr::productShortlabel ); }
 
index 5847d73..fd2a65f 100644 (file)
@@ -44,6 +44,15 @@ namespace zypp
     sat::Solvable referencePackage() const;
 
   public:
+    /***/
+    typedef std::vector<constPtr> ReplacedProducts;
+
+    /** Array of \b installed Products that would be replaced by
+     *  installing this one.
+     */
+    ReplacedProducts replacedProducts() const;
+
+  public:
     /** Untranslated short name like <tt>SLES 10</tt>*/
     std::string shortName() const;
 
diff --git a/zypp/sat/WhatObsoletes.cc b/zypp/sat/WhatObsoletes.cc
new file mode 100644 (file)
index 0000000..077e73d
--- /dev/null
@@ -0,0 +1,114 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/sat/WhatObsoletes.cc
+ *
+*/
+#include <iostream>
+
+#include "zypp/base/LogTools.h"
+#include "zypp/sat/WhatObsoletes.h"
+#include "zypp/sat/detail/PoolImpl.h"
+#include "zypp/PoolItem.h"
+
+using std::endl;
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace sat
+  { /////////////////////////////////////////////////////////////////
+
+    ///////////////////////////////////////////////////////////////////
+    namespace
+    { /////////////////////////////////////////////////////////////////
+
+      /** WhatObsoletes ctor helper collecting obsoleted installed items. */
+      shared_ptr<void> allocatedProviders( sat::Solvable item_r, const sat::detail::IdType *& first_r )
+      {
+        WhatProvides obsoleted( item_r.obsoletes() );
+        if ( obsoleted.empty() )
+        {
+          return shared_ptr<void>();
+        }
+
+        // use allocated private data to store the result (incl. trailing NULL)
+        std::vector<sat::detail::IdType> * pdata = 0;
+        shared_ptr<void> ret;
+
+        for_( it, obsoleted.begin(), obsoleted.end() )
+        {
+          if ( it->isSystem() )
+          {
+            if ( ! pdata )
+            {
+              ret.reset( (pdata = new std::vector<sat::detail::IdType>) );
+            }
+            pdata->push_back( it->id() );
+          }
+        }
+
+        if ( pdata )
+        {
+          pdata->push_back( sat::detail::noId );
+          first_r = &pdata->front();
+        }
+        return ret;
+      }
+
+      /////////////////////////////////////////////////////////////////
+    } // namespace
+    ///////////////////////////////////////////////////////////////////
+
+    WhatObsoletes::WhatObsoletes( Solvable item_r )
+    : _begin( 0 )
+    , _private( allocatedProviders( item_r, _begin ) )
+    {}
+
+    WhatObsoletes::WhatObsoletes( const PoolItem & item_r )
+    : _begin( 0 )
+    , _private( allocatedProviders( item_r.satSolvable(), _begin ) )
+    {}
+
+    WhatObsoletes::WhatObsoletes( const ResObject::constPtr item_r )
+    : _begin( 0 )
+    {
+      if ( item_r )
+        _private = allocatedProviders( item_r->satSolvable(), _begin );
+    }
+
+    WhatObsoletes::size_type WhatObsoletes::size() const
+    {
+      if ( ! _begin )
+        return 0;
+
+      Capabilities::size_type ret = 0;
+      for ( const sat::detail::IdType * end = _begin; *end; ++end )
+      {
+        ++ret;
+      }
+      return ret;
+    }
+
+    /******************************************************************
+    **
+    ** FUNCTION NAME : operator<<
+    ** FUNCTION TYPE : std::ostream &
+    */
+    std::ostream & operator<<( std::ostream & str, const WhatObsoletes & obj )
+    {
+      return dumpRange( str << "(" << obj.size() << ")", obj.begin(), obj.end() );
+    }
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace sat
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
diff --git a/zypp/sat/WhatObsoletes.h b/zypp/sat/WhatObsoletes.h
new file mode 100644 (file)
index 0000000..5911605
--- /dev/null
@@ -0,0 +1,96 @@
+/*---------------------------------------------------------------------\
+|                          ____ _   __ __ ___                          |
+|                         |__  / \ / / . \ . \                         |
+|                           / / \ V /|  _/  _/                         |
+|                          / /__ | | | | | |                           |
+|                         /_____||_| |_| |_|                           |
+|                                                                      |
+\---------------------------------------------------------------------*/
+/** \file      zypp/sat/WhatObsoletes.h
+ *
+*/
+#ifndef ZYPP_SAT_WHATOBSOLETES_H
+#define ZYPP_SAT_WHATOBSOLETES_H
+
+#include <iosfwd>
+#include <vector>
+
+#include "zypp/sat/WhatProvides.h"
+
+///////////////////////////////////////////////////////////////////
+namespace zypp
+{ /////////////////////////////////////////////////////////////////
+  ///////////////////////////////////////////////////////////////////
+  namespace sat
+  { /////////////////////////////////////////////////////////////////
+
+    ///////////////////////////////////////////////////////////////////
+    //
+    // CLASS NAME : WhatObsoletes
+    //
+    /** Container of \b installed \ref Solvable which would be
+     *  obsoleted by the \ref Solvable passed to the ctor.
+     *
+     * \code
+     * \endcode
+     */
+    class WhatObsoletes : public SolvIterMixin<WhatObsoletes,detail::WhatProvidesIterator>,
+                          protected detail::PoolMember
+    {
+      public:
+        typedef Solvable  value_type;
+        typedef unsigned  size_type;
+
+      public:
+        /** Default ctor */
+        WhatObsoletes()
+        : _begin( 0 )
+        {}
+
+        /** Ctor from \ref Solvable. */
+        explicit
+        WhatObsoletes( Solvable item_r );
+
+        /** Ctor from \ref PoolItem. */
+        explicit
+        WhatObsoletes( const PoolItem & item_r );
+
+        /** Ctor from \ref ResObject::constPtr. */
+        explicit
+        WhatObsoletes( const ResObject_constPtr item_r );
+
+     public:
+        /** Whether the container is empty. */
+        bool empty() const
+        { return ! ( _begin && *_begin ); }
+
+        /** Number of solvables inside. */
+        size_type size() const;
+
+      public:
+        typedef detail::WhatProvidesIterator const_iterator;
+
+        /** Iterator pointing to the first \ref Solvable. */
+        const_iterator begin() const
+        { return const_iterator( _begin ); }
+
+        /** Iterator pointing behind the last \ref Solvable. */
+        const_iterator end() const
+        { return const_iterator( 0 ); }
+
+      private:
+        const sat::detail::IdType * _begin;
+        shared_ptr<void> _private;
+    };
+    ///////////////////////////////////////////////////////////////////
+
+    /** \relates WhatObsoletes Stream output */
+    std::ostream & operator<<( std::ostream & str, const WhatObsoletes & obj );
+
+    /////////////////////////////////////////////////////////////////
+  } // namespace sat
+  ///////////////////////////////////////////////////////////////////
+  /////////////////////////////////////////////////////////////////
+} // namespace zypp
+///////////////////////////////////////////////////////////////////
+#endif // ZYPP_SAT_WHATOBSOLETES_H
index 39f45d5..b0c6736 100644 (file)
@@ -48,8 +48,8 @@ namespace zypp
           return shared_ptr<void>();
         }
 
-        // use allocated private data to store the result
-        std::vector<sat::detail::IdType> * pdata = new std::vector<sat::detail::IdType>( ids.size(), sat::detail::noId );
+        // use allocated private data to store the result (incl. trailing NULL)
+        std::vector<sat::detail::IdType> * pdata = new std::vector<sat::detail::IdType>( ids.size()+1, sat::detail::noId );
         pdata->insert( pdata->begin(), ids.begin(), ids.end() );
         first_r = &pdata->front();
         return shared_ptr<void>( pdata );
index d5b61e6..1f827d6 100644 (file)
@@ -6,7 +6,7 @@
 |                         /_____||_| |_| |_|                           |
 |                                                                      |
 \---------------------------------------------------------------------*/
-/** \file      zypp/sat/.h
+/** \file      zypp/sat/WhatProvides.h
  *
 */
 #ifndef ZYPP_SAT_WHATPROVIDES_H
@@ -142,6 +142,7 @@ namespace zypp
     // CLASS NAME : WhatProvides::const_iterator
     //
     /** \ref WhatProvides iterator.
+     * Iterate a NULL terminated sat::detail::IdType array.
      */
     class WhatProvidesIterator : public boost::iterator_adaptor<
           WhatProvidesIterator         // Derived
index 7cf215b..2884567 100644 (file)
@@ -124,6 +124,26 @@ namespace zypp
        */
       PoolItem candidateObj() const;
 
+      /** Return the \ref installedObj resolvable casted to a specific kind.
+       * \code
+       *   Selectable mySelectable;
+       *   Package::constPtr p( mySelectable.installedAsKind<Package>() );
+       * \endcode
+      */
+      template<class _Res>
+      typename ResTraits<_Res>::constPtrType installedAsKind() const
+      { return asKind<_Res>( candidateObj() ); }
+
+      /** Return the \ref candidateObj resolvable casted to a specific kind.
+       * \code
+       *   Selectable mySelectable;
+       *   Package::constPtr p( mySelectable.candidateAsKind<Package>() );
+       * \endcode
+      */
+      template<class _Res>
+      typename ResTraits<_Res>::constPtrType candidateAsKind() const
+      { return asKind<_Res>( candidateObj() ); }
+
       /** Set a candidate (out of available objects).
        * \return The new candidate, or NULL if choice was invalid
        * (NULL or not among availableObjs). An invalid choice
index ee62874..c940289 100644 (file)
@@ -17,7 +17,6 @@
 #include "zypp/base/Iterator.h"
 #include "zypp/PoolItem.h"
 #include "zypp/pool/ByIdent.h"
-//#include "zypp/ResObject.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp