From: Arvin Schnell Date: Tue, 4 Oct 2011 12:04:33 +0000 (+0200) Subject: - added python bindings by Thomas Fehr X-Git-Tag: v0.1.3~277 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=daad7aa58f597f1719dab27135d43aea9328f52f;p=thirdparty%2Fsnapper.git - added python bindings by Thomas Fehr --- diff --git a/.gitignore b/.gitignore index e67cdafb..91408fe6 100644 --- a/.gitignore +++ b/.gitignore @@ -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 index 00000000..141b41ae --- /dev/null +++ b/bindings/python/examples/test.py @@ -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 ) + diff --git a/bindings/python/libsnapper.i b/bindings/python/libsnapper.i index b6f69804..20c13508 100644 --- a/bindings/python/libsnapper.i +++ b/bindings/python/libsnapper.i @@ -8,11 +8,11 @@ using namespace std; #include +#include #include #include #include #include -#include %} 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; + +%template(vectorstring) std::vector; +%std_nodefconst_type(File); +%template(VectorFile) std::vector; +%template(mapstringstring) std::map; +%template(pairstringstring) std::pair; +%template(paircstringstring) std::pair; +%std_nodefconst_type(ConfigInfo); +%template(listConfigInfo) std::list; +%std_nodefconst_type(Snapshot); +%template(listSnapshot) std::list >; + +%extend snapper::Snapshots { +%fragment("SwigPyIterator_T"); + +%{ +template +class MySwigPyIteratorTmpl : public swig::SwigPyIteratorClosed_T +{ +public: + typedef swig::SwigPyIteratorClosed_T base; + typedef typename It::value_type value_type; + + MySwigPyIteratorTmpl(It curr, It first, It last, PyObject *seq) + : swig::SwigPyIteratorClosed_T(curr,first,last,seq), nend(last) + {} + PyObject *value() const { + if (base::current == nend) { + throw swig::stop_iteration(); + } else { + value_type* p = const_cast(&(*base::current)); + return swig::from_ptr(p,0); + } + return NULL; + } +protected: + It nend; +}; + +typedef MySwigPyIteratorTmpl MySnapshotIt; +typedef MySwigPyIteratorTmpl MyCSnapshotIt; +typedef MySwigPyIteratorTmpl MyFileIt; +typedef MySwigPyIteratorTmpl 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 *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(argp); + it = dynamic_cast *>(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 *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(argp); + it = dynamic_cast *>(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 *it1 = NULL; + swig::SwigPyIteratorClosed_T *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(argp); + it1 = dynamic_cast *>(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(argp); + it2 = dynamic_cast *>(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); + } +} diff --git a/examples/.gitignore b/examples/.gitignore index 8b24cf1c..e5fb4c56 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -8,3 +8,5 @@ CmpDirs.o CmpDirs CreateTimeline.o CreateTimeline +SnapTest.o +SnapTest diff --git a/examples/CmpDirs.cc b/examples/CmpDirs.cc index 303a7c5f..1324fb75 100644 --- a/examples/CmpDirs.cc +++ b/examples/CmpDirs.cc @@ -3,6 +3,7 @@ #include #include +#include using namespace snapper; using namespace std; diff --git a/examples/Makefile.am b/examples/Makefile.am index 0fdbf16e..f0fe1b80 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -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 index 00000000..b75526d2 --- /dev/null +++ b/examples/SnapTest.cc @@ -0,0 +1,74 @@ + +#include +#include + +#include +#include + +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 c = Snapper::getConfigs(); + + Snapper* sh = NULL; + + cout << c.size() << endl; + for (list::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 pl = sh->getIgnorePatterns(); + cout << pl.size() << endl; + for (vector::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); +} diff --git a/snapper/Compare.cc b/snapper/Compare.cc index 2ef1a5fc..5c3209ff 100644 --- a/snapper/Compare.cc +++ b/snapper/Compare.cc @@ -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; - } - } diff --git a/snapper/Compare.h b/snapper/Compare.h index b17fa756..3e8cb24a 100644 --- a/snapper/Compare.h +++ b/snapper/Compare.h @@ -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); - } diff --git a/snapper/File.cc b/snapper/File.cc index ff02c85e..94b73453 100644 --- a/snapper/File.cc +++ b/snapper/File.cc @@ -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; + } + } diff --git a/snapper/File.h b/snapper/File.h index 503ebfc3..e17c8e6e 100644 --- a/snapper/File.h +++ b/snapper/File.h @@ -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::iterator iterator; typedef vector::const_iterator const_iterator; + typedef vector::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); + } diff --git a/snapper/Snapshot.h b/snapper/Snapshot.h index 7d2e1250..ff2aabef 100644 --- a/snapper/Snapshot.h +++ b/snapper/Snapshot.h @@ -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 diff --git a/tools/compare-dirs.cc b/tools/compare-dirs.cc index aa510a5b..40990172 100644 --- a/tools/compare-dirs.cc +++ b/tools/compare-dirs.cc @@ -5,6 +5,7 @@ #include #include +#include using namespace snapper; using namespace std;