File: dc-tool

package info (click to toggle)
debiancontributors 0.7.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 404 kB
  • sloc: python: 1,894; makefile: 18
file content (122 lines) | stat: -rwxr-xr-x 4,956 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
#!/usr/bin/python3
# coding: utf8
# Script for mining and/or posting data to contributors.debian.org
#
# Copyright (C) 2013--2014  Enrico Zini <enrico@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals
import sys
import json
import argparse
import logging


def read_json_sources(source_name, pathnames):
    import debiancontributors as dc
    # Read JSON data, parsing it to validate it
    submission = None
    if pathnames:
        for fname in pathnames:
            with open(fname, "r") as fd:
                try:
                    s = dc.Submission.from_json(source_name, fd)
                except dc.parser.ClusterFail as e:
                    for msg in e.errors:
                        print("{}: {}".format(fname, msg), file=sys.stderr)
                    return None
                except dc.parser.Fail as e:
                    print("{}: {}".format(fname, e.msg), file=sys.stderr)
                    return None
            if submission is None:
                submission = s
            else:
                submission.merge_with(s)
    else:
        submission = dc.Submission.from_json(source_name, sys.stdin)
    return submission


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--debug", help="enable debugging output", action="store_true")
    parser.add_argument("--verbose", help="enable verbose output", action="store_true")
    parser.add_argument("--baseurl", metavar="url", help="URL to post data to (default: %(default)s)", action="store",
                        default='https://contributors.debian.org/')
    parser.add_argument("--source", help="Data source name")
    parser.add_argument("--auth-token", help="Authentication token. Use @file to use the file content as auth token.")
    parser.add_argument("--json", help="write the JSON submission to standard output", action="store_true")
    parser.add_argument("--post", help="POST contribution data to the site", action="store_true")
    parser.add_argument("--mine", metavar="conffile", action="store", default=None,
                        help="Perform data mining using the given config file")
    parser.add_argument("--mine-documentation", action="store_true", default=False,
                        help="Print data mining documentation in reStructuredText format")
    parser.add_argument("sources", metavar="source(s)", help="JSON file(s) to post if --mine is not provided",
                        default=None, nargs="*")
    args = parser.parse_args()

    FORMAT = "%(asctime)-15s %(levelname)s %(message)s"
    if args.debug:
        verbose = True
        logging.basicConfig(level=logging.DEBUG, stream=sys.stderr, format=FORMAT)
    elif args.verbose:
        verbose = True
        logging.basicConfig(level=logging.INFO, stream=sys.stderr, format=FORMAT)
    else:
        verbose = False
        logging.basicConfig(level=logging.WARN, stream=sys.stderr, format=FORMAT)

    if args.mine_documentation:
        from debiancontributors import DataMine
        DataMine.print_documentation()
        sys.exit(0)

    if args.mine:
        from debiancontributors import DataMine
        miner = DataMine(args.mine, source_name=args.source)
        miner.scan()
        submission = miner.submission
    else:
        submission = read_json_sources(args.source, args.sources)
        if submission is None:
            sys.exit(1)

    # Override auth_token and baseurl from commandline if requested
    if args.auth_token:
        submission.set_auth_token(args.auth_token)
    if args.baseurl:
        submission.baseurl = args.baseurl

    if args.post:
        success, details = submission.post()
        if success:
            if verbose:
                json.dump(details, sys.stdout, indent=2)
                print()
            sys.exit(0)
        else:
            json.dump(details, sys.stderr, indent=2)
            print(file=sys.stderr)
            sys.exit(1)
    elif args.json:
        submission.to_json(sys.stdout, indent=1)
    else:
        import io
        with io.open(sys.stdout.fileno(), "wt", encoding="utf8", closefd=False) as out:
            submission.print_compact(out)
        sys.exit(0)