1#!/usr/bin/env python3
2
3# Copyright (c) 2022 Nordic Semiconductor ASA
4#
5# SPDX-License-Identifier: Apache-2.0
6
7# stdlib
8import argparse
9import pickle
10import sys
11from pathlib import Path
12from typing import BinaryIO, List
13
14# third party
15from github.Issue import Issue
16
17# other zephyr/scripts modules
18from github_helpers import get_github_object
19
20# Note that type annotations are not currently statically checked, and
21# should only be considered documentation.
22
23def parse_args() -> argparse.Namespace:
24    '''Parse command line arguments.'''
25    parser = argparse.ArgumentParser(
26        description='''
27A helper script which loads all open bugs in the
28zephyrproject-rtos/zephyr repository using the GitHub API, and writes
29them to a new pickle file as a list of github.Issue.Issue objects.
30
31For more information, see:
32
33  - GitHub API: https://docs.github.com/en/rest
34  - github.Issue.Issue:
35    https://pygithub.readthedocs.io/en/latest/github_objects/Issue.html
36  - pickle: https://docs.python.org/3/library/pickle.html
37''',
38        formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False)
39    parser.add_argument('out_file', metavar='OUTFILE', type=Path, nargs='?',
40                        help='''file to write pickle data to (default:
41                        stdout)''')
42    return parser.parse_args()
43
44def get_open_bugs() -> List[Issue]:
45    zephyr_repo = get_github_object().get_repo('zephyrproject-rtos/zephyr')
46    return list(zephyr_repo.get_issues(state='open', labels=['bug']))
47
48def open_out_file(args: argparse.Namespace) -> BinaryIO:
49    if args.out_file is None:
50        return open(sys.stdout.fileno(), 'wb', closefd=False)
51
52    return open(args.out_file, 'wb')
53
54def main() -> None:
55    args = parse_args()
56    open_bugs = get_open_bugs()
57
58    with open_out_file(args) as out_file:
59        pickle.dump(open_bugs, out_file)
60
61if __name__ == '__main__':
62    main()
63