File: cmdline.py

package info (click to toggle)
dijitso 2019.2.0~git20190418.c92dcb0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 412 kB
  • sloc: python: 1,672; makefile: 194; sh: 53; ansic: 1
file content (259 lines) | stat: -rw-r--r-- 8,959 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
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
# -*- coding: utf-8 -*-
# Copyright (C) 2015-2016 Martin Sandve Aln├Žs
#
# This file is part of DIJITSO.
#
# DIJITSO is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DIJITSO 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DIJITSO. If not, see <http://www.gnu.org/licenses/>.

"""This file contains the commands available through command-line dijitso-cache.

Each function cmd_<cmdname> becomes a subcommand invoked by::

    dijitso-cache cmdname ...args

The docstrings in the cmd_<cmdname> are shown when running::

    dijitso-cache cmdname --help

The 'args' argument to cmd_* is a Namespace object with the commandline arguments.

"""

import os
import re

from dijitso import __version__
from dijitso.cache import glob_cache, grep_cache, clean_cache
from dijitso.cache import extract_lib_signatures
from dijitso.cache import extract_files, extract_function
from dijitso.system import read_textfile


def parse_categories(categories):
    if categories == "all":
        return ("inc", "src", "lib", "log")
    return categories.split(",")


def args_version(parser):
    pass


def cmd_version(args, params):
    "print dijitso version"
    print(__version__)


def args_config(parser):
    parser.add_argument("--key", default="", help="specific key to show (e.g. build.cxxflags)")


def cmd_config(args, params):
    "show configuration"
    # Show single value if asked for
    key = args.key
    if key:
        name = key
        value = params
        for k in key.split("."):
            value = value[k]
        # Compiler flags etc are more useful in space separated form:
        if isinstance(value, tuple):
            value = " ".join(value)
        print("    %s: %s" % (name, value))
        return 0

    # Pick non-empty categories
    categories = sorted(c for c in params if params[c])
    print("Showing default flags for dijitso:")
    for category in categories:
        print("%s:" % (category,))
        for name in sorted(params[category]):
            value = params[category][name]
            # Compiler flags etc are more useful in space separated form:
            if isinstance(value, tuple):
                value = " ".join(value)
            print("    %s: %s" % (name, value))
    return 0


def args_show(parser):
    parser.add_argument("--categories", default="all",
                        help="comma separated list to enable file types (inc,src,lib,log)")
    parser.add_argument("--no-summary", action="store_true",
                        help="don't show summary")
    parser.add_argument("--files", action="store_true",
                        help="show file lists")
    parser.add_argument("--signatures", action="store_true",
                        help="show library signatures")


def cmd_show(args, params):
    "show lists of files in cache"
    cache_params = params["cache"]

    summary = not args.no_summary
    files = args.files
    signatures = args.signatures
    categories = parse_categories(args.categories)

    gc = glob_cache(cache_params, categories=categories)

    if signatures:
        sigs = extract_lib_signatures(cache_params)
        print("\n".join("\t" + s for s in sorted(sigs)))
    if files:
        for cat in categories:
            print("\n".join("\t" + f for f in sorted(gc.get(cat, ()))))
    if summary:
        print("dijitso cache summary (number of cached files):")
        for cat in categories:
            print("%s: %d" % (cat, len(gc.get(cat, ()))))
        # TODO: Add summary of file sizes
    return 0


def args_clean(parser):
    parser.add_argument("--categories", default="inc,src,lib,log",
                        help="comma separated list to enable file types (inc,src,lib,log)")


def cmd_clean(args, params):
    "remove files from cache"
    cache_params = params["cache"]

    dryrun = args.dry_run
    categories = parse_categories(args.categories)

    clean_cache(cache_params, dryrun=dryrun, categories=categories)
    return 0


def args_grep(parser):
    parser.add_argument("--categories", default="inc,src",
                        help="comma separated list to enable file types (inc,src,lib,log)")
    parser.add_argument("--pattern", default="",
                        help="line search pattern")
    parser.add_argument("--regexmode", action="store_true",
                        help="pattern is a regular expression (python style)")
    parser.add_argument("--linenumbers", action="store_true",
                        help="show linenumbers on matches")
    parser.add_argument("--countonly", action="store_true",
                        help="show only match line count for each file")
    parser.add_argument("--filesonly", action="store_true",
                        help="show only filenames with matches")
    parser.add_argument("--signature", default="",
                        help="look for module with this signature (default all)")


def cmd_grep(args, params):
    "grep content of header and source file(s) in cache"
    cache_params = params["cache"]

    # Get command-line arguments
    pattern = args.pattern
    signature = args.signature
    regexmode = args.regexmode
    linenumbers = args.linenumbers
    countonly = args.countonly
    filesonly = args.filesonly
    categories = parse_categories(args.categories)

    if not regexmode:
        pattern = ".*(" + pattern + ").*"
    regex = re.compile(pattern)
    allmatches = grep_cache(regex, cache_params,
                            linenumbers=linenumbers, countonly=countonly,
                            signature=signature,
                            categories=categories)
    if filesonly:
        print("\n".join(sorted(allmatches)))
    elif countonly:
        print("\n".join("%s: %d" % (k, v) for k, v in sorted(allmatches.items())))
    else:
        for fn in sorted(allmatches):
            print("\nFile '%s' matches:" % (fn,))
            if linenumbers:
                print("\n".join("%5d:\t%s" % line for line in allmatches[fn]))
            else:
                print("\n".join("\t" + line for line in allmatches[fn]))
    return 0


def args_grepfunction(parser):
    parser.add_argument("--categories", default="src",
                        help="comma separated list to enable file types (inc,src,lib,log)")
    parser.add_argument("--name", default="",
                        help="function name to search for")
    parser.add_argument("--signature", default="",
                        help="look for module with this signature (default all)")
    parser.add_argument("--no-body", action="store_true",
                        help="don't show function bodies")


def cmd_grepfunction(args, params):
    "search for function name in source files in cache"
    cache_params = params["cache"]

    name = args.name
    signature = args.signature
    categories = parse_categories(args.categories)
    no_body = args.no_body

    pattern = r".*(" + name + r")[ ]*\((.*)"
    regex = re.compile(pattern)

    allmatches = grep_cache(regex, cache_params,
                            linenumbers=True, countonly=False,
                            signature=signature,
                            categories=categories)
    for fn in sorted(allmatches):
        if no_body:
            # Just print signature lines
            print("\nFile '%s' matches:" % (fn,))
            for i, line in allmatches[fn]:
                print("%5d: %s" % (i, line))
        else:
            # Print function bodies
            content = read_textfile(fn)
            lines = content.splitlines() if content else ()
            for i, line in allmatches[fn]:
                print("%s:%d" % (fn, i))
                assert name in lines[i]
                print(extract_function(lines[i:]))
                print()
    return 0


def args_checkout(parser):
    parser.add_argument("--categories", default="inc,src,lib,log",
                        help="comma separated list to enable file types (inc,src,lib,log)")
    parser.add_argument("--signature",
                        help="module signature (required)")


def cmd_checkout(args, params):
    "copy files from cache to a directory"
    cache_params = params["cache"]

    signature = args.signature
    categories = parse_categories(args.categories)

    prefix = "jitcheckout-"
    path = os.curdir
    path = extract_files(signature, cache_params,
                         prefix=prefix, path=path,
                         categories=categories)
    print("Extracted files to '%s'." % (path,))
    return 0