Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base repositories data file #2479

Merged
merged 6 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ repos:
args:
- --quiet-level=2
- --ignore-words-list=hass,ba,fo
- --exclude-file=custom_components/hacs/utils/default.repositories

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ update: ## Pull main from hacs/integration
bump-frontend: ## Bump the HACS frontend
@bash manage/bump_frontend;

update-base-repositories: ## Update stored base repositories
@python3 manage/update_default_repositories.py;

clear-storage:
rm -rf config/.storage/hacs
rm config/.storage/hacs*

homeassistant-install: ## Install the latest dev version of Home Assistant
python3 -m pip --disable-pip-version-check install -U "pip>=8.0.3,<20.3";
python3 -m pip --disable-pip-version-check install -U setuptools wheel;
Expand Down
3 changes: 3 additions & 0 deletions custom_components/hacs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,9 @@ async def async_get_category_repositories(self, category: HacsCategory) -> None:
repository = self.repositories.get_by_full_name(repo)
if repository is not None:
self.repositories.mark_default(repository)
if self.status.new:
# Force update for new installations
self.queue.add(repository.common_update())
continue
self.queue.add(
self.async_register_repository(
Expand Down
12 changes: 9 additions & 3 deletions custom_components/hacs/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os

from homeassistant.core import callback
from homeassistant.util import json as json_util

from ..base import HacsBase
from ..enums import HacsGitHubRepo
Expand Down Expand Up @@ -120,15 +121,20 @@ async def async_store_repository_data(self, repository: HacsRepository):

async def restore(self):
"""Restore saved data."""
hacs = await async_load_from_store(self.hacs.hass, "hacs")
self.hacs.status.new = False
hacs = await async_load_from_store(self.hacs.hass, "hacs") or {}
repositories = await async_load_from_store(self.hacs.hass, "repositories") or {}

if not hacs and not repositories:
# Assume new install
self.hacs.status.new = True
return True
self.logger.info("Loading base repository information")
repositories = await self.hacs.hass.async_add_executor_job(
json_util.load_json,
f"{self.hacs.core.config_path}/custom_components/hacs/utils/default.repositories",
)

self.logger.info("Restore started")
self.hacs.status.new = False

# Hacs
self.hacs.configuration.frontend_mode = hacs.get("view", "Grid")
Expand Down
1 change: 1 addition & 0 deletions custom_components/hacs/utils/default.repositories

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions manage/update_default_repositories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Update the shipped default repositories data file."""
import json
import os
import sys


def update():
"""Update the shipped default repositories data file."""
storage, to_store = None, {}

with open(f"{os.getcwd()}/config/.storage/hacs.repositories", encoding="utf-8") as storage_file:
storage = json.load(storage_file)

if storage is None:
sys.exit("No storage file")

for repo in storage["data"]:
storage["data"][repo]["first_install"] = True
for key in ("installed", "show_beta", "new"):
storage["data"][repo][key] = False
for key in ("installed_commit", "selected_tag", "version_installed"):
storage["data"][repo][key] = None

to_store[repo] = storage["data"][repo]

with open(
f"{os.getcwd()}/custom_components/hacs/utils/default.repositories",
mode="w",
encoding="utf-8",
) as to_store_file:
to_store_file.write(json.dumps(to_store))


if __name__ == "__main__":
update()
6 changes: 4 additions & 2 deletions tests/hacsbase/test_hacsbase_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def test_hacs_data_async_write2(hacs):


@pytest.mark.asyncio
async def test_hacs_data_restore_write_new(hacs):
async def test_hacs_data_restore_write_new(hacs, caplog):
data = HacsData(hacs)
await data.restore()
with patch(
Expand All @@ -37,10 +37,11 @@ async def test_hacs_data_restore_write_new(hacs):
):
await data.async_write()
assert mock_async_save_to_store.called
assert "Loading base repository information" in caplog.text


@pytest.mark.asyncio
async def test_hacs_data_restore_write_not_new(hacs):
async def test_hacs_data_restore_write_not_new(hacs, caplog):
data = HacsData(hacs)

async def _mocked_loads(hass, key):
Expand Down Expand Up @@ -99,3 +100,4 @@ def _mocked_load(*_):
await data.async_write()
assert mock_async_save_to_store.called
assert mock_async_save_to_store_default_encoder.called
assert "Loading base repository information" not in caplog.text