xml output in terse mode
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 2 Aug 2007 16:30:35 +0000 (16:30 +0000)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Thu, 2 Aug 2007 16:30:35 +0000 (16:30 +0000)
src/zypper-callbacks.cc
src/zypper-callbacks.h
src/zypper-media-callbacks.h
src/zypper-rpm-callbacks.h
src/zypper-source-callbacks.h
src/zypper.cc

index 05c2b33..a46ce3a 100644 (file)
@@ -6,11 +6,14 @@
 
 using namespace std;
 
-void display_progress (ostream & out, const string& s, int percent) {
+void display_progress ( const std::string &id, ostream & out, const string& s, int percent) {
   static AliveCursor cursor;
 
   if (gSettings.machine_readable)
+  {
+    cout << "<progress id=\"" << id << "\" type=\"percentage\" value=\"" << percent << "\" name=\"" << s << "\"/>" << endl;
     return;
+  }
 
   if ( percent == 100 )
     out << CLEARLN << cursor.done() << " " << s;
@@ -24,11 +27,14 @@ void display_progress (ostream & out, const string& s, int percent) {
 
 // ----------------------------------------------------------------------------
 
-void display_tick (ostream & out, const string& s) {
+void display_tick ( const std::string &id, ostream & out, const string& s) {
   static AliveCursor cursor;
 
   if (gSettings.machine_readable)
+  {
+    cout << "<progress id=\"" << id << "\" type=\"tick\" value=\"" << -1 << "\" name=\"" << s << "\"/>" << endl;
     return;
+  }
 
   cursor++;
   out << CLEARLN << cursor << " " << s;
@@ -37,11 +43,14 @@ void display_tick (ostream & out, const string& s) {
 
 // ----------------------------------------------------------------------------
 
-void display_done (ostream & out, const string& s) {
+void display_done ( const std::string &id, ostream & out, const string& s) {
   static AliveCursor cursor;
 
   if (gSettings.machine_readable)
+  {
+    cout << "<progress id=\"" << id << "\" type=\"done\" name=\"" << s << "\"/>" << endl;
     return;
+  }
 
   out << CLEARLN << cursor.done() << " " << s;
   out << flush;
@@ -50,10 +59,14 @@ void display_done (ostream & out, const string& s) {
 
 // ----------------------------------------------------------------------------
 
-void display_done (ostream & out) {
+void display_done (const std::string &id, ostream & out) {
 
   if (gSettings.machine_readable)
+  {
+    display_done( id, cout, "");
     return;
+  }
+
 
   out << endl;
 }
index 40baca6..500c081 100644 (file)
@@ -25,15 +25,16 @@ enum Error {
     INVALID,
 };
 */
-void display_progress (std::ostream & out, const std::string& s, int percent);
-void display_tick (std::ostream & out, const std::string& s);
-void display_done (ostream & out, const std::string& s);
+void display_progress ( const std::string &id, std::ostream & out, const std::string& s, int percent);
+void display_tick ( const std::string &id, std::ostream & out, const std::string& s);
+void display_done ( const std::string &id, ostream & out, const std::string& s);
 // newline if normal progress is on single line
-void display_done (ostream & out);
+void display_done ( const std::string &id, ostream & out);
 
 template<typename Error>
 void display_error (Error error, const std::string& reason) {
-  if (error != 0 /*NO_ERROR*/) {
+  if (error != 0 /*NO_ERROR*/)
+  {
     static const char * error_s[] = {
       // TranslatorExplanation These are reasons for various failures.
       "", _("Not found"), _("I/O error"), _("Invalid object")
index 5913f32..f853569 100644 (file)
@@ -64,20 +64,20 @@ namespace ZmartRecipients
 
     virtual bool progress(int value, const zypp::Url & /*file*/)
     {
-      display_progress (cout_v, "Downloading", value);
+      display_progress ("download", cout_v, "Downloading", value);
       return true;
     }
 
     virtual DownloadProgressReport::Action problem( const zypp::Url & /*file*/, DownloadProgressReport::Error error, const std::string & description )
     {
-      display_done (cout_v);
+      display_done ("download", cout_v);
       display_error (error, description);
       return DownloadProgressReport::ABORT;
     }
 
     virtual void finish( const zypp::Url & /*file*/, Error error, const std::string & konreason )
     {
-      display_done (cout_v);
+      display_done ("download", cout_v);
       display_error (error, konreason);
     }
   };
index 6bdec6b..9eced08 100644 (file)
@@ -35,8 +35,15 @@ struct MessageResolvableReportReceiver : public zypp::callback::ReceiveReport<zy
 {
   virtual void show( zypp::Message::constPtr message )
   {
-    cout_v << message << endl; // [message]important-msg-1.0-1
-    cout_n << message->text() << endl;
+    if ( !gSettings.machine_readable )
+    {
+      cout_v << message << endl; // [message]important-msg-1.0-1
+      cout_n << message->text() << endl;
+      return;
+    }
+    
+    cout << "<message>" << message->text() << "</message>" << endl;
+    
     //! \todo in interactive mode, wait for ENTER?
   }
 };
@@ -74,13 +81,13 @@ struct ScriptResolvableReportReceiver : public zypp::callback::ReceiveReport<zyp
   }
   /** Report error. */
   virtual void problem( const std::string & description ) {
-    display_done (cout_n);
+    display_done ( "run-script", cout_n);
     cerr << description << endl;
   }
 
   /** Report success. */
   virtual void finish() {
-    display_done (cout_n);
+    display_done ("run-script", cout_n);
   }
 
 };
@@ -107,7 +114,7 @@ struct ScanRpmDbReceive : public zypp::callback::ReceiveReport<zypp::target::rpm
     // this is called too often. relax a bit.
     static int last = -1;
     if (last != value)
-      display_progress (cout, _("Reading installed packages"), value);
+      display_progress ( "read-installed-packages", cout, _("Reading installed packages"), value);
     last = value;
     return true;
   }
@@ -119,7 +126,7 @@ struct ScanRpmDbReceive : public zypp::callback::ReceiveReport<zypp::target::rpm
 
   virtual void finish( Error error, const std::string & reason )
   {
-    display_done (cout);
+    display_done ("read-installed-packages", cout);
     display_error (error, reason);
   }
 };
@@ -130,14 +137,13 @@ struct RemoveResolvableReportReceiver : public zypp::callback::ReceiveReport<zyp
 {
   virtual void start( zypp::Resolvable::constPtr resolvable )
   {
-    cout << boost::format(_("Removing: %s-%s"))
-        % resolvable->name() % resolvable->edition() << endl;
+    display_progress ( "remove-resolvable", cout, _("Removing ") + to_string (resolvable), 0);
   }
 
   virtual bool progress(int value, zypp::Resolvable::constPtr resolvable)
   {
     // TranslatorExplanation This text is a progress display label e.g. "Removing [42%]"
-    display_progress (cout, _("Removing ") + to_string (resolvable), value);
+    display_progress ( "remove-resolvable", cout, _("Removing ") + to_string (resolvable), value);
     return true;
   }
 
@@ -150,7 +156,7 @@ struct RemoveResolvableReportReceiver : public zypp::callback::ReceiveReport<zyp
 
   virtual void finish( zypp::Resolvable::constPtr /*resolvable*/, Error error, const std::string & reason )
   {
-    display_done (cout);
+    display_done ( "remove-resolvable", cout);
     display_error (error, reason);
   }
 };
@@ -169,20 +175,23 @@ struct InstallResolvableReportReceiver : public zypp::callback::ReceiveReport<zy
 {
   zypp::Resolvable::constPtr _resolvable;
   
-  void display_step( zypp::Resolvable::constPtr /*resolvable*/, int value )
+  void display_step( zypp::Resolvable::constPtr resolvable, int value )
   {
     // TranslatorExplanation This text is a progress display label e.g. "Installing [42%]"
-    display_progress (cout, _("Installing ") /* + to_string (resolvable) */,  value);
+    stringstream s;
+    s << (boost::format(_("Installing: %s-%s"))
+        % resolvable->name() % resolvable->edition());
+    display_progress ( "install-resolvable", cout, s.str(),  value);
   }
 
   virtual void start( zypp::Resolvable::constPtr resolvable )
   {
     _resolvable = resolvable;
-    if (gSettings.machine_readable)
-      cout << "Installing: " + resolvable->name() << endl;
-    else
-      cout << boost::format(_("Installing: %s-%s"))
-        % resolvable->name() % resolvable->edition() << endl;
+    
+    stringstream s;
+    s << (boost::format(_("Installing: %s-%s"))
+        % resolvable->name() % resolvable->edition());
+    display_progress ( "install-resolvable", cout, s.str(),  0);
   }
 
   virtual bool progress(int value, zypp::Resolvable::constPtr resolvable)
@@ -210,7 +219,7 @@ struct InstallResolvableReportReceiver : public zypp::callback::ReceiveReport<zy
 
   virtual void finish( zypp::Resolvable::constPtr /*resolvable*/, Error error, const std::string & reason, RpmLevel level )
   {
-    display_done (cout);
+    display_done ( "install-resolvable", cout);
     if (error != NO_ERROR) {
       cerr << level;
     }
index ea7ba34..e44225f 100644 (file)
@@ -16,6 +16,7 @@
 #include <boost/format.hpp>
 
 #include <zypp/base/Logger.h>
+#include <zypp/base/String.h>
 #include <zypp/ZYppCallbacks.h>
 #include <zypp/Pathname.h>
 #include <zypp/KeyRing.h>
@@ -79,9 +80,9 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   zypp::Pathname _patch;
   zypp::ByteCount _patch_size;
   
-  void display_step( const std::string &what, int value )
+  void display_step( const std::string &id, const std::string &what, int value )
   {
-    display_progress (cout_v, what, value);
+    display_progress ("download-resolvable", cout_v, what, value);
   }
   
   // Dowmload delta rpm:
@@ -100,7 +101,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   virtual bool progressDeltaDownload( int value )
   {
     // TranslatorExplanation This text is a progress display label e.g. "Downloading delta [42%]"
-    display_step( _("Downloading delta") /*+ _delta.asString()*/, value );
+    display_step( "apply-delta", _("Downloading delta") /*+ _delta.asString()*/, value );
     return true;
   }
 
@@ -111,7 +112,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   
   virtual void finishDeltaDownload()
   {
-    display_done (cout_v);
+    display_done ("download-resolvable", cout_v);
   }
 
   // Apply delta rpm:
@@ -127,7 +128,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   virtual void progressDeltaApply( int value )
   {
     // TranslatorExplanation This text is a progress display label e.g. "Applying delta [42%]"
-    display_step( _("Applying delta") /* + _delta.asString()*/, value );
+    display_step( "apply-delta", _("Applying delta") /* + _delta.asString()*/, value );
   }
 
   virtual void problemDeltaApply( const std::string & description )
@@ -137,7 +138,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
 
   virtual void finishDeltaApply()
   {
-    display_done (cout_v);
+    display_done ("apply-delta", cout_v);
   }
 
   // Dowmload patch rpm:
@@ -155,7 +156,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   virtual bool progressPatchDownload( int value )
   {
     // TranslatorExplanation This text is a progress display label e.g. "Applying patch rpm [42%]"
-    display_step( _("Applying patch rpm") /* + _patch.asString() */, value );
+    display_step( "apply-delta", _("Applying patch rpm") /* + _patch.asString() */, value );
     return true;
   }
   
@@ -166,7 +167,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   
   virtual void finishPatchDownload()
   {
-    display_done (cout_v);
+    display_done ("apply-delta", cout_v);
   }
   
   
@@ -195,7 +196,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
   virtual bool progress(int value, zypp::Resolvable::constPtr /*resolvable_ptr*/)
   {
     // TranslatorExplanation This text is a progress display label e.g. "Downloading [42%]"
-    display_step( _("Downloading") /* + resolvable_ptr->name() */, value );
+    display_step( "download-resolvable", _("Downloading") /* + resolvable_ptr->name() */, value );
     return true;
   }
 
@@ -207,7 +208,7 @@ struct DownloadResolvableReportReceiver : public zypp::callback::ReceiveReport<z
 
   virtual void finish( zypp::Resolvable::constPtr /*resolvable_ptr*/, Error error, const std::string & reason )
   {
-    display_done (cout_v);
+    display_done ("download-resolvable", cout_v);
     display_error (error, reason);
   }
 };
@@ -219,11 +220,11 @@ struct ProgressReportReceiver  : public zypp::callback::ReceiveReport<zypp::Prog
     //std::cout << "TICK!" << std::endl;
     if ( data.reportAlive() )
     {
-      display_tick (cout, data.name() );
+      display_tick (zypp::str::numstring(data.numericId()), cout, data.name() );
     }
     else
     {
-      display_progress (cout, data.name() , data.val() );
+      display_progress ( zypp::str::numstring(data.numericId()), cout, data.name() , data.val() );
     }
     
     
@@ -249,7 +250,7 @@ struct ProgressReportReceiver  : public zypp::callback::ReceiveReport<zypp::Prog
 
   virtual void finish( const zypp::ProgressData &data )
   {
-    display_done(cout, data.name() );
+    display_done(zypp::str::numstring(data.numericId()), cout, data.name() );
   }
 };
 
@@ -266,7 +267,7 @@ struct RepoReportReceiver  : public zypp::callback::ReceiveReport<zypp::repo::Re
 
   void display_step( int value )
   {
-    display_progress (cout, "(" + _repo.info().alias() + ") " + _task , value);
+    display_progress ( "repo", cout, "(" + _repo.info().alias() + ") " + _task , value);
   }
 
   virtual bool progress( int value )
@@ -277,7 +278,7 @@ struct RepoReportReceiver  : public zypp::callback::ReceiveReport<zypp::repo::Re
   
   virtual Action problem( zypp::Repository /*repo*/, Error error, const std::string & description )
   {
-    display_done (cout);
+    display_done ("repo", cout);
     display_error (error, description);
     return (Action) read_action_ari (ABORT);
   }
@@ -289,7 +290,7 @@ struct RepoReportReceiver  : public zypp::callback::ReceiveReport<zypp::repo::Re
     if (boost::algorithm::starts_with (task, "Reading patch"))
       cout << '\r' << flush;
     else
-      display_done (cout);
+      display_done ("repo", cout);
     display_error (error, reason);
   }
 
index 722c4eb..3ec6081 100644 (file)
@@ -651,7 +651,11 @@ int one_command(const ZypperCommand & command, int argc, char **argv)
   // === process options ===
 
   if (gopts.count("terse")) 
+  {
     gSettings.machine_readable = true;
+    cout << "<?xml version='1.0'?>" << endl;
+    cout << "<stream>" << endl;
+  }
 
   if (gopts.count("disable-repositories") ||
       gopts.count("disable-system-sources"))
@@ -1216,7 +1220,6 @@ int one_command(const ZypperCommand & command, int argc, char **argv)
     cond_load_resolvables();
     establish ();
 
-    cout << "<?xml version='1.0'?>" << endl;
     cout << "<update-status version=\"0.4\">" << endl;
     cout << "<update-list>" << endl;
     if (!xml_list_patches ())  // Only list updates if no
@@ -1447,6 +1450,9 @@ int main(int argc, char **argv)
     }
   } say_goodbye __attribute__ ((__unused__));
 
+  if ( gSettings.machine_readable )
+    cout << "</stream>" << endl;
+  
        // set locale
   setlocale (LC_ALL, "");
   bindtextdomain (PACKAGE, LOCALEDIR);