Refactor GPG key check and update SCons build tools.

Converted GPG key check logic into a more modular function `is_sunnypilot_developer` for better readability and maintainability. Updated `External Tools.xml` to include `--sunnypilot` flag in SCons build commands.
This commit is contained in:
DevTekVE
2024-07-13 08:24:32 +02:00
parent ec6f86efbf
commit c7932f8b73
3 changed files with 40 additions and 35 deletions
+2 -2
View File
@@ -2,7 +2,7 @@
<tool name="Poetry SCons Build Debug" showInMainMenu="false" showInEditor="false" showInProject="false" showInSearchPopup="false" disabled="false" useConsole="true" showConsoleOnStdOut="false" showConsoleOnStdErr="false" synchronizeAfterRun="true">
<exec>
<option name="COMMAND" value="bash" />
<option name="PARAMETERS" value="-c &quot;source .venv/bin/activate &amp;&amp; scons -u -j$(nproc) --compile_db --ccflags=\&quot;-fno-inline\&quot;&quot;" />
<option name="PARAMETERS" value="-c &quot;source .venv/bin/activate &amp;&amp; scons -u -j$(nproc) --compile_db --ccflags=\&quot;-fno-inline\&quot; --sunnypilot&quot;" />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
</exec>
</tool>
@@ -16,7 +16,7 @@
<tool name="Poetry SCons Build Release" showInMainMenu="false" showInEditor="false" showInProject="false" showInSearchPopup="false" disabled="false" useConsole="true" showConsoleOnStdOut="false" showConsoleOnStdErr="false" synchronizeAfterRun="true">
<exec>
<option name="COMMAND" value="bash" />
<option name="PARAMETERS" value="-c &quot;source .venv/bin/activate &amp;&amp; scons -u -j$(nproc) --compile_db&quot; " />
<option name="PARAMETERS" value="-c &quot;source .venv/bin/activate &amp;&amp; scons -u -j$(nproc) --compile_db --sunnypilot&quot; " />
<option name="WORKING_DIRECTORY" value="$ProjectFileDir$" />
</exec>
</tool>
+35 -30
View File
@@ -19,35 +19,38 @@ AGNOS = TICI
UBUNTU_FOCAL = int(subprocess.check_output('[ -f /etc/os-release ] && . /etc/os-release && [ "$ID" = "ubuntu" ] && [ "$VERSION_ID" = "20.04" ] && echo 1 || echo 0', shell=True, encoding='utf-8').rstrip())
Export('UBUNTU_FOCAL')
keys_dir = os.path.join(BASEDIR, ".git-crypt/keys/default/0")
def is_sunnypilot_developer():
"""Check if the current user is a SunnyPilot developer."""
def collect_required_gpg_key_ids(keys_dir):
try:
key_ids = [filename.split('.')[0] for filename in os.listdir(keys_dir) if filename.endswith(".gpg")]
print(f"Required GPG key IDs: {key_ids}")
return key_ids
except OSError as e:
print(f"Failed to read GPG key IDs from {keys_dir}. Error: {e}")
return []
def is_sunnypilot_key_available(required_gpg_key_ids):
for key_id in required_gpg_key_ids:
try:
result = subprocess.check_output(['gpg', '--list-keys', key_id], stderr=subprocess.STDOUT)
if key_id in result.decode():
print(f"GPG key {key_id} is available.")
return True
except subprocess.CalledProcessError as e:
print(f"Failed to list GPG key {key_id}. Error:", e.output.decode().strip())
return False
# Collect the required GPG key IDs from the filenames in the keys directory
required_gpg_key_ids = []
try:
for filename in os.listdir(keys_dir):
if filename.endswith(".gpg"):
required_gpg_key_ids.append(filename.split('.')[0])
print(f"Required GPG key IDs: {required_gpg_key_ids}")
except OSError as e:
print(f"Failed to read GPG key IDs from {keys_dir}. Error: {e}")
required_gpg_key_ids = []
keys_dir = os.path.join(BASEDIR, ".git-crypt/keys/default/0")
required_gpg_key_ids = collect_required_gpg_key_ids(keys_dir)
# Check for the specific GPG keys in the local keyring
SUNNYPILOT = False
for key_id in required_gpg_key_ids:
try:
result = subprocess.check_output(['gpg', '--list-keys', key_id], stderr=subprocess.STDOUT)
if key_id in result.decode():
SUNNYPILOT = True
print(f"GPG key {key_id} is available.")
break
except subprocess.CalledProcessError as e:
print(f"Failed to list GPG key {key_id}. Error:", e.output.decode().strip())
sunnypilot = is_sunnypilot_key_available(required_gpg_key_ids)
if not SUNNYPILOT:
print("None of the required GPG keys are available.")
print("SUNNYPILOT: ", SUNNYPILOT)
Export('SUNNYPILOT')
if not sunnypilot:
print("None of the required GPG keys are available.")
return sunnypilot
Decider('MD5-timestamp')
@@ -103,6 +106,12 @@ AddOption('--minimal',
dest='extras',
default=os.path.exists(File('#.lfsconfig').abspath), # minimal by default on release branch (where there's no LFS)
help='the minimum build to run openpilot. no tests, tools, etc.')
AddOption('--sunnypilot',
action='store_true',
dest='sunnypilot',
default=is_sunnypilot_developer(), # check if the current user is a SunnyPilot developer
help='Will make sure it builds SP ui and other SP specific things that are not public (encrypted sources)')
## Architecture name breakdown (arch)
## - larch64: linux tici aarch64
@@ -207,10 +216,6 @@ if arch != "Darwin":
cflags += ['-DSWAGLOG="\\"common/swaglog.h\\""']
cxxflags += ['-DSWAGLOG="\\"common/swaglog.h\\""']
if SUNNYPILOT:
cflags += ['-DSUNNYPILOT']
cxxflags += ['-DSUNNYPILOT']
ccflags_option = GetOption('ccflags')
if ccflags_option:
ccflags += ccflags_option.split(' ')
+3 -3
View File
@@ -1,6 +1,6 @@
import os
import json
Import('qt_env', 'arch', 'common', 'messaging', 'visionipc', 'transformations', 'UBUNTU_FOCAL', 'SUNNYPILOT')
Import('qt_env', 'arch', 'common', 'messaging', 'visionipc', 'transformations', 'UBUNTU_FOCAL')
base_libs = [common, messaging, visionipc, transformations,
'm', 'OpenCL', 'ssl', 'crypto', 'pthread'] + qt_env["LIBS"]
@@ -19,7 +19,7 @@ qt_env['CXXFLAGS'] += ["-Wno-deprecated-declarations"]
sp_widgets_src = []
sp_qt_src = []
if SUNNYPILOT:
if GetOption('sunnypilot'):
SConscript(['sunnypilot/SConscript'])
Import('sp_widgets_src', 'sp_qt_src')
@@ -30,7 +30,7 @@ widgets_src = ["ui.cc", "qt/widgets/input.cc", "qt/widgets/wifi.cc",
"qt/widgets/scrollview.cc", "qt/widgets/cameraview.cc", "#third_party/qrcode/QrCode.cc",
"qt/request_repeater.cc", "qt/qt_window.cc", "qt/network/networking.cc", "qt/network/wifi_manager.cc"] + sp_widgets_src
qt_env['CPPDEFINES'] = ["SUNNYPILOT"] if SUNNYPILOT else []
qt_env['CPPDEFINES'] = ["SUNNYPILOT"] if GetOption('sunnypilot') else []
if maps:
base_libs += ['QMapLibre']
widgets_src += ["qt/maps/map_helpers.cc", "qt/maps/map_settings.cc", "qt/maps/map.cc", "qt/maps/map_panel.cc",