printout of buglists for review
[utils.git] / src / budget_sync / update.py
1 import toml
2 from budget_sync.write_budget_csv import write_budget_csv
3 from bugzilla import Bugzilla
4 import logging
5 import argparse
6 from pathlib import Path
7 from budget_sync.config import Config, ConfigParseError, Milestone
8 from budget_sync.budget_graph import (BudgetGraph, BudgetGraphBaseError,
9 PaymentSummary)
10 from budget_sync.write_budget_markdown import write_budget_markdown
11 from datetime import datetime, date
12
13 logging.basicConfig(level=logging.INFO)
14
15 def main():
16 parser = argparse.ArgumentParser(
17 description="Check for errors in "
18 "Libre-SOC's style of budget tracking in Bugzilla.")
19 parser.add_argument(
20 "-c", "--config", type=argparse.FileType('r'),
21 required=True, help="The path to the configuration TOML file",
22 dest="config", metavar="<path/to/budget-sync-config.toml>")
23 parser.add_argument(
24 "-o", "--output-dir", type=Path, default=None,
25 help="The path to the output directory, will be created if it "
26 "doesn't exist",
27 dest="output_dir", metavar="<path/to/output/dir>")
28 parser.add_argument('--username', help="Log in with this username")
29 parser.add_argument('--password', help="Log in with this password")
30 parser.add_argument('--bug', help="bug number")
31 parser.add_argument('--user', help="set payee user")
32 parser.add_argument('--paid', help="set paid date")
33 parser.add_argument('--submitted', help="set submitted date")
34 args = parser.parse_args()
35 try:
36 with args.config as config_file:
37 config = Config.from_file(config_file)
38 except (IOError, ConfigParseError) as e:
39 logging.error("Failed to parse config file: %s", e)
40 return
41 logging.info("Using Bugzilla instance at %s", config.bugzilla_url)
42 bz = Bugzilla(config.bugzilla_url)
43 if args.username:
44 logging.debug("logging in...")
45 bz.interactive_login(args.username, args.password)
46 logging.debug("Connected to Bugzilla")
47 bugs = str(args.bug).split(",")
48 buglist = bz.getbugs(bugs)
49 logging.info("got bugs %s" % args.bug)
50 for bug in buglist:
51 print ("payees", bug.id)
52 print (" "+"\n ".join(bug.cf_payees_list.split('\n')))
53
54 parsed_toml = toml.loads(bug.cf_payees_list)
55
56 payee = parsed_toml[args.user]
57 if isinstance(payee, int):
58 payee = {'amount': payee}
59
60 modified = False
61
62 if args.submitted and 'submitted' not in payee:
63 modified = True
64 d = datetime.strptime(args.submitted, "%Y-%m-%d")
65 payee['submitted'] = date(d.year, d.month, d.day)
66
67 if args.paid and 'paid' not in payee:
68 modified = True
69 d = datetime.strptime(args.paid, "%Y-%m-%d")
70 payee['paid'] = date(d.year, d.month, d.day)
71
72 # skip over not modified
73 if not modified:
74 continue
75
76 parsed_toml[args.user] = payee
77
78 encoder = toml.encoder.TomlPreserveInlineDictEncoder()
79 ttxt = toml.dumps(parsed_toml, encoder=encoder)
80 print(ttxt)
81
82 #update = bz.build_update(cf_payees_list=ttxt)
83 bz.update_bugs([bug.id], {'cf_payees_list': ttxt})
84
85 if __name__ == "__main__":
86 main()