-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathdownload-tools.py
151 lines (133 loc) · 5.42 KB
/
download-tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""Basic script to get adb and fastboot.
Inspired by: https://gitlab.com/ubports/installer/android-tools-bin/-/blob/master/build.js
"""
# This file is part of OpenAndroidInstaller.
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
# the GNU General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
# OpenAndroidInstaller is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along with OpenAndroidInstaller.
# If not, see <https://www.gnu.org/licenses/>."""
# Author: Tobias Sterbak
import os
import sys
import zipfile
from io import BytesIO
from pathlib import Path
import py7zr
import requests
from loguru import logger
def download_adb_fastboot(platform: str):
"""Download adb and fastboot executable from dl.google.com, extract the zip and save to file."""
if platform == "win32":
platform = "win"
logger.info(f"Download adb and fastboot for {platform}...")
url = f"https://dl.google.com/android/repository/platform-tools_r35.0.2-{platform}.zip"
# Downloading the file by sending the request to the URL
response = requests.get(url, allow_redirects=True)
# Writing the file to the local file system
download_path = Path(__file__).parent.joinpath(Path("tools")).resolve()
logger.info(download_path)
file = zipfile.ZipFile(BytesIO(response.content))
file.extractall(download_path.name)
logger.info("DONE.")
def download_heimdall(platform: str):
"""Download heimdall executable from ubuntu.com, extract the zip and save to file."""
logger.info(f"Download heimdall for {platform}...")
url = f"https://people.ubuntu.com/~neothethird/heimdall-{platform}.zip"
# Downloading the file by sending the request to the URL
response = requests.get(url, allow_redirects=True)
# Writing the file to the local file system
download_path = Path(__file__).parent.joinpath(Path("heimdall")).resolve()
logger.info(download_path)
file = zipfile.ZipFile(BytesIO(response.content))
file.extractall(download_path.name)
logger.info("DONE.")
def download_libusb(platform: str):
"""Download libusb-1.0, extract the 7z and save to file."""
logger.info(f"Download libusb-1.0 for {platform}...")
url = "https://github.com/libusb/libusb/releases/download/v1.0.27/libusb-1.0.27.7z"
# Downloading the file by sending the request to the URL
response = requests.get(url, allow_redirects=True)
# Writing the file to the local file system
download_path = Path(__file__).parent.joinpath(Path("libusb-windows")).resolve()
logger.info(download_path)
with py7zr.SevenZipFile(BytesIO(response.content)) as file:
file.extractall(download_path.name)
logger.info("DONE.")
def move_files_to_lib(platform: str):
"""Move files to the expected path in the openandroidinstaller package."""
target_path = Path(os.sep.join(["openandroidinstaller", "bin"]), exist_ok=True)
logger.info(f"Move executables to {target_path}...")
# move the platformtools
pt_path = (
Path(__file__)
.parent.joinpath(Path(os.sep.join(["..", "tools", "platform-tools"])))
.resolve()
)
logger.info(pt_path)
pt_target_path = (
Path(__file__)
.parent.joinpath(Path(os.sep.join(["..", "openandroidinstaller", "bin"])))
.resolve()
)
logger.info(pt_target_path)
pt_path.rename(pt_target_path)
# move heimdall
hd_path = (
Path(__file__)
.parent.joinpath(Path(os.sep.join(["..", "heimdall", "heimdall"])))
.resolve()
)
hd_target_path = (
Path(__file__)
.parent.joinpath(
Path(os.sep.join(["..", "openandroidinstaller", "bin", "heimdall"]))
)
.resolve()
)
if platform == "win32":
hd_path = hd_path.parents[0] / "heimdall.exe"
hd_target_path = hd_target_path.parents[0] / "heimdall.exe"
hd_path.rename(hd_target_path)
# move libusb
libusb_path = (
Path(__file__)
.parent.joinpath(
Path(
os.sep.join(
["..", "libusb-windows", "MinGW32", "dll", "libusb-1.0.dll"]
)
)
)
.resolve()
)
logger.info(libusb_path)
libusb_target_path = (
Path(__file__)
.parent.joinpath(
Path(os.sep.join(["..", "openandroidinstaller", "bin", "libusb-1.0.dll"]))
)
.resolve()
)
logger.info(libusb_target_path)
libusb_path.rename(libusb_target_path)
logger.info("DONE.")
# make executable
logger.info("Allow the executables to be executed.")
for executable_name in ["fastboot", "adb", "mke2fs", "heimdall"]:
if platform == "win32":
(pt_target_path / (executable_name + ".exe")).chmod(0o755)
else:
(pt_target_path / executable_name).chmod(0o755)
logger.info("DONE.")
def main(platform: str):
logger.info(f"Run downloads for {platform} ...")
download_adb_fastboot(platform=platform)
download_heimdall(platform=platform)
download_libusb(platform=platform)
move_files_to_lib(platform=platform)
if __name__ == "__main__":
main(platform=sys.platform)