-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
115 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
Bundle Homebrew dependencies from a installed with Homebrew Bundle | ||
""" | ||
|
||
import subprocess | ||
import shutil | ||
import os | ||
|
||
from pathlib import Path | ||
|
||
import macho | ||
|
||
SRC_PREFIX = Path(subprocess.run(["brew", "--prefix"], stdout=subprocess.PIPE, check=True).stdout.decode().strip()) | ||
DST_PREFIX = Path("usr") | ||
|
||
def change_lib_id(lib, *, id): | ||
subprocess.run(["install_name_tool", "-id", id, lib], check=True) | ||
subprocess.run(["codesign", "--force", "--preserve-metadata=entitlements,requirements,flags,runtime", "--sign", "-", lib], check=True) | ||
|
||
def copy_installed_formula(formula): | ||
print(f"Bundling {formula}") | ||
|
||
src_prefix = SRC_PREFIX / "opt" / formula.name | ||
dst_prefix = DST_PREFIX / "opt" / formula.name | ||
|
||
src_cellar = SRC_PREFIX / "Cellar" / formula.name | ||
dst_cellar = DST_PREFIX / "Cellar" / formula.name | ||
|
||
shutil.copytree(src_cellar, dst_cellar) | ||
|
||
dst_prefix.parent.mkdir(exist_ok=True) | ||
shutil.copy(src_prefix, dst_prefix, follow_symlinks=False) | ||
|
||
# Replace library IDs and references to other libraries (install_names) | ||
for file in dst_cellar.rglob("*"): | ||
if not file.is_file(): | ||
continue | ||
if (file.suffix == ".dylib" or file.suffix == ".so"): | ||
macho.change_lib_id(file, id=dst_prefix.absolute() / Path(*file.relative_to(dst_cellar).parts[1:])) | ||
if (file.suffix == "" or file.suffix == ".bin" or file.suffix == ".dylib" or file.suffix == ".so"): | ||
for install_name in macho.get_install_names(file): | ||
if install_name.is_absolute() and install_name.is_relative_to(SRC_PREFIX): | ||
if install_name.is_relative_to(SRC_PREFIX): | ||
new_install_name = DST_PREFIX.absolute() / install_name.relative_to(SRC_PREFIX) | ||
relative_install_name = Path("@loader_path") / os.path.relpath(new_install_name, start=file.parent) | ||
macho.change_install_name(file, install_name, relative_install_name) | ||
|
||
def get_deps(*, recursive=True): | ||
if not recursive: | ||
return {Path(formula) for formula in subprocess.run(["brew", "bundle", "list"], stdout=subprocess.PIPE, check=True).stdout.decode().splitlines()} | ||
|
||
deps = get_deps(recursive=False) | ||
for dep in list(deps): | ||
recursive_deps = {Path(formula) for formula in subprocess.run(["brew", "deps", dep], stdout=subprocess.PIPE, check=True).stdout.decode().splitlines()} | ||
deps.update(recursive_deps) | ||
|
||
return deps | ||
|
||
|
||
for formula in get_deps(): | ||
copy_installed_formula(formula) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import subprocess | ||
|
||
from pathlib import Path | ||
|
||
def change_lib_id(lib, *, id): | ||
subprocess.run(["install_name_tool", "-id", id, lib], check=True) | ||
subprocess.run(["codesign", "--force", "--preserve-metadata=entitlements,requirements,flags,runtime", "--sign", "-", lib], check=True) | ||
|
||
def get_install_names(file): | ||
otool_stdout = subprocess.run(["otool", "-L", file], stdout=subprocess.PIPE, check=True).stdout.decode() | ||
install_names = [Path(line.split(" (compatibility version ")[0].strip()) for line in otool_stdout.splitlines()[1:]] | ||
return install_names | ||
|
||
def change_install_name(file, old_install_name, new_install_name): | ||
subprocess.run(["install_name_tool", "-change", old_install_name, new_install_name, file], check=True) |
This file contains 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