From 4300998b3cba1e8123e24b6bc0c2797ef50091de Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Thu, 19 Jan 2023 16:58:09 -0800 Subject: [PATCH 1/2] Read pending updates from the cache preference if present --- luggage/luggage.make | 11 ++++-- .../sal/checkin_modules/apple_sus_checkin.py | 37 ++++++++++++++++++- requirements.txt | 2 +- sal_python_pkg/sal/version.py | 2 +- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/luggage/luggage.make b/luggage/luggage.make index 105dad8..431af2a 100644 --- a/luggage/luggage.make +++ b/luggage/luggage.make @@ -340,17 +340,22 @@ ${PACKAGE_PLIST}: ${PLIST_PATH} define PYTHON_PLISTER import plistlib -component = plistlib.readPlist('${SCRATCH_D}/luggage.pkg.component.plist') +file_name = '${SCRATCH_D}/luggage.pkg.component.plist' +with open(file_name, 'rb') as infile: + component = plistlib.load(infile) + for payload in component: if payload.get('BundleIsRelocatable'): payload['BundleIsRelocatable'] = False -plistlib.writePlist(component, '${SCRATCH_D}/luggage.pkg.component.plist') + +with open(file_name, 'wb') as outfile: + plistlib.dump(component, outfile) endef export PYTHON_PLISTER kill_relocate: - @-sudo /usr/bin/python -c "$${PYTHON_PLISTER}" + @-sudo python3 -c "$${PYTHON_PLISTER}" # Target directory rules diff --git a/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py b/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py index a778d5d..e38cf53 100755 --- a/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py +++ b/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py @@ -2,6 +2,7 @@ import datetime +import os import pathlib import platform import plistlib @@ -12,7 +13,7 @@ import sal -__version__ = "1.1.0" +__version__ = "1.2.0" def main(): @@ -105,6 +106,11 @@ def _get_log_time(line): def get_pending(): + if os.path.exists("/Library/Preferences/com.apple.SoftwareUpdate.plist"): + pending_items = get_pending_updates_from_preferences() + if pending_items != None: + return pending_items + pending_items = {} cmd = ["softwareupdate", "-l", "--no-scan"] try: @@ -174,11 +180,40 @@ def get_pending(): if "recommended" in m.group("recommended") else "FALSE", "action": _bracket_cleanup(m, "action"), + "type": "Apple SUS Install" }, } for m in rexp.finditer(output) } +def get_pending_updates_from_preferences(): + pending = {} + try: + pref = plistlib.loads( + pathlib.Path("/Library/Preferences/com.apple.SoftwareUpdate.plist").read_bytes() + ) + except (IOError, plistlib.InvalidFileException): + return None + + recommended_updates = pref.get("RecommendedUpdates", None) + if not recommended_updates: + return pending + + # Convert local time to UTC time represented as a ISO 8601 str. + now = datetime.datetime.now().astimezone(datetime.timezone.utc).isoformat() + for update in recommended_updates: + name = update.get("Display Name") + version = update.get("Display Version") + item = {} + item["date_managed"] = now + item["status"] = "PENDING" + item["data"] = { + "version": version, + "recommended": "TRUE", + "type": "Apple SUS Install" + } + pending[name] = item + return pending def _bracket_cleanup(match, key): """Strip out [ and ] and uppercase SUS output""" diff --git a/requirements.txt b/requirements.txt index 9af859b..864a170 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ sal_python_pkg/ -pyobjc==7.2 +pyobjc==8.5 #urllib3==1.26.5 requests==2.26.0 MacSesh==0.3.0 diff --git a/sal_python_pkg/sal/version.py b/sal_python_pkg/sal/version.py index aef46ac..111dc91 100644 --- a/sal_python_pkg/sal/version.py +++ b/sal_python_pkg/sal/version.py @@ -1 +1 @@ -__version__ = "4.2.1" +__version__ = "4.3.0" From 91f8af7d1cb9e4b3f3d5b1074789f62b3bc0e9bc Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Thu, 19 Jan 2023 17:00:51 -0800 Subject: [PATCH 2/2] black --- .../usr/local/sal/checkin_modules/apple_sus_checkin.py | 10 +++++++--- .../usr/local/sal/checkin_modules/machine_checkin.py | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py b/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py index e38cf53..ab350b2 100755 --- a/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py +++ b/payload/usr/local/sal/checkin_modules/apple_sus_checkin.py @@ -180,17 +180,20 @@ def get_pending(): if "recommended" in m.group("recommended") else "FALSE", "action": _bracket_cleanup(m, "action"), - "type": "Apple SUS Install" + "type": "Apple SUS Install", }, } for m in rexp.finditer(output) } + def get_pending_updates_from_preferences(): pending = {} try: pref = plistlib.loads( - pathlib.Path("/Library/Preferences/com.apple.SoftwareUpdate.plist").read_bytes() + pathlib.Path( + "/Library/Preferences/com.apple.SoftwareUpdate.plist" + ).read_bytes() ) except (IOError, plistlib.InvalidFileException): return None @@ -210,11 +213,12 @@ def get_pending_updates_from_preferences(): item["data"] = { "version": version, "recommended": "TRUE", - "type": "Apple SUS Install" + "type": "Apple SUS Install", } pending[name] = item return pending + def _bracket_cleanup(match, key): """Strip out [ and ] and uppercase SUS output""" return re.sub(r"[\[\]]", "", match.group(key) or "").upper() diff --git a/payload/usr/local/sal/checkin_modules/machine_checkin.py b/payload/usr/local/sal/checkin_modules/machine_checkin.py index 7c397a3..b2d1571 100755 --- a/payload/usr/local/sal/checkin_modules/machine_checkin.py +++ b/payload/usr/local/sal/checkin_modules/machine_checkin.py @@ -127,7 +127,9 @@ def get_friendly_model(serial, udid): data[0] .get("product-name") .decode("ascii", "ignore") - .strip().strip('\x00').strip() + .strip() + .strip("\x00") + .strip() ) except: pass