|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | import subprocess |
4 | | -import plistlib |
5 | | -import six |
6 | | - |
7 | | -_LAUNCHCTL_CMD = "launchctl" |
8 | | - |
9 | | - |
10 | | -def jobs_cmd(): |
11 | | - ''' |
12 | | - Wrapper for `launchctl list` |
13 | | -
|
14 | | - Returns a generator for LaunchdJob |
15 | | - ''' |
16 | | - if six.PY2: |
17 | | - stdout = launchctl("list") |
18 | | - else: |
19 | | - stdout = launchctl("list").decode("utf-8") |
20 | | - # PID, Status, Label |
21 | | - lines = iter(stdout.splitlines()) |
22 | | - # skip first line |
23 | | - next(lines) |
24 | | - sep = "\t" |
25 | | - Ox = "0x" |
26 | | - for line in lines: |
27 | | - pid, _, last = line.strip().partition(sep) |
28 | | - status, _, label = last.strip().partition(sep) |
29 | | - if label.startswith(Ox): |
30 | | - continue |
31 | | - if pid.isdigit(): |
32 | | - pid = int(pid) |
33 | | - else: |
34 | | - pid = None |
35 | | - if status.isdigit(): |
36 | | - status = int(status) |
37 | | - else: |
38 | | - status = None |
39 | | - yield LaunchdJob(label, pid, status) |
40 | 4 |
|
41 | | - |
42 | | -def job_properties_cmd(joblabel): |
43 | | - ''' |
44 | | - Wrapper for `launchctl -x LABEL` |
45 | | -
|
46 | | - Returns dictionary |
47 | | - :param job: string label or LaunchdJob |
48 | | - ''' |
49 | | - if six.PY2: |
50 | | - return dict(plistlib.readPlistFromString(launchctl("list", "-x", joblabel.encode("utf-8")))) |
51 | | - else: |
52 | | - return dict(plistlib.readPlistFromBytes(launchctl("list", "-x", joblabel))) |
| 5 | +import six |
53 | 6 |
|
54 | 7 |
|
55 | 8 | def launchctl(subcommand, *args): |
56 | 9 | ''' |
57 | 10 | A minimal wrapper to call the launchctl binary and capture the output |
58 | 11 | :param subcommand: string |
59 | 12 | ''' |
| 13 | + if not isinstance(subcommand, six.string_types): |
| 14 | + raise ValueError("Argument is invalid: %r" % repr(subcommand)) |
60 | 15 | if isinstance(subcommand, six.text_type): |
61 | 16 | subcommand = subcommand.encode('utf-8') |
62 | | - else: |
63 | | - raise ValueError("Argument is invalid: %r" % repr(subcommand)) |
64 | | - cmd = [_LAUNCHCTL_CMD, subcommand] |
| 17 | + |
| 18 | + cmd = ["launchctl", subcommand] |
65 | 19 | for arg in args: |
66 | | - if isinstance(arg, six.text_type): |
67 | | - cmd.append(arg.encode('utf-8')) |
| 20 | + if isinstance(arg, six.string_types): |
| 21 | + if isinstance(arg, six.text_type): |
| 22 | + cmd.append(arg.encode('utf-8')) |
| 23 | + else: |
| 24 | + cmd.append(arg) |
68 | 25 | else: |
69 | 26 | raise ValueError("Argument is invalid: %r" % repr(arg)) |
70 | | - output = subprocess.check_output(cmd, stdin=None, stderr=subprocess.STDOUT, shell=False) |
71 | | - return output |
| 27 | + return subprocess.check_output(cmd, stdin=None, stderr=subprocess.STDOUT, shell=False) |
0 commit comments