Skip to content

Commit

Permalink
Merge pull request #2728 from brianegge/default-manufacturer
Browse files Browse the repository at this point in the history
Add default manufacturer file if missing
  • Loading branch information
bramstroker authored Nov 29, 2024
2 parents 35add7e + d8b4dec commit e235d28
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
20 changes: 8 additions & 12 deletions .github/scripts/profile_library/update-authors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,36 @@


def run_git_command(command):
""" Run a git command and return the output. """
"""Run a git command and return the output."""
result = subprocess.run(command, shell=True, capture_output=True, text=True)
result.check_returncode() # Raise an error if the command fails
return result.stdout.strip()


def get_commits_affected_directory(directory: str) -> list:
""" Get a list of commits that affected the given directory, including renames. """
"""Get a list of commits that affected the given directory, including renames."""
command = f"git log --follow --format='%H' -- '{directory}'"
commits = run_git_command(command)
return commits.splitlines()


def get_commit_author(commit_hash: str) -> str:
""" Get the author of a given commit. """
"""Get the author of a given commit."""
command = f"git show -s --format='%an <%ae>' {commit_hash}"
author = run_git_command(command)
return author


def find_first_commit_author(file: str, check_paths: bool = True) -> str | None:
""" Find the first commit that affected the directory and return the author's name. """
"""Find the first commit that affected the directory and return the author's name."""
commits = get_commits_affected_directory(file)
for commit in reversed(commits): # Process commits from the oldest to newest
command = f"git diff-tree --no-commit-id --name-only -r {commit}"
if not check_paths:
return get_commit_author(commit)

affected_files = run_git_command(command)
paths = [
file.replace("profile_library", "custom_components/powercalc/data"),
file.replace("profile_library", "data"),
file
]
paths = [file.replace("profile_library", "custom_components/powercalc/data"), file.replace("profile_library", "data"), file]
if any(path in affected_files.splitlines() for path in paths):
author = get_commit_author(commit)
return author
Expand All @@ -48,7 +44,7 @@ def find_first_commit_author(file: str, check_paths: bool = True) -> str | None:

def process_model_json_files(root_dir):
# Find all model.json files in the directory tree
model_json_files = glob.glob(os.path.join(root_dir, '**', 'model.json'), recursive=True)
model_json_files = glob.glob(os.path.join(root_dir, "**", "model.json"), recursive=True)

for model_json_file in model_json_files:
# Skip sub profiles
Expand All @@ -71,7 +67,7 @@ def process_model_json_files(root_dir):

def read_author_from_file(file_path: str) -> str | None:
"""Read the author from the model.json file."""
with open(file_path, "r") as file:
with open(file_path) as file:
json_data = json.load(file)

return json_data.get("author")
Expand All @@ -80,7 +76,7 @@ def read_author_from_file(file_path: str) -> str | None:
def write_author_to_file(file_path: str, author: str) -> None:
"""Write the author to the model.json file."""
# Read the existing content
with open(file_path, "r") as file:
with open(file_path) as file:
json_data = json.load(file)

json_data["author"] = author
Expand Down
24 changes: 16 additions & 8 deletions .github/scripts/profile_library/update-library-json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import glob
import json
import os
import git
import sys
from datetime import datetime
from pathlib import Path

from pytablewriter import MarkdownTableWriter
import git

sys.path.insert(
1,
Expand Down Expand Up @@ -39,7 +38,8 @@ def generate_library_json(model_listing: list[dict]) -> None:
if not manufacturer:
manufacturer = {
**get_manufacturer_json(manufacturer_name),
**{"models": [], "device_types": []},
"models": [],
"device_types": [],
}
manufacturers[manufacturer_name] = manufacturer

Expand All @@ -53,7 +53,7 @@ def generate_library_json(model_listing: list[dict]) -> None:
"device_type": "device_type",
"aliases": "aliases",
"updated_at": "updated_at",
"color_modes": "color_modes"
"color_modes": "color_modes",
}

# Create a new dictionary with updated keys
Expand All @@ -74,8 +74,17 @@ def generate_library_json(model_listing: list[dict]) -> None:


def get_manufacturer_json(manufacturer: str) -> dict:
with open(os.path.join(DATA_DIR, manufacturer, "manufacturer.json")) as json_file:
return json.load(json_file)
json_path = os.path.join(DATA_DIR, manufacturer, "manufacturer.json")
try:
with open(json_path) as json_file:
return json.load(json_file)
except FileNotFoundError:
default_json = {"name": manufacturer, "aliases": []}
with open(json_path, "w", encoding="utf-8") as json_file:
json.dump(default_json, json_file, ensure_ascii=False, indent=4)
git.Repo(PROJECT_ROOT).git.add(json_path)
print(f"Added {json_path}")
return default_json


def get_model_list() -> list[dict]:
Expand Down Expand Up @@ -129,8 +138,7 @@ def get_last_commit_time(directory: str) -> datetime:
if commits:
last_commit = commits[0]
return last_commit.committed_datetime
else:
return datetime.fromtimestamp(0)
return datetime.fromtimestamp(0)


model_list = get_model_list()
Expand Down
1 change: 1 addition & 0 deletions .github/scripts/update_hacs_manifest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Update the manifest file."""

import json
import os
import sys
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
.vscode
__pycache__
venv/

.coverage
cov.xml
Expand Down

0 comments on commit e235d28

Please sign in to comment.