File: hook.py

package info (click to toggle)
click 0.5.2-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,372 kB
  • sloc: python: 7,135; ansic: 857; makefile: 441; sh: 236; perl: 26; xml: 11
file content (96 lines) | stat: -rw-r--r-- 3,073 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright (C) 2013 Canonical Ltd.
# Author: Colin Watson <cjwatson@ubuntu.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""Install or remove a Click system hook."""

from __future__ import print_function

from optparse import OptionParser
import sys
from textwrap import dedent

import gi
gi.require_version('Click', '0.4')
from gi.repository import Click, GLib


per_hook_subcommands = {
    "install": "install",
    "remove": "remove",
    }


def run(argv):
    parser = OptionParser(dedent("""\
        %prog hook [options] SUBCOMMAND [...]

        Subcommands are as follows:

          install HOOK
          remove HOOK
          run-system
          run-user [--user=USER]"""))
    parser.add_option(
        "--root", metavar="PATH", help="look for additional packages in PATH")
    parser.add_option(
        "--user", metavar="USER",
        help=(
            "run user-level hooks for USER (default: current user; only "
            "applicable to run-user)"))
    options, args = parser.parse_args(argv)
    if len(args) < 1:
        parser.error("need subcommand (install, remove, run-system, run-user)")
    subcommand = args[0]
    if subcommand in per_hook_subcommands:
        if len(args) < 2:
            parser.error("need hook name")
        db = Click.DB()
        db.read(db_dir=None)
        if options.root is not None:
            db.add(options.root)
        name = args[1]
        hook = Click.Hook.open(db, name)
        getattr(hook, per_hook_subcommands[subcommand])(user_name=None)
    elif subcommand == "run-system":
        db = Click.DB()
        db.read(db_dir=None)
        if options.root is not None:
            db.add(options.root)
        try:
            Click.run_system_hooks(db)
        except GLib.GError as e:
            if e.domain == "click-hooks-error-quark":
                print(e.message, file=sys.stderr)
                return 1
            else:
                raise
    elif subcommand == "run-user":
        db = Click.DB()
        db.read(db_dir=None)
        if options.root is not None:
            db.add(options.root)
        try:
            Click.run_user_hooks(db, user_name=options.user)
        except GLib.GError as e:
            if e.domain == "click-hooks-error-quark":
                print(e.message, file=sys.stderr)
                return 1
            else:
                raise
    else:
        parser.error(
            "unknown subcommand '%s' (known: install, remove, run-system,"
            "run-user)" % subcommand)
    return 0