From 3951a2b22a4830c64553299b1a3835f312dbc6f8 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Thu, 28 Feb 2019 10:15:57 +0100 Subject: [PATCH] Fix os.symlink on Windows (#460) --- esphome/helpers.py | 13 +++++++++++++ esphome/writer.py | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/esphome/helpers.py b/esphome/helpers.py index e6135a108c..e958315c22 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -132,3 +132,16 @@ def resolve_ip_address(host): raise EsphomeError("Error resolving IP address: {}".format(err)) return ip + + +def symlink(src, dst): + if hasattr(os, 'symlink'): + os.symlink(src, dst) + else: + import ctypes + csl = ctypes.windll.kernel32.CreateSymbolicLinkW + csl.argtypes = (ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32) + csl.restype = ctypes.c_ubyte + flags = 1 if os.path.isdir(src) else 0 + if csl(dst, src, flags) == 0: + raise ctypes.WinError() diff --git a/esphome/writer.py b/esphome/writer.py index 381746dbc0..fdcba4687f 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -14,7 +14,7 @@ from esphome.const import ARDUINO_VERSION_ESP32_1_0_1, ARDUINO_VERSION_ESP32_DEV CONF_TAG, CONF_USE_CUSTOM_CODE from esphome.core import CORE, EsphomeError from esphome.core_config import GITHUB_ARCHIVE_ZIP, LIBRARY_URI_REPO, VERSION_REGEX -from esphome.helpers import mkdir_p, run_system_command +from esphome.helpers import mkdir_p, run_system_command, symlink from esphome.pins import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS from esphome.py_compat import IS_PY3, string_types from esphome.storage_json import StorageJSON, storage_path @@ -229,7 +229,7 @@ def symlink_esphome_core_version(esphome_core_version): do_write = False if do_write: mkdir_p(lib_path) - os.symlink(src_path, dst_path) + symlink(src_path, dst_path) else: # Remove symlink when changing back from local version if os.path.islink(dst_path):