Debian Codegraph @

dc-tool

raw symbols SCIP
1 #!/usr/bin/python3
2 # coding: utf8
3 # Script for mining and/or posting data to contributors.debian.org
4 #
5 # Copyright (C) 2013--2014 Enrico Zini <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16 #
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 import sys
21 import json
22 import argparse
23 import logging
24
25
26 def read_json_sources(source_name, pathnames):
27 import debiancontributors as dc
28 # Read JSON data, parsing it to validate it
29 submission = None
30 if pathnames:
31 for fname in pathnames:
32 with open(fname, "r") as fd:
33 try:
34 s = dc.Submission.from_json(source_name, fd)
35 except dc.parser.ClusterFail as e:
36 for msg in e.errors:
37 print("{}: {}".format(fname, msg), file=sys.stderr)
38 return None
39 except dc.parser.Fail as e:
40 print("{}: {}".format(fname, e.msg), file=sys.stderr)
41 return None
42 if submission is None:
43 submission = s
44 else:
45 submission.merge_with(s)
46 else:
47 submission = dc.Submission.from_json(source_name, sys.stdin)
48 return submission
49
50
51 if __name__ == "__main__":
52 parser = argparse.ArgumentParser()
53 parser.add_argument("--debug", help="enable debugging output", action="store_true")
54 parser.add_argument("--verbose", help="enable verbose output", action="store_true")
55 parser.add_argument("--baseurl", metavar="url", help="URL to post data to (default: %(default)s)", action="store",
56 default='https://contributors.debian.org/')
57 parser.add_argument("--source", help="Data source name")
58 parser.add_argument("--auth-token", help="Authentication token. Use @file to use the file content as auth token.")
59 parser.add_argument("--json", help="write the JSON submission to standard output", action="store_true")
60 parser.add_argument("--post", help="POST contribution data to the site", action="store_true")
61 parser.add_argument("--method", metavar="method", choices=["replace", "extend"], default="replace",
62 help="Submission method ('replace' or 'extend')")
63 parser.add_argument("--mine", metavar="conffile", action="store", default=None,
64 help="Perform data mining using the given config file")
65 parser.add_argument("--mine-documentation", action="store_true", default=False,
66 help="Print data mining documentation in reStructuredText format")
67 parser.add_argument("sources", metavar="source(s)", help="JSON file(s) to post if --mine is not provided",
68 default=None, nargs="*")
69 args = parser.parse_args()
70
71 FORMAT = "%(asctime)-15s %(levelname)s %(message)s"
72 if args.debug:
73 verbose = True
74 logging.basicConfig(level=logging.DEBUG, stream=sys.stderr, format=FORMAT)
75 elif args.verbose:
76 verbose = True
77 logging.basicConfig(level=logging.INFO, stream=sys.stderr, format=FORMAT)
78 else:
79 verbose = False
80 logging.basicConfig(level=logging.WARN, stream=sys.stderr, format=FORMAT)
81
82 if args.mine_documentation:
83 from debiancontributors import DataMine
84 DataMine.print_documentation()
85 sys.exit(0)
86
87 if args.mine:
88 from debiancontributors import DataMine
89 miner = DataMine(args.mine, source_name=args.source)
90 miner.scan()
91 submission = miner.submission
92 else:
93 submission = read_json_sources(args.source, args.sources)
94 if submission is None:
95 sys.exit(1)
96
97 # Override auth_token and baseurl from commandline if requested
98 if args.auth_token:
99 submission.set_auth_token(args.auth_token)
100 if args.baseurl:
101 submission.baseurl = args.baseurl
102 submission.method = args.method
103
104 if args.post:
105 success, details = submission.post()
106 if success:
107 if verbose:
108 json.dump(details, sys.stdout, indent=2)
109 print()
110 sys.exit(0)
111 else:
112 json.dump(details, sys.stderr, indent=2)
113 print(file=sys.stderr)
114 sys.exit(1)
115 elif args.json:
116 submission.to_json(sys.stdout, indent=1)
117 else:
118 import io
119 with io.open(sys.stdout.fileno(), "wt", encoding="utf8", closefd=False) as out:
120 submission.print_compact(out)
121 sys.exit(0)