~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/parser/RepoFileReader.cc

  • Committer: Thomas-Karl Pietrowski
  • Date: 2014-01-29 22:44:28 UTC
  • Revision ID: thopiekar@googlemail.com-20140129224428-gpcqnsdakby362n8
firstĀ import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*---------------------------------------------------------------------\
 
2
|                          ____ _   __ __ ___                          |
 
3
|                         |__  / \ / / . \ . \                         |
 
4
|                           / / \ V /|  _/  _/                         |
 
5
|                          / /__ | | | | | |                           |
 
6
|                         /_____||_| |_| |_|                           |
 
7
|                                                                      |
 
8
\---------------------------------------------------------------------*/
 
9
/** \file       zypp/repo/RepoFileReader.cc
 
10
 *
 
11
*/
 
12
#include <iostream>
 
13
#include "zypp/base/Logger.h"
 
14
#include "zypp/base/String.h"
 
15
#include "zypp/base/Regex.h"
 
16
#include "zypp/base/InputStream.h"
 
17
#include "zypp/base/UserRequestException.h"
 
18
 
 
19
#include "zypp/parser/IniDict.h"
 
20
#include "zypp/parser/RepoFileReader.h"
 
21
 
 
22
using std::endl;
 
23
using zypp::parser::IniDict;
 
24
 
 
25
///////////////////////////////////////////////////////////////////
 
26
namespace zypp
 
27
{ /////////////////////////////////////////////////////////////////
 
28
  ///////////////////////////////////////////////////////////////////
 
29
  namespace parser
 
30
  { /////////////////////////////////////////////////////////////////
 
31
 
 
32
    /**
 
33
   * \short List of RepoInfo's from a file.
 
34
   * \param file pathname of the file to read.
 
35
   */
 
36
    static void repositories_in_stream( const InputStream &is,
 
37
                                        const RepoFileReader::ProcessRepo &callback,
 
38
                                        const ProgressData::ReceiverFnc &progress )
 
39
    {
 
40
      parser::IniDict dict(is);
 
41
      for ( parser::IniDict::section_const_iterator its = dict.sectionsBegin();
 
42
            its != dict.sectionsEnd();
 
43
            ++its )
 
44
      {
 
45
        RepoInfo info;
 
46
        info.setAlias(*its);
 
47
        Url url;
 
48
 
 
49
        for ( IniDict::entry_const_iterator it = dict.entriesBegin(*its);
 
50
              it != dict.entriesEnd(*its);
 
51
              ++it )
 
52
        {
 
53
          //MIL << (*it).first << endl;
 
54
          if (it->first == "name" )
 
55
            info.setName(it-> second);
 
56
          else if ( it->first == "enabled" )
 
57
            info.setEnabled( str::strToTrue( it->second ) );
 
58
          else if ( it->first == "priority" )
 
59
            info.setPriority( str::strtonum<unsigned>( it->second ) );
 
60
          else if ( it->first == "baseurl" && !it->second.empty())
 
61
            url = it->second;
 
62
          else if ( it->first == "path" )
 
63
            info.setPath( Pathname(it->second) );
 
64
          else if ( it->first == "type" )
 
65
            info.setType(repo::RepoType(it->second));
 
66
          else if ( it->first == "autorefresh" )
 
67
            info.setAutorefresh( str::strToTrue( it->second ) );
 
68
          else if ( it->first == "mirrorlist" && !it->second.empty())
 
69
            info.setMirrorListUrl(Url(it->second));
 
70
          else if ( it->first == "gpgkey" && !it->second.empty())
 
71
          {
 
72
            std::vector<std::string> keys;
 
73
            str::split( it->second, std::back_inserter(keys) );
 
74
            if ( ! keys.empty() )
 
75
              info.setGpgKeyUrl( Url(*keys.begin()) );
 
76
          }
 
77
          else if ( it->first == "gpgcheck" )
 
78
            info.setGpgCheck( str::strToTrue( it->second ) );
 
79
          else if ( it->first == "keeppackages" )
 
80
            info.setKeepPackages( str::strToTrue( it->second ) );
 
81
          else if ( it->first == "service" )
 
82
            info.setService( it->second );
 
83
          else if ( it->first == "proxy" )
 
84
          {
 
85
            if (it->second != "_none_" )
 
86
            {
 
87
              str::regex ex("^(.*):([0-9]+)$");
 
88
              str::smatch what;
 
89
              if(str::regex_match(it->second, what, ex)){
 
90
               url.setQueryParam("proxy", what[1]);
 
91
               url.setQueryParam("proxyport", what[2]);
 
92
              }
 
93
            }
 
94
          } else
 
95
            ERR << "Unknown attribute in [" << *its << "]: " << it->first << "=" << it->second << " ignored" << endl;
 
96
        }
 
97
        if (url.isValid())
 
98
            info.addBaseUrl(url);
 
99
        info.setFilepath(is.path());
 
100
        MIL << info << endl;
 
101
        // add it to the list.
 
102
        callback(info);
 
103
        //if (!progress.tick())
 
104
        //  ZYPP_THROW(AbortRequestException());
 
105
      }
 
106
    }
 
107
 
 
108
    ///////////////////////////////////////////////////////////////////
 
109
    //
 
110
    //  CLASS NAME : RepoFileReader
 
111
    //
 
112
    ///////////////////////////////////////////////////////////////////
 
113
 
 
114
    RepoFileReader::RepoFileReader( const Pathname & repo_file,
 
115
                                    const ProcessRepo & callback,
 
116
                                    const ProgressData::ReceiverFnc &progress )
 
117
      : _callback(callback)
 
118
    {
 
119
      repositories_in_stream(InputStream(repo_file), _callback, progress);
 
120
    }
 
121
 
 
122
    RepoFileReader::RepoFileReader( const InputStream &is,
 
123
                                    const ProcessRepo & callback,
 
124
                                    const ProgressData::ReceiverFnc &progress )
 
125
      : _callback(callback)
 
126
    {
 
127
      repositories_in_stream(is, _callback, progress);
 
128
    }
 
129
 
 
130
    RepoFileReader::~RepoFileReader()
 
131
    {}
 
132
 
 
133
 
 
134
    std::ostream & operator<<( std::ostream & str, const RepoFileReader & obj )
 
135
    {
 
136
      return str;
 
137
    }
 
138
 
 
139
    /////////////////////////////////////////////////////////////////
 
140
  } // namespace parser
 
141
  ///////////////////////////////////////////////////////////////////
 
142
  /////////////////////////////////////////////////////////////////
 
143
} // namespace zypp
 
144
///////////////////////////////////////////////////////////////////