This file is indexed.

/usr/lib/python3/dist-packages/decopy/parsers.py is in decopy 0.2.2-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
#!/usr/bin/env python3
# -*- coding: utf-8 -*- vim60:fdm=marker
#
# Copyright: 2016, Maximiliano Curia <maxy@debian.org>
#
# License: ISC
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#  .
#  THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
#  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
#  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
#  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
#  PERFORMANCE OF THIS SOFTWARE.

'''File Parsers'''

import functools
import logging
import shutil
import string
import subprocess
import tokenize

import xdg.Mime

# Local modules
from .datatypes import CopyrightHolder
from .matchers import find_licenses, parse_copyright, clean_comments, parse_holders


def generic_parser(filename):

    content = ''
    holders = []

    with open(filename, 'rb') as f:
        continuation = None
        for raw_line in f:
            try:
                line = raw_line.decode('utf-8')
            except UnicodeDecodeError:
                if has_control_chars(raw_line):
                    # binary?
                    chars = (str(char) for char in raw_line
                             if char in PRINTABLE_BYTES)
                    line = ''.join(chars)
                else:
                    line = raw_line.decode('latin1')
            content += line
            line = line.rstrip('\n')
            copyrights, continuation = parse_copyright(line, continuation)
            if not copyrights:
                continue
            for copyright_ in copyrights:
                holder = CopyrightHolder.from_copyright(copyright_)
                if holder:
                    holders.append(holder)

    content = clean_comments(content)

    licenses = find_licenses(content)

    return holders, licenses


@functools.lru_cache()
def cmd_parser_factory(*cmd):

    def _parser(filename):

        fullcmd = cmd + (filename,)

        try:
            content = subprocess.check_output(fullcmd, universal_newlines=True,
                                              stderr=subprocess.STDOUT)
        except (subprocess.CalledProcessError, UnicodeDecodeError) as e:
            logging.info('failed to parse %s with %s, ignoring (%s)',
                         filename, cmd, e)
            content = ''

        holders = parse_holders(content)
        content = clean_comments(content)
        licenses = find_licenses(content)

        return holders, licenses

    if shutil.which(cmd[0]) is None:
        logging.warn('command %s not found, using generic parser as fallback',
                     cmd[0])
        return generic_parser

    return _parser


def python_parser(filename):
    '''Extract comments and doc strings from a python file'''

    lines = []
    newline = True
    with open(filename, 'rb') as f:
        for token in tokenize.tokenize(f.readline):
            if (token.type == tokenize.COMMENT) or \
               (newline and token.type == tokenize.STRING):
                lines.append(token.string)
            elif token.type == tokenize.NEWLINE:
                newline = True
            elif newline and (token.type in {tokenize.INDENT, tokenize.NL}):
                continue
            else:
                newline = False
    content = '\n'.join(lines)

    holders = parse_holders(content)
    content = clean_comments(content)
    licenses = find_licenses(content)

    return holders, licenses


KNOWN_PARSERS = {
    'application/gzip': cmd_parser_factory('zcat'),
    'application/x-bzip': cmd_parser_factory('bzcat'),
    'application/x-lzma': cmd_parser_factory('xzcat'),
    'application/x-xz': cmd_parser_factory('xzcat'),
    'text/x-python': python_parser,
    'image/jpeg': cmd_parser_factory('exiv2'),
    'image/png': cmd_parser_factory('exiv2'),
}


def has_control_chars(bytes_seq):
    for i in bytes_seq:
        # ascii control chars (C0), and delete (DEL)
        if i < 32 or i == 127:
            # except tab (HT), line feed (LF) and carriage return (CR)
            if i in (9, 10, 13):
                continue
            return True
    return False


PRINTABLE_BYTES = bytes(string.printable, encoding='ascii')


def parse_file(fullname, options):
    '''Parses the received file with the matching parser.

    Returns:
        The list of copyrights and licenses obtained by the parser.
    '''
    if options.text:
        parser = generic_parser
    else:
        xdg_type = xdg.Mime.get_type(fullname)
        logging.debug('Type for %s is: %s', fullname, xdg_type)

        parser = KNOWN_PARSERS.get(str(xdg_type), generic_parser)

    copyrights, licenses = parser(fullname)
    logging.debug('Parsed %s: %s, %s', fullname, copyrights, licenses)
    return copyrights, licenses