libzypp 9.41.1

RepoFileReader.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include "zypp/base/LogTools.h"
00014 #include "zypp/base/String.h"
00015 #include "zypp/base/Regex.h"
00016 #include "zypp/base/InputStream.h"
00017 #include "zypp/base/UserRequestException.h"
00018 
00019 #include "zypp/parser/IniDict.h"
00020 #include "zypp/parser/RepoFileReader.h"
00021 
00022 using std::endl;
00023 
00025 namespace zypp
00026 {
00028   namespace parser
00029   {
00031     namespace {
00032 
00037       class RepoFileParser : public IniDict
00038       {
00039       public:
00040         RepoFileParser( const InputStream & is_r )
00041         { read( is_r ); }
00042 
00043         using IniDict::consume; // don't hide overloads we don't redefine here
00044 
00045         virtual void consume( const std::string & section_r, const std::string & key_r, const std::string & value_r )
00046         {
00047           if ( key_r == "baseurl" )
00048           {
00049             setInBaseurls( true );
00050             _baseurls[section_r].push_back( Url(value_r) );
00051           }
00052           else if ( key_r.empty() && _inBaseurls )
00053           {
00054             _baseurls[section_r].push_back( Url(value_r) );
00055           }
00056           else
00057           {
00058             setInBaseurls( false );
00059             IniDict::consume( section_r, key_r, value_r );
00060           }
00061         }
00062 
00063         std::list<Url> & baseurls( const std::string & section_r )
00064         { return _baseurls[section_r]; }
00065 
00066       private:
00067         void setInBaseurls( bool yesno_r )
00068         { if ( _inBaseurls != yesno_r ) _inBaseurls = yesno_r; }
00069 
00070         DefaultIntegral<bool,false> _inBaseurls;
00071         std::map<std::string,std::list<Url> > _baseurls;
00072       };
00073 
00074     } //namespace
00076 
00081     static void repositories_in_stream( const InputStream &is,
00082                                         const RepoFileReader::ProcessRepo &callback,
00083                                         const ProgressData::ReceiverFnc &progress )
00084     {
00085       RepoFileParser dict(is);
00086       for_( its, dict.sectionsBegin(), dict.sectionsEnd() )
00087       {
00088         RepoInfo info;
00089         info.setAlias(*its);
00090         std::string proxy;
00091         std::string proxyport;
00092 
00093         for_( it, dict.entriesBegin(*its), dict.entriesEnd(*its) )
00094         {
00095           //MIL << (*it).first << endl;
00096           if (it->first == "name" )
00097             info.setName(it-> second);
00098           else if ( it->first == "enabled" )
00099             info.setEnabled( str::strToTrue( it->second ) );
00100           else if ( it->first == "priority" )
00101             info.setPriority( str::strtonum<unsigned>( it->second ) );
00102           else if ( it->first == "path" )
00103             info.setPath( Pathname(it->second) );
00104           else if ( it->first == "type" )
00105             info.setType(repo::RepoType(it->second));
00106           else if ( it->first == "autorefresh" )
00107             info.setAutorefresh( str::strToTrue( it->second ) );
00108           else if ( it->first == "mirrorlist" && !it->second.empty())
00109             info.setMirrorListUrl(Url(it->second));
00110           else if ( it->first == "gpgkey" && !it->second.empty())
00111           {
00112             std::vector<std::string> keys;
00113             str::split( it->second, std::back_inserter(keys) );
00114             if ( ! keys.empty() ) 
00115               info.setGpgKeyUrl( Url(*keys.begin()) );
00116           }
00117           else if ( it->first == "gpgcheck" )
00118             info.setGpgCheck( str::strToTrue( it->second ) );
00119           else if ( it->first == "keeppackages" )
00120             info.setKeepPackages( str::strToTrue( it->second ) );
00121           else if ( it->first == "service" )
00122             info.setService( it->second );
00123           else if ( it->first == "proxy" )
00124           {
00125             // Translate it into baseurl queryparams
00126             // NOTE: The hack here does not add proxy to mirrorlist urls but the
00127             //       original code worked without complains, so keep it for now.
00128             static const str::regex ex( ":[0-9]+$" );   // portspec
00129             str::smatch what;
00130             if ( str::regex_match( it->second, what, ex ) )
00131             {
00132               proxy = it->second.substr( 0, it->second.size() - what[0].size() );
00133               proxyport = what[0].substr( 1 );
00134             }
00135             else
00136             {
00137               proxy = it->second;
00138             }
00139           }
00140           else
00141             ERR << "Unknown attribute in [" << *its << "]: " << it->first << "=" << it->second << " ignored" << endl;
00142         }
00143 
00144         for_( urlit, dict.baseurls( *its ).begin(), dict.baseurls( *its ).end() )
00145         {
00146           Url & url( *urlit );
00147 
00148           if ( ! proxy.empty() && url.getQueryParam( "proxy" ).empty() )
00149           {
00150             url.setQueryParam( "proxy", proxy );
00151             url.setQueryParam( "proxyport", proxyport );
00152           }
00153           info.addBaseUrl( url );
00154         }
00155 
00156         info.setFilepath(is.path());
00157         MIL << info << endl;
00158         // add it to the list.
00159         callback(info);
00160         //if (!progress.tick())
00161         //  ZYPP_THROW(AbortRequestException());
00162       }
00163     }
00164 
00166     //
00167     //  CLASS NAME : RepoFileReader
00168     //
00170 
00171     RepoFileReader::RepoFileReader( const Pathname & repo_file,
00172                                     const ProcessRepo & callback,
00173                                     const ProgressData::ReceiverFnc &progress )
00174       : _callback(callback)
00175     {
00176       repositories_in_stream(InputStream(repo_file), _callback, progress);
00177     }
00178 
00179     RepoFileReader::RepoFileReader( const InputStream &is,
00180                                     const ProcessRepo & callback,
00181                                     const ProgressData::ReceiverFnc &progress )
00182       : _callback(callback)
00183     {
00184       repositories_in_stream(is, _callback, progress);
00185     }
00186 
00187     RepoFileReader::~RepoFileReader()
00188     {}
00189 
00190 
00191     std::ostream & operator<<( std::ostream & str, const RepoFileReader & obj )
00192     {
00193       return str;
00194     }
00195 
00196   } // namespace parser
00198 } // namespace zypp