24#define ZYPP_DBG_VAREXPAND 0
25#if ( ZYPP_DBG_VAREXPAND )
26#warning ZYPP_DBG_VAREXPAND is on
38 const char *
env = getenv(
"ZYPP_REPO_RELEASEVER");
65 FindVar(
const std::string & str_r,
bool embedded_r )
66 : _embedded( embedded_r )
67 , _sbeg( str_r.c_str() )
72 , _send( findVarStart( _sbeg ) )
86 if ( _vbeg && !_vend )
87 _send = findVarStart( _vbeg+1 );
89 _nbeg = _nend = _vend = _send =
nullptr;
92 }
while( ! findVarEnd() );
106 std::string var()
const
107 {
return std::string( _vbeg, _vend ); }
110 std::string varName()
const
111 {
return std::string( _nbeg, _nend ); }
114 bool varIsConditional()
const
115 {
return( *(_vbeg+1) ==
'{' && *_nend ==
':' ); }
124 {
return( varIsConditional() ? *(_nend+1) : *_vbeg ); }
127 std::string varEmbedded()
const
128 {
return( varIsConditional() ? std::string( _nend+2, _vend-1 ) : std::string() ); }
132 bool hasVarPrefix()
const
133 {
return ( _sbeg != _vbeg ); }
136 std::string varPrefix()
const
137 {
return std::string( _sbeg, _vbeg ); }
145 const char * findVarStart(
const char * sbeg_r )
const
147 for ( ; *sbeg_r; ++sbeg_r )
148 if ( *sbeg_r ==
'$' || ( _embedded && *sbeg_r ==
'\\' ) )
154 bool isnamech(
int ch )
const
155 {
return ch ==
'_' || isalnum( ch ); }
161 if ( ! findVarEnd( _vbeg, _nbeg, _nend, _vend ) )
163 _send = findVarStart( _vend );
168 const char * findVarEnd(
const char * vbeg )
const
171 const char * nbeg =
nullptr;
172 const char * nend =
nullptr;
173 const char * vend =
nullptr;
174 findVarEnd( vbeg, nbeg, nend, vend );
179 bool findVarEnd(
const char * vbeg,
const char *& nbeg,
const char *& nend,
const char *& vend )
const
182 if ( *_vbeg ==
'\\' )
189 nend = vend = vbeg+2;
198 bool braced = ( *(vbeg+1) ==
'{' );
199 nbeg = vbeg+( braced ? 2 : 1 );
200 if ( !isnamech( *nbeg ) )
202 for ( nend = nbeg+1; isnamech( *nend ); ++nend )
214 else if ( *nend ==
':' )
216 const char * scan = nend+1;
217 if ( *scan ==
'+' || *scan ==
'-' )
229 else if ( *scan ==
'$' )
232 if ( ! (scan = findVarEnd( scan )) )
235 else if ( *scan ==
'}' )
255 return( vend !=
nullptr );
264 if ( ! _expand( ret, value_r, level_r, varRetriever_r ) )
272 if ( ! _expand( ret, value_r, level_r, varRetriever_r ) )
273 ret = std::move(value_r);
280 inline bool _expand( std::string & result_r,
const std::string & value_r,
unsigned level_r,
RepoVarExpand::VarRetriever & varRetriever_r )
282#if ( ZYPP_DBG_VAREXPAND )
283 cout << std::string( 2*level_r,
' ' ) <<
"\033[7m>>" << value_r <<
"<<\033[27m" << endl;
284 std::ostringstream dbg;
285 const char * dbgsbeg = value_r.c_str();
287 dbg << std::string( 2*level_r,
' ' ) <<
">>";
290 bool expanded =
false;
292 if ( ! value_r.empty() )
294 FindVar scan( value_r, level_r );
295 while ( scan.nextVar() )
297 static const std::string _emptyValue;
298 const std::string *
const knownVar = ( varRetriever_r ? varRetriever_r( scan.varName() ) : nullptr );
299 const std::string & varValue( knownVar ? *knownVar : _emptyValue );
301#if ( ZYPP_DBG_VAREXPAND )
302 dbg << std::string(dbgsbeg,scan._vbeg) <<
"\033[3" << ((dbgi%5)+1) <<
"m" << scan.var() <<
"\033[0m";
303 cout << dbg.str() <<
"|<< " << scan.varName() <<
" " << (knownVar?
"("+varValue+
")":
"-") <<
" {" << scan.varEmbedded() <<
"}" << endl;
304 dbgsbeg = scan._vend;
308 bool mustSubstitute =
false;
309 std::string substitutionValue;
311 int varType = scan.varType();
312 if ( varType ==
'$' )
316 mustSubstitute =
true;
317 substitutionValue = varValue;
322 else if ( varType ==
'-' )
324 mustSubstitute =
true;
325 if ( varValue.empty() )
326 substitutionValue = expand( scan.varEmbedded(), level_r+1, varRetriever_r );
328 substitutionValue = varValue;
330 else if ( varType ==
'+' )
332 mustSubstitute =
true;
333 if ( ! varValue.empty() )
334 substitutionValue = expand( scan.varEmbedded(), level_r+1, varRetriever_r );
338 else if ( varType ==
'\\' )
340 mustSubstitute =
true;
341 substitutionValue = scan.varName();
346 if ( mustSubstitute )
348 if ( scan.hasVarPrefix() )
349 result_r += scan.varPrefix();
350 if ( ! substitutionValue.empty() )
351 result_r += substitutionValue;
356#if ( ZYPP_DBG_VAREXPAND )
357 dbg << std::string( dbgsbeg ) << (scan._sbeg == value_r.c_str() ?
"<<\033[36m(moved)\033[0m" :
"");
361 if ( scan._sbeg != value_r.c_str() )
365 result_r += std::string( scan._sbeg );
371#if ( ZYPP_DBG_VAREXPAND )
373 cout << dbg.str() << endl;
374 cout << std::string( 2*level_r,
' ' ) <<
"\033[36m->" << result_r <<
"<-\033[0m" << endl;
382 {
return expand( value_r, 0, varRetriever_r ); }
385 {
return expand( std::move(value_r), 0, varRetriever_r ); }
392 class RepoVarsMap :
public std::map<std::string,std::string>
395 static RepoVarsMap & instance()
396 {
static RepoVarsMap _instance;
return _instance; }
398 static const std::string * lookup(
const std::string & name_r )
399 {
return instance()._lookup( name_r ); }
402 const std::string * _lookup(
const std::string & name_r )
416 if ( contextRoot != _contextRoot ) {
417 MIL <<
"RepoVars context changed from " << _contextRoot <<
" -> " << contextRoot << endl;
418 _contextRoot = contextRoot;
429 deriveFromReleasever(
"releasever",
false );
436 std::string & var(
operator[](
"arch" ) );
437 if ( var.empty() ) var = arch.asString();
440 std::string & var(
operator[](
"basearch" ) );
441 if ( var.empty() ) var = arch.baseArch().asString();
446 const std::string * ret = checkOverride( name_r, contextRoot );
450 iterator it = find( name_r );
458 std::ostream &
dumpOn( std::ostream & str )
const
460 for (
auto && kv : *
this )
462 str <<
'{' << kv.first <<
'=' << kv.second <<
'}' << endl;
469 bool parse(
const Pathname & dir_r,
const std::string & str_r )
471 std::ifstream file( (dir_r/str_r).c_str() );
477 void deriveFromReleasever(
const std::string & stem_r,
bool overwrite_r )
479 if ( count( stem_r ) )
481 const std::string & stem_major( stem_r+
"_major" );
482 const std::string & stem_minor( stem_r+
"_minor" );
484 splitReleaseverTo(
operator[]( stem_r ), &
operator[]( stem_major ), &
operator[]( stem_minor ) );
486 splitReleaseverTo(
operator[]( stem_r ),
487 count( stem_major ) ?
nullptr : &
operator[]( stem_major ),
488 count( stem_minor ) ?
nullptr : &
operator[]( stem_minor ) );
493 void splitReleaseverTo(
const std::string & releasever_r, std::string * major_r, std::string * minor_r )
const
495 if ( major_r || minor_r )
497 std::string::size_type pos = releasever_r.find(
'.' );
498 if ( pos == std::string::npos )
500 if ( major_r ) *major_r = releasever_r;
501 if ( minor_r ) minor_r->clear();
505 if ( major_r ) *major_r = releasever_r.substr( 0, pos );
506 if ( minor_r ) *minor_r = releasever_r.substr( pos+1 ) ;
512 const std::string * checkOverride(
const std::string & name_r,
const Pathname & contextRoot_r )
517 && ( name_r.size() == 10
518 || strcmp( name_r.c_str()+10,
"_minor" ) == 0
519 || strcmp( name_r.c_str()+10,
"_major" ) == 0 ) )
525 if ( val !=
operator[](
"$releasever" ) )
527 operator[](
"$releasever" ) = std::move(val);
528 deriveFromReleasever(
"$releasever",
true );
530 return &operator[](
"$"+name_r );
532 else if ( !count( name_r ) )
537 if ( val !=
operator[](
"$_releasever" ) )
539 operator[](
"$_releasever" ) = std::move(val);
540 deriveFromReleasever(
"$_releasever",
true );
542 return &operator[](
"$_"+name_r );
553 Pathname _contextRoot;
564 return RepoVarExpand()( std::move(value), RepoVarsMap::lookup );
573 Url tmpurl { value };
578 if ( !replaced.empty() )
594 using namespace zypp;
597 { repo::RepoVarsMap::instance().clear(); }
std::string distributionVersion() const
This is version attribute of the installed base product.
void setViewOptions(const ViewOptions &vopts)
Change the view options of the current object.
zypp::url::ViewOptions ViewOptions
View options.
std::string getUsername(EEncoding eflag=zypp::url::E_DECODED) const
Returns the username from the URL authority.
ViewOptions getViewOptions() const
Return the view options of the current object.
void setPassword(const std::string &pass, EEncoding eflag=zypp::url::E_DECODED)
Set the password in the URL authority.
void setUsername(const std::string &user, EEncoding eflag=zypp::url::E_DECODED)
Set the username in the URL authority.
std::string getPassword(EEncoding eflag=zypp::url::E_DECODED) const
Returns the password from the URL authority.
Pathname repoManagerRoot() const
The RepoManager root directory.
static ZConfig & instance()
Singleton ctor.
Pathname varsPath() const
Path containing custom repo variable definitions (configPath()/vars.d).
ZYpp::Ptr getZYpp()
Convenience to get the Pointer to the ZYpp instance.
Namespace intended to collect all environment variables we use.
std::string ZYPP_REPO_RELEASEVER()
Use faked releasever (e.g.
const StrMatcher & matchNoDots()
Convenience returning StrMatcher( "[^.]*", Match::GLOB )
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
std::string asString(const Url &url_r)
bool empty() const
Whether neither idents nor provides are set.
void parse(const C_Str &spec_r)
Parse and add spec from a string (IDENT or provides:CAPABILITY`).
bool startsWith(const C_Str &str_r, const C_Str &prefix_r)
alias for hasPrefix
std::string getline(std::istream &str, const Trim trim_r)
Return stream content up to (but not returning) the next newline.
@ E_ENCODED
Flag to request encoded string(s).
Easy-to use interface to the ZYPP dependency resolver.
std::ostream & dumpOn(std::ostream &str, const Capability &obj)
void repoVariablesReset()
Functor expanding repo variables in a string.
function< const std::string *(const std::string &)> VarRetriever
Function taking a variable name and returning a pointer to the variable value or nullptr if unset.
std::string operator()(const std::string &value_r, VarRetriever varRetriever_r) const
Return a copy of value_r with embedded variables expanded.
std::string operator()(const std::string &value_r) const
Url operator()(const Url &url_r) const