cfc74a5b464c27c06756a8ef53a0b7673e62b5cb
[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.cf_payees_list)
52
53 parsed_toml = toml.loads(bug.cf_payees_list)
54 print (parsed_toml)
55
56 payee = parsed_toml[args.user]
57 if isinstance(payee, int):
58 payee = {'amount': payee}
59
60 if args.submitted and 'submitted' not in payee:
61 d = datetime.strptime(args.submitted, "%Y-%m-%d")
62 payee['submitted'] = date(d.year, d.month, d.day)
63
64 if args.paid and 'paid' not in payee:
65 d = datetime.strptime(args.paid, "%Y-%m-%d")
66 payee['paid'] = date(d.year, d.month, d.day)
67
68 parsed_toml[args.user] = payee
69
70 encoder = toml.encoder.TomlPreserveInlineDictEncoder()
71 ttxt = toml.dumps(parsed_toml, encoder=encoder)
72 print(ttxt)
73
74 #update = bz.build_update(cf_payees_list=ttxt)
75 bz.update_bugs([bug.id], {'cf_payees_list': ttxt})
76
77 if __name__ == "__main__":
78 main()