This file is indexed.

/usr/lib/python2.7/dist-packages/commando/application.py is in python-commando 0.3.4-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# -*- coding: utf-8 -*-
"""
Declarative interface for argparse
"""
from argparse import ArgumentParser
from collections import namedtuple
from commando.util import getLoggerWithConsoleHandler

import logging
import sys

# pylint: disable-msg=R0903,C0103,C0301

try:
    import pkg_resources
    # pylint: disable-msg=E1103
    __version__ = pkg_resources.get_distribution('commando').version
except Exception: # pylint: disable-msg=W0703
    __version__ = 'unknown'

__all__ = [
    '__version__',
    'command',
    'subcommand',
    'param',
    'version',
    'store',
    'true',
    'false',
    'append',
    'const',
    'append_const',
    'Application'
]


class Commando(type):
    """
    Meta class that enables declarative command definitions
    """
    # pylint: disable-msg=R0912
    def __new__(mcs, name, bases, attrs):
        instance = super(Commando, mcs).__new__(mcs, name, bases, attrs)
        subcommands = []
        main_command = None
        main_parser = None
        # pylint: disable-msg=C0111
        # Collect commands based on decorators
        for member in attrs.itervalues():
            if hasattr(member, "command"):
                main_command = member
            elif hasattr(member, "subcommand"):
                subcommands.append(member)

        def add_arguments(func):
            params = getattr(func, 'params', [])
            for parameter in reversed(params):
                func.parser.add_argument(*parameter.args, **parameter.kwargs)

        def add_subparser(func):
            if getattr(func, 'parser', None):
                # Already initialized
                return
            if not func.parent:
                # Sub of main
                func.parent = main_command
            else:
                # Sub of something else
                if not getattr(func.parent, 'parser', None):
                    # Parser doesn't exist for the parent.
                    add_subparser(func.parent)
                if not getattr(func.parent, 'subparsers', None):
                    # Subparser collection doesn't exist for the parent.
                    func.parent.subparsers = func.parent.parser.add_subparsers()

            func.parser = func.parent.subparsers.add_parser(
                                                    *func.subcommand.args,
                                                    **func.subcommand.kwargs)
            add_arguments(func)

        if main_command:
            main_parser = ArgumentParser(*main_command.command.args,
                                        **main_command.command.kwargs)

            main_command.parser = main_parser
            add_arguments(main_command)

            if len(subcommands):
                main_command.subparsers = main_parser.add_subparsers()
                for sub in subcommands:
                    add_subparser(sub)

        for sub in subcommands:
            # Map the functions to the subparser actions
            if not getattr(sub, 'subparsers', None):
                # Only if there are no subcommands
                sub.parser.set_defaults(run=sub)

        instance.__main__ = main_command
        instance.__parser__ = main_parser

        return instance

values = namedtuple('__meta_values', 'args, kwargs')


class metarator(object):
    """
    A generic decorator that tags the decorated method with
    the passed in arguments for meta classes to process them.
    """

    def __init__(self, *args, **kwargs):
        self.values = values._make((args, kwargs))  # pylint: disable-msg=W0212

    def metarate(self, func, name='values'):
        """
        Set the values object to the function object's namespace
        """
        setattr(func, name, self.values)
        return func

    def __call__(self, func):
        return self.metarate(func)


class command(metarator):
    """
    Used to decorate the main entry point
    """

    def __call__(self, func):
        return self.metarate(func, name='command')


class subcommand(metarator):
    """
    Used to decorate the subcommands
    """

    def __init__(self, *args, **kwargs):
        self.parent = kwargs.get('parent')
        try:
            del kwargs['parent']
        except KeyError:
            pass
        super(subcommand, self).__init__(*args, **kwargs)

    def __call__(self, func):
        func.parent = self.parent
        return self.metarate(func, name='subcommand')


class param(metarator):
    """
    Use this decorator instead of `ArgumentParser.add_argument`.
    """

    def __call__(self, func):
        func.params = func.params if hasattr(func, 'params') else []
        func.params.append(self.values)
        return func


class version(param):
    """
    Use this decorator for adding the version argument.
    """

    def __init__(self, *args, **kwargs):
        super(version, self).__init__(*args, action='version', **kwargs)


class store(param):
    """
    Use this decorator for adding the simple params that store data.
    """

    def __init__(self, *args, **kwargs):
        super(store, self).__init__(*args, action='store', **kwargs)


class true(param):
    """
    Use this decorator as a substitute for 'store_true' action.
    """

    def __init__(self, *args, **kwargs):
        super(true, self).__init__(*args, action='store_true', **kwargs)


class false(param):
    """
    Use this decorator as a substitute for 'store_false' action.
    """

    def __init__(self, *args, **kwargs):
        super(false, self).__init__(*args, action='store_false', **kwargs)


class const(param):
    """
    Use this decorator as a substitute for 'store_const' action.
    """

    def __init__(self, *args, **kwargs):
        super(const, self).__init__(*args, action='store_const', **kwargs)


class append(param):
    """
    Use this decorator as a substitute for 'append' action.
    """

    def __init__(self, *args, **kwargs):
        super(append, self).__init__(*args, action='append', **kwargs)


class append_const(param):
    """
    Use this decorator as a substitute for 'append_const' action.
    """

    def __init__(self, *args, **kwargs):
        super(append_const, self).__init__(*args,
                                                action='append_const',
                                                **kwargs)


class Application(object):
    """
    Barebones base class for command line applications.
    """
    __metaclass__ = Commando

    def __init__(self, raise_exceptions=False, logger=None):
        self.raise_exceptions = raise_exceptions
        self.logger = logger or getLoggerWithConsoleHandler()

    def parse(self, argv):
        """
        Delegates to `ArgumentParser.parse_args`
        """
        return self.__parser__.parse_args(argv) # pylint: disable-msg=E1101

    def exit(self, status=0, message=None):
        """
        Delegates to `ArgumentParser.exit`
        """
        if status:
            self.logger.error(message)
        if self.__parser__: # pylint: disable-msg=E1101
            self.__parser__.exit(status, message) # pylint: disable-msg=E1101
        else:
            sys.exit(status)


    def error(self, message=None):
        """
        Delegates to `ArgumentParser.error`
        """
        if self.__parser__: # pylint: disable-msg=E1101
            self.__parser__.error(message) # pylint: disable-msg=E1101
        else:
            self.logger.error(message)
            sys.exit(2)

    def print_usage(self, out_file=None):
        """
        Delegates to `ArgumentParser.print_usage`
        """
        return self.__parser__.print_usage(out_file) # pylint: disable-msg=E1101

    def print_help(self, out_file=None):
        """
        Delegates to `ArgumentParser.print_help`
        """
        return self.__parser__.print_help(out_file) # pylint: disable-msg=E1101

    def format_usage(self):
        """
        Delegates to `ArgumentParser.format_usage`
        """
        return self.__parser__.format_usage() # pylint: disable-msg=E1101

    def format_help(self):
        """
        Delegates to `ArgumentParser.format_help`
        """
        return self.__parser__.format_help() # pylint: disable-msg=E1101

    def run(self, args=None):
        """
        Runs the main command or sub command based on user input
        """

        if not args:
            args = self.parse(sys.argv[1:])

        if getattr(args, 'verbose', False):
            self.logger.setLevel(logging.DEBUG)

        try:
            if hasattr(args, 'run'):
                args.run(self, args)
            else:
                self.__main__(args) # pylint: disable-msg=E1101
        except Exception, e: # pylint: disable-msg=W0703
            import traceback
            self.logger.debug(traceback.format_exc())
            self.logger.error(e.message)
            if self.raise_exceptions:
                raise
            sys.exit(2)