mpflash 1.25.0rc4__py3-none-any.whl → 1.25.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mpflash/add_firmware.py DELETED
@@ -1,125 +0,0 @@
1
- import shutil
2
- from pathlib import Path
3
- from typing import Union
4
-
5
- import jsonlines
6
- import requests
7
- from loguru import logger as log
8
- # re-use logic from mpremote
9
- from mpremote.mip import _rewrite_url as rewrite_url # type: ignore
10
- from pytest import Session
11
-
12
- from mpflash.config import config
13
- from mpflash.db.core import Session
14
- from mpflash.db.models import Firmware
15
- from mpflash.versions import get_preview_mp_version, get_stable_mp_version
16
-
17
- # github.com/<owner>/<repo>@<branch>#<commit>
18
- # $remote_url = git remote get-url origin
19
- # $branch = git rev-parse --abbrev-ref HEAD
20
- # $commit = git rev-parse --short HEAD
21
- # if ($remote_url -match "github.com[:/](.+)/(.+?)(\.git)?$") {
22
- # $owner = $matches[1]
23
- # $repo = $matches[2]
24
- # "github.com/$owner/$repo@$branch#$commit"
25
- # }
26
-
27
-
28
- def add_firmware(
29
- source: Union[Path, str],
30
- new_fw: Firmware,
31
- *,
32
- force: bool = False,
33
- custom: bool = False,
34
- description: str = "",
35
- ) -> bool:
36
- """Add a firmware to the firmware folder.
37
-
38
- stored in the port folder, with the same filename as the source.
39
- """
40
- # Check minimal info needed
41
-
42
- if not new_fw.board_id or not new_fw.board or not new_fw.port:
43
- log.error("board_id, board and port are required")
44
- return False
45
- if not isinstance(source, Path) and not source.startswith("http"):
46
- log.error(f"Invalid source {source}")
47
- return False
48
-
49
- # use sensible defaults
50
- source_2 = Path(source)
51
- # new_fw.variant = new_fw.variant or new_fw.board
52
- new_fw.custom = new_fw.custom or custom
53
- if not new_fw.version:
54
- # TODO: Get version from filename
55
- # or use the last preview version
56
- new_fw.version = get_preview_mp_version()
57
-
58
- config.firmware_folder.mkdir(exist_ok=True)
59
-
60
- fw_filename = config.firmware_folder / new_fw.port / source_2.name
61
-
62
- new_fw.firmware_file = str(fw_filename.relative_to(config.firmware_folder))
63
- new_fw.source = source.as_uri() if isinstance(source, Path) else source
64
-
65
- if not copy_firmware(source, fw_filename, force):
66
- log.error(f"Failed to copy {source} to {fw_filename}")
67
- return False
68
- # add to inventory
69
- with Session() as session:
70
- # check if the firmware already exists
71
- existing_fw = (
72
- session.query(Firmware)
73
- .filter(
74
- Firmware.board_id == new_fw.board_id,
75
- Firmware.version == new_fw.version,
76
- Firmware.port == new_fw.port,
77
- )
78
- .first()
79
- )
80
- if existing_fw:
81
- log.warning(f"Firmware {existing_fw} already exists")
82
- if not force:
83
- return False
84
- # update the existing firmware
85
- existing_fw.firmware_file = new_fw.firmware_file
86
- existing_fw.source = new_fw.source
87
- existing_fw.custom = custom
88
- existing_fw.description = description
89
- else:
90
- session.add(new_fw)
91
-
92
- return True
93
-
94
-
95
- def copy_firmware(source: Union[Path, str], fw_filename: Path, force: bool = False):
96
- """Add a firmware to the firmware folder.
97
- stored in the port folder, with the same filename as the source.
98
- """
99
- if fw_filename.exists() and not force:
100
- log.error(f" {fw_filename} already exists. Use --force to overwrite")
101
- return False
102
- fw_filename.parent.mkdir(exist_ok=True)
103
- if isinstance(source, Path):
104
- if not source.exists():
105
- log.error(f"File {source} does not exist")
106
- return False
107
- # file copy
108
- log.debug(f"Copy {source} to {fw_filename}")
109
- shutil.copy(source, fw_filename)
110
- return True
111
- # handle github urls
112
- url = rewrite_url(source)
113
- if str(source).startswith("http://") or str(source).startswith("https://"):
114
- log.debug(f"Download {url} to {fw_filename}")
115
- response = requests.get(url)
116
-
117
- if response.status_code == 200:
118
- with open(fw_filename, "wb") as file:
119
- file.write(response.content)
120
- log.info("File downloaded and saved successfully.")
121
- return True
122
- else:
123
- print("Failed to download the file.")
124
- return False
125
- return False