1#!/usr/bin/python
2"""
3SPDX-License-Identifier: Apache-2.0
4Copyright (c) 2019 STMicroelectronics.
5This script will update the stm32cube module for Zephyr
6from the latest version on https://github.com/STMicroelectronics
7get the patch between initial version and latest version
8apply this patch on the zephyr module
9basic usage for a single serie update :
10   'python3 update_stm32_package.py -s <stm32_serie>'
11basic usage for all serie update at once:
12   'python3 update_stm32_package.py
13"""
14
15import os
16import subprocess
17import argparse
18from pathlib import Path
19import serie_update
20import logging
21
22logging.basicConfig(level=logging.INFO)
23
24parser = argparse.ArgumentParser()
25parser.add_argument(
26    "-s",
27    "--stm32_serie",
28    type=str,
29    help="Update a stm32 serie. ex: stm32f1.\n"
30    + "If omitted all STM32 series will be updated,\n"
31    + "a commit is created for each serie. ",
32)
33parser.add_argument(
34    "-c",
35    "--noclean",
36    action="store_true",
37    default=False,
38    help="Do NOT clean the STM32Cube repo directory",
39)
40parser.add_argument(
41    "-f",
42    "--force",
43    action="store_true",
44    default=False,
45    help="Forces the merge except .rej files \n"
46    + "applicable only with -s option. "
47    + "Otrherwise merge is forced systematically "
48    + "for each serie",
49)
50parser.add_argument(
51    "-r",
52    "--repo",
53    type=str,
54    help="Allows to provide path where STM32Cube repo " + "are stored",
55)
56args = parser.parse_args()
57
58
59def update_cubes():
60    """Update all STM32Cubes"""
61    module_path = (
62        Path(os.getenv("ZEPHYR_BASE")).absolute()
63        / r".."
64        / "modules"
65        / "hal"
66        / "stm32"
67        / "stm32cube"
68    )
69    if not module_path.exists():
70        raise Exception("Error: cannot find ./zephyr project")
71
72    for stmyyxx in module_path.iterdir():
73        if stmyyxx.is_dir() and "common_ll" not in stmyyxx.name:
74            logging.info("updating module " + stmyyxx.name)
75            # Force the commit for each serie
76            update_serie = serie_update.Stm32SerieUpdate(
77                stmyyxx.name[:-2], repo_path, force=True, noclean=args.noclean
78            )
79            update_serie.update_stm32_hal_serie()
80
81
82################################################
83# Main
84################################################
85if args.repo:
86    repo_path = Path(args.repo)
87else:
88    repo_path = Path(os.getenv("HOME")) / "STM32Cube_repo"
89
90if not os.getenv("ZEPHYR_BASE"):
91    raise Exception("ZEPHYR_BASE Not defined")
92
93if not args.noclean:
94    print("Do you want to clean downloaded repo (" + str(repo_path) + ") at the end of updates?")
95    res = input("(Enter y/n) ").lower()
96    while res not in ("y", "n"):
97        res = input("(Enter y/n) ").lower()
98    if res == "n":
99        logging.info("Add option --noclean")
100        args.noclean = True
101
102if args.stm32_serie:
103    update = serie_update.Stm32SerieUpdate(
104        args.stm32_serie, repo_path, args.force, args.noclean
105    )
106    update.update_stm32_hal_serie()
107else:
108    update_cubes()
109
110logging.info("The end")
111