modm_data.dl.stmicro.cubemx
1# Copyright 2014, Niklas Hauser 2# SPDX-License-Identifier: MPL-2.0 3 4 5import urllib.request 6import zipfile 7import shutil 8import re 9import io 10import os 11import random 12import time 13import logging 14import subprocess 15 16from pathlib import Path 17 18from ...utils import pkg_apply_patch 19from ..store import _hdr 20 21LOGGER = logging.getLogger(__name__) 22 23_hdr = { 24 'Accept': '*', 25 'Accept-Encoding': '*', 26 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15', 27 'Accept-Language': 'en-GB,en;q=0.9', 28 'Connection': 'keep-alive', 29} 30 31 32def _dl(url): 33 cmd = f"curl '{url}' -L -s --max-time 120 -o - " + " ".join(f"-H '{k}: {v}'" for k,v in _hdr.items()) 34 return subprocess.run(cmd, shell=True, stdout=subprocess.PIPE).stdout 35 36 37def download_cubemx(extraction_path: Path, with_download: bool = True, with_patch: bool = True) -> bool: 38 # First check STMUpdaterDefinitions.xml from this zip 39 update_url = "https://sw-center.st.com/packs/resource/utility/updaters.zip" 40 update_url2 = "https://www.ebuc23.com/s3/stm_test/software/utility/updaters.zip" 41 # Then Release="MX.6.2.0" maps to this: -win, -lin, -mac 42 cube_url = "https://sw-center.st.com/packs/resource/library/stm32cube_mx_v{}-lin.zip" 43 cube_url2 = "https://www.ebuc23.com/s3/stm_test/software/library/stm32cube_mx_v{}-lin.zip" 44 45 if with_download: 46 LOGGER.info("Downloading Update Info...") 47 # try: 48 # urllib.request.urlopen(urllib.request.Request(update_url, headers=_hdr)) 49 # except: 50 # update_url = update_url2 51 # cube_url = cube_url2 52 # LOGGER.debug(update_url) 53 # time.sleep(random.randrange(0,2)) 54 55 z = zipfile.ZipFile(io.BytesIO(_dl(update_url))) 56 with io.TextIOWrapper(z.open("STMUpdaterDefinitions.xml"), encoding="utf-8") as defs: 57 version = re.search(r'Release="MX\.(.*?)"', defs.read()) 58 version = version.group(1).replace(".", "") 59 60 shutil.rmtree(extraction_path / "mcu", ignore_errors=True) 61 shutil.rmtree(extraction_path / "plugins", ignore_errors=True) 62 extraction_path.mkdir(exist_ok=True, parents=True) 63 64 LOGGER.info("Downloading Database...") 65 LOGGER.debug(cube_url.format(version)) 66 time.sleep(random.randrange(1,6)) 67 68 z = zipfile.ZipFile(io.BytesIO(_dl(cube_url.format(version)))) 69 LOGGER.info("Extracting Database...") 70 for file in z.namelist(): 71 if any(file.startswith(prefix) for prefix in ("MX/db/mcu", "MX/db/plugins")): 72 z.extract(file, extraction_path) 73 74 LOGGER.info("Moving Database...") 75 shutil.move(extraction_path / "MX/db/mcu", extraction_path / "mcu") 76 shutil.move(extraction_path / "MX/db/plugins", extraction_path / "plugins") 77 shutil.rmtree(extraction_path / "MX", ignore_errors=True) 78 79 LOGGER.info("Normalizing file endings...") 80 for file in Path(extraction_path).glob("**/*"): 81 if str(file).endswith(".xml"): 82 with file.open("r", newline=None, encoding="utf-8", errors="replace") as rfile: 83 content = [l.rstrip()+"\n" for l in rfile.readlines()] 84 with file.open("w", encoding="utf-8") as wfile: 85 wfile.writelines(content) 86 87 if with_patch: 88 LOGGER.info("Patching Database...") 89 from . import data 90 return pkg_apply_patch(data, "cubemx.patch", extraction_path) 91 92 return True
LOGGER =
<Logger modm_data.dl.stmicro.cubemx (WARNING)>
def
download_cubemx( extraction_path: pathlib.Path, with_download: bool = True, with_patch: bool = True) -> bool:
38def download_cubemx(extraction_path: Path, with_download: bool = True, with_patch: bool = True) -> bool: 39 # First check STMUpdaterDefinitions.xml from this zip 40 update_url = "https://sw-center.st.com/packs/resource/utility/updaters.zip" 41 update_url2 = "https://www.ebuc23.com/s3/stm_test/software/utility/updaters.zip" 42 # Then Release="MX.6.2.0" maps to this: -win, -lin, -mac 43 cube_url = "https://sw-center.st.com/packs/resource/library/stm32cube_mx_v{}-lin.zip" 44 cube_url2 = "https://www.ebuc23.com/s3/stm_test/software/library/stm32cube_mx_v{}-lin.zip" 45 46 if with_download: 47 LOGGER.info("Downloading Update Info...") 48 # try: 49 # urllib.request.urlopen(urllib.request.Request(update_url, headers=_hdr)) 50 # except: 51 # update_url = update_url2 52 # cube_url = cube_url2 53 # LOGGER.debug(update_url) 54 # time.sleep(random.randrange(0,2)) 55 56 z = zipfile.ZipFile(io.BytesIO(_dl(update_url))) 57 with io.TextIOWrapper(z.open("STMUpdaterDefinitions.xml"), encoding="utf-8") as defs: 58 version = re.search(r'Release="MX\.(.*?)"', defs.read()) 59 version = version.group(1).replace(".", "") 60 61 shutil.rmtree(extraction_path / "mcu", ignore_errors=True) 62 shutil.rmtree(extraction_path / "plugins", ignore_errors=True) 63 extraction_path.mkdir(exist_ok=True, parents=True) 64 65 LOGGER.info("Downloading Database...") 66 LOGGER.debug(cube_url.format(version)) 67 time.sleep(random.randrange(1,6)) 68 69 z = zipfile.ZipFile(io.BytesIO(_dl(cube_url.format(version)))) 70 LOGGER.info("Extracting Database...") 71 for file in z.namelist(): 72 if any(file.startswith(prefix) for prefix in ("MX/db/mcu", "MX/db/plugins")): 73 z.extract(file, extraction_path) 74 75 LOGGER.info("Moving Database...") 76 shutil.move(extraction_path / "MX/db/mcu", extraction_path / "mcu") 77 shutil.move(extraction_path / "MX/db/plugins", extraction_path / "plugins") 78 shutil.rmtree(extraction_path / "MX", ignore_errors=True) 79 80 LOGGER.info("Normalizing file endings...") 81 for file in Path(extraction_path).glob("**/*"): 82 if str(file).endswith(".xml"): 83 with file.open("r", newline=None, encoding="utf-8", errors="replace") as rfile: 84 content = [l.rstrip()+"\n" for l in rfile.readlines()] 85 with file.open("w", encoding="utf-8") as wfile: 86 wfile.writelines(content) 87 88 if with_patch: 89 LOGGER.info("Patching Database...") 90 from . import data 91 return pkg_apply_patch(data, "cubemx.patch", extraction_path) 92 93 return True