-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathbrew_install_from_source.py
executable file
·95 lines (80 loc) · 3.23 KB
/
brew_install_from_source.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2018 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import hashlib
import os
import shutil
import subprocess
import tempfile
import urllib.request
def main():
try:
temp_dir = tempfile.mkdtemp()
compressed_snapcraft_source = download_snapcraft_source(temp_dir)
compressed_snapcraft_sha256 = sha256_checksum(compressed_snapcraft_source)
brew_formula_path = os.path.join(temp_dir, "snapcraft_legacy.rb")
download_brew_formula(brew_formula_path)
patched_dir = os.path.join(temp_dir, "patched")
os.mkdir(patched_dir)
brew_formula_from_source_path = os.path.join(patched_dir, "snapcraft_legacy.rb")
patch_brew_formula_source(
brew_formula_path,
brew_formula_from_source_path,
compressed_snapcraft_source,
compressed_snapcraft_sha256,
)
install_brew_formula(brew_formula_from_source_path)
finally:
shutil.rmtree(temp_dir)
def download_snapcraft_source(dest_dir):
dest_file = os.path.join(dest_dir, "snapcraft-0.1.tar.gz")
branch_source = "https://github.com/canonical/snapcraft/archive/main.tar.gz"
print("Downloading branch source from {}".format(branch_source))
urllib.request.urlretrieve(branch_source, dest_file) # noqa S310
return dest_file
def sha256_checksum(filename):
sha256 = hashlib.sha256()
with open(filename, "rb") as f:
for block in iter(lambda: f.read(65536), b""):
sha256.update(block)
return sha256.hexdigest()
def download_brew_formula(destination_path):
brew_formula_url = (
"https://raw.githubusercontent.com/Homebrew/homebrew-core/master/"
"Formula/snapcraft.rb"
)
urllib.request.urlretrieve(brew_formula_url, destination_path) # noqa S310
def patch_brew_formula_source(
original_formula_path,
destination_formula_path,
compressed_source,
compressed_sha256,
):
with open(original_formula_path, "r") as original_file:
with open(destination_formula_path, "w") as destination_file:
for line in original_file:
if line.startswith(" url "):
destination_file.write(
' url "file://{}"\n'.format(compressed_source)
)
elif line.startswith(" sha256 "):
destination_file.write(' sha256 "{}"\n'.format(compressed_sha256))
else:
destination_file.write(line)
def install_brew_formula(formula_path):
subprocess.check_call(["brew", "install", "--build-from-source", formula_path])
if __name__ == "__main__":
main()