633efb138daa45a6d0b8a0a19bfae2fb3838cade
[platform/upstream/libzypp.git] / test / devel.jsrain / PatchRead.cc
1 #include <iostream>
2 #include <zypp/base/Logger.h>
3
4 ///////////////////////////////////////////////////////////////////
5
6 #include <zypp/Message.h>
7 #include <zypp/detail/MessageImpl.h>
8 #include <zypp/detail/PatchImpl.h>
9 #include <zypp/Package.h>
10 #include <zypp/detail/PackageImpl.h>
11 #include <zypp/Script.h>
12 #include <zypp/detail/ScriptImpl.h>
13 #include <zypp/Resolvable.h>
14 #include <zypp/detail/ResolvableImpl.h>
15 #include <zypp/Capability.h>
16 #include <zypp/capability/CapabilityImpl.h>
17
18 #include <zypp/parser/yum/YUMParser.h>
19 #include <zypp/base/Logger.h>
20 #include <zypp/source/yum/YUMScriptImpl.h>
21
22
23 #include <map>
24 #include <set>
25
26 #include <zypp/CapFactory.h>
27
28 using namespace zypp::detail;
29
30 using namespace std;
31 using namespace zypp;
32 using namespace zypp::parser::YUM;
33 using namespace zypp::source::YUM;
34
35
36 CapFactory _f;
37
38 DEFINE_PTR_TYPE(MyMessageImpl)
39 class MyMessageImpl : public detail::MessageImpl
40 {
41   public:
42     MyMessageImpl (string name, string type, std::string text)
43     : MessageImpl (name,
44                    Edition (),
45                    Arch ("noarch"))
46     {
47       _text = text;
48       _type = type;
49     }
50 };
51 IMPL_PTR_TYPE(MyMessageImpl)
52
53 void AddDependency (detail::ResolvableImplPtr res, Capability& cap) {
54   Dependencies deps = res->deps();
55   CapSet req = deps.requires();
56   req.insert( cap );
57   deps.setRequires( req );
58   res->setDeps( deps );
59 }
60
61 void AddAllRequires( detail::ResolvableImplPtr res, list<Capability> & caps) {
62     for (list<Capability>::iterator it = caps.begin();
63          it != caps.end();
64          it++)
65     {
66       AddDependency( res, *it );
67     }
68 }
69
70 DEFINE_PTR_TYPE(MyPatchImpl)
71
72 class MyPatchImpl : public detail::PatchImpl
73 {
74   public:
75     MyPatchImpl( YUMPatchData & p )
76     : PatchImpl (p.name,
77                  Edition (),
78                  Arch ("noarch"))
79     {
80       // Create a requires capability to the patch itself
81       Capability cap( _f.parse( p.name, ResKind( "patch" )));
82
83       // Process atoms
84       atom_list atoms;
85
86       for (std::list<YUMPatchAtom>::iterator it = p.atoms.begin();
87         it != p.atoms.end();
88         it++)
89       {
90         if (it->type == "script")
91         {
92           ScriptImplPtr impl = new YUMScriptImpl( *it->script );
93           AddDependency(impl, cap);
94           ScriptPtr script = new Script(impl);
95           cout << *script << endl;
96           cout << script->deps() << endl;
97           atoms.push_back(script);
98         }
99       }
100
101 /*      for (list<PACKAGE>::iterator it = p.pack.begin();
102            it != p.pack.end();
103            it++)
104       {
105         detail::PackageImplPtr pi( new detail::PackageImpl(
106           it->name,
107           Edition( it->version, it->release ),
108           Arch( it->arch )));
109         AddDependency( pi, cap );
110         AddAllRequires( pi, it->requires );
111         PackagePtr p( new Package( pi ));
112         DBG << *p << endl;
113         DBG << p->deps() << endl;
114         atoms.push_back( p );
115       }
116       for (list<MESSAGE>::iterator it = p.msg.begin();
117            it != p.msg.end();
118            it++)
119       {
120         detail::MessageImplPtr pi( new MyMessageImpl(
121           it->name,
122           it->type,
123           it->text));
124         AddDependency( pi, cap );
125         MessagePtr p( new Message( pi ));
126         DBG << *p << endl;
127         DBG << p->deps() << endl;
128         atoms.push_back( p );
129       }
130 */
131       // FIXME mve this piece of code after solutions are selected
132       // this orders the atoms of a patch
133       ResolvablePtr previous;
134       bool first = true;
135       for (atom_list::iterator it = atoms.begin();
136            it != atoms.end();
137            it++)
138       {
139         if (! first)
140         {
141           Dependencies deps = (*it)->deps();
142           CapSet req = deps.prerequires();
143           req.insert( Capability( _f.parse( previous->name(), previous->kind())));
144           deps.setPrerequires( req );
145           (*it)->setDeps( deps );
146         }
147         first = false;
148         previous = *it;
149       }
150
151       _reboot_needed = false; // FIXME
152       _atoms = atoms;
153       _category = "recommended"; // FIXME
154     }
155 };
156
157 IMPL_PTR_TYPE(MyPatchImpl)
158
159
160 /******************************************************************
161 **
162 **
163 **      FUNCTION NAME : main
164 **      FUNCTION TYPE : int
165 **
166 **      DESCRIPTION :
167 */
168 int main( int argc, char * argv[] )
169 {
170   INT << "===[START]==========================================" << endl;
171
172 // filling structures
173 /*
174 PACKAGE foo;
175 foo.name = "foo";
176 foo.version = "3.0";
177 foo.release = "5";
178 foo.arch = "noarch";
179
180 PACKAGE foocomp;
181 foocomp.name = "foo-compat";
182 foocomp.version = "3.0";
183 foocomp.release = "5";
184 foocomp.arch = "noarch";
185 foocomp.requires.push_back( Capability( _f.parse( "foo", ResKind( "package" ))));
186
187 PACKAGE bar;
188 bar.name = "bar";
189 bar.version = "2.8";
190 bar.release = "2";
191 bar.arch = "noarch";
192
193 MESSAGE msg;
194 msg.name = "msg";
195 msg.type = "OK";
196 msg.text = "Hello World";
197
198 SCRIPT scr;
199 scr.name = "scr";
200 scr.do_script = "/bin/bash";
201 scr.undo_script = "/bin/unbash";
202
203 list<PACKAGE> pkgs;
204 list<MESSAGE> msgs;
205 list<SCRIPT> scrs;
206
207 pkgs.push_back( foo );
208 pkgs.push_back( foocomp );
209 pkgs.push_back( bar );
210 msgs.push_back( msg );
211 scrs.push_back( scr );
212
213 PATCH ptch;
214 ptch.name = "patch";
215 ptch.pack = pkgs;
216 ptch.msg = msgs;
217 ptch.scr = scrs;
218 */
219 PatchPtr patch1;
220 YUMPatchParser iter(cin,"");
221 for (;
222      !iter.atEnd();
223      ++iter) {
224        MyPatchImplPtr q(new MyPatchImpl(**iter));
225         patch1 = new Patch (q);
226      }
227 if (iter.errorStatus())
228   throw *iter.errorStatus();
229
230
231
232 // process the patch
233
234 DBG << patch1 << endl;
235 DBG << *patch1 << endl;
236 atom_list at = patch1->atoms();
237 for (atom_list::iterator it = at.begin();
238      it != at.end();
239      it++)
240 {
241   DBG << **it << endl;
242   DBG << (**it).deps() << endl;
243 }
244
245   INT << "===[END]============================================" << endl;
246   return 0;
247 }