~ubuntu-branches/ubuntu/trusty/desktopcouch/trusty

« back to all changes in this revision

Viewing changes to desktopcouch/__init__.py

  • Committer: Ken VanDine
  • Date: 2010-09-15 13:44:49 UTC
  • mfrom: (1.5.7 upstream)
  • Revision ID: ken.vandine@canonical.com-20100915134449-z85ue5na2h9v40e9
* Add Ubuntu One pairing to desktopcouch, the code looks for
  credentials at start time and will listen to them while the dbus
  service is running.
* On couchdb failure, reconnect with logic that starts up the
  couchdb server again and uses the new port. Instead of giving
  python-couchdb view objects to the user, give an object we own
  that implements reconnecting to the server on errors, and proxies
  commands otherwise. (LP: #522538)
* Support new Basic auth for HTTP that our bookmark file
  requires. Make Basic-auth part of compulsory INI file. (LP:
  #599745)
* When a stored record exists and is marked as deleted, and a user
  tries to store a new record with the same ID, do some ugly work to
  make the user's record be the deleted-record's successor. (LP:
  #462245)
* Removed debian/patches/lp_599745.patch since it was included in
  upstream.
* Fix errors in parameters in new code. (LP: #634396 #634784)

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
"Desktop Couch helper files"
17
17
 
18
18
from __future__ import with_statement
19
 
import os, re, xdg.BaseDirectory
20
 
from desktopcouch.start_local_couchdb import process_is_couchdb, read_pidfile
 
19
import os
 
20
import re
 
21
import xdg.BaseDirectory
21
22
import dbus
22
23
import logging
 
24
import platform
 
25
import os
 
26
from desktopcouch import local_files
 
27
 
 
28
 
 
29
def process_is_couchdb_linux(pid):
 
30
    """Find if the process with the given pid is couchdb."""
 
31
    if pid is None:
 
32
        return False
 
33
    pid = int(pid)  # ensure it's a number
 
34
    proc_dir = "/proc/%d" % (pid,)
 
35
    try:
 
36
        # check to make sure it is actually a desktop-couch instance
 
37
        with open(os.path.join(proc_dir, 'cmdline')) as cmd_file:
 
38
            cmd = cmd_file.read()
 
39
        if 'beam' not in cmd:
 
40
            return False
 
41
 
 
42
        # make sure it's our process.
 
43
        if not os.access(os.path.join(proc_dir, "mem"), os.W_OK):
 
44
            return False
 
45
 
 
46
    except IOError:
 
47
        return False
 
48
 
 
49
    return True
 
50
 
 
51
os_name = platform.system()
 
52
try:
 
53
    process_is_couchdb = {
 
54
        "Linux": process_is_couchdb_linux
 
55
        } [os_name]
 
56
except KeyError:
 
57
    raise NotImplementedError("os %r is not yet supported" % (os_name,))
 
58
 
 
59
def read_pidfile(ctx=local_files.DEFAULT_CONTEXT):
 
60
    """Read the pid file for the required information."""
 
61
    try:
 
62
        pid_file = ctx.file_pid
 
63
        if not os.path.exists(pid_file):
 
64
            return None
 
65
        with open(pid_file) as fp:
 
66
            try:
 
67
                contents = fp.read()
 
68
                if contents == "\n":
 
69
                    return None  # not yet written to pid file
 
70
                return int(contents)
 
71
            except ValueError:
 
72
                logging.warn("Pid file does not contain int: %r", contents)
 
73
                return None
 
74
    except IOError, e:
 
75
        logging.warn("Reading pid file caused error.  %s", e)
 
76
        return None
23
77
 
24
78
 
25
79
def find_pid(start_if_not_running=True, ctx=local_files.DEFAULT_CONTEXT):
33
87
            logging.info("Desktop CouchDB is not running; starting it.")
34
88
            from desktopcouch import start_local_couchdb
35
89
            pid = start_local_couchdb.start_couchdb(ctx=ctx)
36
 
            # now load the design documents, because it's started
 
90
            # now load the design documents and pair records updates,
 
91
            # because it's started
37
92
            start_local_couchdb.update_design_documents()
38
 
 
 
93
            start_local_couchdb.update_pairing_service()
39
94
            if not process_is_couchdb(pid):
40
95
                logging.error("CouchDB process did not start up")
41
96
                raise RuntimeError("desktop-couch not started")
77
132
 
78
133
    if pid is None:
79
134
        if retries_left:
80
 
            return __find_port__linux(pid, ctx, retries_left-1)
 
135
            return __find_port__linux(pid, ctx, retries_left - 1)
81
136
        raise RuntimeError("Have no PID to use to look up port.")
82
137
 
83
138
    proc_dir = "/proc/%d" % (pid,)
91
146
                dirent_path = os.path.join(fd_dir, dirent)
92
147
                fd_paths.append(os.readlink(dirent_path))
93
148
            except OSError:
94
 
                logging.debug("dirent %r disappeared before we could read it. " %
95
 
                        (dirent_path,))
 
149
                logging.debug("dirent %r disappeared before " +
 
150
                    "we could read it. ", dirent_path)
96
151
                continue
97
152
    except OSError:
98
153
        if retries_left:
99
 
            return __find_port__linux(pid, ctx, retries_left-1)
 
154
            return __find_port__linux(pid, ctx, retries_left - 1)
100
155
        logging.exception("Unable to find file descriptors in %s" % proc_dir)
101
156
        raise RuntimeError("Unable to find file descriptors in %s" % proc_dir)
102
157
 
135
190
 
136
191
    if port is None:
137
192
        if retries_left:
138
 
            return __find_port__linux(pid, ctx, retries_left-1)
 
193
            return __find_port__linux(pid, ctx, retries_left - 1)
139
194
        raise RuntimeError("Unable to find listening port")
140
195
 
141
196
    return port
142
197
 
143
 
 
144
 
 
145
198
import platform
146
199
os_name = platform.system()
147
200
try: