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