libzypp: zypp/Pattern.cc Source File

libzypp 17.38.11

Pattern.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <iostream>
13#include <zypp-core/base/LogTools.h>
14
15#include <zypp/ResPool.h>
16#include <zypp/Pattern.h>
17#include <zypp/Filter.h>
18
19using std::endl;
20
22namespace 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 using PatternMap = std::map<Pattern::constPtr, DefaultIntegral<bool, false>>;
50 using size_type = PatternMap::size_type;
51 using value_type = PatternMap::key_type;
52 using const_iterator = MapKVIteratorTraits<PatternMap>::Key_const_iterator;
53
54 public:
55 PatternExpander()
56 {}
57
59 size_type doExpand( const 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
177 Pattern::~Pattern()
178 {}
179
180 bool Pattern::isDefault() const
181 { return lookupBoolAttribute( sat::SolvAttr::isdefault ); }
182
183 bool Pattern::userVisible() const
184 {
185 // bsc#900769: If visibility is a string(solvable ident) the pattern
186 // is visible IFF ident is available in the pool.
187 IdString ident( lookupStrAttribute( sat::SolvAttr::isvisible ) );
188 return( ident.empty() ? lookupBoolAttribute( sat::SolvAttr::isvisible )
189 : ! ResPool::instance().byIdent( ident ).empty() );
190 }
191
192 std::string Pattern::category( const Locale & lang_r ) const
193 { return lookupStrAttribute( sat::SolvAttr::category, lang_r ); }
194
195 Pathname Pattern::icon() const
196 { return lookupStrAttribute( sat::SolvAttr::icon ); }
197
198 Pathname Pattern::script() const
199 { return lookupStrAttribute( sat::SolvAttr::script ); }
200
201 std::string Pattern::order() const
202 { return lookupStrAttribute( sat::SolvAttr::order ); }
203
204 bool Pattern::isAutoPattern() const
205 { return bool(autoCapability( dep_provides() )); }
206
207 sat::Solvable Pattern::autoPackage() const
208 {
209 Capability autocap( autoCapability( dep_provides() ) );
210 if ( autocap )
211 {
212 Capability pkgCap( arch(), autocap.detail().ed().asString(), Rel::EQ, edition() );
213 for ( const auto & solv: sat::WhatProvides( pkgCap ) )
214 if ( solv.repository() == repository() )
215 return solv;
216 }
217 return sat::Solvable();
218 }
219
220 Pattern::NameList Pattern::includes() const
221 { return NameList( sat::SolvAttr::includes, satSolvable() ); }
222
223 Pattern::NameList Pattern::extends() const
224 { return NameList( sat::SolvAttr::extends, satSolvable() ); }
225
227 namespace
228 {
229 inline void addCaps( CapabilitySet & caps_r, sat::Solvable solv_r, Dep dep_r )
230 {
231 Capabilities c( solv_r[dep_r] );
232 if ( ! c.empty() )
233 {
234 caps_r.insert( c.begin(),c.end() );
235 }
236 }
237 } //namespace
239
240 Pattern::Contents Pattern::core() const
241 {
242 // Content dependencies are either associated with
243 // the autoPackage or the (oldstype) pattern itself.
244 // load requires
245 CapabilitySet caps;
246 addCaps( caps, *this, Dep::REQUIRES );
247
248 sat::Solvable depKeeper( autoPackage() );
249 if ( depKeeper )
250 addCaps( caps, depKeeper, Dep::REQUIRES );
251 // get items providing the requirements
252 sat::WhatProvides prv( caps );
253 // return packages only.
254 return Pattern::Contents( make_filter_begin( filter::byKind<Package>(), prv ),
255 make_filter_end( filter::byKind<Package>(), prv ) );
256 }
257
258 Pattern::Contents Pattern::depends( bool includeSuggests_r ) const
259 {
260 // Content dependencies are either associated with
261 // the autoPackage or the (oldstype) pattern itself.
262 // load requires, recommends[, suggests]
263 CapabilitySet caps;
264 addCaps( caps, *this, Dep::REQUIRES );
265 addCaps( caps, *this, Dep::RECOMMENDS );
266 if ( includeSuggests_r )
267 addCaps( caps, *this, Dep::SUGGESTS );
268
269 sat::Solvable depKeeper( autoPackage() );
270 if ( depKeeper )
271 {
272 addCaps( caps, depKeeper, Dep::REQUIRES );
273 addCaps( caps, depKeeper, Dep::RECOMMENDS );
274 if ( includeSuggests_r )
275 addCaps( caps, depKeeper, Dep::SUGGESTS );
276 }
277 // get items providing the above
278 sat::WhatProvides prv( caps );
279 // return packages only.
280 return Pattern::Contents( make_filter_begin( filter::byKind<Package>(), prv ),
281 make_filter_end( filter::byKind<Package>(), prv ) );
282 }
283
284 Pattern::Contents Pattern::contents( bool includeSuggests_r ) const
285 {
286 PatternExpander expander;
287 if ( ! expander.doExpand( this ) )
288 return Contents(); // empty pattern set
289
290 Contents result;
291 for_( it, expander.begin(), expander.end() )
292 {
293 Contents c( (*it)->depends( includeSuggests_r ) );
294 result.get().insert( c.begin(), c.end() );
295 }
296 return result;
297 }
298
299#if LEGACY(1735)
301 namespace
302 {
303 // Get packages referenced by depKeeper dependency.
304 inline void dependsSetDoCollect( sat::Solvable depKeeper_r, Dep dep_r, Pattern::Contents & set_r )
305 {
306 CapabilitySet caps;
307 addCaps( caps, depKeeper_r, dep_r );
308 sat::WhatProvides prv( caps );
309 for ( const ui::Selectable::Ptr& sel : prv.selectable() )
310 {
311 const PoolItem & pi( sel->theObj() );
312 if ( pi.isKind<Package>() )
313 set_r.insert( pi );
314 }
315 }
316
317 // Get packages referenced by depKeeper.
318 inline void dependsSet( sat::Solvable depKeeper_r, Pattern::ContentsSet & collect_r )
319 {
320 dependsSetDoCollect( depKeeper_r, Dep::REQUIRES, collect_r.req );
321 dependsSetDoCollect( depKeeper_r, Dep::RECOMMENDS, collect_r.rec ),
322 dependsSetDoCollect( depKeeper_r, Dep::SUGGESTS, collect_r.sug );
323 }
324
325 // Whether this is a patterns depkeeper.
326 inline bool isPatternsPackage( sat::Solvable depKeeper_r )
327 {
328 static const Capability indicator( "pattern()" );
329 return depKeeper_r.dep_provides().matches( indicator );
330 }
331 } // namespace
333 void Pattern::contentsSet( ContentsSet & collect_r, bool recursively_r ) const
334 {
335 sat::Solvable depKeeper( autoPackage() ); // (my required) patterns-package
336 if ( ! depKeeper )
337 return;
338
339 // step 2 data
340 std::set<sat::Solvable> recTodo; // recommended patterns-packages to process
341 std::set<sat::Solvable> allDone; // patterns-packages already expanded
342 {
343 // step 1: Expand requirements, remember recommends....
344 // step 1 data (scoped to step1)
345 std::set<sat::Solvable> reqTodo; // required patterns-packages to process
346
347 collect_r.req.insert( depKeeper );// collect the depKeeper
348 reqTodo.insert( depKeeper ); // and expand it...
349
350 while ( ! reqTodo.empty() )
351 {
352 // pop one patterns-package from todo
353 depKeeper = ( *reqTodo.begin() );
354 reqTodo.erase( reqTodo.begin() );
355 allDone.insert( depKeeper );
356
357 // collects stats
358 ContentsSet result;
359 dependsSet( depKeeper, result );
360
361 // evaluate result....
362 for ( sat::Solvable solv : result.req ) // remember unprocessed required patterns-packages...
363 {
364 if ( collect_r.req.insert( solv ) && recursively_r && isPatternsPackage( solv ) )
365 reqTodo.insert( solv );
366 }
367 for ( sat::Solvable solv : result.rec ) // remember unprocessed recommended patterns-packages...
368 {
369 if ( collect_r.rec.insert( solv ) && recursively_r && isPatternsPackage( solv ) )
370 recTodo.insert( solv );
371 }
372 for ( sat::Solvable solv : result.sug ) // NOTE: We don't expand suggested patterns!
373 {
374 collect_r.sug.insert( solv );
375 }
376 }
377 }
378 // step 2: All requirements are expanded, now check remaining recommends....
379 while ( ! recTodo.empty() )
380 {
381 // pop one patterns-package from todo
382 depKeeper = ( *recTodo.begin() );
383 recTodo.erase( recTodo.begin() );
384 if ( ! allDone.insert( depKeeper ).second )
385 continue; // allready expanded (in requires)
386
387 // collects stats
388 ContentsSet result;
389 dependsSet( depKeeper, result );
390
391 // evaluate result....
392 for ( sat::Solvable solv : result.req ) // remember unprocessed required patterns-packages...
393 {
394 // NOTE: Requirements of recommended patterns count as 'recommended'
395 if ( collect_r.rec.insert( solv ) && recursively_r && isPatternsPackage( solv ) )
396 recTodo.insert( solv );
397 }
398 for ( sat::Solvable solv : result.rec ) // remember unprocessed recommended patterns-packages...
399 {
400 if ( collect_r.rec.insert( solv ) && recursively_r && isPatternsPackage( solv ) )
401 recTodo.insert( solv );
402 }
403 for ( sat::Solvable solv : result.sug ) // NOTE: We don't expand suggested patterns!
404 {
405 collect_r.sug.insert( solv );
406 }
407 }
408 }
409#endif
410
412} // namespace zypp
for_
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition Easy.h:27
Filter.h
LogTools.h
Pattern.h
ResPool.h
bool
zypp::CapDetail::ed
Edition ed() const
Definition Capability.h:421
zypp::CapMatch::yes
static const CapMatch yes
Definition CapMatch.h:52
zypp::Capabilities
Container of Capability (currently read only).
Definition Capabilities.h:35
zypp::Capability
A sat capability.
Definition Capability.h:63
zypp::Capability::detail
CapDetail detail() const
Helper providing more detailed information about a Capability.
Definition Capability.h:453
zypp::IdString
Access to the sat-pools string space.
Definition IdString.h:55
zypp::Locale
'Language[_Country]' codes.
Definition Locale.h:51
zypp::Package
Package interface.
Definition Package.h:34
zypp::Pattern
Pattern interface.
Definition Pattern.h:32
zypp::Pattern::core
Contents core() const
Ui hint: Required Packages.
Definition Pattern.cc:240
zypp::Pattern::extends
NameList extends() const
Ui hint: patterns this one extends.
Definition Pattern.cc:223
zypp::Pattern::category
std::string category(const Locale &lang_r=Locale()) const
Definition Pattern.cc:192
zypp::Pattern::constPtr
TraitsType::constPtrType constPtr
Definition Pattern.h:37
zypp::Pattern::depends
Contents depends(bool includeSuggests_r=true) const
Ui hint: Dependent packages.
Definition Pattern.cc:258
zypp::Pattern::script
Pathname script() const
Definition Pattern.cc:198
zypp::Pattern::Contents
sat::SolvableSet Contents
Definition Pattern.h:41
zypp::Pattern::isDefault
bool isDefault() const
Definition Pattern.cc:180
zypp::Pattern::Pattern
Pattern(const sat::Solvable &solvable_r)
Ctor.
Definition Pattern.cc:173
zypp::Pattern::userVisible
bool userVisible() const
Definition Pattern.cc:183
zypp::Pattern::order
std::string order() const
Definition Pattern.cc:201
zypp::Pattern::autoPackage
sat::Solvable autoPackage() const
The corresponding patterns- package if isAutoPattern.
Definition Pattern.cc:207
zypp::Pattern::includes
NameList includes() const
Ui hint: included patterns.
Definition Pattern.cc:220
zypp::Pattern::~Pattern
~Pattern() override
Dtor.
Definition Pattern.cc:177
zypp::Pattern::NameList
sat::ArrayAttr< IdString, IdString > NameList
Definition Pattern.h:40
zypp::Pattern::contents
Contents contents(bool includeSuggests_r=true) const
The collection of packages associated with this pattern.
Definition Pattern.cc:284
zypp::Pattern::icon
Pathname icon() const
Definition Pattern.cc:195
zypp::Pattern::isAutoPattern
bool isAutoPattern() const
This patterns is auto-defined by a patterns- package.
Definition Pattern.cc:204
zypp::PoolItem
Combining sat::Solvable and ResStatus.
Definition PoolItem.h:51
zypp::ResObject::ResObject
ResObject(const sat::Solvable &solvable_r)
Ctor.
Definition ResObject.cc:23
zypp::ResPool::instance
static ResPool instance()
Singleton ctor.
Definition ResPool.cc:38
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::sat::SolvAttr::icon
static const SolvAttr icon
Definition SolvAttr.h:151
zypp::sat::SolvAttr::isvisible
static const SolvAttr isvisible
Definition SolvAttr.h:150
zypp::sat::SolvAttr::category
static const SolvAttr category
Definition SolvAttr.h:154
zypp::sat::SolvAttr::isdefault
static const SolvAttr isdefault
Definition SolvAttr.h:153
zypp::sat::SolvAttr::extends
static const SolvAttr extends
Definition SolvAttr.h:157
zypp::sat::SolvAttr::script
static const SolvAttr script
Definition SolvAttr.h:155
zypp::sat::SolvAttr::order
static const SolvAttr order
Definition SolvAttr.h:152
zypp::sat::SolvAttr::includes
static const SolvAttr includes
Definition SolvAttr.h:156
zypp::sat::SolvableSet::get
Container & get()
The set.
Definition SolvableSet.h:98
zypp::sat::SolvableSet::insert
bool insert(const TSolv &solv_r)
Insert a Solvable.
Definition SolvableSet.h:88
zypp::sat::SolvableSet::end
const_iterator end() const
Iterator pointing behind the last Solvable.
Definition SolvableSet.h:76
zypp::sat::SolvableSet::begin
const_iterator begin() const
Iterator pointing to the first Solvable.
Definition SolvableSet.h:72
zypp::sat::Solvable
A Solvable object within the sat Pool.
Definition Solvable.h:54
zypp::sat::WhatProvides
Container of Solvable providing a Capability (read only).
Definition WhatProvides.h:89
zypp::ui::Selectable::Ptr
intrusive_ptr< Selectable > Ptr
Definition Selectable.h:58
zyppng::IdStringType::asString
std::string asString() const
Definition IdStringType.h:116
zypp::filter::byKind
ByKind byKind()
relates: ByKind templated convenience ctor.
Definition Filter.h:132
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::make_map_key_begin
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_begin(const TMap &map_r)
Convenience to create the key iterator from container::begin().
Definition Iterator.h:226
zypp::make_filter_begin
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_begin(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::begin().
Definition Iterator.h:101
zypp::make_map_key_end
MapKVIteratorTraits< TMap >::Key_const_iterator make_map_key_end(const TMap &map_r)
Convenience to create the key iterator from container::end().
Definition Iterator.h:231
zypp::CapabilitySet
std::unordered_set< Capability > CapabilitySet
Definition Capability.h:35
zypp::make_filter_end
filter_iterator< TFilter, typename TContainer::const_iterator > make_filter_end(TFilter f, const TContainer &c)
Convenience to create filter_iterator from container::end().
Definition Iterator.h:117
zypp::asKind
ResTraits< TRes >::PtrType asKind(const sat::Solvable &solvable_r)
Directly create a certain kind of ResObject from sat::Solvable.
Definition ResObject.h:127
zypp::Dep
Enumeration class of dependency types.
Definition Dep.h:31
zypp::MapKVIteratorTraits::Key_const_iterator
transform_iterator< GetPairFirst< typename MapType::value_type >, typename MapType::const_iterator > Key_const_iterator
The key iterator type.
Definition Iterator.h:217
zypp::Rel::EQ
static const Rel EQ
Definition Rel.h:52
zypp::sat::SolvableType< Resolvable >::arch
Arch arch() const
Definition SolvableType.h:72
zypp::sat::SolvableType< Resolvable >::lookupBoolAttribute
bool lookupBoolAttribute(const SolvAttr &attr) const
Definition SolvableType.h:160
zypp::sat::SolvableType< Resolvable >::dep_provides
Capabilities dep_provides() const
Definition SolvableType.h:110
zypp::sat::SolvableType< Resolvable >::lookupStrAttribute
std::string lookupStrAttribute(const SolvAttr &attr) const
Definition SolvableType.h:158
zypp::sat::SolvableType< Resolvable >::satSolvable
Solvable satSolvable() const
Definition SolvableType.h:57
zypp::sat::SolvableType< Resolvable >::ident
IdString ident() const
Definition SolvableType.h:61
zypp::sat::SolvableType< Resolvable >::repository
Repository repository() const
Definition SolvableType.h:75
zypp::sat::SolvableType< Resolvable >::edition
Edition edition() const
Definition SolvableType.h:71
IMPL_PTR_TYPE
#define IMPL_PTR_TYPE(NAME)
Definition ReferenceCounted.h:137