yum parser WIP backup
authorJan Kupec <jkupec@suse.cz>
Thu, 19 Apr 2007 13:36:37 +0000 (13:36 +0000)
committerJan Kupec <jkupec@suse.cz>
Thu, 19 Apr 2007 13:36:37 +0000 (13:36 +0000)
devel/CMakeLists.txt
devel/devel.jkupec/CMakeLists.txt [new file with mode: 0644]
devel/devel.jkupec/YUMParser.cc [new file with mode: 0644]
devel/devel.jkupec/YUMParser.h [new file with mode: 0644]
devel/devel.jkupec/YUMParser_test.cc [new file with mode: 0644]
zypp/CMakeLists.txt
zypp/parser/yum/PrimaryFileReader.cc [new file with mode: 0644]
zypp/parser/yum/PrimaryFileReader.h [new file with mode: 0644]

index 7e7ab20..3ca3a66 100644 (file)
@@ -2,3 +2,4 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_CUR
 
 ADD_SUBDIRECTORY(devel.dmacvicar)
 ADD_SUBDIRECTORY(devel.ma)
+ADD_SUBDIRECTORY(devel.jkupec)
diff --git a/devel/devel.jkupec/CMakeLists.txt b/devel/devel.jkupec/CMakeLists.txt
new file mode 100644 (file)
index 0000000..5ac34e8
--- /dev/null
@@ -0,0 +1,9 @@
+INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} )
+
+ADD_DEFINITIONS(-DSRC_DIR=\\\"${CMAKE_CURRENT_SOURCE_DIR}\\\")
+
+#ADD_EXECUTABLE(sourcedownload sourcedownload.cc)
+#TARGET_LINK_LIBRARIES(sourcedownload zypp zypp2)
+
+ADD_EXECUTABLE( yumparsertest YUMParser_test.cc YUMParser.cc)
+TARGET_LINK_LIBRARIES(yumparsertest zypp zypp2)
diff --git a/devel/devel.jkupec/YUMParser.cc b/devel/devel.jkupec/YUMParser.cc
new file mode 100644 (file)
index 0000000..563dc4f
--- /dev/null
@@ -0,0 +1,55 @@
+//#include "zypp/data/ResolvableDataConsumer.h"
+#include "zypp/ZYpp.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/base/Logger.h"
+
+#include "YUMParser.h"
+
+using std::endl;
+
+namespace zypp
+{
+  namespace parser
+  {
+    namespace yum
+    {
+
+
+  YUMParser::YUMParser(const zypp::data::RecordId &catalog_id, zypp::cache::CacheStore &consumer)
+    : _consumer(consumer), _catalog_id(catalog_id)
+  {
+    ZYpp::Ptr z = getZYpp();
+//    _system_arch = z->architecture();
+
+    MIL << "constructed" << endl;
+  }
+  
+  
+  
+  bool YUMParser::primary_CB(const zypp::data::Package &package)
+  {
+//    data::RecordId pkgid = _consumer.appendResolvable( _catalog_id, ResTraits<Package>::kind, nvra, deps );
+
+    MIL << "got package "
+      << package.name << package.edition << " "
+      << package.arch
+      << endl;
+/*    MIL << "checksum: " << package.checksum << endl;
+    MIL << "summary: " << package.summary << endl;*/
+  }
+
+  void YUMParser::start(const Pathname &cache_dir, Progress progress_fnc)
+  {
+    progress_fnc(0);
+
+    zypp::parser::yum::PrimaryFileReader(
+        cache_dir + "/repodata/primary.xml.gz",
+        bind(&YUMParser::primary_CB, this, _1));
+
+    progress_fnc(100);
+  }
+
+
+    } // ns yum
+  } // ns parser
+} // ns zypp
diff --git a/devel/devel.jkupec/YUMParser.h b/devel/devel.jkupec/YUMParser.h
new file mode 100644 (file)
index 0000000..ad13b26
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef YUMPARSER_H_
+#define YUMPARSER_H_
+
+#include "zypp/base/Logger.h"
+#include "zypp2/cache/CacheStore.h"
+#include "zypp/data/ResolvableData.h"
+#include "zypp/parser/yum/PrimaryFileReader.h"
+
+#undef ZYPP_BASE_LOGGER_LOGGROUP
+#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
+
+
+namespace zypp
+{
+  namespace parser
+  {
+    namespace yum
+    {
+
+
+  class YUMParser
+  {
+  public:
+    typedef function<bool( int )> Progress;
+
+    YUMParser(const zypp::data::RecordId &catalog_id, zypp::cache::CacheStore &consumer);
+
+    void start(const zypp::Pathname &path, Progress progress_fnc);
+
+    bool primary_CB(const zypp::data::Package &package);
+    bool test() { return true; }
+
+  private:
+    zypp::cache::CacheStore &_consumer;
+    zypp::data::RecordId _catalog_id;
+  };
+
+
+    }
+  }
+} // ns zypp
+
+#endif /*YUMPARSER_H_*/
diff --git a/devel/devel.jkupec/YUMParser_test.cc b/devel/devel.jkupec/YUMParser_test.cc
new file mode 100644 (file)
index 0000000..a199d72
--- /dev/null
@@ -0,0 +1,48 @@
+#include "zypp/ZYpp.h"
+#include "zypp/ZYppFactory.h"
+#include "zypp/base/Logger.h"
+#include "zypp/base/LogControl.h"
+#include "zypp/parser/yum/PrimaryFileReader.h"
+#include "YUMParser.h"
+
+
+#undef ZYPP_BASE_LOGGER_LOGGROUP
+#define ZYPP_BASE_LOGGER_LOGGROUP "yumparsertest"
+
+using namespace std;
+using namespace zypp;
+using namespace zypp::parser::yum;
+
+bool progress_function(int p)
+{
+  MIL << p << "%" << endl;
+}
+
+int main(int argc, char **argv)
+{
+  base::LogControl::instance().logfile("yumparsertest.log");
+
+  try
+  {
+    ZYpp::Ptr z = getZYpp();
+//, bind( &YUMDownloader::patches_Callback, this, _1, _2));
+
+//    Pathname dbfile = Pathname(getenv("PWD")) + "data.db";
+    cache::CacheStore store(getenv("PWD"));
+    data::RecordId catalog_id = store.lookupOrAppendCatalog( Url("http://www.google.com"), "/");
+
+    MIL << "creating PrimaryFileParser" << endl;
+    parser::yum::YUMParser parser( catalog_id, store);
+    parser.start(argv[1], &progress_function);
+
+/*
+      YUMDownloader downloader(Url(argv[1]), "/");
+      downloader.download(argv[2]);*/
+  }
+  catch ( const Exception &e )
+  {
+    cout << "Oops! " << e.msg() << std::endl;
+  }
+
+  return 0;
+}
index 86ab0e1..527ddae 100644 (file)
@@ -540,6 +540,7 @@ SET( zypp_parser_yum_SRCS
   parser/yum/YUMRepomdParser.cc
   parser/yum/RepomdFileReader.cc
   parser/yum/PatchesFileReader.cc
+  parser/yum/PrimaryFileReader.cc
 )
 
 SET( zypp_parser_yum_HEADERS
@@ -556,6 +557,7 @@ SET( zypp_parser_yum_HEADERS
   parser/yum/YUMRepomdParser.h
   parser/yum/RepomdFileReader.h
   parser/yum/PatchesFileReader.h
+  parser/yum/PrimaryFileReader.h
   parser/yum/schemanames.h
 )
 
diff --git a/zypp/parser/yum/PrimaryFileReader.cc b/zypp/parser/yum/PrimaryFileReader.cc
new file mode 100644 (file)
index 0000000..16408e7
--- /dev/null
@@ -0,0 +1,155 @@
+#include "zypp/base/String.h"
+#include "zypp/base/Logger.h"
+#include "zypp/parser/yum/PrimaryFileReader.h"
+#include "zypp/Arch.h"
+#include "zypp/Edition.h"
+#include "zypp/TranslatedText.h"
+
+
+using namespace std;
+using namespace zypp::xml;
+
+namespace zypp
+{
+  namespace parser
+  {
+    namespace yum
+    {
+
+
+  PrimaryFileReader::PrimaryFileReader(const Pathname &primary_file, ProcessPackage callback)
+     : _callback(callback), _package(NULL), _count(0), _tag(tag_NONE)
+  {
+    Reader reader( primary_file );
+    MIL << "Reading " << primary_file << endl;
+    reader.foreachNode( bind( &PrimaryFileReader::consumeNode, this, _1 ) );
+  }
+  
+  bool PrimaryFileReader::consumeNode(Reader & reader_r)
+  {
+    if (_tag = tag_format)
+      return consumeFormatChildNodes(reader_r);
+  
+    if (reader_r->nodeType() == XML_READER_TYPE_ELEMENT)
+    {
+      if (reader_r->name() == "package")
+      {
+        _tag = tag_package;
+  //      DBG << "got " << reader_r->getAttribute("type") << " package" << endl;
+        if (_package) delete _package;
+        _package = new zypp::data::Package();
+  
+        return true; 
+      }
+  
+      if (reader_r->name() == "name")
+      {
+        _package->name = reader_r.nodeText().asString();
+        return true;
+      }
+  
+      if (reader_r->name() == "arch")
+      {
+        //if (arch == "src" || arch == "nosrc") arch = "noarch";
+        _package->arch = Arch(reader_r.nodeText().asString());
+        return true;
+      }
+  
+      if (reader_r->name() == "version")
+      {
+        _package->edition = Edition(reader_r->getAttribute("ver").asString(),
+                                    reader_r->getAttribute("rel").asString(),
+                                    reader_r->getAttribute("epoch").asString());
+      }
+      
+      if (reader_r->name() == "checksum")
+      {
+        _package->checksum = CheckSum(reader_r->getAttribute("type").asString(),
+                                     reader_r.nodeText().asString());
+        // ignoring pkgid attribute 
+        return true;
+      }
+  
+      if (reader_r->name() == "summary")
+      {
+        _package->summary.setText(
+            reader_r.nodeText().asString(),
+            Locale(reader_r->getAttribute("lang").asString()));
+        return true;
+      }
+  
+      if (reader_r->name() == "description")
+      {
+        _package->description.setText(
+            reader_r.nodeText().asString(),
+            Locale(reader_r->getAttribute("lang").asString()));
+        return true;
+      }
+  
+      if (reader_r->name() == "packager")
+      {
+        _package->packager = reader_r.nodeText().asString(); 
+        return true;
+      }
+  
+      // TODO url
+      // TODO time
+      // TODO size
+  
+      if (reader_r->name() == "location")
+      {
+        _package->location = reader_r->getAttribute("href").asString();
+      }
+  
+      if (reader_r->name() == "format")
+      {
+        _tag = tag_format;
+        consumeFormatChildNodes(reader_r);
+      }
+    }
+    else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
+    {
+      if (reader_r->name() == "package")
+      {
+        _callback(*_package);
+        if (_package)
+        {
+          delete _package;
+          _package = NULL;
+        }
+        _count++;
+        _tag = tag_NONE;
+      }
+      if (reader_r->name() == "metadata")
+      {
+        MIL << _count << " packages read." << endl;
+      }
+      return true;
+    }
+  
+    return true;
+  }
+  
+  bool PrimaryFileReader::consumeFormatChildNodes(Reader & reader_r)
+  {
+    if (reader_r->nodeType() == XML_READER_TYPE_ELEMENT)
+    {
+      
+    }
+    else if ( reader_r->nodeType() == XML_READER_TYPE_END_ELEMENT )
+    {
+      if (reader_r->name() == "format");
+      {
+        _tag = tag_package;
+        return true;
+      }
+    }
+    return true;
+  }
+
+
+    } // ns yum
+  } // ns parser
+} //ns zypp
+
+// vim: set ts=2 sts=2 sw=2 et ai:
diff --git a/zypp/parser/yum/PrimaryFileReader.h b/zypp/parser/yum/PrimaryFileReader.h
new file mode 100644 (file)
index 0000000..33601db
--- /dev/null
@@ -0,0 +1,81 @@
+#ifndef ZYPP_PARSER_YUM_PRIMARYFILEPARSER_H
+#define ZYPP_PARSER_YUM_PRIMARYFILEPARSER_H
+
+#include "zypp/Date.h"
+#include "zypp/base/Function.h"
+#include "zypp/base/Logger.h"
+#include "zypp/parser/xml/Reader.h"
+#include "zypp/data/ResolvableData.h"
+
+#undef ZYPP_BASE_LOGGER_LOGGROUP
+#define ZYPP_BASE_LOGGER_LOGGROUP "parser"
+
+namespace zypp
+{
+  namespace parser
+  {
+    namespace yum
+    {
+
+      enum Tag
+      {
+        tag_NONE,
+        tag_package,
+        tag_format
+      };
+
+/**
+ * Iterates through a primary.xml file giving on each iteration
+ * a \ref OnMediaLocation object with the resource and its
+ * type ( primary, patches, filelists, etc ).
+ * The iteration is done via a callback provided on
+ * construction.
+ *
+ * \code
+ * RepomdFileReader reader(repomd_file, 
+ *                  bind( &SomeClass::callbackfunc, &object, _1, _2 ) );
+ * \endcode
+ */
+class PrimaryFileReader
+{
+public:
+  /**
+   * Callback definition.
+   */
+  typedef function<bool(const zypp::data::Package&)> ProcessPackage;
+
+  /**
+   * Constructor
+   * \param primary_file the primary.xml.gz file you want to read
+   * \param function to process \ref _package data.
+   * 
+   * \see PrimaryFileReader::ProcessPackage
+   */
+  PrimaryFileReader(const Pathname &primary_file, ProcessPackage callback);
+
+  /**
+   * Callback provided to the XML parser.
+   */
+  bool consumeNode(zypp::xml::Reader & reader_r);
+
+private:
+  bool consumeFormatChildNodes(zypp::xml::Reader & reader_r);
+
+private:
+  Tag _tag;
+  unsigned _count;
+  zypp::data::Package *_package;
+  ProcessPackage _callback;
+/*  CheckSum _checksum;
+  std::string _checksum_type;
+  Date _timestamp;*/
+};
+
+
+    } // ns zypp
+  } // ns parser
+} // ns yum
+
+#endif /* ZYPP_PARSER_YUM_PRIMARYFILEPARSER_H */
+
+// vim: set ts=2 sts=2 sw=2 et ai: