| 1 |
#!/usr/bin/env python3 |
| 2 |
# ========================================================================== |
| 3 |
# ____ _ _ _ _____ _ |
| 4 |
# | __ ) _ _(_) | __| | |_ _|__ ___ | |___ |
| 5 |
# | _ \| | | | | |/ _` |_____| |/ _ \ / _ \| / __| |
| 6 |
# | |_) | |_| | | | (_| |_____| | (_) | (_) | \__ \ |
| 7 |
# |____/ \__,_|_|_|\__,_| |_|\___/ \___/|_|___/ |
| 8 |
# |
| 9 |
# --- Build-Tools --- |
| 10 |
# https://www.nntb.no/~dreibh/system-tools/ |
| 11 |
# ========================================================================== |
| 12 |
# |
| 13 |
# GitHub Actions Scripts |
| 14 |
# Copyright (C) 2018-2026 by Thomas Dreibholz |
| 15 |
# |
| 16 |
# This program is free software: you can redistribute it and/or modify |
| 17 |
# it under the terms of the GNU General Public License as published by |
| 18 |
# the Free Software Foundation, either version 3 of the License, or |
| 19 |
# (at your option) any later version. |
| 20 |
# |
| 21 |
# This program is distributed in the hope that it will be useful, |
| 22 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
# GNU General Public License for more details. |
| 25 |
# |
| 26 |
# You should have received a copy of the GNU General Public License |
| 27 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
# |
| 29 |
# Contact: [email protected] |
| 30 |
|
| 31 |
import glob |
| 32 |
import os |
| 33 |
import re |
| 34 |
import subprocess |
| 35 |
import sys |
| 36 |
|
| 37 |
if sys.version_info < (3, 9): |
| 38 |
sys.stderr.write('ERROR: ' + sys.argv[0] + ' requires Python 3.9 or later!\n') |
| 39 |
sys.exit(1) |
| 40 |
|
| 41 |
from typing import Final, TextIO |
| 42 |
|
| 43 |
try: |
| 44 |
import distro |
| 45 |
except ImportError: |
| 46 |
sys.stderr.write('ERROR: ' + sys.argv[0] + ' requires the Python distro package!\n') |
| 47 |
sys.exit(1) |
| 48 |
|
| 49 |
|
| 50 |
# ###### Extract Deb file dependencies ###################################### |
| 51 |
debDepLine : Final[re.Pattern[str]] = re.compile(r'^(.*:[ \t]*)(.*)$') |
| 52 |
debDepItem : Final[re.Pattern[str]] = re.compile(r'^([a-zA-Z0-9-+\.]+)[\s]*(|\|.*|\(.*)[\s]*$') |
| 53 |
def extractDebDependencies(line : str, |
| 54 |
system : str) -> list[str]: |
| 55 |
|
| 56 |
dependencies : list[str] = [] |
| 57 |
distribution : Final[str] = distro.codename() |
| 58 |
|
| 59 |
# Remove "build-depends:", etc.: |
| 60 |
match : re.Match[str] | None = debDepLine.match(line) |
| 61 |
if match is not None: |
| 62 |
line = match.group(2) |
| 63 |
line = line.strip() |
| 64 |
|
| 65 |
# Split into segments |
| 66 |
for l in line.split(','): |
| 67 |
l = l.strip() |
| 68 |
match= debDepItem.match(l) |
| 69 |
if match is not None: |
| 70 |
dependency = l |
| 71 |
|
| 72 |
# ------ Ugly work-around for cmake -------------------------------- |
| 73 |
# We need cmake >= 3.0! |
| 74 |
if ((match.group(1) == 'cmake') or (match.group(1) == 'cmake3')): |
| 75 |
if ((system == 'debian') or (system == 'ubuntu')): |
| 76 |
try: |
| 77 |
if distribution in [ 'trusty' ]: |
| 78 |
dependency = 'cmake3' |
| 79 |
except: |
| 80 |
pass |
| 81 |
|
| 82 |
dependencies.append(dependency) |
| 83 |
|
| 84 |
return dependencies |
| 85 |
|
| 86 |
|
| 87 |
# ###### Extract RPM spec dependencies ###################################### |
| 88 |
rpmDepLine : Final[re.Pattern[str]] = re.compile(r'^(.*:[ \t]*)(.*)$') |
| 89 |
rpmDepItem : Final[re.Pattern[str]] = re.compile(r'^([a-zA-Z0-9-+\.]+)[\s]*(|or[ \t]*.*|\(.*)[\s]*$') |
| 90 |
def extractRPMDependencies(line : str, |
| 91 |
system : str) -> list[str]: |
| 92 |
|
| 93 |
dependencies : list[str] = [] |
| 94 |
distribution : Final[str] = distro.codename() |
| 95 |
|
| 96 |
# Remove "build-depends:", etc.: |
| 97 |
match : re.Match[str] | None = rpmDepLine.match(line) |
| 98 |
if match is not None: |
| 99 |
dependency = match.group(2).strip() |
| 100 |
dependencies.append(dependency) |
| 101 |
|
| 102 |
return dependencies |
| 103 |
|
| 104 |
|
| 105 |
# ###### Main program ####################################################### |
| 106 |
|
| 107 |
# ====== Check arguments ==================================================== |
| 108 |
system : str = '' |
| 109 |
runInstall : bool = False |
| 110 |
i = 1 |
| 111 |
while i < len(sys.argv): |
| 112 |
if sys.argv[i] == '-s' or sys.argv[i] == '--system': |
| 113 |
if i + 1 < len(sys.argv): |
| 114 |
system = sys.argv[i + 1] |
| 115 |
i = i + 1 |
| 116 |
else: |
| 117 |
sys.stderr.write('ERROR: Invalid system setting!\n') |
| 118 |
sys.exit(1) |
| 119 |
elif sys.argv[i] == '-i' or sys.argv[i] == '--install': |
| 120 |
runInstall = True |
| 121 |
elif sys.argv[i] == '-h' or sys.argv[i] == '--help': |
| 122 |
sys.stderr.write('Usage: ' + sys.argv[0] + ' [-h|--help] [-s|--system debian|ubuntu|fedora|freebsd|auto] [-i|--install]\n') |
| 123 |
sys.exit(0) |
| 124 |
else: |
| 125 |
sys.stderr.write('ERROR: Bad parameter ' + sys.argv[i] + '!\n') |
| 126 |
sys.exit(1) |
| 127 |
i = i + 1 |
| 128 |
|
| 129 |
if system == '': |
| 130 |
with open("/etc/os-release") as osReleaseFile: |
| 131 |
osRelease = { } |
| 132 |
for line in osReleaseFile: |
| 133 |
key, value = line.rstrip().split("=") |
| 134 |
osRelease[key] = value.strip('"') |
| 135 |
system = osRelease['ID'] |
| 136 |
|
| 137 |
# ====== Debian/Ubuntu ====================================================== |
| 138 |
dependencies : list[str] = [ ] |
| 139 |
if ((system == 'debian') or (system == 'ubuntu')): |
| 140 |
if os.path.exists('debian/control'): |
| 141 |
with open('debian/control', 'r', encoding='utf-8') as fp: |
| 142 |
inside = False |
| 143 |
for line in fp: |
| 144 |
if not line: |
| 145 |
break |
| 146 |
line_lower = line.lower() |
| 147 |
if inside: |
| 148 |
if line.startswith((' ', "\t")): |
| 149 |
dependencies = dependencies + extractDebDependencies(line, system) |
| 150 |
continue |
| 151 |
elif line.startswith('#'): |
| 152 |
continue |
| 153 |
inside = False |
| 154 |
if line_lower.startswith(('build-depends:', 'build-depends-indep:')): |
| 155 |
dependencies = dependencies + extractDebDependencies(line, system) |
| 156 |
inside = True |
| 157 |
|
| 158 |
aptCall = [ 'apt-get', 'satisfy', '-qy' ] |
| 159 |
i=0 |
| 160 |
for dependency in sorted(set(dependencies)): |
| 161 |
if i > 0: |
| 162 |
sys.stdout.write(', ') |
| 163 |
sys.stdout.write(dependency) |
| 164 |
aptCall.append(dependency) |
| 165 |
i = i + 1 |
| 166 |
sys.stdout.write('\n') |
| 167 |
|
| 168 |
if runInstall == True: |
| 169 |
subprocess.call(aptCall, |
| 170 |
env = { 'DEBIAN_FRONTEND': 'noninteractive' }) |
| 171 |
|
| 172 |
else: |
| 173 |
sys.stderr.write('ERROR: Unable to locate Debian control file!\n') |
| 174 |
sys.exit(1) |
| 175 |
|
| 176 |
|
| 177 |
# ====== Fedora ============================================================= |
| 178 |
elif system == 'fedora': |
| 179 |
specFiles = glob.glob('rpm/*.spec') |
| 180 |
if len(specFiles) == 1: |
| 181 |
with open(specFiles[0], 'r', encoding='utf-8') as fp: |
| 182 |
inside = False |
| 183 |
for line in fp: |
| 184 |
if not line: |
| 185 |
break |
| 186 |
line_lower = line.lower() |
| 187 |
if inside: |
| 188 |
if line.startswith('#'): |
| 189 |
continue |
| 190 |
inside = False |
| 191 |
if line_lower.startswith('buildrequires:'): |
| 192 |
dependencies = dependencies + extractRPMDependencies(line, system) |
| 193 |
inside = True |
| 194 |
|
| 195 |
dnfCall = [ 'dnf', 'install', '-y' ] |
| 196 |
i=0 |
| 197 |
for dependency in sorted(set(dependencies)): |
| 198 |
if i > 0: |
| 199 |
sys.stdout.write(', ') |
| 200 |
sys.stdout.write(dependency) |
| 201 |
dnfCall.append(dependency) |
| 202 |
i = i + 1 |
| 203 |
sys.stdout.write('\n') |
| 204 |
|
| 205 |
if runInstall == True: |
| 206 |
subprocess.call(dnfCall) |
| 207 |
|
| 208 |
else: |
| 209 |
sys.stderr.write('ERROR: Unable to locate RPM spec file!\n') |
| 210 |
sys.exit(1) |
| 211 |
|
| 212 |
|
| 213 |
# ====== FreeBSD ============================================================ |
| 214 |
elif system == 'freebsd': |
| 215 |
freeBSDMakefile = glob.glob('freebsd/*/Makefile') |
| 216 |
if len(freeBSDMakefile) == 1: |
| 217 |
freeBSDDirectory = os.path.dirname(freeBSDMakefile[0]) |
| 218 |
|
| 219 |
cmd = 'make build-depends-list && make run-depends-list' |
| 220 |
try: |
| 221 |
os.chdir(freeBSDDirectory) |
| 222 |
output = subprocess.check_output(cmd, shell=True) |
| 223 |
except Exception as e: |
| 224 |
sys.stderr.write('ERROR: Getting FreeBSD dependencies failed: ' + str(e) + '\n') |
| 225 |
sys.exit(1) |
| 226 |
|
| 227 |
if output is not None: |
| 228 |
ports = output.decode('utf-8').splitlines() |
| 229 |
for port in ports: |
| 230 |
if port[0:3] != '---': |
| 231 |
basename = os.path.basename(port) |
| 232 |
if basename == 'glib20': |
| 233 |
basename = 'glib' # May be there is a better solution here? |
| 234 |
dependencies.append(basename) |
| 235 |
|
| 236 |
for dependency in sorted(set(dependencies)): |
| 237 |
sys.stdout.write(dependency + ' ') |
| 238 |
sys.stdout.write('\n') |
| 239 |
|
| 240 |
if runInstall == True: |
| 241 |
subprocess.call([ 'pkg', 'install', '-y' ] + dependencies) |
| 242 |
|
| 243 |
else: |
| 244 |
sys.stderr.write('ERROR: Unable to locate FreeBSD port makefile!\n') |
| 245 |
sys.exit(1) |
| 246 |
|
| 247 |
|
| 248 |
# ====== Error ============================================================== |
| 249 |
else: |
| 250 |
sys.stderr.write('ERROR: Invalid system name "' + system + '"!\n') |
| 251 |
sys.exit(1) |