~ubuntu-branches/debian/squeeze/desktopcouch/squeeze

« back to all changes in this revision

Viewing changes to desktopcouch/stop_local_couchdb.py

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-03-04 19:11:48 UTC
  • mfrom: (1.5.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100304191148-zcwp3rffruwkalvc
Tags: 0.6.2-1
* New upstream version
* debian/patches/01-use_simplejson.patch removed, better handled
  upstream (simplejson for py2.5 and json for py2.6)
* debian/rules: chmod desktop-stop to 755, since it's intended as
  an executable script
* debian/control: bump debhelper dependency to >= 7.0.50~ to use
  overridden rules in debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
1
# Copyright 2009 Canonical Ltd.
3
2
#
4
3
# This file is part of desktopcouch.
16
15
# along with desktopcouch.  If not, see <http://www.gnu.org/licenses/>.
17
16
#
18
17
# Author: Chad Miller <chad.miller@canonical.com>
 
18
#         Stuart Langridge <stuart.langridge@canonical.com>
 
19
 
19
20
"""
20
21
Stop local CouchDB server.
21
22
"""
22
23
 
23
 
import os
24
 
import desktopcouch
25
 
import time
26
 
import signal
27
 
import errno
28
 
 
29
 
def stop_couchdb(pid=None):
30
 
    by_pid = pid
31
 
    if pid is None:
32
 
        pid = desktopcouch.find_pid(start_if_not_running=False)
33
 
    while pid is not None:
34
 
        try:
35
 
            os.kill(pid, signal.SIGTERM)
36
 
        except OSError, e:
37
 
            if e.errno == errno.ESRCH:
38
 
                break
39
 
            raise
40
 
 
41
 
        for retry in xrange(300):
42
 
            try:
43
 
                os.kill(pid, 0)  # test existence. sig-zero is special.
44
 
            except OSError:
45
 
                break
46
 
            time.sleep(0.01)
47
 
        if by_pid:
48
 
            pid = None
49
 
        else:
50
 
            pid = desktopcouch.find_pid(start_if_not_running=False)
 
24
import subprocess, sys
 
25
from desktopcouch import local_files
 
26
 
 
27
def stop_couchdb(ctx=local_files.DEFAULT_CONTEXT):
 
28
    local_exec = ctx.couch_exec_command + ["-k"]
 
29
    try:
 
30
        retcode = subprocess.call(local_exec, shell=False)
 
31
        if retcode < 0:
 
32
            print >> sys.stderr, "Child was terminated by signal", -retcode
 
33
        elif retcode > 0:
 
34
            print >> sys.stderr, "Child returned", retcode
 
35
    except OSError, e:
 
36
        print >> sys.stderr, "Execution failed: %s: %s" % (e, local_exec)
 
37
        exit(1)
 
38
 
51
39
 
52
40
if __name__ == "__main__":
53
41
    stop_couchdb()