Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / devel / devel.dmacvicar / rpmbuilder.cc
1 #include <sys/time.h>
2
3 #include <iostream>
4 #include <fstream>
5
6 #include <zypp/base/Logger.h>
7 #include <zypp/ZYpp.h>
8 #include <zypp/ZYppFactory.h>
9
10 #include "zypp/Product.h"
11 #include "zypp/Package.h"
12
13 #include "zypp/TmpPath.h"
14 #include "zypp/ExternalProgram.h"
15 #include "zypp/ProgressData.h"
16 #include "zypp/repo/yum/Downloader.h"
17
18 #include "zypp/sat/Pool.h"
19
20 #include "zypp/PoolQuery.h"
21
22 using namespace std;
23 using namespace zypp;
24 using namespace zypp::repo;
25 using namespace zypp::filesystem;
26
27 class RpmBuilder
28 {
29 public:
30     RpmBuilder()
31     {
32         Pathname top = _tmptop.path();
33         Pathname rcpath = _tmprc.path();
34         
35         assert_dir(top / "build" );
36         assert_dir(top / "rpms" );
37         
38         /* create a rpm configuration file and
39            setup the macros file */
40         std::ofstream rcfile(rcpath.c_str());
41         if (!rcfile)
42             ZYPP_THROW (Exception( "Can't open " + rcpath.asString() ) );
43         
44         rcfile << "macrofiles: " << _tmpmacros.path() << endl;
45         rcfile.close();
46     }
47     
48     Pathname rpmsDir() const
49     {
50         return _tmptop.path() / "rpms";
51     }
52     
53     void createRpmMetadata() const
54     {
55         const char* argv[] =
56         {
57             "createrepo",
58             rpmsDir().c_str(),
59             NULL
60         };
61         ExternalProgram prog(argv,ExternalProgram::Normal_Stderr, false, -1, true);
62         string line;
63         int count;
64         for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
65         {
66             cout << line;
67         }
68         prog.close();
69
70     }
71     
72     void buildSpec( const Pathname &spec )
73     {
74         Pathname basedir = spec.dirname();
75         Pathname rcpath = _tmprc.path();
76         Pathname macrospath = _tmpmacros.path();
77         
78         std::ofstream macrosfile(macrospath.c_str());
79         if (!macrosfile)
80             ZYPP_THROW (Exception( "Can't open " + macrospath.asString() ) );
81         
82         macrosfile << "%topdir " << _tmptop.path() << endl;
83         macrosfile << "%_builddir %{topdir}/build" << endl;
84         macrosfile << "%_rpmdir %{topdir}/rpms" << endl;
85         macrosfile << "%_srcrpmdir %{topdir}/rpms" << endl;
86         macrosfile << "%_sourcedir " << basedir << endl;
87         macrosfile << "%_specdir " << basedir << endl;
88  
89         macrosfile.close();
90         
91         const char* argv[] =
92         {
93             "rpmbuild",
94             "--rcfile",
95             rcpath.c_str(),
96             "-bb",
97             //"--clean",
98             "--buildroot",
99             _tmpbuildroot.path().c_str(),
100             spec.c_str(),
101             NULL
102         };
103         ExternalProgram prog(argv,ExternalProgram::Normal_Stderr, false, -1, true);
104         string line;
105         int count;
106         for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
107         {
108             cout << line;
109         }
110         prog.close();
111
112     }
113     
114 private:
115     TmpDir _tmptop;
116     TmpFile _tmprc;
117     TmpFile _tmpmacros;
118     TmpDir _tmpbuildroot;
119 };
120
121 int main(int argc, char **argv)
122 {
123     try
124     {
125       ZYpp::Ptr z = getZYpp();
126     
127       //z->initializeTarget("/");
128       //z->target()->load();
129
130       //sat::Pool::instance().addRepoSolv("./foo.solv");
131
132 //       for ( ResPool::const_iterator it = z->pool().begin(); it != z->pool().end(); ++it )
133 //       {
134 //         ResObject::constPtr res = it->resolvable();
135 //         if ( res->name() == "kde4-kcolorchooser")
136 //         {
137 //           cout << res << endl;
138 //           cout << res->summary() << " | " << res->size() << endl;
139 //         }
140 //       }
141
142       //query.execute("kde", &result_cb);
143       
144       RpmBuilder builder;
145       builder.buildSpec("/space/git/hwenable/spec/testdriver.spec");
146       builder.createRpmMetadata();
147       
148       
149     }
150     catch ( const Exception &e )
151     {
152       ZYPP_CAUGHT(e);
153       cout << e.msg() << endl;
154     }
155     
156     return 0;
157 }
158
159
160