Beispiel #1
0
    def get(self, configuration=None):
        type = self.get_argument("type", "firmware.bin")

        if type == "firmware.bin":
            storage_path = ext_storage_path(settings.config_dir, configuration)
            storage_json = StorageJSON.load(storage_path)
            if storage_json is None:
                self.send_error(404)
                return
            filename = f"{storage_json.name}.bin"
            path = storage_json.firmware_bin_path

        elif type == "firmware-factory.bin":
            storage_path = ext_storage_path(settings.config_dir, configuration)
            storage_json = StorageJSON.load(storage_path)
            if storage_json is None:
                self.send_error(404)
                return
            filename = f"{storage_json.name}-factory.bin"
            path = storage_json.firmware_bin_path.replace(
                "firmware.bin", "firmware-factory.bin")

        else:
            args = ["esphome", "idedata", settings.rel_path(configuration)]
            rc, stdout, _ = run_system_command(*args)

            if rc != 0:
                self.send_error(404 if rc == 2 else 500)
                return

            idedata = platformio_api.IDEData(json.loads(stdout))

            found = False
            for image in idedata.extra_flash_images:
                if image.path.endswith(type):
                    path = image.path
                    filename = type
                    found = True
                    break

            if not found:
                self.send_error(404)
                return

        self.set_header("Content-Type", "application/octet-stream")
        self.set_header("Content-Disposition",
                        f'attachment; filename="{filename}"')
        self.set_header("Cache-Control", "no-cache")
        if not Path(path).is_file():
            self.send_error(404)
            return

        with open(path, "rb") as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Beispiel #2
0
 def storage(self):  # type: () -> Optional[StorageJSON]
     if not self._loaded_storage:
         self._storage = StorageJSON.load(
             ext_storage_path(settings.config_dir, self.filename)
         )
         self._loaded_storage = True
     return self._storage
Beispiel #3
0
def wizard_write(path, **kwargs):
    name = kwargs['name']
    board = kwargs['board']
    if 'platform' not in kwargs:
        kwargs[
            'platform'] = 'ESP8266' if board in ESP8266_BOARD_PINS else 'ESP32'
    platform = kwargs['platform']

    with codecs.open(path, 'w') as f_handle:
        f_handle.write(wizard_file(**kwargs))
    storage = StorageJSON.from_wizard(name, name + '.local', platform, board)
    storage_path = ext_storage_path(os.path.dirname(path),
                                    os.path.basename(path))
    storage.save(storage_path)
Beispiel #4
0
    def post(self, configuration=None):
        config_file = settings.rel_path(configuration)
        storage_path = ext_storage_path(settings.config_dir, configuration)

        trash_path = trash_storage_path(settings.config_dir)
        mkdir_p(trash_path)
        shutil.move(config_file, os.path.join(trash_path, configuration))

        storage_json = StorageJSON.load(storage_path)
        if storage_json is not None:
            # Delete build folder (if exists)
            name = storage_json.name
            build_folder = os.path.join(settings.config_dir, name)
            if build_folder is not None:
                shutil.rmtree(build_folder, os.path.join(trash_path, name))
Beispiel #5
0
def wizard_write(path, **kwargs):
    name = kwargs["name"]
    board = kwargs["board"]

    kwargs["ssid"] = sanitize_double_quotes(kwargs["ssid"])
    kwargs["psk"] = sanitize_double_quotes(kwargs["psk"])
    kwargs["password"] = sanitize_double_quotes(kwargs["password"])

    if "platform" not in kwargs:
        kwargs["platform"] = "ESP8266" if board in ESP8266_BOARD_PINS else "ESP32"
    platform = kwargs["platform"]

    write_file(path, wizard_file(**kwargs))
    storage = StorageJSON.from_wizard(name, name + ".local", platform, board)
    storage_path = ext_storage_path(os.path.dirname(path), os.path.basename(path))
    storage.save(storage_path)
Beispiel #6
0
def wizard_write(path, **kwargs):
    name = kwargs['name']
    board = kwargs['board']

    kwargs['ssid'] = sanitize_double_quotes(kwargs['ssid'])
    kwargs['psk'] = sanitize_double_quotes(kwargs['psk'])
    kwargs['password'] = sanitize_double_quotes(kwargs['password'])

    if 'platform' not in kwargs:
        kwargs['platform'] = 'ESP8266' if board in ESP8266_BOARD_PINS else 'ESP32'
    platform = kwargs['platform']

    write_file(path, wizard_file(**kwargs))
    storage = StorageJSON.from_wizard(name, name + '.local', platform, board)
    storage_path = ext_storage_path(os.path.dirname(path), os.path.basename(path))
    storage.save(storage_path)
Beispiel #7
0
    def post(self, configuration=None):
        config_file = os.path.join(CONFIG_DIR, configuration)
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.set_status(500)
            return

        name = storage_json.name
        trash_path = trash_storage_path(CONFIG_DIR)
        mkdir_p(trash_path)
        shutil.move(config_file, os.path.join(trash_path, configuration))

        # Delete build folder (if exists)
        build_folder = os.path.join(CONFIG_DIR, name)
        if build_folder is not None:
            shutil.rmtree(build_folder, os.path.join(trash_path, name))
Beispiel #8
0
    def get(self, configuration=None):
        # pylint: disable=no-value-for-parameter
        storage_path = ext_storage_path(settings.config_dir, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header("Content-Type", "application/octet-stream")
        filename = f"{storage_json.name}.bin"
        self.set_header("Content-Disposition", f'attachment; filename="{filename}"')
        with open(path, "rb") as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Beispiel #9
0
    def get(self, configuration=None):
        # pylint: disable=no-value-for-parameter
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header('Content-Type', 'application/octet-stream')
        filename = '{}.bin'.format(storage_json.name)
        self.set_header("Content-Disposition",
                        'attachment; filename="{}"'.format(filename))
        with open(path, 'rb') as f:
            while True:
                data = f.read(16384)
                if not data:
                    break
                self.write(data)
        self.finish()
Beispiel #10
0
def wizard_write(path, **kwargs):
    from esphome.components.esp8266 import boards as esp8266_boards

    name = kwargs["name"]
    board = kwargs["board"]

    for key in ("ssid", "psk", "password", "ota_password"):
        if key in kwargs:
            kwargs[key] = sanitize_double_quotes(kwargs[key])

    if "platform" not in kwargs:
        kwargs["platform"] = (
            "ESP8266" if board in esp8266_boards.ESP8266_BOARD_PINS else "ESP32"
        )
    hardware = kwargs["platform"]

    write_file(path, wizard_file(**kwargs))
    storage = StorageJSON.from_wizard(name, f"{name}.local", hardware)
    storage_path = ext_storage_path(os.path.dirname(path), os.path.basename(path))
    storage.save(storage_path)
Beispiel #11
0
    def get(self):
        if not self.is_authenticated():
            self.redirect('/login')
            return

        configuration = self.get_argument('configuration')
        storage_path = ext_storage_path(CONFIG_DIR, configuration)
        storage_json = StorageJSON.load(storage_path)
        if storage_json is None:
            self.send_error()
            return

        path = storage_json.firmware_bin_path
        self.set_header('Content-Type', 'application/octet-stream')
        filename = '{}.bin'.format(storage_json.name)
        self.set_header("Content-Disposition", 'attachment; filename="{}"'.format(filename))
        with open(path, 'rb') as f:
            while 1:
                data = f.read(16384)  # or some other nice-sized chunk
                if not data:
                    break
                self.write(data)
        self.finish()
Beispiel #12
0
 def storage(self):  # type: () -> Optional[StorageJSON]
     if not self._loaded_storage:
         self._storage = StorageJSON.load(
             ext_storage_path(CONFIG_DIR, self.filename))
         self._loaded_storage = True
     return self._storage