chiark / gitweb /
fixes and test for disorder-dump
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 17:31:18 +0000 (17:31 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 17:31:18 +0000 (17:31 +0000)
python/disorder.py.in
server/dump.c
tests/Makefile.am
tests/dump.py [new file with mode: 0755]

index 95465c022777a0913895090311606fc1940566ab..01b92edbcdbc3bcc561c7b654456c87daf844344 100644 (file)
@@ -755,6 +755,34 @@ class client:
     ret, details = self._simple("part", track, context, part)
     return details
 
+  def setglobal(self, key, value):
+    """Set a global preference value.
+
+    Arguments:
+    key -- the preference name
+    value -- the new preference value
+    """
+    self._simple("set-global", key, value)
+
+  def unsetglobal(self, key):
+    """Unset a global preference value.
+
+    Arguments:
+    key -- the preference to remove
+    """
+    self._simple("set-global", key, value)
+
+  def getglobal(self, key):
+    """Get a global preference value.
+
+    Arguments:
+    key -- the preference to look up
+
+    The return value is the preference 
+    """
+    ret, details = self._simple("get-global", key)
+    return details
+
   ########################################################################
   # I/O infrastructure
 
index 84b45d046199f1c28cd43391e02eba6cd1bb1daa..9363a344a0d5ead7de6c78d3dd1a0f00bf2c37db 100644 (file)
@@ -106,6 +106,7 @@ static void do_dump(FILE *fp, const char *tag,
       fatal(errno, "error calling ftruncate");
     if(fprintf(fp, "V%c\n", (tracksdb || searchdb) ? '1' : '0') < 0)
       fatal(errno, "error writing to %s", tag);
+    /* dump the preferences */
     cursor = trackdb_opencursor(trackdb_prefsdb, tid);
     err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
                         DB_FIRST);
@@ -122,6 +123,23 @@ static void do_dump(FILE *fp, const char *tag,
     if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
     cursor = 0;
 
+    /* dump the global preferences */
+    cursor = trackdb_opencursor(trackdb_globaldb, tid);
+    err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
+                        DB_FIRST);
+    while(err == 0) {
+      if(fputc('G', fp) < 0
+         || urlencode(s, k.data, k.size)
+         || fputc('\n', fp) < 0
+         || urlencode(s, d.data, d.size)
+         || fputc('\n', fp) < 0)
+        fatal(errno, "error writing to %s", tag);
+      err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
+                          DB_NEXT);
+    }
+    if(trackdb_closecursor(cursor)) { cursor = 0; goto fail; }
+    cursor = 0;
+    
     if(tracksdb) {
       cursor = trackdb_opencursor(trackdb_tracksdb, tid);
       err = cursor->c_get(cursor, prepare_data(&k), prepare_data(&d),
@@ -256,6 +274,8 @@ static int undump_dbt(FILE *fp, const char *tag, DBT *dbt) {
 static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
   int err, c;
   DBT k, d;
+  const char *which_name;
+  DB *which_db;
 
   info("undumping");
   if(fseek(fp, 0, SEEK_SET) < 0)
@@ -273,17 +293,25 @@ static int undump_from_fp(DB_TXN *tid, FILE *fp, const char *tag) {
     case 'E':
       return 0;
     case 'P':
+    case 'G':
+      if(c == 'P') {
+       which_db = trackdb_prefsdb;
+       which_name = "prefs.db";
+      } else {
+       which_db = trackdb_globaldb;
+       which_name = "global.db";
+      }
       if(undump_dbt(fp, tag, prepare_data(&k))
          || undump_dbt(fp, tag, prepare_data(&d)))
         break;
-      switch(err = trackdb_prefsdb->put(trackdb_prefsdb, tid, &k, &d, 0)) {
+      switch(err = trackdb_prefsdb->put(which_db, tid, &k, &d, 0)) {
       case 0:
         break;
       case DB_LOCK_DEADLOCK:
-        error(0, "error updating prefs.db: %s", db_strerror(err));
+        error(0, "error updating %s: %s", which_name, db_strerror(err));
         return err;
       default:
-        fatal(0, "error updating prefs.db: %s", db_strerror(err));
+        fatal(0, "error updating %s: %s", which_name, db_strerror(err));
       }
       break;
     case 'T':
index 962a320464099268225e14e6dfab55dcf6278573..56c64be47c922aa795f66b27a8354b44b143905b 100644 (file)
@@ -22,5 +22,5 @@ check:
        ${PYTHON} ${srcdir}/alltests
 
 EXTRA_DIST=alltests dtest.py nothing.py version.py dbversion.py search.py \
-       queue.py
+       queue.py dump.py
 
diff --git a/tests/dump.py b/tests/dump.py
new file mode 100755 (executable)
index 0000000..99d29b6
--- /dev/null
@@ -0,0 +1,56 @@
+#! /usr/bin/env python
+#
+# This file is part of DisOrder.
+# Copyright (C) 2007 Richard Kettlewell
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+#
+import dtest,time,disorder,re
+
+def test():
+    """Exercise database dumper"""
+    dtest.start_daemon()
+    c = disorder.client()
+    track = "%s/Joe Bloggs/First Album/02:Second track.ogg" % dtest.tracks
+    dump = "%s/dumpfile" % dtest.testroot
+    print "setting a track pref"
+    c.set(track, "foo", "before")
+    assert c.get(track, "foo") == "before", "checking track foo=before"
+    print "setting a global pref"
+    c.setglobal("foo", "before");
+    assert c.getglobal("foo") == "before", "checking global foo=before"
+    print "dumping database"
+    print dtest.command(["disorder-dump", "--config", disorder._configfile,
+                         "--dump", dump])
+    print "changing track pref"
+    c.set(track, "foo", "after");
+    assert c.get(track, "foo") == "after", "checking track foo=before"
+    print "changing global pref"
+    c.setglobal("foo", "after");
+    assert c.getglobal("foo") == "after", "checking global foo=before"
+    dtest.stop_daemon();
+    print "restoring database"
+    print dtest.command(["disorder-dump", "--config", disorder._configfile,
+                         "--undump", dump])
+    dtest.start_daemon(); 
+    c = disorder.client()
+    print "checking track pref"
+    assert c.get(track, "foo") == "before", "checking track foo=before after undump"
+    print "checking global pref"
+    assert c.getglobal("foo") == "before", "checking global foo=before after undump"
+
+if __name__ == '__main__':
+    dtest.run()