Martí Bolívar | 78edc12 | 2022-05-11 00:34:12 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # Copyright (c) 2022 Nordic Semiconductor ASA |
| 4 | # |
| 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | # stdlib |
| 8 | import argparse |
| 9 | import pickle |
| 10 | import sys |
| 11 | from pathlib import Path |
| 12 | from typing import BinaryIO, List |
| 13 | |
| 14 | # third party |
| 15 | from github.Issue import Issue |
| 16 | |
| 17 | # other zephyr/scripts modules |
| 18 | from 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 | |
| 23 | def parse_args() -> argparse.Namespace: |
| 24 | '''Parse command line arguments.''' |
| 25 | parser = argparse.ArgumentParser( |
| 26 | description=''' |
| 27 | A helper script which loads all open bugs in the |
| 28 | zephyrproject-rtos/zephyr repository using the GitHub API, and writes |
| 29 | them to a new pickle file as a list of github.Issue.Issue objects. |
| 30 | |
| 31 | For 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 | ''', |
Jamie McCrae | ec70444 | 2023-01-04 16:08:36 +0000 | [diff] [blame] | 38 | formatter_class=argparse.RawDescriptionHelpFormatter, allow_abbrev=False) |
Martí Bolívar | 78edc12 | 2022-05-11 00:34:12 -0700 | [diff] [blame] | 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 | |
| 44 | def 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 | |
| 48 | def 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 | |
| 54 | def 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 | |
| 61 | if __name__ == '__main__': |
| 62 | main() |