Skip to content

Commit 464424e

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

7 files changed

Lines changed: 117 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/app.py

Lines changed: 9 additions & 2 deletions
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...')
@@ -226,7 +234,6 @@ def download_archive(url: str, stream: Optional[bool] = False) -> str:
226234

227235
return filename
228236

229-
230237
def main():
231238
App.run_github_action()
232239

homebrew_releaser/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@
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) if os.getenv('INPUT_UPDATE_PYTHON_RESOURCES') != 'false' else False
56+
) # Must check for string `false` since GitHub Actions passes the bool as a string

homebrew_releaser/formula.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,34 @@ def _generate_class_name(repo_name: str) -> str:
265265
repo_name.title(),
266266
),
267267
)
268+
269+
@staticmethod
270+
def update_python_resources(formula_path: str, formula_name: str) -> None:
271+
"""Runs brew update-python-resources on the formula to add Python resources.
272+
273+
Args:
274+
formula_path: The path to the formula file
275+
formula_name: The name of the formula
276+
"""
277+
logger = woodchips.get(LOGGER_NAME)
278+
279+
try:
280+
import subprocess
281+
282+
logger.info(f'Running brew update-python-resources for {formula_name}...')
283+
result = subprocess.run(
284+
['brew', 'update-python-resources', formula_path],
285+
capture_output=True,
286+
text=True,
287+
check=True
288+
)
289+
290+
logger.info(f'Successfully updated Python resources for {formula_name}')
291+
logger.debug(f'brew update-python-resources output: {result.stdout}')
292+
except subprocess.CalledProcessError as e:
293+
logger.error(
294+
"Failed to update Python resources: %s\nCommand output: %s\nCommand error: %s",
295+
e, e.stdout, e.stderr
296+
)
297+
except Exception as e:
298+
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.formula.Formula.update_python_resources')
151+
@patch('homebrew_releaser.checksum.Checksum.get_checksum', return_value=('123', 'mock-repo'))
152+
@patch('homebrew_releaser.app.App.download_archive')
153+
@patch('homebrew_releaser.utils.Utils.make_github_get_request')
154+
@patch('homebrew_releaser.app.App.check_required_env_variables')
155+
def test_run_github_action_update_python_resources(
156+
mock_check_env_variables,
157+
mock_make_github_get_request,
158+
mock_download_archive,
159+
mock_get_checksum,
160+
mock_generate_formula,
161+
mock_write_file,
162+
mock_push_formula,
163+
mock_commit_formula,
164+
mock_add_formula,
165+
mock_setup_git,
166+
mock_logger,
167+
mock_upload_checksum_file,
168+
mock_update_python_resources,
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,20 @@ def test_generate_class_name(repo_name, expected_class_name):
880880
class_name = Formula._generate_class_name(repo_name)
881881

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

0 commit comments

Comments
 (0)