backup
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 9 Sep 2008 22:37:17 +0000 (22:37 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 9 Sep 2008 22:37:17 +0000 (22:37 +0000)
simple rpmbuilder that can be used to create repos on runtime from simple spec files
this would be very useful in the testsuite, and I want it to test for example automatic
driver selection

We need to define some base testsuite utilities (a small library with utilities), as Jano mentioned
we also want to create the solv files while the testsuite runs

devel/devel.dmacvicar/CMakeLists.txt
devel/devel.dmacvicar/rpmbuilder.cc [new file with mode: 0644]

index 31e00fa..9cabc61 100644 (file)
@@ -1,29 +1,7 @@
-ADD_DEFINITIONS(-DSRC_DIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\")
+ADD_DEFINITIONS(-DSRC_DIR=${CMAKE_CURRENT_SOURCE_DIR})
 
-ADD_EXECUTABLE(mediaaccess mediaaccess.cc)
-TARGET_LINK_LIBRARIES(mediaaccess  zypp )
-
-#ADD_EXECUTABLE(multiple-download multiple-download.cc)
-#TARGET_LINK_LIBRARIES(multiple-download zypp boost_thread)
-
-#ADD_EXECUTABLE(scansource ScanSource.cc)
-#TARGET_LINK_LIBRARIES(scansource  zypp )
-
-#ADD_EXECUTABLE(susetags-downloader SUSETagsDownloader_tp.cc)
-#TARGET_LINK_LIBRARIES(susetags-downloader  zypp )
-
-ADD_EXECUTABLE(testbed testbed.cc)
-TARGET_LINK_LIBRARIES(testbed  zypp )
-
-#ADD_EXECUTABLE(yum-downloader YUMDownloader_tp.cc)
-#TARGET_LINK_LIBRARIES(yum-downloader  zypp )
-
-# ADD_EXECUTABLE(yum-reader YUMReader_tp.cc)
-# TARGET_LINK_LIBRARIES(yum-reader  zypp )
-
-
-ADD_EXECUTABLE(repomanager RepositoryManager_tp.cc)
-TARGET_LINK_LIBRARIES(repomanager zypp )
+ADD_EXECUTABLE(rpmbuilder rpmbuilder.cc)
+TARGET_LINK_LIBRARIES(rpmbuilder  zypp )
 
 FIND_PACKAGE(Zsync)
 IF(ZSYNC_FOUND)
@@ -31,4 +9,4 @@ IF(ZSYNC_FOUND)
   TARGET_LINK_LIBRARIES(zsync  ${ZSYNC_LIBRARY} ${RCKSUM_LIBRARY})
   TARGET_LINK_LIBRARIES(zsync  zypp )
 #  TARGET_LINK_LIBRARIES(zsync  zypp2 )
-ENDIF(ZSYNC_FOUND)
\ No newline at end of file
+ENDIF(ZSYNC_FOUND)
diff --git a/devel/devel.dmacvicar/rpmbuilder.cc b/devel/devel.dmacvicar/rpmbuilder.cc
new file mode 100644 (file)
index 0000000..c9b4edf
--- /dev/null
@@ -0,0 +1,160 @@
+#include <sys/time.h>
+
+#include <iostream>
+#include <fstream>
+
+#include <zypp/base/Logger.h>
+#include <zypp/ZYpp.h>
+#include <zypp/ZYppFactory.h>
+
+#include "zypp/Product.h"
+#include "zypp/Package.h"
+
+#include "zypp/TmpPath.h"
+#include "zypp/ExternalProgram.h"
+#include "zypp/ProgressData.h"
+#include "zypp/repo/yum/Downloader.h"
+
+#include "zypp/sat/Pool.h"
+
+#include "zypp/PoolQuery.h"
+
+using namespace std;
+using namespace zypp;
+using namespace zypp::repo;
+using namespace zypp::filesystem;
+
+class RpmBuilder
+{
+public:
+    RpmBuilder()
+    {
+        Pathname top = _tmptop.path();
+        Pathname rcpath = _tmprc.path();
+        
+        assert_dir(top / "build" );
+        assert_dir(top / "rpms" );
+        
+        /* create a rpm configuration file and
+           setup the macros file */
+        std::ofstream rcfile(rcpath.c_str());
+        if (!rcfile)
+            ZYPP_THROW (Exception( "Can't open " + rcpath.asString() ) );
+        
+        rcfile << "macrofiles: " << _tmpmacros.path() << endl;
+        rcfile.close();
+    }
+    
+    Pathname rpmsDir() const
+    {
+        return _tmptop.path() / "rpms";
+    }
+    
+    void createRpmMetadata() const
+    {
+        const char* argv[] =
+        {
+            "createrepo",
+            rpmsDir().c_str(),
+            NULL
+        };
+        ExternalProgram prog(argv,ExternalProgram::Normal_Stderr, false, -1, true);
+        string line;
+        int count;
+        for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
+        {
+            cout << line;
+        }
+        prog.close();
+
+    }
+    
+    void buildSpec( const Pathname &spec )
+    {
+        Pathname basedir = spec.dirname();
+        Pathname rcpath = _tmprc.path();
+        Pathname macrospath = _tmpmacros.path();
+        
+        std::ofstream macrosfile(macrospath.c_str());
+        if (!macrosfile)
+            ZYPP_THROW (Exception( "Can't open " + macrospath.asString() ) );
+        
+        macrosfile << "%topdir " << _tmptop.path() << endl;
+        macrosfile << "%_builddir %{topdir}/build" << endl;
+        macrosfile << "%_rpmdir %{topdir}/rpms" << endl;
+        macrosfile << "%_srcrpmdir %{topdir}/rpms" << endl;
+        macrosfile << "%_sourcedir " << basedir << endl;
+        macrosfile << "%_specdir " << basedir << endl;
+        macrosfile.close();
+        
+        const char* argv[] =
+        {
+            "rpmbuild",
+            "--rcfile",
+            rcpath.c_str(),
+            "-bb",
+            //"--clean",
+            "--buildroot",
+            _tmpbuildroot.path().c_str(),
+            spec.c_str(),
+            NULL
+        };
+        ExternalProgram prog(argv,ExternalProgram::Normal_Stderr, false, -1, true);
+        string line;
+        int count;
+        for(line = prog.receiveLine(), count=0; !line.empty(); line = prog.receiveLine(), count++ )
+        {
+            cout << line;
+        }
+        prog.close();
+
+    }
+    
+private:
+    TmpDir _tmptop;
+    TmpFile _tmprc;
+    TmpFile _tmpmacros;
+    TmpDir _tmpbuildroot;
+};
+
+int main(int argc, char **argv)
+{
+    try
+    {
+      ZYpp::Ptr z = getZYpp();
+    
+      //z->initializeTarget("/");
+      //z->target()->load();
+
+      //sat::Pool::instance().addRepoSolv("./foo.solv");
+
+//       for ( ResPool::const_iterator it = z->pool().begin(); it != z->pool().end(); ++it )
+//       {
+//         ResObject::constPtr res = it->resolvable();
+//         if ( res->name() == "kde4-kcolorchooser")
+//         {
+//           cout << res << endl;
+//           cout << res->summary() << " | " << res->size() << endl;
+//         }
+//       }
+
+      //query.execute("kde", &result_cb);
+      
+      RpmBuilder builder;
+      builder.buildSpec("/space/git/hwenable/spec/testdriver.spec");
+      builder.createRpmMetadata();
+      
+      
+    }
+    catch ( const Exception &e )
+    {
+      ZYPP_CAUGHT(e);
+      cout << e.msg() << endl;
+    }
+    
+    return 0;
+}
+
+
+