~sil/desktopcouch/create-oauth-tokens-startup

« back to all changes in this revision

Viewing changes to desktopcouch/records/server.py

  • Committer: Tarmac
  • Author(s): Ken VanDine
  • Date: 2009-07-28 03:49:10 UTC
  • mfrom: (14.1.20 desktopcouch)
  • Revision ID: elliot@elliotmurphy.com-20090728034910-pmll010azyaaw3aw
A collection of cleanups in preparation for packaging:

Bug #399019: losf "command of doom" in advertisePort doesn't work on Karmic (Medium – Confirmed)
Bug #400157: provide dbus api to couchdb port (High – Incomplete)
Don't fallback to system couchdb.
Use xdg to find the path for the bookmark template.
Added a shortcut file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
from couchdb import Server
24
24
from couchdb.client import ResourceNotFound, ResourceConflict
25
 
 
26
 
from record import Record
 
25
import desktopcouch
 
26
from desktopcouch.record import Record
27
27
 
28
28
 
29
29
class NoSuchDatabase(Exception):
43
43
 
44
44
    def __init__(self, database, uri=None, record_factory=None, create=False):
45
45
        if not uri:
46
 
            port = self._find_couchdb_port()
 
46
            pid = desktopcouch.find_pid()
 
47
            port = desktopcouch.find_port(pid)
47
48
            self.server_uri = "http://localhost:%s" % port
48
49
        else:
49
50
            self.server_uri = uri
56
57
        self.db = self._server[database]
57
58
        self.record_factory = record_factory or Record
58
59
 
59
 
    def _find_couchdb_port(self):
60
 
        """Look for a running Desktop CouchDB. If there isn't one, fall back
61
 
           to the system CouchDB on 5984."""
62
 
        # Currently using horrible lsof command; this will eventually use D-Bus
63
 
        # pylint: disable-msg=W0702
64
 
        try:
65
 
            import subprocess
66
 
            from desktopcouch import local_files
67
 
            lsofcmd = 'lsof -i4TCP@localhost | grep $(couchdb -C %s ' + \
68
 
               '-p %s -s | cut -d" " -f7 | cut -d "," -f1) 2>/dev/null | ' + \
69
 
                      'grep "TCP " | grep LISTEN | ' + \
70
 
                      'awk \'{print $8}\' | cut -d":" -f2'
71
 
            actual_lsof_cmd = lsofcmd % (local_files.FILE_INI,
72
 
              local_files.FILE_PID)
73
 
            stdout, stderr = subprocess.Popen(actual_lsof_cmd, shell=True,
74
 
              stdout=subprocess.PIPE).communicate()
75
 
            port = stdout.strip()
76
 
        except:
77
 
            port = 5984
78
 
        if not port:
79
 
            port = 5984
80
 
        return port
81
 
 
82
60
    def query(self, map_fun, reduce_fun=None, language='javascript',
83
61
      wrapper=None, **options):
84
62
        """Pass-through to CouchDB query"""