File: click

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 (110 lines) | stat: -rwxr-xr-x 3,289 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/python3

# 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/>.

"""Operations on Click packages."""

from __future__ import print_function

from optparse import OptionParser
import os
import signal
import sys
from textwrap import dedent

# Support running from the build tree.
sys.path.insert(0, os.path.join(sys.path[0], os.pardir))

import gi
gi.require_version('Click', '0.4')

# There is an unfortunate name clash with
# https://pypi.python.org/pypi/click; try to detect this and take evasive
# action.
import click_package as click
if not getattr(click, "_CLICK_IS_A_PACKAGING_FORMAT_", None):
    import site
    wrong_click_mods = [
        mod for mod in sys.modules if mod.split(".")[0] == "click"]
    for mod in wrong_click_mods:
        del sys.modules[mod]
    try:
        user_site_index = sys.path.index(site.getusersitepackages())
    except ValueError:
        print(
            "Cannot start click due to a conflict with a different "
            "locally-installed Python 'click' package.  Remove it using "
            "Python packaging tools and try again.",
            file=sys.stderr)
        sys.exit(1)
    del sys.path[user_site_index]

from click_package import commands


def fix_stdout():
    if sys.version >= "3":
        # Force encoding to UTF-8 even in non-UTF-8 locales.
        import io
        sys.stdout = io.TextIOWrapper(
            sys.stdout.detach(), encoding="UTF-8", line_buffering=True)
    else:
        # Avoid having to do .encode("UTF-8") everywhere.
        import codecs
        sys.stdout = codecs.EncodedFile(sys.stdout, "UTF-8")

        def null_decode(input, errors="strict"):
            return input, len(input)

        sys.stdout.decode = null_decode


def main():
    fix_stdout()
    # Python's default handling of SIGPIPE is not helpful to us.
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)

    parser = OptionParser(dedent("""\
        %%prog COMMAND [options]

        Commands are as follows ('%%prog COMMAND --help' for more):

        %s""") % commands.help_text())

    parser.disable_interspersed_args()
    _, args = parser.parse_args()
    if not args:
        parser.print_help()
        return 0
    command = args[0]
    args = args[1:]

    if command == "help":
        if args and args[0] in commands.all_commands:
            mod = commands.load_command(args[0])
            mod.run(["--help"])
        else:
            parser.print_help()
        return 0

    if command not in commands.all_commands:
        parser.error("unknown command: %s" % command)
    mod = commands.load_command(command)
    return mod.run(args)


if __name__ == "__main__":
    sys.exit(main())