-
Notifications
You must be signed in to change notification settings - Fork 0
/
cla_tool.py
140 lines (111 loc) · 5.43 KB
/
cla_tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import argparse
import datetime
import glob
import json
import os
import os.path
import sys
def log(message, command=False):
prefix = "$" if command else "#"
print(f"{prefix} {message}", file=sys.stderr)
def get_date():
return datetime.datetime.now(datetime.timezone.utc).isoformat()
def write_json(val, out_dir, out_name, out_ext="json"):
os.makedirs(out_dir, exist_ok=True)
out_path = os.path.join(out_dir, f"{out_name}.{out_ext}")
with open(out_path, "w") as fout:
json.dump(val, fout, indent=4)
fout.write("\n")
def sign(args, company=False):
date = args.date or get_date()
signature = dict(
version=args.sig_version,
date=date,
github_id=args.github_id,
cla=args.cla,
name=args.name,
attestation=args.attestation,
)
if company:
extra = dict(
company=args.company,
representative_name=args.representative_name,
representative_attestation=args.representative_attestation,
)
signature = {**signature, **extra}
log(f"signature = {signature}")
write_json(signature, args.sig_dir, args.github_id)
def sign_individual(args):
log("*** SIGNING FOR INDIVIDUAL ***")
sign(args, False)
def sign_company(args):
log("*** SIGNING FOR COMPANY ***")
sign(args, True)
def get_contributors(sig_dir):
return [path[len(sig_dir)+1:-5] for path in glob.glob(os.path.join(sig_dir, "*.json"))]
def generate_config(args):
log("*** GENERATING CLA-BOT CONFIG ***")
int_contribs = get_contributors(args.int_sig_dir)
ind_contribs = get_contributors(args.ind_sig_dir)
com_contribs = get_contributors(args.com_sig_dir)
contribs = sorted(int_contribs + ind_contribs + com_contribs)
# We need github-actions in the list for automated PRs.
contribs += ["github-actions[bot]"]
write_json(contribs, args.conf_dir, args.contributors_file)
def _main(argv):
def nonempty_str(arg):
arg = arg.strip()
if not arg:
raise argparse.ArgumentTypeError("Empty string!")
return arg
def attestation(arg):
arg = nonempty_str(arg)
if not arg.upper() == "I AGREE":
raise argparse.ArgumentTypeError("Attestation must be \"I AGREE\"!")
return arg
parser = argparse.ArgumentParser(description="OpenDP CLA tool")
subparsers = parser.add_subparsers(dest="COMMAND", help="Command to run")
subparsers.required = True
subparser = subparsers.add_parser("sign-int", help="Sign CLA for internal contributor")
subparser.set_defaults(func=sign_individual)
subparser.add_argument("-V", "--sig-version", default="1.0.0")
subparser.add_argument("-d", "--date", type=nonempty_str)
subparser.add_argument("-g", "--github-id", type=nonempty_str, required=True)
subparser.add_argument("-u", "--cla", default="https://docs.opendp.org/stable/_static/opendp-cla-individual-1.0.0.pdf")
subparser.add_argument("-n", "--name", type=nonempty_str, required=True)
subparser.add_argument("-a", "--attestation", type=nonempty_str, required=True)
subparser.add_argument("-s", "--sig-dir", default="signatures/internal")
subparser = subparsers.add_parser("sign-ind", help="Sign CLA for individual contributor")
subparser.set_defaults(func=sign_individual)
subparser.add_argument("-V", "--sig-version", default="1.0.0")
subparser.add_argument("-d", "--date", type=nonempty_str)
subparser.add_argument("-g", "--github-id", type=nonempty_str, required=True)
subparser.add_argument("-u", "--cla", default="https://docs.opendp.org/stable/_static/opendp-cla-individual-1.0.0.pdf")
subparser.add_argument("-n", "--name", type=nonempty_str, required=True)
subparser.add_argument("-a", "--attestation", type=attestation, required=True)
subparser.add_argument("-s", "--sig-dir", default="signatures/individual")
subparser = subparsers.add_parser("sign-com", help="Sign CLA for company contributor")
subparser.set_defaults(func=sign_company)
subparser.add_argument("-V", "--sig-version", default="1.0.0")
subparser.add_argument("-d", "--date", type=nonempty_str)
subparser.add_argument("-g", "--github-id", type=nonempty_str, required=True)
subparser.add_argument("-u", "--cla", default="https://docs.opendp.org/stable/_static/opendp-cla-company-1.0.0.pdf")
subparser.add_argument("-n", "--name", type=nonempty_str, required=True)
subparser.add_argument("-a", "--attestation", type=attestation, required=True)
subparser.add_argument("-X", "--company", type=nonempty_str, required=True)
subparser.add_argument("-N", "--representative-name", type=nonempty_str, required=True)
subparser.add_argument("-A", "--representative-attestation", type=attestation, required=True)
subparser.add_argument("-s", "--sig-dir", default="signatures/company")
subparser = subparsers.add_parser("gen-conf", help="Generate cla-bot config")
subparser.set_defaults(func=generate_config)
subparser.add_argument("-s1", "--int-sig-dir", default="signatures/internal")
subparser.add_argument("-s2", "--ind-sig-dir", default="signatures/individual")
subparser.add_argument("-s3", "--com-sig-dir", default="signatures/company")
subparser.add_argument("-c", "--conf-dir", default=".")
subparser.add_argument("-f", "--contributors-file", default="contributors")
args = parser.parse_args(argv[1:])
args.func(args)
def main():
_main(sys.argv)
if __name__ == "__main__":
main()