def copy_src_tree(): source_files: Dict[Path, loader.SourceFile] = CORE.extra_source_files for _, component, _ in iter_components(CORE.config): source_files.update(component.source_files) # Convert to list and sort source_files_l = list(source_files.items()) source_files_l.sort() # Build #include list for esphome.h include_l = [] for target, _ in source_files_l: if target.suffix in HEADER_FILE_EXTENSIONS: include_l.append(f'#include "{target}"') include_l.append("") include_s = "\n".join(include_l) source_files_copy = source_files.copy() ignore_targets = [Path(x) for x in (DEFINES_H_TARGET, VERSION_H_TARGET)] for t in ignore_targets: source_files_copy.pop(t) for fname in walk_files(CORE.relative_src_path("esphome")): p = Path(fname) if p.suffix not in SOURCE_FILE_EXTENSIONS: # Not a source file, ignore continue # Transform path to target path name target = p.relative_to(CORE.relative_src_path()) if target in ignore_targets: # Ignore defines.h, will be dealt with later continue if target not in source_files_copy: # Source file removed, delete target p.unlink() else: src_file = source_files_copy.pop(target) with src_file.path() as src_path: copy_file_if_changed(src_path, p) # Now copy new files for target, src_file in source_files_copy.items(): dst_path = CORE.relative_src_path(*target.parts) with src_file.path() as src_path: copy_file_if_changed(src_path, dst_path) # Finally copy defines write_file_if_changed( CORE.relative_src_path("esphome", "core", "defines.h"), generate_defines_h()) write_file_if_changed(CORE.relative_src_path("esphome", "README.txt"), ESPHOME_README_TXT) write_file_if_changed(CORE.relative_src_path("esphome.h"), ESPHOME_H_FORMAT.format(include_s)) write_file_if_changed( CORE.relative_src_path("esphome", "core", "version.h"), VERSION_H_FORMAT.format(__version__), )
def copy_src_tree(): source_files = {} for _, component, _ in iter_components(CORE.config): source_files.update(component.source_files) # Convert to list and sort source_files_l = list(source_files.items()) source_files_l.sort() # Build #include list for esphome.h include_l = [] for target, path in source_files_l: if os.path.splitext(path)[1] in HEADER_FILE_EXTENSIONS: include_l.append(f'#include "{target}"') include_l.append("") include_s = "\n".join(include_l) source_files_copy = source_files.copy() source_files_copy.pop(DEFINES_H_TARGET) for path in walk_files(CORE.relative_src_path("esphome")): if os.path.splitext(path)[1] not in SOURCE_FILE_EXTENSIONS: # Not a source file, ignore continue # Transform path to target path name target = os.path.relpath(path, CORE.relative_src_path()).replace( os.path.sep, "/" ) if target in (DEFINES_H_TARGET, VERSION_H_TARGET): # Ignore defines.h, will be dealt with later continue if target not in source_files_copy: # Source file removed, delete target os.remove(path) else: src_path = source_files_copy.pop(target) copy_file_if_changed(src_path, path) # Now copy new files for target, src_path in source_files_copy.items(): dst_path = CORE.relative_src_path(*target.split("/")) copy_file_if_changed(src_path, dst_path) # Finally copy defines write_file_if_changed( CORE.relative_src_path("esphome", "core", "defines.h"), generate_defines_h() ) write_file_if_changed( CORE.relative_src_path("esphome", "README.txt"), ESPHOME_README_TXT ) write_file_if_changed( CORE.relative_src_path("esphome.h"), ESPHOME_H_FORMAT.format(include_s) ) write_file_if_changed( CORE.relative_src_path("esphome", "core", "version.h"), VERSION_H_FORMAT.format(__version__), )
def copy_src_tree(): import filecmp import shutil source_files = {} for _, component, _ in iter_components(CORE.config): source_files.update(component.source_files) # Convert to list and sort source_files_l = [it for it in source_files.items()] source_files_l.sort() # Build #include list for esphome.h include_l = [] for target, path in source_files_l: if os.path.splitext(path)[1] in HEADER_FILE_EXTENSIONS: include_l.append(u'#include "{}"'.format(target)) include_l.append(u'') include_s = u'\n'.join(include_l) source_files_copy = source_files.copy() source_files_copy.pop(DEFINES_H_TARGET) for path in walk_files(CORE.relative_src_path('esphome')): if os.path.splitext(path)[1] not in SOURCE_FILE_EXTENSIONS: # Not a source file, ignore continue # Transform path to target path name target = os.path.relpath(path, CORE.relative_src_path()).replace(os.path.sep, '/') if target in (DEFINES_H_TARGET, VERSION_H_TARGET): # Ignore defines.h, will be dealt with later continue if target not in source_files_copy: # Source file removed, delete target os.remove(path) else: src_path = source_files_copy.pop(target) if not filecmp.cmp(path, src_path): # Files are not same, copy shutil.copy(src_path, path) # Now copy new files for target, src_path in source_files_copy.items(): dst_path = CORE.relative_src_path(*target.split('/')) mkdir_p(os.path.dirname(dst_path)) shutil.copy(src_path, dst_path) # Finally copy defines write_file_if_changed(generate_defines_h(), CORE.relative_src_path('esphome', 'core', 'defines.h')) write_file_if_changed(ESPHOME_README_TXT, CORE.relative_src_path('esphome', 'README.txt')) write_file_if_changed(ESPHOME_H_FORMAT.format(include_s), CORE.relative_src_path('esphome.h')) write_file_if_changed(VERSION_H_FORMAT.format(__version__), CORE.relative_src_path('esphome', 'core', 'version.h'))
def write_cpp(code_s): path = CORE.relative_src_path('main.cpp') if os.path.isfile(path): text = read_file(path) code_format = find_begin_end(text, CPP_AUTO_GENERATE_BEGIN, CPP_AUTO_GENERATE_END) code_format_ = find_begin_end(code_format[0], CPP_INCLUDE_BEGIN, CPP_INCLUDE_END) code_format = (code_format_[0], code_format_[1], code_format[1]) else: code_format = CPP_BASE_FORMAT copy_src_tree() global_s = u'#include "esphome.h"\n' global_s += CORE.cpp_global_section full_file = code_format[0] + CPP_INCLUDE_BEGIN + u'\n' + global_s + CPP_INCLUDE_END full_file += code_format[1] + CPP_AUTO_GENERATE_BEGIN + u'\n' + code_s + CPP_AUTO_GENERATE_END full_file += code_format[2] write_file_if_changed(full_file, path)