|
| 1 | +from pathlib import Path |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import getopt |
| 5 | +import fileinput |
| 6 | +from shutil import copyfile |
| 7 | + |
| 8 | +ESP8266_PACKAGE_PATH = Path("packages/esp8266/hardware/esp8266/") |
| 9 | +ESP32_PACKAGE_PATH = Path("packages/esp32/hardware/esp32/") |
| 10 | +ARDUINO_PACKAGES_PATH = None # Determined by user opts or platform |
| 11 | + |
| 12 | + |
| 13 | +def update_line_file( |
| 14 | + file_path, str_line_to_update, str_append, comment_only=False, |
| 15 | + comment_str=None): |
| 16 | + ''' |
| 17 | + Updates a line on a file with a replacement (preserving the original line) |
| 18 | + or comments it out |
| 19 | +
|
| 20 | + :param file_path: The path to the file |
| 21 | + :type file_path: str |
| 22 | + :param str_to_update: The string which will be replaced |
| 23 | + :type str_line_to_update str: |
| 24 | + :param str_append: The string which replace the line of str_to_update |
| 25 | + :type str_append str: |
| 26 | + :param comment_only: Determines whether to replace or only comment out a |
| 27 | + line |
| 28 | + :type comment_only boolean: |
| 29 | + :param comment_str: Str to use for a comment if commenting |
| 30 | + :type comment_str str: |
| 31 | + :raises: :class:`FileNotFound`: File couldn't be opened |
| 32 | +
|
| 33 | + :returns: whether the string was replaced in the file or it was commented |
| 34 | + out |
| 35 | + :rtype: boolean |
| 36 | + ''' |
| 37 | + file_modified = False |
| 38 | + for line in fileinput.input(file_path, inplace=True): |
| 39 | + if line.startswith(str_line_to_update): |
| 40 | + if comment_only: |
| 41 | + line = f"{comment_str} {line}" |
| 42 | + file_modified = True |
| 43 | + else: |
| 44 | + line = line.rstrip() |
| 45 | + line = f"{line}{str_append}\n" |
| 46 | + file_modified = True |
| 47 | + sys.stdout.write(line) |
| 48 | + return file_modified |
| 49 | + |
| 50 | + |
| 51 | +def confirm_overwrite(file_path): |
| 52 | + ''' |
| 53 | + Confirms whether to overwrite changes otherwise exits program |
| 54 | +
|
| 55 | + :param file_path: The path to the file |
| 56 | + :type file_path: str |
| 57 | + ''' |
| 58 | + prompt = f"There is already a backup file at" \ |
| 59 | + f" {file_path}; proceeding will" \ |
| 60 | + f" overwrite this file. Do you wish to proceed?" \ |
| 61 | + f" Input Y or N:" \ |
| 62 | + f" " |
| 63 | + while True: |
| 64 | + response = input(prompt) |
| 65 | + response = response.lower() |
| 66 | + if response == 'n': |
| 67 | + print("No changes made... exiting") |
| 68 | + sys.exit() |
| 69 | + elif response == 'y': |
| 70 | + print("Backup will be overwritten") |
| 71 | + break |
| 72 | + else: |
| 73 | + print("Ensure your response is a Y or N") |
| 74 | + |
| 75 | + |
| 76 | +def usage(): |
| 77 | + ''' |
| 78 | + Prints script's opt usage |
| 79 | + ''' |
| 80 | + print( |
| 81 | + "automate_board_config.py usage:\n" |
| 82 | + " -h or --help: Print usage text\n" |
| 83 | + " -p or --packages_path: Set custom path for Arduino packages path") |
| 84 | + sys.exit() |
| 85 | + |
| 86 | + |
| 87 | +def parse_opts(): |
| 88 | + ''' |
| 89 | + Prints script's command line options |
| 90 | + ''' |
| 91 | + options, _ = getopt.gnu_getopt( |
| 92 | + sys.argv[1:], |
| 93 | + 'hp:', |
| 94 | + ['help', 'packages_path']) |
| 95 | + |
| 96 | + for opt, arg in options: |
| 97 | + if opt in ('-h', '--help'): |
| 98 | + usage() |
| 99 | + elif opt in ('-p', '--packages_path'): |
| 100 | + global ARDUINO_PACKAGES_PATH |
| 101 | + ARDUINO_PACKAGES_PATH = Path(arg) |
| 102 | + |
| 103 | + |
| 104 | +def main(): |
| 105 | + parse_opts() |
| 106 | + disclaimer_prompt = \ |
| 107 | + "This script will attempt to automatically update" \ |
| 108 | + " your ESP8266 and/or ESP32 board files to work with Azure IoT Hub" \ |
| 109 | + " for the repo https://github.com/Azure/azure-iot-arduino" \ |
| 110 | + "\nPlease refer to the license agreement there." \ |
| 111 | + "\nThis script will update all installed versions of board" \ |
| 112 | + " libraries for ESP8266 and/or ESP32." \ |
| 113 | + "\nDo you wish to proceed? Please answer Y or N:" \ |
| 114 | + " " |
| 115 | + |
| 116 | + while True: |
| 117 | + response = input(disclaimer_prompt) |
| 118 | + response = response.lower() |
| 119 | + if response == 'n': |
| 120 | + print("No changes made... exiting") |
| 121 | + sys.exit() |
| 122 | + elif response == 'y': |
| 123 | + print("Proceeding") |
| 124 | + break |
| 125 | + else: |
| 126 | + print("Ensure your response is a Y or N") |
| 127 | + |
| 128 | + board_prompt = \ |
| 129 | + "Would you like to update your ESP8266 or ESP32 board files?\n" \ |
| 130 | + "For ESP8266 please respond: 8266\n" \ |
| 131 | + "For ESP32 please respond: 32\n" \ |
| 132 | + "Which board files would you like to update:" \ |
| 133 | + " " |
| 134 | + |
| 135 | + board_to_update = "" |
| 136 | + PACKAGE_PATH = "" |
| 137 | + while True: |
| 138 | + response = input(board_prompt) |
| 139 | + response = response.lower() |
| 140 | + if response == '8266': |
| 141 | + board_to_update = '8266' |
| 142 | + PACKAGE_PATH = ESP8266_PACKAGE_PATH |
| 143 | + break |
| 144 | + elif response == '32': |
| 145 | + board_to_update = '32' |
| 146 | + PACKAGE_PATH = ESP32_PACKAGE_PATH |
| 147 | + break |
| 148 | + else: |
| 149 | + print("Ensure your response is either 8226 or 32") |
| 150 | + |
| 151 | + global ARDUINO_PACKAGES_PATH |
| 152 | + if ARDUINO_PACKAGES_PATH is None: |
| 153 | + if sys.platform == "darwin": |
| 154 | + ARDUINO_PACKAGES_PATH = Path(Path.home() / "Library/Arduino15") |
| 155 | + elif sys.platform == "linux": |
| 156 | + ARDUINO_PACKAGES_PATH = Path(Path.home() / ".arduino15") |
| 157 | + elif sys.platform == "win32": |
| 158 | + ARDUINO_PACKAGES_PATH = Path( |
| 159 | + Path.home() / "AppData/Local/Arduino15") |
| 160 | + else: |
| 161 | + print(f"Error: no valid board path condition for platform:" |
| 162 | + f" {sys.platform}") |
| 163 | + sys.exit() |
| 164 | + |
| 165 | + print(f"Arduino path for platform {sys.platform} is:" |
| 166 | + f" {ARDUINO_PACKAGES_PATH}") |
| 167 | + |
| 168 | + # Check for and change other versions if they exist |
| 169 | + BOARD_PATH = Path(ARDUINO_PACKAGES_PATH / PACKAGE_PATH) |
| 170 | + try: |
| 171 | + versions = [] |
| 172 | + with os.scandir(BOARD_PATH) as entries: |
| 173 | + for version in entries: |
| 174 | + # avoid files and hidden files |
| 175 | + if version.is_dir and not version.name.startswith('.'): |
| 176 | + versions.append( |
| 177 | + Path(BOARD_PATH / version)) |
| 178 | + if len(versions) == 0: |
| 179 | + raise FileNotFoundError |
| 180 | + except FileNotFoundError: |
| 181 | + print( |
| 182 | + f'Error: Board files for ESP{board_to_update} not found!\n' |
| 183 | + f'Directory searched was: {BOARD_PATH}\n' |
| 184 | + f'Please ensure that the board library exists at the location' |
| 185 | + f', or check command line parameters to specify a' |
| 186 | + f' custom Arduino packages path') |
| 187 | + sys.exit(1) |
| 188 | + |
| 189 | + for path in versions: |
| 190 | + if PACKAGE_PATH == ESP8266_PACKAGE_PATH: |
| 191 | + # 8266 has specific files which 32 aren't required to change |
| 192 | + arduino_header_backup = Path(path / "cores/esp8266/Arduino.h.orig") |
| 193 | + if arduino_header_backup.exists(): |
| 194 | + confirm_overwrite(arduino_header_backup) |
| 195 | + arduino_header_file = Path(path / "cores/esp8266/Arduino.h") |
| 196 | + if arduino_header_file.exists(): |
| 197 | + print(f"Updating: {arduino_header_file}") |
| 198 | + copyfile( |
| 199 | + arduino_header_file, |
| 200 | + Path(path / "cores/esp8266/Arduino.h.orig")) |
| 201 | + print( |
| 202 | + f"Backup created:" |
| 203 | + f" {Path(path / 'cores/esp8266/Arduino.h.orig')}") |
| 204 | + get_update = update_line_file( |
| 205 | + arduino_header_file, "#define round(x)", |
| 206 | + str_append=None, comment_only=True, |
| 207 | + comment_str="//") |
| 208 | + print(f"Updated: {get_update} for {arduino_header_file}") |
| 209 | + else: |
| 210 | + print(f"Could not find {arduino_header_file}") |
| 211 | + sys.exit(1) |
| 212 | + |
| 213 | + platform_txt_backup = Path(path / "platform.txt.orig") |
| 214 | + if platform_txt_backup.exists(): |
| 215 | + confirm_overwrite(platform_txt_backup) |
| 216 | + platform_txt_file = Path(path / "platform.txt") |
| 217 | + if platform_txt_file.exists(): |
| 218 | + print(f"Updating: {platform_txt_file}") |
| 219 | + copyfile(platform_txt_file, Path(path / "platform.txt.orig")) |
| 220 | + print(f"Backup created: {Path(path / 'platform.txt.orig')}") |
| 221 | + append_str = "" |
| 222 | + if PACKAGE_PATH == ESP8266_PACKAGE_PATH: |
| 223 | + # Ensure to include spaces for flags |
| 224 | + append_str = " -DDONT_USE_UPLOADTOBLOB" \ |
| 225 | + " -DUSE_BALTIMORE_CERT" |
| 226 | + elif PACKAGE_PATH == ESP32_PACKAGE_PATH: |
| 227 | + append_str = " -DDONT_USE_UPLOADTOBLOB" |
| 228 | + get_update = update_line_file( |
| 229 | + platform_txt_file, "build.extra_flags=", |
| 230 | + str_append=append_str) |
| 231 | + print(f"Updated: {get_update} for {platform_txt_file}") |
| 232 | + else: |
| 233 | + print(f"Could not find {platform_txt_file}") |
| 234 | + sys.exit(1) |
| 235 | + |
| 236 | + |
| 237 | +main() |
0 commit comments