File: __init__.py

package info (click to toggle)
debian-codesearch-cli 1.0.0%2Bnmu3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 224 kB
  • sloc: python: 1,409; makefile: 10
file content (239 lines) | stat: -rwxr-xr-x 6,831 bytes parent folder | download
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
#!/usr/bin/env python3

"""
CLI tool for Debian Code Search (https://codesearch.debian.net/)


"""

import argparse
from typing import List, Dict
import requests
import sys
import collections
import html

import debian_codesearch_client.swagger_client as swclient
from debian_codesearch_client.swagger_client.rest import ApiException

configuration = swclient.Configuration()
# API key specifically for https://salsa.debian.org/debian/codesearch-cli,
# please see https://codesearch.debian.net/apikeys/ for details.
configuration.api_key["x-dcs-apikey"] = (
    "MTYxMjczNjE4NnxkTGp0SHdDUnRuUU5WeDh2dGE0dWxtZC1MSFRwQ1o2eUJEdWs1S1VvTjlHYXBYazItUWo3U2daX2pNZ0hHWDVsajJ2TUdhUFNlZ2doU0dIMnFZQk9XNl9zLS1MOU83dTBhNGdnNzV6ZnNDWWVVYXkxVDh0WEJuLTdnUGc9fOH1MJNBLYYnkLVOmnFXFEAkXoHu1SknNqADZuJ2u8nY"
)

dcs = swclient.SearchApi(swclient.ApiClient(configuration))

PATHCOLOR = 33
DUPE_PATHCOLOR = 36

dedupe_results: List[swclient.models.search_result.SearchResult] = []


def say(quiet: bool, msg: str) -> None:
    """Print messages to stderr"""
    if not quiet:
        sys.stderr.write("%s\n" % msg)


def is_excluded(chunk, exclusions: list[str]) -> bool:
    for exclude in exclusions:
        if exclude in chunk.path:
            return True

    return False


def get_result_body(chunk, print_linenum: bool, nocolor: bool) -> str:
    body = ""
    for line in chunk.context_before or ():
        line = html.unescape(line)
        if print_linenum:
            body += "        %s\n" % line
        else:
            body += "%s\n" % line

    line = html.unescape(chunk.context)
    if print_linenum:
        body += "%7d %s\n" % (chunk.line, line)
    else:
        body += "%s\n" % line
        for line in chunk.context_after or ():
            line = html.unescape(line)
            if print_linenum:
                body += "        %s\n" % line
            else:
                body += "%s\n" % line

    return body[:-1]  # trim trailing line


def print_results(
    chunk,
    print_linenum: bool,
    print_only_filenames: bool,
    nocolor: bool
) -> None:
    """Print search results"""
    pathline = "path: %s" % chunk.path
    if nocolor:
        print(pathline)

    else:
        print("\033[%dm%s\033[0m" % (PATHCOLOR, pathline))

    if print_only_filenames:
        return

    print(get_result_body(chunk, print_linenum, nocolor))


def print_dedupe(
    print_linenum: bool,
    print_only_filenames: bool,
    nocolor: bool
) -> None:
    """amalgamate duplicate results and print summary"""

    bodies = collections.defaultdict(list)
    for chunk in dedupe_results:
        body = get_result_body(chunk, print_linenum, nocolor)
        bodies[body].append(chunk)

    for body, chunks in bodies.items():
        print_results(chunks[0], print_linenum, print_only_filenames, nocolor)

        first_path = chunks[0].path
        for chunk in chunks[1:]:
            pathline = "also: %s" % chunk.path
            if nocolor:
                print(pathline)

            else:
                for common_suffix in range(1, len(pathline)):
                    if pathline[-common_suffix:] != first_path[-common_suffix:]:
                        break

                i = 1 - common_suffix
                print(
                    "\033[%dm%s\033[%dm%s\033[0m"
                    % (PATHCOLOR, pathline[:i], DUPE_PATHCOLOR, pathline[i:])
                )
        else:
            print("")


def parse_args() -> argparse.Namespace:
    ap = argparse.ArgumentParser()
    ap.add_argument("searchstring")
    ap.add_argument("--max-results", type=int, default=200)
    ap.add_argument("-q", "--quiet", action="store_true")
    ap.add_argument("-l", "--linenumber", action="store_true")
    ap.add_argument("--nocolor", action="store_true", help="Do not colorize output")
    ap.add_argument(
        "-n",
        "--print-filenames",
        action="store_true",
        help="Print only matching filenames, no contents",
    )
    ap.add_argument(
        "-d",
        "--dedupe",
        action="store_true",
        help="amalgamate results for the same file in different packages",
    )
    ap.add_argument(
        "-x",
        "--exclude",
        action="append",
        help="list of path fragments to exclude from results",
    )
    ap.add_argument(
        "-m",
        "--mode",
        choices=["literal", "regexp"],
        default="regexp",
        help="search mode (literal or regexp)",
    )
    ap.add_argument(
        "--per-package",
        action="store_true",
        default=False,
        help="group results per source package",
    )
    ap.add_argument(
        "--only-package",
        action="store_true",
        default=False,
        help="only print matching source package name",
    )
    args = ap.parse_args()
    if not sys.stdout.isatty():
        args.nocolor = True

    return args


def main() -> None:
    args = parse_args()

    if args.quiet:
        requests.packages.urllib3.disable_warnings()  # type: ignore

    printed_chunks = set()
    try:
        # Searches through source code, see
        # https://codesearch.debian.net/apikeys/#/search/search
        if args.per_package or args.only_package:
            api_response, status, headers = dcs.searchperpackage_with_http_info(
                args.searchstring, match_mode=args.mode, _return_http_data_only=False
            )
        else:
            api_response, status, headers = dcs.search_with_http_info(
                args.searchstring, match_mode=args.mode, _return_http_data_only=False
            )
        printed = 0
        packages = set()
        for chunk in api_response:
            if is_excluded(chunk, args.exclude or []):
                continue
            packages.add(chunk.package)
            if printed == args.max_results:
                break
            printed += 1
            if args.per_package:
                chunk = chunk.results[0]
            if args.only_package:
                continue
            if args.dedupe:
                dedupe_results.append(chunk)
            else:
                print_results(
                    chunk, args.linenumber, args.print_filenames, args.nocolor
                )
            printed_chunks.add((chunk.path, chunk.line))

        if args.only_package:
            if packages:
                print('\n'.join(sorted(packages)))
            return

        if args.dedupe:
            print_dedupe(args.linenumber, args.print_filenames, args.nocolor)

        say(
            args.quiet,
            "--\n%d files found." % int(headers["X-Codesearch-Filestotal"]),
        )

    except ApiException as e:
        print("Exception when calling SearchApi->search: %s\n" % e)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("")
        sys.exit()