libzypp  16.2.1
RepoFileReader.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 #include "zypp/base/String.h"
15 #include "zypp/base/Regex.h"
16 #include "zypp/base/InputStream.h"
18 
19 #include "zypp/parser/IniDict.h"
21 
22 using std::endl;
23 
25 namespace zypp
26 {
28  namespace parser
29  {
31  namespace {
32 
37  class RepoFileParser : public IniDict
38  {
39  public:
40  RepoFileParser( const InputStream & is_r )
41  { read( is_r ); }
42 
43  using IniDict::consume; // don't hide overloads we don't redefine here
44 
45  virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
46  {
47  if ( key_r == "baseurl" )
48  {
49  setInBaseurls( true );
50  _baseurls[section_r].push_back( Url(value_r) );
51  }
52  else
53  {
54  setInBaseurls( false );
55  IniDict::consume( section_r, key_r, value_r );
56  }
57  }
58 
59  virtual void garbageLine( const std::string & section_r, const std::string & line_r )
60  {
61  if ( _inBaseurls )
62  _baseurls[section_r].push_back( Url(line_r) );
63  else
64  IniDict::garbageLine( section_r, line_r ); // throw
65  }
66 
67  std::list<Url> & baseurls( const std::string & section_r )
68  { return _baseurls[section_r]; }
69 
70  private:
71  void setInBaseurls( bool yesno_r )
72  { if ( _inBaseurls != yesno_r ) _inBaseurls = yesno_r; }
73 
74  DefaultIntegral<bool,false> _inBaseurls;
75  std::map<std::string,std::list<Url>> _baseurls;
76  };
77 
78  } //namespace
80 
85  static void repositories_in_stream( const InputStream &is,
86  const RepoFileReader::ProcessRepo &callback,
87  const ProgressData::ReceiverFnc &progress )
88  {
89  RepoFileParser dict(is);
90  for_( its, dict.sectionsBegin(), dict.sectionsEnd() )
91  {
92  RepoInfo info;
93  info.setAlias(*its);
94  std::string proxy;
95  std::string proxyport;
96 
97  for_( it, dict.entriesBegin(*its), dict.entriesEnd(*its) )
98  {
99  //MIL << (*it).first << endl;
100  if (it->first == "name" )
101  info.setName(it-> second);
102  else if ( it->first == "enabled" )
103  info.setEnabled( str::strToTrue( it->second ) );
104  else if ( it->first == "priority" )
105  info.setPriority( str::strtonum<unsigned>( it->second ) );
106  else if ( it->first == "path" )
107  info.setPath( Pathname(it->second) );
108  else if ( it->first == "type" )
109  info.setType(repo::RepoType(it->second));
110  else if ( it->first == "autorefresh" )
111  info.setAutorefresh( str::strToTrue( it->second ) );
112  else if ( it->first == "mirrorlist" && !it->second.empty())
113  info.setMirrorListUrl(Url(it->second));
114  else if ( it->first == "metalink" && !it->second.empty())
115  info.setMetalinkUrl(Url(it->second));
116  else if ( it->first == "gpgkey" && !it->second.empty())
117  {
118  std::vector<std::string> keys;
119  str::split( it->second, std::back_inserter(keys) );
120  if ( ! keys.empty() )
121  info.setGpgKeyUrl( Url(*keys.begin()) );
122  }
123  else if ( it->first == "gpgcheck" )
124  info.setGpgCheck( str::strToTriBool( it->second ) );
125  else if ( it->first == "repo_gpgcheck" )
126  info.setRepoGpgCheck( str::strToTrue( it->second ) );
127  else if ( it->first == "pkg_gpgcheck" )
128  info.setPkgGpgCheck( str::strToTrue( it->second ) );
129  else if ( it->first == "keeppackages" )
130  info.setKeepPackages( str::strToTrue( it->second ) );
131  else if ( it->first == "service" )
132  info.setService( it->second );
133  else if ( it->first == "proxy" )
134  {
135  // Translate it into baseurl queryparams
136  // NOTE: The hack here does not add proxy to mirrorlist urls but the
137  // original code worked without complains, so keep it for now.
138  static const str::regex ex( ":[0-9]+$" ); // portspec
139  str::smatch what;
140  if ( str::regex_match( it->second, what, ex ) )
141  {
142  proxy = it->second.substr( 0, it->second.size() - what[0].size() );
143  proxyport = what[0].substr( 1 );
144  }
145  else
146  {
147  proxy = it->second;
148  }
149  }
150  else
151  ERR << "Unknown attribute in [" << *its << "]: " << it->first << "=" << it->second << " ignored" << endl;
152  }
153 
154  for ( auto & url : dict.baseurls( *its ) )
155  {
156  if ( ! proxy.empty() && url.getQueryParam( "proxy" ).empty() )
157  {
158  url.setQueryParam( "proxy", proxy );
159  url.setQueryParam( "proxyport", proxyport );
160  }
161  info.addBaseUrl( url );
162  }
163 
164  info.setFilepath(is.path());
165  MIL << info << endl;
166  // add it to the list.
167  callback(info);
168  //if (!progress.tick())
169  // ZYPP_THROW(AbortRequestException());
170  }
171  }
172 
174  //
175  // CLASS NAME : RepoFileReader
176  //
178 
179  RepoFileReader::RepoFileReader( const Pathname & repo_file,
180  const ProcessRepo & callback,
181  const ProgressData::ReceiverFnc &progress )
182  : _callback(callback)
183  {
184  repositories_in_stream(InputStream(repo_file), _callback, progress);
185  }
186 
188  const ProcessRepo & callback,
189  const ProgressData::ReceiverFnc &progress )
190  : _callback(callback)
191  {
192  repositories_in_stream(is, _callback, progress);
193  }
194 
196  {}
197 
198 
199  std::ostream & operator<<( std::ostream & str, const RepoFileReader & obj )
200  {
201  return str;
202  }
203 
204  } // namespace parser
206 } // namespace zypp
TriBool strToTriBool(const C_Str &str)
Parse str into a bool if it&#39;s a legal true or false string; else indterminate.
Definition: String.cc:91
#define MIL
Definition: Logger.h:64
void setGpgKeyUrl(const Url &gpgkey)
Key to use for gpg checking of this repository.
Definition: RepoInfo.cc:333
void setQueryParam(const std::string &param, const std::string &value)
Set or add value for the specified query parameter.
Definition: Url.cc:827
function< bool(const RepoInfo &)> ProcessRepo
Callback definition.
void setAutorefresh(bool autorefresh)
enable or disable autorefresh
Definition: RepoInfoBase.cc:91
Regular expression.
Definition: Regex.h:86
void setPriority(unsigned newval_r)
Set repository priority for solver.
Definition: RepoInfo.cc:271
virtual void consume(const std::string &section)
Called when a section is found.
Definition: IniDict.cc:60
void setMirrorListUrl(const Url &url)
Set mirror list url.
Definition: RepoInfo.cc:327
void setEnabled(bool enabled)
enable or disable the repository
Definition: RepoInfoBase.cc:88
std::map< std::string, std::list< Url > > _baseurls
void setAlias(const std::string &alias)
set the repository alias
Definition: RepoInfoBase.cc:94
String related utilities and Regular expression matching.
void setFilepath(const Pathname &filename)
set the path to the .repo file
What is known about a repository.
Definition: RepoInfo.h:71
void setGpgCheck(TriBool value_r)
Set the value for gpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:278
Helper to create and pass std::istream.
Definition: InputStream.h:56
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:27
RepoFileReader(const Pathname &repo_file, const ProcessRepo &callback, const ProgressData::ReceiverFnc &progress=ProgressData::ReceiverFnc())
Constructor.
map< string, string > read(const Pathname &_path)
Read sysconfig file path_r and return (key,valye) pairs.
Definition: Sysconfig.cc:34
function< bool(const ProgressData &)> ReceiverFnc
Most simple version of progress reporting The percentage in most cases.
Definition: ProgressData.h:139
const ProcessCredentials & _callback
#define ERR
Definition: Logger.h:66
static void repositories_in_stream(const InputStream &is, const RepoFileReader::ProcessRepo &callback, const ProgressData::ReceiverFnc &progress)
List of RepoInfo&#39;s from a file.
unsigned size() const
Definition: Regex.cc:95
unsigned split(const C_Str &line_r, TOutputIterator result_r, const C_Str &sepchars_r=" \t")
Split line_r into words.
Definition: String.h:519
void setRepoGpgCheck(TriBool value_r)
Set the value for repoGpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:292
void setPath(const Pathname &path)
set the product path.
Definition: RepoInfo.cc:353
void setService(const std::string &name)
sets service which added this repository
Definition: RepoInfo.cc:372
const Pathname & path() const
Path to the input file or empty if no file.
Definition: InputStream.h:111
void setType(const repo::RepoType &t)
set the repository type
Definition: RepoInfo.cc:356
void setKeepPackages(bool keep)
Set if packaqes downloaded from this repository will be kept in local cache.
Definition: RepoInfo.cc:369
zypp::Url url
Definition: MediaCurl.cc:192
Read repository data from a .repo file.
void addBaseUrl(const Url &url)
Add a base url.
Definition: RepoInfo.cc:336
std::string getQueryParam(const std::string &param, EEncoding eflag=zypp::url::E_DECODED) const
Return the value for the specified query parameter.
Definition: Url.cc:654
virtual void garbageLine(const std::string &section, const std::string &line)
Called whenever a garbage line is found.
Definition: IniParser.cc:70
void setMetalinkUrl(const Url &url)
Like setMirrorListUrl but expect metalink format.
Definition: RepoInfo.cc:330
bool strToTrue(const C_Str &str)
Parsing boolean from string.
Definition: String.cc:63
Regular expression match result.
Definition: Regex.h:145
DefaultIntegral< bool, false > _inBaseurls
friend std::ostream & operator<<(std::ostream &str, const RepoFileReader &obj)
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
void setName(const std::string &name)
set the repository name
Definition: RepoInfoBase.cc:97
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
void setPkgGpgCheck(TriBool value_r)
Set the value for pkgGpgCheck (or indeterminate to use the default).
Definition: RepoInfo.cc:304
Url manipulation class.
Definition: Url.h:87
Repository type enumeration.
Definition: RepoType.h:27