~ubuntu-branches/ubuntu/trusty/plainbox-provider-checkbox/trusty

« back to all changes in this revision

Viewing changes to bin/process_wait

  • Committer: Package Import Robot
  • Author(s): Zygmunt Krynicki
  • Date: 2014-04-07 19:00:31 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140407190031-rf836grml6oilfyt
Tags: 0.4-1
* New upstream release. List of bugfixes:
  https://launchpad.net/plainbox-provider-checkbox/14.04/0.4
* debian/watch: look for new releases on launchpad
* debian/rules: stop using pybuild and use manage.py
  {i18n,build,install,validate} instead. This also drops dependency on
  python3-distutils-extra and replaces that with intltool
* debian/control: drop X-Python3-Version
* debian/control: make plainbox-provider-checkbox depend on python and
  python2.7 (for some scripts) rather than suggesting them.
* debian/upstream/signing-key.asc: Use armoured gpg keys to avoid having to
  keep binary files in Debian packaging. Also, replace that with my key
  since I made the 0.3 release upstream.
* debian/source/lintian-overrides: add an override for warning about no
  source for flash movie with reference to a bug report that discusses that
  issue.
* debian/source/include-binaries: drop (no longer needed)
* debian/patches: drop (no longer needed)
* debian/plainbox-provider-checkbox.lintian-overrides: drop (no longer
  needed)
* Stop being a python3 module, move to from DPMT to PAPT

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
 
 
3
import os
 
4
import sys
 
5
import time
 
6
 
 
7
from optparse import OptionParser
 
8
from subprocess import Popen, PIPE
 
9
 
 
10
 
 
11
COMMAND_FORMAT = "pgrep -f %(options)s %(process)s"
 
12
 
 
13
 
 
14
def process_pids(process, *options):
 
15
    options_string = " ".join(options)
 
16
    command = COMMAND_FORMAT % {"options": options_string, "process": process}
 
17
 
 
18
    # Exclude this process and the pgrep process
 
19
    subprocess = Popen(
 
20
        command, stdout=PIPE, shell=True, universal_newlines=True)
 
21
    exclude_pids = [os.getpid(), os.getppid(), subprocess.pid]
 
22
 
 
23
    pids_string = subprocess.communicate()[0]
 
24
    pids = [int(pid) for pid in pids_string.split()]
 
25
 
 
26
    result = set(pids).difference(exclude_pids)
 
27
    return list(result)
 
28
 
 
29
 
 
30
def process_count(*args):
 
31
    return len(process_pids(*args))
 
32
 
 
33
 
 
34
def main(args):
 
35
    default_sleep = 1
 
36
 
 
37
    usage = "Usage: %prog PROCESS [PROCESS...]"
 
38
    parser = OptionParser(usage=usage)
 
39
    parser.add_option("-s", "--sleep",
 
40
        type="int",
 
41
        default=default_sleep,
 
42
        help="Number of seconds to sleep between checks.")
 
43
    parser.add_option("-t", "--timeout",
 
44
        type="int",
 
45
        help="Number of seconds to timeout from sleeping.")
 
46
    parser.add_option("-u", "--uid",
 
47
        help="Effective user name or id of the running processes")
 
48
    (options, processes) = parser.parse_args(args)
 
49
 
 
50
    process_args = []
 
51
    if options.uid is not None:
 
52
        process_args.extend(["-u", options.uid])
 
53
 
 
54
    while True:
 
55
        for process in processes:
 
56
            if process_count(process, *process_args):
 
57
                break
 
58
        else:
 
59
            break
 
60
 
 
61
        if options.timeout is not None:
 
62
            if options.timeout <= 0:
 
63
                return 1
 
64
            else:
 
65
                options.timeout -= options.sleep
 
66
 
 
67
        time.sleep(options.sleep)
 
68
 
 
69
    return 0
 
70
 
 
71
 
 
72
if __name__ == "__main__":
 
73
    sys.exit(main(sys.argv[1:]))