Created
September 7, 2022 05:02
-
-
Save Shahaed/a37c62bd89bf85c649fc0e72fd4d6d94 to your computer and use it in GitHub Desktop.
Script to link homebrew packages to the GitHub actions tool cache. Run before each job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import platform | |
import shutil | |
import glob | |
import os | |
WORK_TOOL_CACHE_DIR = '/Users/runner/actions-runner/_work/_tool' | |
ARCHITECTURES = ['arm64', 'x64', 'x86'] | |
TOOL_CACHE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
def link_homebrew_tool_to_hosted_tool_cache(dir, formula_dir): | |
shutil.rmtree(dir, ignore_errors=True) | |
os.makedirs(dir, exist_ok=True) | |
for arch in ARCHITECTURES: | |
complete = os.path.join(dir, f'{arch}.complete') | |
open(complete, 'x') if not os.path.isfile(complete) else 0 | |
os.symlink(formula_dir, os.path.join(dir, arch), target_is_directory=True) | |
return | |
def link_homebrew_tool_to_work_tool_cache(work_tool_dir, formula_dir): | |
shutil.rmtree(work_tool_dir, ignore_errors=True) | |
os.makedirs(work_tool_dir, exist_ok=True) | |
for arch in ARCHITECTURES: | |
complete = os.path.join(work_tool_dir, f'{arch}.complete') | |
open(complete, 'x') if not os.path.isfile(complete) else 0 | |
os.symlink(formula_dir, os.path.join(work_tool_dir, arch), target_is_directory=True) | |
return | |
homebrew_root = '/opt/homebrew' if platform.processor() == 'arm' else '/usr/local' | |
homebrew_cellar = os.path.join(homebrew_root, 'Cellar') | |
for formula in glob.glob(os.path.join(homebrew_cellar, '*/*/')): | |
rel_dir = os.path.relpath(formula, homebrew_cellar) | |
# Homebrew has some formula appended with '_X' indicating a homebrew specific change. | |
# This is an ugly way of getting rid of that since tool-cache won't work with it. This probs breaks fairly easily | |
rel_dir = rel_dir.split('_')[0] | |
tool_dir = os.path.join(TOOL_CACHE_DIR, rel_dir) | |
work_tool_dir_formula = os.path.join(WORK_TOOL_CACHE_DIR, rel_dir) | |
link_homebrew_tool_to_hosted_tool_cache(tool_dir, formula) | |
link_homebrew_tool_to_work_tool_cache(work_tool_dir_formula, formula) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment