34
34
from __future__ import with_statement
35
35
import os, subprocess, sys
36
37
from desktopcouch import local_files
38
import xdg.BaseDirectory
38
41
def dump_ini(data, filename):
39
42
"""Dump INI data with sorted sections and keywords"""
102
105
def write_bookmark_file():
103
106
"""Write out an HTML document that the user can bookmark to find their DB"""
104
107
bookmark_file = os.path.join(local_files.DIR_DB, "couchdb.html")
105
bookmark_template = os.path.join(os.path.split(__file__)[0], "couchdb.tmpl")
109
if os.path.exists(os.path.join(os.path.split(__file__)[0], "../data/couchdb.tmpl")):
110
bookmark_template = os.path.join(os.path.split(__file__)[0], "../data/couchdb.tmpl")
112
for base in xdg.BaseDirectory.xdg_data_dirs:
113
template_path = os.path.join(base, "desktopcouch", "couchdb.tmpl")
114
if os.path.exists(template_path):
115
bookmark_template = os.path.join(os.path.split(__file__)[0], template_path)
106
117
fp = open(bookmark_template)
110
# work out port number the horrible lsof way
113
lsofcmd = 'lsof -i4TCP@localhost | grep $(couchdb -C %s -p %s -s | ' + \
114
'cut -d" " -f7 | cut -d "," -f1) | grep "TCP " | ' + \
115
'grep LISTEN | awk \'{print $8}\' | cut -d":" -f2'
116
actual_lsof_cmd = lsofcmd % (local_files.FILE_INI, local_files.FILE_PID)
117
stdout, stderr = subprocess.Popen(actual_lsof_cmd, shell=True,
118
stdout=subprocess.PIPE).communicate()
119
port = stdout.strip()
122
pid = desktopcouch.find_pid()
123
port = desktopcouch.find_port(pid)
121
125
fp = open(bookmark_file, "w")
122
126
fp.write(html.replace("[[COUCHDB_PORT]]", port))