#!/usr/bin/python3 # coding: utf8 # Script for mining and/or posting data to contributors.debian.org # # Copyright (C) 2013--2014 Enrico Zini # # 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 . 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("--method", metavar="method", choices=["replace", "extend"], default="replace", help="Submission method ('replace' or 'extend')") 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 submission.method = args.method 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)