libzypp  14.36.0
RepoVariables.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
9 
10 #include "zypp/base/LogTools.h"
11 #include "zypp/base/String.h"
12 #include "zypp/base/Regex.h"
13 
14 #include "zypp/ZConfig.h"
15 #include "zypp/Target.h"
16 #include "zypp/Arch.h"
18 #include "zypp/base/NonCopyable.h"
19 
21 namespace zypp
22 {
23  namespace env
24  {
26  inline std::string ZYPP_REPO_RELEASEVER()
27  {
28  const char * env = getenv("ZYPP_REPO_RELEASEVER");
29  return( env ? env : "" );
30  }
31  }
32 
34  namespace repo
35  {
37  namespace
38  {
41  struct ReplacerData : private zypp::base::NonCopyable
42  {
43  typedef const std::string & (ReplacerData::*Getter)() const;
44 
45  const std::string & arch() const
46  {
47  assertArchStr();
48  return _arch;
49  }
50 
51  const std::string & basearch() const
52  {
53  assertArchStr();
54  return _basearch;
55  }
56 
57  const std::string & releasever() const
58  {
59  assertReleaseverStr();
60  return _releasever;
61  }
62 
63  const std::string & releaseverMajor() const
64  {
65  assertReleaseverStr();
66  return _releaseverMajor;
67  }
68 
69  const std::string & releaseverMinor() const
70  {
71  assertReleaseverStr();
72  return _releaseverMinor;
73  }
74 
75  private:
76  void assertArchStr() const
77  {
78  if ( _arch.empty() )
79  {
80  Arch arch( ZConfig::instance().systemArchitecture() );
81  _arch = arch.asString();
82  _basearch = arch.baseArch().asString();
83  }
84  }
85  void assertReleaseverStr() const
86  {
87  if ( _releasever.empty() )
88  {
90  if( _releasever.empty() )
91  _releasever = Target::distributionVersion( Pathname()/*guess*/ );
92  else
93  WAR << "ENV overwrites $releasever=" << _releasever << endl;
94 
95  // split major/minor for SLE
96  std::string::size_type pos = _releasever.find( "." );
97  if ( pos == std::string::npos )
98  {
100  _releaseverMinor.clear();
101  }
102  else
103  {
104  _releaseverMajor = _releasever.substr( 0, pos );
105  _releaseverMinor = _releasever.substr( pos+1 ) ;
106  }
107  }
108  }
109  private:
110  mutable std::string _arch;
111  mutable std::string _basearch;
112  mutable std::string _releasever;
113  mutable std::string _releaseverMajor;
114  mutable std::string _releaseverMinor;
115  };
116 
122  std::string replacer( std::string value_r )
123  {
124  std::string ret;
125  if ( ! value_r.empty() )
126  {
127  static const str::regex rxVAR( "^([^$]*)\\$(\\{[[:alnum:]_]+\\}|[[:alnum:]_]+)([^$]*)" );
128  str::smatch what;
129  while ( str::regex_match( value_r, what, rxVAR ) )
130  {
131  ReplacerData::Getter getter = nullptr;
132 
133  const char * varStart = value_r.c_str() + what.begin( 2 );
134  std::string::size_type varSize = what.size( 2 );
135  if ( *varStart == '{' ) // enclosed in {}
136  {
137  ++varStart;
138  varSize -= 2;
139  }
140 
141  switch ( varSize )
142  {
143 #define ASSIGN_IF(NAME,GETTER) if ( ::strncmp( varStart, NAME, varSize ) == 0 ) getter = GETTER
144 
145  case 4: ASSIGN_IF( "arch", &ReplacerData::arch ); break;
146  case 8: ASSIGN_IF( "basearch", &ReplacerData::basearch ); break;
147  case 10: ASSIGN_IF( "releasever", &ReplacerData::releasever ); break;
148  case 16: ASSIGN_IF( "releasever_major", &ReplacerData::releaseverMajor );
149  else ASSIGN_IF( "releasever_minor", &ReplacerData::releaseverMinor ); break;
150 #undef ASSIGN_IF
151  }
152 
153  if ( getter ) // known var?
154  {
155  static const ReplacerData _data;
156  if ( what.size( 1 ) > 0 ) ret += what[1]; // pre
157  ret += (_data.*getter)(); // var
158  if ( what.size( 3 ) > 0 ) ret += what[3]; // post
159  }
160  else
161  {
162  ret += what[0]; // unchanged
163  }
164 
165  value_r.erase( 0, what.size( 0 ) );
166  if ( value_r.empty() )
167  break;
168  }
169  if ( ! value_r.empty() )
170  ret += std::move(value_r); // no match
171  }
172  return ret;
173  }
174  } // namespace
176 
177  std::string RepoVariablesStringReplacer::operator()( const std::string & value ) const
178  {
179  return replacer( value );
180  }
181 
183  {
184  Url newurl( value );
185  newurl.setPathData( replacer( value.getPathData() ) );
186  newurl.setQueryString( replacer( value.getQueryString() ) );
187  return newurl;
188  }
189 
190  } // namespace repo
192 } // namespace zypp
std::string _releaseverMajor
std::string getPathData() const
Returns the encoded path component of the URL.
Definition: Url.cc:542
static ZConfig & instance()
Singleton ctor.
Definition: ZConfig.cc:655
std::string _basearch
std::string distributionVersion() const
This is version attribute of the installed base product.
Definition: Target.cc:134
std::string _arch
#define ASSIGN_IF(NAME, GETTER)
std::string _releasever
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition: NonCopyable.h:26
std::list< PublicKeyData > _data
Definition: KeyRing.cc:123
std::string operator()(const std::string &value_r) const
#define WAR
Definition: Logger.h:48
void setPathData(const std::string &pathdata)
Set the path data component in the URL.
Definition: Url.cc:700
std::string _releaseverMinor
std::string getQueryString() const
Returns the encoded query string component of the URL.
Definition: Url.cc:550
SolvableIdType size_type
Definition: PoolMember.h:99
void setQueryString(const std::string &querystr)
Set the query string in the URL.
Definition: Url.cc:708
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
Url operator()(const Url &url_r) const
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
std::string ZYPP_REPO_RELEASEVER()
Use faked releasever (e.g.
Url manipulation class.
Definition: Url.h:87