def create(self): """Create a backup in /tmp""" if not os.path.exists(self.local_path): return if not is_safe_to_remove(self.local_path): return if os.path.exists(self.backup_path): shutil.rmtree(self.backup_path) while os.path.exists(self.backup_path): sleep(0.1) os.makedirs(self.backup_path, exist_ok=True) try: if os.path.isfile(self.local_path): shutil.copyfile(self.local_path, self.backup_path_full) os.remove(self.local_path) else: shutil.copytree(self.local_path, self.backup_path_full) shutil.rmtree(self.local_path) while os.path.exists(self.local_path): sleep(0.1) self.logger.debug( f"Backup for {self.local_path}, created in {self.backup_path_full}" ) except (Exception, BaseException): # pylint: disable=broad-except pass
async def remove_local_directory(self): """Check the local directory.""" import shutil from asyncio import sleep try: if self.data.category == "python_script": local_path = f"{self.content.path.local}/{self.data.name}.py" elif self.data.category == "theme": if os.path.exists( f"{self.hacs.core.config_path}/{self.hacs.configuration.theme_path}/{self.data.name}.yaml" ): os.remove( f"{self.hacs.core.config_path}/{self.hacs.configuration.theme_path}/{self.data.name}.yaml" ) local_path = self.content.path.local elif self.data.category == "integration": if not self.data.domain: self.logger.error("%s Missing domain", self) return False local_path = self.content.path.local else: local_path = self.content.path.local if os.path.exists(local_path): if not is_safe_to_remove(local_path): self.logger.error( "%s Path %s is blocked from removal", self, local_path ) return False self.logger.debug("%s Removing %s", self, local_path) if self.data.category in ["python_script"]: os.remove(local_path) else: shutil.rmtree(local_path) while os.path.exists(local_path): await sleep(1) else: self.logger.debug( "%s Presumed local content path %s does not exist", self, local_path ) except (Exception, BaseException) as exception: self.logger.debug( "%s Removing %s failed with %s", self, local_path, exception ) return False return True
def create(self): """Create a backup in /tmp""" if not is_safe_to_remove(self.repository.content.path.local): return if os.path.exists(self.backup_path): shutil.rmtree(self.backup_path) while os.path.exists(self.backup_path): sleep(0.1) os.makedirs(self.backup_path, exist_ok=True) for filename in os.listdir(self.repository.content.path.local): if filename.endswith(".yaml"): source_file_name = f"{self.repository.content.path.local}/{filename}" target_file_name = f"{self.backup_path}/{filename}" shutil.copyfile(source_file_name, target_file_name)
async def remove_local_directory(self): """Check the local directory.""" import shutil from asyncio import sleep try: if self.data.category == "python_script": local_path = f"{self.content.path.local}/{self.data.name}.py" elif self.data.category == "theme": if os.path.exists( f"{self.hacs.system.config_path}/{self.hacs.configuration.theme_path}/{self.data.name}.yaml" ): os.remove( f"{self.hacs.system.config_path}/{self.hacs.configuration.theme_path}/{self.data.name}.yaml" ) local_path = self.content.path.local elif self.data.category == "integration": if not self.data.domain: self.logger.error("Missing domain") return False local_path = self.content.path.local else: local_path = self.content.path.local if os.path.exists(local_path): if not is_safe_to_remove(local_path): return False self.logger.debug(f"Removing {local_path}") if self.data.category in ["python_script"]: os.remove(local_path) else: shutil.rmtree(local_path) while os.path.exists(local_path): await sleep(1) except (Exception, BaseException) as exception: self.logger.debug(f"Removing {local_path} failed with {exception}") return False return True