def test_valid_objects(): tree = [ AIOGitHubAPIRepositoryTreeContent( { "path": "test/file.file", "type": "blob" }, "test/test", "master"), AIOGitHubAPIRepositoryTreeContent( { "path": "test/newfile.file", "type": "blob" }, "test/test", "master"), AIOGitHubAPIRepositoryTreeContent( { "path": "test/file.png", "type": "blob" }, "test/test", "master"), ] files = [ x.filename for x in filter_content_return_one_of_type( tree, "test", "file", "full_path") ] assert "file.file" in files assert "newfile.file" not in files assert "file.png" in files
def test_valid_list(): tree = ["test/file.file", "test/newfile.file", "test/file.png"] files = filter_content_return_one_of_type(tree, "test", "file") assert "test/file.file" in files assert "test/newfile.file" not in files assert "test/file.png" in files
def gather_files_to_download(repository): """Return a list of file objects to be downloaded.""" files = [] tree = repository.tree ref = f"{repository.ref}".replace("tags/", "") releaseobjects = repository.releases.objects category = repository.data.category remotelocation = repository.content.path.remote if should_try_releases(repository): for release in releaseobjects or []: if ref == release.tag_name: for asset in release.assets or []: files.append(asset) if files: return files if repository.content.single: for treefile in tree: if treefile.filename == repository.data.file_name: files.append( FileInformation(treefile.download_url, treefile.full_path, treefile.filename)) return files if category == "plugin": for treefile in tree: if treefile.path in ["", "dist"]: if remotelocation == "dist" and not treefile.filename.startswith( "dist"): continue if not remotelocation: if not treefile.filename.endswith(".js"): continue if treefile.path != "": continue if not treefile.is_directory: files.append( FileInformation(treefile.download_url, treefile.full_path, treefile.filename)) if files: return files if repository.data.content_in_root: if not repository.data.filename: if category == "theme": tree = filter_content_return_one_of_type( repository.tree, "", "yaml", "full_path") for path in tree: if path.is_directory: continue if path.full_path.startswith(repository.content.path.remote): files.append( FileInformation(path.download_url, path.full_path, path.filename)) return files
async def download_content(repository, validate, local_directory): """Download the content of a directory.""" contents = [] try: if repository.releases.releases and repository.information.category in [ "plugin", "theme", ]: for release in repository.releases.objects: if repository.status.selected_tag == release.tag_name: for asset in release.assets: contents.append(asset) if not contents: if repository.content.single: for repository_object in repository.content.objects: contents.append( contentObj( repository_object.download_url, repository_object.path, repository_object.name, )) else: tree = repository.tree if repository.repository_manifest.content_in_root: if repository.repository_manifest.filename is None: if repository.information.category == "theme": tree = filter_content_return_one_of_type( repository.tree, "themes", "yaml", "full_path") for path in tree: if path.is_directory: continue if path.full_path.startswith( repository.content.path.remote): contents.append( contentObj(path.download_url, path.full_path, path.filename)) if not contents: raise HacsException("No content to download") for content in contents: if repository.repository_manifest.content_in_root: if repository.repository_manifest.filename is not None: if content.name != repository.repository_manifest.filename: continue repository.logger.debug(f"Downloading {content.name}") filecontent = await async_download_file(repository.hass, content.download_url) if filecontent is None: validate.errors.append(f"[{content.name}] was not downloaded.") continue # Save the content of the file. if repository.content.single or content.path is None: local_directory = repository.content.path.local else: _content_path = content.path if not repository.repository_manifest.content_in_root: _content_path = _content_path.replace( f"{repository.content.path.remote}", "") local_directory = f"{repository.content.path.local}/{_content_path}" local_directory = local_directory.split("/") del local_directory[-1] local_directory = "/".join(local_directory) # Check local directory pathlib.Path(local_directory).mkdir(parents=True, exist_ok=True) local_file_path = f"{local_directory}/{content.name}" result = await async_save_file(local_file_path, filecontent) if result: repository.logger.info(f"download of {content.name} complete") continue validate.errors.append(f"[{content.name}] was not downloaded.") except Exception as exception: # pylint: disable=broad-except validate.errors.append(f"Download was not complete [{exception}]") return validate