16
16
"Desktop Couch helper files"
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
21
import xdg.BaseDirectory
26
from desktopcouch import local_files
29
def process_is_couchdb_linux(pid):
30
"""Find if the process with the given pid is couchdb."""
33
pid = int(pid) # ensure it's a number
34
proc_dir = "/proc/%d" % (pid,)
36
# check to make sure it is actually a desktop-couch instance
37
with open(os.path.join(proc_dir, 'cmdline')) as cmd_file:
42
# make sure it's our process.
43
if not os.access(os.path.join(proc_dir, "mem"), os.W_OK):
51
os_name = platform.system()
53
process_is_couchdb = {
54
"Linux": process_is_couchdb_linux
57
raise NotImplementedError("os %r is not yet supported" % (os_name,))
59
def read_pidfile(ctx=local_files.DEFAULT_CONTEXT):
60
"""Read the pid file for the required information."""
62
pid_file = ctx.file_pid
63
if not os.path.exists(pid_file):
65
with open(pid_file) as fp:
69
return None # not yet written to pid file
72
logging.warn("Pid file does not contain int: %r", contents)
75
logging.warn("Reading pid file caused error. %s", e)
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()
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")
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.")
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))
94
logging.debug("dirent %r disappeared before we could read it. " %
149
logging.debug("dirent %r disappeared before " +
150
"we could read it. ", dirent_path)
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)
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")
146
199
os_name = platform.system()