Skip to content

Commit e8fa5f5

Browse files
committed
Add update_python_resources parameter to build python resources
1 parent ae86772 commit e8fa5f5

8 files changed

Lines changed: 128 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ jobs:
136136
# Logs debugging info to console.
137137
# Default is shown - boolean
138138
debug: false
139+
140+
# Run 'brew update-python-resources' on the formula to add Python resources.
141+
# Default is shown - boolean
142+
update_python_resources: false
139143
```
140144
141145
## Development

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ inputs:
6767
debug:
6868
description: "Logs debugging info to console."
6969
required: false
70+
update_python_resources:
71+
description: "Run 'brew update-python-resources' on the formula to add Python resources."
72+
required: false
7073
runs:
7174
using: "docker"
7275
image: "Dockerfile"
@@ -91,3 +94,4 @@ runs:
9194
- ${{ inputs.update_readme_table }}
9295
- ${{ inputs.skip_commit }}
9396
- ${{ inputs.debug }}
97+
- ${{ inputs.update_python_resources }}

homebrew_releaser/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.0.3'
1+
__version__ = '2.1.0'

homebrew_releaser/app.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
TARGET_DARWIN_ARM64,
2222
TARGET_LINUX_AMD64,
2323
TARGET_LINUX_ARM64,
24+
UPDATE_PYTHON_RESOURCES,
2425
VERSION,
2526
)
2627
from homebrew_releaser.formula import Formula
@@ -162,7 +163,14 @@ def run_github_action():
162163
version_no_v if VERSION else None,
163164
)
164165

165-
Utils.write_file(os.path.join(HOMEBREW_TAP, FORMULA_FOLDER, f'{repository["name"]}.rb'), template, 'w')
166+
formula_path = os.path.join(HOMEBREW_TAP, FORMULA_FOLDER, f'{repository["name"]}.rb')
167+
Utils.write_file(formula_path, template, 'w')
168+
169+
if UPDATE_PYTHON_RESOURCES:
170+
logger.info('Attempting to update Python resources in the formula...')
171+
Formula.update_python_resources(formula_path, repository["name"])
172+
else:
173+
logger.debug('Skipping update to Python resources.')
166174

167175
if UPDATE_README_TABLE:
168176
logger.info('Attempting to update the README\'s project table...')

homebrew_releaser/constants.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,10 @@
4949
TARGET_LINUX_ARM64 = (
5050
os.getenv('INPUT_TARGET_LINUX_ARM64', False) if os.getenv('INPUT_TARGET_LINUX_ARM64') != 'false' else False
5151
) # Must check for string `false` since GitHub Actions passes the bool as a string
52+
53+
# Python resources
54+
UPDATE_PYTHON_RESOURCES = (
55+
os.getenv("INPUT_UPDATE_PYTHON_RESOURCES", False)
56+
if os.getenv("INPUT_UPDATE_PYTHON_RESOURCES") != "false"
57+
else False
58+
)

homebrew_releaser/formula.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import re
2+
import shutil
3+
import subprocess # nosec B404
24
import textwrap
35
from typing import (
46
Any,
@@ -265,3 +267,37 @@ def _generate_class_name(repo_name: str) -> str:
265267
repo_name.title(),
266268
),
267269
)
270+
271+
@staticmethod
272+
def update_python_resources(formula_path: str, formula_name: str) -> None:
273+
"""Runs brew update-python-resources on the formula to add Python resources.
274+
275+
Args:
276+
formula_path: The path to the formula file
277+
formula_name: The name of the formula
278+
"""
279+
logger = woodchips.get(LOGGER_NAME)
280+
281+
brew_path = shutil.which('brew')
282+
if not brew_path:
283+
logger.error("brew not found in PATH")
284+
return
285+
286+
try:
287+
logger.info(f'Running brew update-python-resources for {formula_name}...')
288+
result = subprocess.run( # nosec B603
289+
[brew_path, 'update-python-resources', formula_path],
290+
capture_output=True,
291+
text=True,
292+
check=True,
293+
shell=False, # Skip shell code to make it more secure
294+
)
295+
296+
logger.info(f'Successfully updated Python resources for {formula_name}')
297+
logger.debug(f'brew update-python-resources output: {result.stdout}')
298+
except subprocess.CalledProcessError as e:
299+
logger.error(
300+
"Failed to update Python resources: %s\nCommand output: %s\nCommand error: %s", e, e.stdout, e.stderr
301+
)
302+
except Exception as e:
303+
logger.error(f'An error occurred while updating Python resources: {e}')

test/unit/test_app.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,53 @@ def test_run_github_action_update_readme(
137137
mock_update_readme.assert_called_once()
138138

139139

140+
@patch('homebrew_releaser.app.HOMEBREW_TAP', '123')
141+
@patch('homebrew_releaser.app.UPDATE_PYTHON_RESOURCES', True)
142+
@patch('homebrew_releaser.checksum.Checksum.upload_checksum_file')
143+
@patch('woodchips.get')
144+
@patch('homebrew_releaser.git.Git.setup')
145+
@patch('homebrew_releaser.git.Git.add')
146+
@patch('homebrew_releaser.git.Git.commit')
147+
@patch('homebrew_releaser.git.Git.push')
148+
@patch('homebrew_releaser.utils.Utils.write_file')
149+
@patch('homebrew_releaser.formula.Formula.generate_formula_data')
150+
@patch('homebrew_releaser.checksum.Checksum.get_checksum', return_value=('123', 'mock-repo'))
151+
@patch('homebrew_releaser.app.App.download_archive')
152+
@patch('homebrew_releaser.utils.Utils.make_github_get_request')
153+
@patch('homebrew_releaser.app.App.check_required_env_variables')
154+
@patch('homebrew_releaser.formula.Formula.update_python_resources')
155+
def test_run_github_action_update_python_resources(
156+
mock_update_python_resources,
157+
mock_check_env_variables,
158+
mock_make_github_get_request,
159+
mock_download_archive,
160+
mock_get_checksum,
161+
mock_generate_formula,
162+
mock_write_file,
163+
mock_push_formula,
164+
mock_commit_formula,
165+
mock_add_formula,
166+
mock_setup_git,
167+
mock_logger,
168+
mock_upload_checksum_file,
169+
):
170+
App.run_github_action()
171+
172+
# TODO: Assert these `called_with` eventually
173+
mock_logger.assert_called()
174+
mock_check_env_variables.assert_called_once()
175+
assert mock_make_github_get_request.call_count == 2
176+
mock_download_archive.call_count == 2
177+
mock_get_checksum.call_count == 2
178+
mock_generate_formula.assert_called_once()
179+
mock_write_file.call_count == 2
180+
mock_setup_git.assert_called_once()
181+
mock_add_formula.assert_called_once()
182+
mock_commit_formula.assert_called_once()
183+
mock_push_formula.assert_called_once()
184+
mock_update_python_resources.assert_called_once()
185+
186+
140187
@patch('homebrew_releaser.app.HOMEBREW_TAP', '123')
141188
@patch('homebrew_releaser.app.TARGET_DARWIN_AMD64', True)
142189
@patch('homebrew_releaser.app.TARGET_DARWIN_ARM64', True)

test/unit/test_formula.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import inspect
22
import os
3+
import shutil
34
from unittest.mock import patch
45

56
import pytest
@@ -880,3 +881,22 @@ def test_generate_class_name(repo_name, expected_class_name):
880881
class_name = Formula._generate_class_name(repo_name)
881882

882883
assert class_name == expected_class_name
884+
885+
886+
@patch('woodchips.get')
887+
@patch('subprocess.run', side_effect=Exception('Test error'))
888+
def test_update_python_resources_error(mock_subprocess_run, mock_logger):
889+
formula_path = '/path/to/formula.rb'
890+
formula_name = 'test-formula'
891+
892+
Formula.update_python_resources(formula_path, formula_name)
893+
894+
brew_path = shutil.which('brew')
895+
mock_subprocess_run.assert_called_once_with(
896+
[brew_path, 'update-python-resources', formula_path],
897+
capture_output=True,
898+
text=True,
899+
check=True,
900+
shell=False,
901+
)
902+
mock_logger.assert_called()

0 commit comments

Comments
 (0)