libzypp  14.48.5
Pattern.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include "zypp/base/LogTools.h"
14 
15 #include "zypp/ResPool.h"
16 #include "zypp/Pattern.h"
17 #include "zypp/Filter.h"
18 
19 using std::endl;
20 
22 namespace zypp
23 {
25  namespace
26  {
27  inline Capability autoCapability( const Capabilities & provides_r )
28  {
29  static const Capability autopattern( "autopattern()" );
30  for ( const auto & cap : provides_r )
31  if ( cap.matches( autopattern ) == CapMatch::yes )
32  return cap;
33  return Capability();
34  }
35 
37  //
38  // CLASS NAME : PatternExpander
39  //
46  class PatternExpander
47  {
48  public:
49  typedef std::map<Pattern::constPtr, DefaultIntegral<bool, false> > PatternMap;
51  typedef PatternMap::key_type value_type;
52  typedef MapKVIteratorTraits<PatternMap>::Key_const_iterator const_iterator;
53 
54  public:
55  PatternExpander()
56  {}
57 
59  size_type doExpand( Pattern::constPtr pat_r )
60  {
61  // INT << "+++ " << pat_r << " ++++++++++++++++++++++++++++++++++" << endl;
62  _patternMap.clear();
63  if ( pat_r )
64  {
65  _patternMap[pat_r];
66  Pattern::constPtr unprocessed( pat_r );
67  // MIL << _patternMap << endl;
68  do {
69  expandIncludes( unprocessed );
70  expandExtending( unprocessed );
71  _patternMap[unprocessed] = true;
72  // MIL << _patternMap << endl;
73  } while( (unprocessed = nextUnprocessed()) );
74  }
75  // SEC << "--- " << _patternMap.size() << " ----------------------------------" << endl;
76  return _patternMap.size();
77  }
78 
79  const_iterator begin() const
80  { return make_map_key_begin( _patternMap ); }
81 
82  const_iterator end() const
83  { return make_map_key_end( _patternMap ); }
84 
85  private:
87  Pattern::constPtr nextUnprocessed() const
88  {
89  for_( it, _patternMap.begin(), _patternMap.end() )
90  {
91  if ( ! it->second )
92  return it->first;
93  }
94  return NULL;
95  }
96 
97  private:
99  void expandIncludes( const Pattern::constPtr & pat_r )
100  {
101  Pattern::NameList c( pat_r->includes() );
102  for_( it, c.begin(), c.end() )
103  {
104  expandInclude( Capability( it->c_str()/*, *ResKind::pattern*/ ) );
105  }
106  }
107 
109  void expandInclude( const Capability & include_r )
110  {
111  sat::WhatProvides w( include_r );
112  for_( it, w.begin(), w.end() )
113  {
114  _patternMap[asKind<Pattern>(PoolItem(*it))];
115  }
116  }
117 
118  private:
120  void expandExtending( const Pattern::constPtr & pat_r )
121  {
122  ResPool pool( ResPool::instance() );
123  for_( it, pool.byKindBegin<Pattern>(), pool.byKindEnd<Pattern>() )
124  {
125  expandIfExtends( pat_r, *it );
126  }
127  }
128 
130  void expandIfExtends( const Pattern::constPtr & pat_r, const PoolItem & extending_r )
131  {
132  Pattern::constPtr extending( asKind<Pattern>(extending_r) );
133  Pattern::NameList c( extending->extends() );
134  for_( it, c.begin(), c.end() )
135  {
136  if ( providedBy( pat_r, Capability( it->c_str()/*, *ResKind::pattern*/ ) ) )
137  {
138  // an extends matches the Pattern
139  _patternMap[extending];
140  break;
141  }
142  }
143  }
144 
146  bool providedBy( const Pattern::constPtr & pat_r, const Capability & extends_r )
147  {
148  if ( !pat_r )
149  return false;
150 
151  sat::Solvable pat( pat_r->satSolvable() );
152  sat::WhatProvides w( extends_r );
153  for_( it, w.begin(), w.end() )
154  {
155  if ( pat == *it )
156  return true;
157  }
158  return false;
159  }
160 
161  private:
162  PatternMap _patternMap;
163  };
164  } // namespace
166 
168  // Pattern
170 
171  IMPL_PTR_TYPE(Pattern);
172 
173  Pattern::Pattern( const sat::Solvable & solvable_r )
174  : ResObject( solvable_r )
175  {}
176 
178  {}
179 
180  bool Pattern::isDefault() const
182 
183  bool Pattern::userVisible() const
185 
186  std::string Pattern::category( const Locale & lang_r ) const
187  { return lookupStrAttribute( sat::SolvAttr::category, lang_r ); }
188 
189  Pathname Pattern::icon() const
191 
192  Pathname Pattern::script() const
194 
195  std::string Pattern::order() const
197 
199  { return bool(autoCapability( provides() )); }
200 
202  {
203  Capability autocap( autoCapability( provides() ) );
204  if ( autocap )
205  {
206  Capability pkgCap( arch(), autocap.detail().ed().asString(), Rel::EQ, edition() );
207  for ( const auto & solv: sat::WhatProvides( pkgCap ) )
208  if ( solv.repository() == repository() )
209  return solv;
210  }
211  return sat::Solvable();
212  }
213 
216 
219 
221  namespace
222  {
223  inline void addCaps( CapabilitySet & caps_r, sat::Solvable solv_r, Dep dep_r )
224  {
225  Capabilities c( solv_r[dep_r] );
226  if ( ! c.empty() )
227  {
228  caps_r.insert( c.begin(),c.end() );
229  }
230  }
231  } //namespace
233 
235  {
236  // Content dependencies are either associated with
237  // the autoPackage or the (oldstype) pattern itself.
238  // load requires
239  CapabilitySet caps;
240  addCaps( caps, *this, Dep::REQUIRES );
241 
242  sat::Solvable depKeeper( autoPackage() );
243  if ( depKeeper )
244  addCaps( caps, depKeeper, Dep::REQUIRES );
245  // get items providing the requirements
246  sat::WhatProvides prv( caps );
247  // return packages only.
248  return Pattern::Contents( make_filter_begin( filter::byKind<Package>(), prv ),
249  make_filter_end( filter::byKind<Package>(), prv ) );
250  }
251 
252  Pattern::Contents Pattern::depends( bool includeSuggests_r ) const
253  {
254  // Content dependencies are either associated with
255  // the autoPackage or the (oldstype) pattern itself.
256  // load requires, recommends[, suggests]
257  CapabilitySet caps;
258  addCaps( caps, *this, Dep::REQUIRES );
259  addCaps( caps, *this, Dep::RECOMMENDS );
260  if ( includeSuggests_r )
261  addCaps( caps, *this, Dep::SUGGESTS );
262 
263  sat::Solvable depKeeper( autoPackage() );
264  if ( depKeeper )
265  {
266  addCaps( caps, depKeeper, Dep::REQUIRES );
267  addCaps( caps, depKeeper, Dep::RECOMMENDS );
268  if ( includeSuggests_r )
269  addCaps( caps, depKeeper, Dep::SUGGESTS );
270  }
271  // get items providing the above
272  sat::WhatProvides prv( caps );
273  // return packages only.
274  return Pattern::Contents( make_filter_begin( filter::byKind<Package>(), prv ),
275  make_filter_end( filter::byKind<Package>(), prv ) );
276  }
277 
278  Pattern::Contents Pattern::contents( bool includeSuggests_r ) const
279  {
280  PatternExpander expander;
281  if ( ! expander.doExpand( this ) )
282  return Contents(); // empty pattern set
283 
284  Contents result;
285  for_( it, expander.begin(), expander.end() )
286  {
287  Contents c( (*it)->depends( includeSuggests_r ) );
288  result.get().insert( c.begin(), c.end() );
289  }
290  return result;
291  }
292 
294 } // namespace zypp
std::string order() const
Definition: Pattern.cc:195
bool userVisible() const
Definition: Pattern.cc:183
sat::Solvable autoPackage() const
The corresponding patterns- package if isAutoPattern.
Definition: Pattern.cc:201
Pathname icon() const
Definition: Pattern.cc:189
A Solvable object within the sat Pool.
Definition: Solvable.h:55
Container of Solvable providing a Capability (read only).
Definition: WhatProvides.h:87
static const Dep RECOMMENDS
Definition: Dep.h:47
static const SolvAttr order
Definition: SolvAttr.h:132
static const SolvAttr isvisible
Definition: SolvAttr.h:130
static const SolvAttr isdefault
Definition: SolvAttr.h:133
Container of Capability (currently read only).
Definition: Capabilities.h:35
sat::ArrayAttr< IdString, IdString > NameList
Definition: Pattern.h:39
Enumeration class of dependency types.
Definition: Dep.h:29
Repository repository() const
Definition: ResObject.h:184
static const Rel EQ
Definition: Rel.h:50
Arch arch() const
Definition: Resolvable.h:79
std::string lookupStrAttribute(const SolvAttr &attr) const
returns the string attribute value for attr or an empty string if it does not exists.
Definition: Solvable.cc:144
Capabilities provides() const
Definition: Resolvable.h:100
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
bool isAutoPattern() const
This patterns is auto-defined by a patterns- package.
Definition: Pattern.cc:198
LookupAttr::transformIterator based container to retrieve list attributes.
Definition: LookupAttr.h:591
filter_iterator< _Filter, typename _Container::const_iterator > make_filter_begin(_Filter f, const _Container &c)
Convenience to create filter_iterator from container::begin().
Definition: Iterator.h:99
std::tr1::unordered_set< Capability > CapabilitySet
Definition: Capability.h:33
static const SolvAttr icon
Definition: SolvAttr.h:131
static const Dep SUGGESTS
Definition: Dep.h:48
Pathname script() const
Definition: Pattern.cc:192
CapDetail detail() const
Helper providing more detailed information about a Capability.
Definition: Capability.h:370
std::string asString() const
Definition: IdStringType.h:106
sat::SolvableSet Contents
Definition: Pattern.h:40
NameList extends() const
Ui hint: patterns this one extends.
Definition: Pattern.cc:217
bool lookupBoolAttribute(const SolvAttr &attr) const
returns the boolean attribute value for attr or false if it does not exists.
Definition: Solvable.cc:176
bool isDefault() const
Definition: Pattern.cc:180
const sat::Solvable & satSolvable() const
Access the corresponding ::Solvable.
Definition: Resolvable.h:136
IMPL_PTR_TYPE(Application)
static const SolvAttr category
Definition: SolvAttr.h:134
static const Dep REQUIRES
Definition: Dep.h:44
Contents core() const
Ui hint: Required Packages.
Definition: Pattern.cc:234
Interface base for resolvable objects (common data).
Definition: ResObject.h:44
TraitsType::constPtrType constPtr
Definition: Pattern.h:36
Container & get()
The set.
Definition: SolvableSet.h:90
SolvableIdType size_type
Definition: PoolMember.h:147
static const SolvAttr includes
Definition: SolvAttr.h:136
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_begin(const _Map &map_r)
Convenience to create the key iterator from container::begin()
Definition: Iterator.h:226
virtual ~Pattern()
Dtor.
Definition: Pattern.cc:177
Contents contents(bool includeSuggests_r=true) const
The collection of packages associated with this pattern.
Definition: Pattern.cc:278
NameList includes() const
Ui hint: included patterns.
Definition: Pattern.cc:214
Contents depends(bool includeSuggests_r=true) const
Ui hint: Dependent packages.
Definition: Pattern.cc:252
Pattern(const sat::Solvable &solvable_r)
Ctor.
Definition: Pattern.cc:173
A sat capability.
Definition: Capability.h:59
Edition ed() const
Definition: Capability.h:338
filter_iterator< _Filter, typename _Container::const_iterator > make_filter_end(_Filter f, const _Container &c)
Convenience to create filter_iterator from container::end().
Definition: Iterator.h:115
Edition edition() const
Definition: Resolvable.h:76
std::string category(const Locale &lang_r=Locale()) const
Definition: Pattern.cc:186
MapKVIteratorTraits< _Map >::Key_const_iterator make_map_key_end(const _Map &map_r)
Convenience to create the key iterator from container::end()
Definition: Iterator.h:231
static const CapMatch yes
Definition: CapMatch.h:51
PatternMap _patternMap
Definition: Pattern.cc:162
static const SolvAttr extends
Definition: SolvAttr.h:137
static const SolvAttr script
Definition: SolvAttr.h:135
static ResPool instance()
Singleton ctor.
Definition: ResPool.cc:33
Solvable set wrapper to allow adding additioanal convenience iterators.
Definition: SolvableSet.h:35