]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- added python bindings by Thomas Fehr
authorArvin Schnell <aschnell@suse.de>
Tue, 4 Oct 2011 12:04:33 +0000 (14:04 +0200)
committerArvin Schnell <aschnell@suse.de>
Tue, 4 Oct 2011 12:04:33 +0000 (14:04 +0200)
13 files changed:
.gitignore
bindings/python/examples/test.py [new file with mode: 0755]
bindings/python/libsnapper.i
examples/.gitignore
examples/CmpDirs.cc
examples/Makefile.am
examples/SnapTest.cc [new file with mode: 0644]
snapper/Compare.cc
snapper/Compare.h
snapper/File.cc
snapper/File.h
snapper/Snapshot.h
tools/compare-dirs.cc

index e67cdafb7b0acd5f765f01d0289297621cf7c05f..91408fe6b5771d97f794f51829cb8c032977d93d 100644 (file)
@@ -12,3 +12,4 @@ libtool
 ltmain.sh
 missing
 stamp-h1
+py-compile
diff --git a/bindings/python/examples/test.py b/bindings/python/examples/test.py
new file mode 100755 (executable)
index 0000000..141b41a
--- /dev/null
@@ -0,0 +1,70 @@
+#!/usr/bin/python
+
+import libsnapper
+
+def print_snap_info( s ):
+  if isinstance( s, libsnapper.SwigPyIterator ): 
+    try:
+      s = s.value()
+    except StopIteration:
+      print "end iterator"
+      return
+  print s.getNum(), s.getType(), s.getDescription()
+
+sh = libsnapper.createSnapper("root", False)
+pl = sh.getIgnorePatterns()
+cl = libsnapper.Snapper.getConfigs()
+print cl.size()
+for i in cl: print i.config_name, i.subvolume
+print pl.size()
+for i in pl: print i
+sl = sh.getSnapshots()
+for i in sl: print_snap_info( i )
+print sl.size()
+i = sl.begin()
+while i!=sl.end():
+  print_snap_info( i )
+  i.incr()
+i = sl.find(3)
+print_snap_info( i )
+i = sl.getSnapshotCurrent()
+print_snap_info( i )
+i = sl.find(11)
+print_snap_info( i )
+j = sl.findPre(i)
+print_snap_info( j )
+i = sl.find(10)
+print_snap_info( i )
+j = sl.findPost(i)
+print_snap_info( j )
+s=j.value().getDescription()
+j.value().setDescription(s+" 1")
+j = sl.findPost(i)
+print_snap_info( j )
+s=j.value().getDescription()
+j.value().setDescription(s[:-2])
+j = sl.findPost(i)
+print_snap_info( j )
+s=j.value().getUserdata()
+print s.items()
+s["key1"] = "value1"
+s["key2"] = "value2"
+s["key3"] = "value3"
+j.value().setUserdata(s)
+j = sl.findPost(i)
+print_snap_info( j )
+t=j.value().getUserdata()
+print t.items()
+t.clear()
+j.value().setUserdata(t)
+j = sl.findPost(i)
+print_snap_info( j )
+print j.value().getUserdata().items()
+j=sl.find(11);
+i=sl.findPre(j);
+cmp=libsnapper.Comparison(sh,i,j)
+i1=cmp.getSnapshot1()
+i2=cmp.getSnapshot2()
+print_snap_info( i1 )
+print_snap_info( i2 )
+
index b6f69804df08d28fef942ede9e564899b70bed3c..20c13508dfbfc21ee6f9734beba9de525a74c30b 100644 (file)
@@ -8,11 +8,11 @@
 using namespace std;
 
 #include <snapper/Factory.h>
+#include <snapper/Exception.h>
 #include <snapper/Snapshot.h>
 #include <snapper/Snapper.h>
 #include <snapper/File.h>
 #include <snapper/Comparison.h>
-#include <snapper/Exception.h>
 %}
 
 using namespace std;
@@ -20,19 +20,214 @@ using namespace std;
 %include "std_string.i"
 %include "std_vector.i"
 %include "std_list.i"
+%include "std_map.i"
 
 %typemap(out) std::string* {
     $result = PyString_FromString($1->c_str());
 }
 
+%ignore snapper::Snapshots::begin();
+%ignore snapper::Snapshots::begin() const;
+%ignore snapper::Snapshots::end();
+%ignore snapper::Snapshots::end() const;
+%ignore snapper::Snapshots::find(unsigned int);
+%ignore snapper::Snapshots::find(unsigned int) const;
+%ignore snapper::Snapshots::findPre(snapper::Snapshots::const_iterator);
+%ignore snapper::Snapshots::findPre(snapper::Snapshots::const_iterator) const;
+%ignore snapper::Snapshots::findPost(snapper::Snapshots::const_iterator);
+%ignore snapper::Snapshots::findPost(snapper::Snapshots::const_iterator) const;
+%ignore snapper::Snapshots::getSnapshotCurrent() const;
+%ignore snapper::Comparison::getSnapshot1() const;
+%ignore snapper::Comparison::getSnapshot2() const;
+
 %include "../../snapper/Factory.h"
+%include "../../snapper/Exception.h"
 %include "../../snapper/Snapshot.h"
 %include "../../snapper/Snapper.h"
 %include "../../snapper/File.h"
 %include "../../snapper/Comparison.h"
-%include "../../snapper/Exception.h"
 
 using namespace snapper;
 
-%template(vectorstring) vector<string>;
+
+%template(vectorstring) std::vector<string>;
+%std_nodefconst_type(File);
+%template(VectorFile) std::vector<File>;
+%template(mapstringstring) std::map<string,string>;
+%template(pairstringstring) std::pair<string,string>;
+%template(paircstringstring) std::pair<const string,string>;
+%std_nodefconst_type(ConfigInfo);
+%template(listConfigInfo) std::list<ConfigInfo>;
+%std_nodefconst_type(Snapshot);
+%template(listSnapshot) std::list<Snapshot, allocator< Snapshot > >;
+
+%extend snapper::Snapshots {
+%fragment("SwigPyIterator_T");
+
+%{
+template<class It>
+class MySwigPyIteratorTmpl : public swig::SwigPyIteratorClosed_T<It>
+{
+public:
+    typedef swig::SwigPyIteratorClosed_T<It> base;
+    typedef typename It::value_type value_type;
+
+    MySwigPyIteratorTmpl(It curr, It first, It last, PyObject *seq)
+        : swig::SwigPyIteratorClosed_T<It>(curr,first,last,seq), nend(last)
+       {}
+    PyObject *value() const {
+      if (base::current == nend) {
+       throw swig::stop_iteration();
+      } else {
+       value_type* p = const_cast<value_type *>(&(*base::current));
+       return swig::from_ptr(p,0);
+      }
+    return NULL;
+    }
+protected:
+    It nend;
+};
+
+typedef MySwigPyIteratorTmpl<snapper::Snapshots::iterator> MySnapshotIt;
+typedef MySwigPyIteratorTmpl<snapper::Snapshots::const_iterator> MyCSnapshotIt;
+typedef MySwigPyIteratorTmpl<snapper::Files::iterator> MyFileIt;
+typedef MySwigPyIteratorTmpl<snapper::Files::const_iterator> MyCFileIt;
+%}
+
+swig::SwigPyIterator* __iter__(PyObject **PYTHON_SELF)
+    {
+    return new MySnapshotIt(self->begin(), self->begin(),
+                            self->end(), *PYTHON_SELF);
+    }
+swig::SwigPyIterator* begin(PyObject **PYTHON_SELF)
+    {
+    return new MySnapshotIt(self->begin(), self->begin(),
+                           self->end(), *PYTHON_SELF);
+    }
+swig::SwigPyIterator* end(PyObject **PYTHON_SELF)
+    {
+    return new MySnapshotIt(self->end(), self->begin(),
+                           self->end(), *PYTHON_SELF);
+    }
+swig::SwigPyIterator* find(PyObject **PYTHON_SELF,unsigned int num)
+    {
+    return new MySnapshotIt(self->find(num), self->begin(),
+                           self->end(), *PYTHON_SELF);
+    }
+swig::SwigPyIterator* findPre(PyObject **PYTHON_SELF, PyObject *pobj )
+    {
+    swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *it = NULL;
+    void *argp = 0;
+    int ret = SWIG_ConvertPtr(pobj, &argp, SWIGTYPE_p_swig__SwigPyIterator, 0 );
+    if( SWIG_IsOK(ret))
+       {
+       swig::SwigPyIterator *pi = reinterpret_cast<swig::SwigPyIterator*>(argp);
+       it = dynamic_cast<swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *>(pi);
+       }
+    else
+       SWIG_exception_fail(SWIG_ArgError(ret), "in method '" "swig::SwigPyIterator* findPre" "', argument not of type swig::SwigPyIterator");
+
+    if( it )
+       return new MySnapshotIt(self->findPre(it->get_current()),
+                               self->begin(), self->end(), *PYTHON_SELF);
+    else
+
+       SWIG_exception_fail( SWIG_ArgError(ret),"argument of wrong swig::SwigPyIterator derived class in method '" "swig::SwigPyIterator* findPre" "'" );
+fail:
+    return NULL;
+    }
+
+swig::SwigPyIterator* findPost(PyObject **PYTHON_SELF, PyObject *pobj )
+    {
+    swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *it = NULL;
+    void *argp = 0;
+    int ret = SWIG_ConvertPtr(pobj, &argp, SWIGTYPE_p_swig__SwigPyIterator, 0 );
+    if( SWIG_IsOK(ret))
+       {
+       swig::SwigPyIterator *pi = reinterpret_cast<swig::SwigPyIterator*>(argp);
+       it = dynamic_cast<swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *>(pi);
+       }
+    else
+       SWIG_exception_fail(SWIG_ArgError(ret), "in method '" "swig::SwigPyIterator* findPre" "', argument not of type swig::SwigPyIterator");
+
+    if( it )
+       return new MySnapshotIt(self->findPost(it->get_current()),
+                               self->begin(), self->end(), *PYTHON_SELF);
+    else
+
+       SWIG_exception_fail( SWIG_ArgError(ret),"argument of wrong swig::SwigPyIterator derived class in method '" "swig::SwigPyIterator* findPre" "'" );
+fail:
+    return NULL;
+    }
+
+swig::SwigPyIterator* getSnapshotCurrent(PyObject **PYTHON_SELF)
+    {
+    return new MySnapshotIt(self->begin(), self->begin(),
+                           self->end(), *PYTHON_SELF);
+    }
+}
+
+%extend snapper::Comparison {
+
+Comparison( const snapper::Snapper* sn, PyObject *pobj1, PyObject *pobj2 )
+    {
+    swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *it1 = NULL;
+    swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *it2 = NULL;
+    void *argp = 0;
+    int ret = SWIG_ConvertPtr(pobj1, &argp, SWIGTYPE_p_swig__SwigPyIterator, 0 );
+    if( SWIG_IsOK(ret))
+       {
+       swig::SwigPyIterator *pi = reinterpret_cast<swig::SwigPyIterator*>(argp);
+       it1 = dynamic_cast<swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *>(pi);
+       }
+    else
+       SWIG_exception_fail(SWIG_ArgError(ret), "in method '" "swig::SwigPyIterator* findPre" "', argument 2 not of type swig::SwigPyIterator");
+
+    ret = SWIG_ConvertPtr(pobj2, &argp, SWIGTYPE_p_swig__SwigPyIterator, 0 );
+    if( SWIG_IsOK(ret))
+       {
+       swig::SwigPyIterator *pi = reinterpret_cast<swig::SwigPyIterator*>(argp);
+       it2 = dynamic_cast<swig::SwigPyIteratorClosed_T<snapper::Snapshots::iterator> *>(pi);
+       }
+    else
+       SWIG_exception_fail(SWIG_ArgError(ret), "in method '" "swig::SwigPyIterator* findPre" "', argument 3 not of type swig::SwigPyIterator");
+
+    if( it1 && it2 )
+       return new snapper::Comparison( sn, it1->get_current(), it2->get_current() );
+    else if( it1 )
+
+       SWIG_exception_fail( SWIG_ArgError(ret),"argument 3 of wrong swig::SwigPyIterator derived class in method '" "swig::SwigPyIterator* findPre" "'" );
+    else
+       SWIG_exception_fail( SWIG_ArgError(ret),"argument 2 of wrong swig::SwigPyIterator derived class in method '" "swig::SwigPyIterator* findPre" "'" );
+fail:
+    return NULL;
+    }
+
+swig::SwigPyIterator* getSnapshot1(PyObject **PYTHON_SELF)
+    {
+    snapper::Snapshots::const_iterator c = self->getSnapshot1();
+    snapper::Snapshots::const_iterator b = self->getSnapper()->getSnapshots().begin();
+    snapper::Snapshots::const_iterator e = self->getSnapper()->getSnapshots().end();
+
+    return new MyCSnapshotIt(c, b, e, *PYTHON_SELF);
+    }
+
+swig::SwigPyIterator* getSnapshot2(PyObject **PYTHON_SELF)
+    {
+    snapper::Snapshots::const_iterator c = self->getSnapshot2();
+    snapper::Snapshots::const_iterator b = self->getSnapper()->getSnapshots().begin();
+    snapper::Snapshots::const_iterator e = self->getSnapper()->getSnapshots().end();
+
+    return new MyCSnapshotIt(c, b, e, *PYTHON_SELF);
+    }
+}
+
+%extend snapper::Files {
+
+swig::SwigPyIterator* __iter__(PyObject **PYTHON_SELF)
+    {
+    return new MyFileIt(self->begin(), self->begin(),
+                        self->end(), *PYTHON_SELF);
+    }
+}
 
index 8b24cf1c5f7c8c72dfad5fe5d0fdaaf3d7113f9d..e5fb4c563bdbdabb2624d2ab3987ddf907967ad2 100644 (file)
@@ -8,3 +8,5 @@ CmpDirs.o
 CmpDirs
 CreateTimeline.o
 CreateTimeline
+SnapTest.o
+SnapTest
index 303a7c5f06c299c0fd092d91f55bffb497010dd0..1324fb7562b2d4cb3c94fbe08f693ae82ebd8f8b 100644 (file)
@@ -3,6 +3,7 @@
 #include <iostream>
 
 #include <snapper/Compare.h>
+#include <snapper/File.h>
 
 using namespace snapper;
 using namespace std;
index 0fdbf16e81ea486f973e1cf7ab89fb7975f3eade..f0fe1b809c44caedd5c9f7478ef732c465051376 100644 (file)
@@ -6,7 +6,7 @@ INCLUDES = -I$(top_srcdir)
 
 LDADD = ../snapper/libsnapper.la
 
-noinst_PROGRAMS = List ListAll Create CmpDirs CreateTimeline
+noinst_PROGRAMS = List ListAll Create CmpDirs CreateTimeline SnapTest
 
 List_SOURCES = List.cc
 
@@ -18,3 +18,5 @@ CmpDirs_SOURCES = CmpDirs.cc
 
 CreateTimeline_SOURCES = CreateTimeline.cc
 
+SnapTest_SOURCES = SnapTest.cc
+
diff --git a/examples/SnapTest.cc b/examples/SnapTest.cc
new file mode 100644 (file)
index 0000000..b75526d
--- /dev/null
@@ -0,0 +1,74 @@
+
+#include <stdlib.h>
+#include <iostream>
+
+#include <snapper/Factory.h>
+#include <snapper/Snapper.h>
+
+using namespace snapper;
+using namespace std;
+
+void print_snap_info( Snapshots::const_iterator s, 
+                      Snapshots::const_iterator e )
+    {
+    if( s==e )
+       cout << "end iterator" << endl;
+    else
+       cout << s->getNum() << " " << s->getType() << " " 
+            << s->getDescription() << endl;
+    }
+
+int
+main(int argc, char** argv)
+{
+    list<ConfigInfo> c = Snapper::getConfigs();
+
+    Snapper* sh = NULL;
+
+    cout << c.size() << endl;
+    for (list<ConfigInfo>::const_iterator it = c.begin(); it != c.end(); ++it)
+       cout << it->config_name << " " << it->subvolume << endl;
+
+    if( c.begin()!=c.end() )
+       sh = new Snapper(c.front().config_name);
+
+    if( sh )
+       {
+       vector<string> pl = sh->getIgnorePatterns();
+       cout << pl.size() << endl;
+       for (vector<string>::const_iterator s = pl.begin(); s!=pl.end(); ++s)
+           cout << *s << endl;
+       Snapshots& snap = sh->getSnapshots();
+       for (Snapshots::const_iterator s = snap.begin(); s!=snap.end(); ++s)
+           print_snap_info( s, snap.end() );
+       cout << snap.size() << endl;
+
+       Snapshots::const_iterator s = snap.begin();
+       while( s!=snap.end() )
+           {
+           print_snap_info( s, snap.end() );
+           ++s;
+           }
+       s = snap.find(3);
+       print_snap_info( s, snap.end() );
+       s = snap.getSnapshotCurrent();
+       print_snap_info( s, snap.end() );
+       s = snap.find(8);
+       print_snap_info( s, snap.end() );
+       Snapshots::iterator j = snap.findPre(s);
+       print_snap_info( j, snap.end() );
+       s = snap.find(6);
+       print_snap_info( s, snap.end() );
+       j = snap.findPost(s);
+       print_snap_info( j, snap.end() );
+       string desc = j->getDescription();
+       desc += " 1";
+       j->setDescription(desc);
+       j = snap.findPost(s);
+       print_snap_info( j, snap.end() );
+
+       delete sh;
+       }
+
+    exit(EXIT_SUCCESS);
+}
index 2ef1a5fc06978e899632787ec99fc08a8ca9ea4e..5c3209ffbfd327b9b377b544138b740a210be92f 100644 (file)
@@ -478,75 +478,4 @@ namespace snapper
        y2mil("stopwatch " << stopwatch << " for comparing directories");
     }
 
-
-    string
-    statusToString(unsigned int status)
-    {
-       string ret;
-
-       if (status & CREATED)
-           ret += "+";
-       else if (status & DELETED)
-           ret += "-";
-       else if (status & TYPE)
-           ret += "t";
-       else if (status & CONTENT)
-           ret += "c";
-       else
-           ret += ".";
-
-       ret += status & PERMISSIONS ? "p" : ".";
-       ret += status & USER ? "u" : ".";
-       ret += status & GROUP ? "g" : ".";
-
-       return ret;
-    }
-
-
-    unsigned int
-    stringToStatus(const string& str)
-    {
-       unsigned int ret = 0;
-
-       if (str.length() >= 1)
-       {
-           switch (str[0])
-           {
-               case '+': ret |= CREATED; break;
-               case '-': ret |= DELETED; break;
-               case 't': ret |= TYPE; break;
-               case 'c': ret |= CONTENT; break;
-           }
-       }
-
-       if (str.length() >= 2)
-       {
-           if (str[1] == 'p')
-               ret |= PERMISSIONS;
-       }
-
-       if (str.length() >= 3)
-       {
-           if (str[2] == 'u')
-               ret |= USER;
-       }
-
-       if (str.length() >= 4)
-       {
-           if (str[3] == 'g')
-               ret |= GROUP;
-       }
-
-       return ret;
-    }
-
-
-    unsigned int
-    invertStatus(unsigned int status)
-    {
-       if (status & (CREATED | DELETED))
-           return status ^ (CREATED | DELETED);
-       return status;
-    }
-
 }
index b17fa75601a28c56d501d06c3453b7f2107ca33c..3e8cb24a5bc4702ee9c84c1baef651af40e08804 100644 (file)
@@ -42,15 +42,6 @@ namespace snapper
     void
     cmpDirs(const string& path1, const string& path2, cmpdirs_cb_t cb);
 
-    string
-    statusToString(unsigned int status);
-
-    unsigned int
-    stringToStatus(const string& str);
-
-    unsigned int
-    invertStatus(unsigned int status);
-
 }
 
 
index ff02c85e430a37033c0259f977c85b4abfb62848..94b7345305e60c3f89459aae4e121799e0cd722a 100644 (file)
@@ -804,4 +804,75 @@ namespace snapper
        return true;
     }
 
+
+    string
+    statusToString(unsigned int status)
+    {
+       string ret;
+
+       if (status & CREATED)
+           ret += "+";
+       else if (status & DELETED)
+           ret += "-";
+       else if (status & TYPE)
+           ret += "t";
+       else if (status & CONTENT)
+           ret += "c";
+       else
+           ret += ".";
+
+       ret += status & PERMISSIONS ? "p" : ".";
+       ret += status & USER ? "u" : ".";
+       ret += status & GROUP ? "g" : ".";
+
+       return ret;
+    }
+
+
+    unsigned int
+    stringToStatus(const string& str)
+    {
+       unsigned int ret = 0;
+
+       if (str.length() >= 1)
+       {
+           switch (str[0])
+           {
+               case '+': ret |= CREATED; break;
+               case '-': ret |= DELETED; break;
+               case 't': ret |= TYPE; break;
+               case 'c': ret |= CONTENT; break;
+           }
+       }
+
+       if (str.length() >= 2)
+       {
+           if (str[1] == 'p')
+               ret |= PERMISSIONS;
+       }
+
+       if (str.length() >= 3)
+       {
+           if (str[2] == 'u')
+               ret |= USER;
+       }
+
+       if (str.length() >= 4)
+       {
+           if (str[3] == 'g')
+               ret |= GROUP;
+       }
+
+       return ret;
+    }
+
+
+    unsigned int
+    invertStatus(unsigned int status)
+    {
+       if (status & (CREATED | DELETED))
+           return status ^ (CREATED | DELETED);
+       return status;
+    }
+
 }
index 503ebfc3a3fb67d7777c3eee6f362c64bb75b759..e17c8e6e96af5e54a5b86dfb641497bea4715088 100644 (file)
@@ -97,8 +97,10 @@ namespace snapper
        void setUndo(bool value) { undo = value; }
        bool doUndo();
 
+#ifndef SWIG
        void setRollback(bool value) __attribute__ ((deprecated)) { setUndo(value); }
        bool doRollback() __attribute__ ((deprecated)) { return doUndo(); }
+#endif
 
        enum Action { CREATE, MODIFY, DELETE };
 
@@ -153,6 +155,7 @@ namespace snapper
 
        typedef vector<File>::iterator iterator;
        typedef vector<File>::const_iterator const_iterator;
+       typedef vector<File>::size_type size_type;
 
        iterator begin() { return entries.begin(); }
        const_iterator begin() const { return entries.begin(); }
@@ -160,6 +163,7 @@ namespace snapper
        iterator end() { return entries.end(); }
        const_iterator end() const { return entries.end(); }
 
+       size_type size() const { return entries.size(); }
        bool empty() const { return entries.empty(); }
 
        iterator find(const string& name);
@@ -187,6 +191,16 @@ namespace snapper
 
     };
 
+
+    string
+    statusToString(unsigned int status);
+
+    unsigned int
+    stringToStatus(const string& str);
+
+    unsigned int
+    invertStatus(unsigned int status);
+
 }
 
 
index 7d2e12502e27eb3e899ad40859a6df16957aeef3..ff2aabef6ccb85c5684dacf66605b71789d8e8e1 100644 (file)
@@ -119,11 +119,11 @@ namespace snapper
 
        const Snapper* snapper;
 
-       const SnapshotType type;
+       SnapshotType type;
 
-       const unsigned int num;
+       unsigned int num;
 
-       const time_t date;
+       time_t date;
 
        unsigned int pre_num;   // valid only for type=POST
 
index aa510a5b6e66375bea779c2ceb90930dd9aaf6ff..409901728415191bd64096cf42f755241cfbadca 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <snapper/AppUtil.h>
 #include <snapper/Compare.h>
+#include <snapper/File.h>
 
 using namespace snapper;
 using namespace std;