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

« back to all changes in this revision

Viewing changes to desktopcouch/start_local_couchdb.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:
33
33
 
34
34
from __future__ import with_statement
35
35
import os, subprocess, sys
 
36
import desktopcouch
36
37
from desktopcouch import local_files
 
38
import xdg.BaseDirectory
 
39
import time
37
40
 
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")
 
108
 
 
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")
 
111
    else:
 
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)
 
116
 
106
117
    fp = open(bookmark_template)
107
118
    html = fp.read()
108
119
    fp.close()
109
120
 
110
 
    # work out port number the horrible lsof way
111
 
    import time
112
121
    time.sleep(1)
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)
120
124
 
121
125
    fp = open(bookmark_file, "w")
122
126
    fp.write(html.replace("[[COUCHDB_PORT]]", port))