From 442fbc505ad2e5a82ad3eab6182fb509871cf8d7 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Sat, 31 Aug 2024 13:22:51 +0200 Subject: [PATCH] feat: compress apk to be under github file size limit --- psa_car_controller/psa/setup/apk_parser.py | 6 ++++-- psa_car_controller/psa/setup/app_decoder.py | 6 +++--- psa_car_controller/psa/setup/github.py | 11 ++++++++--- tests/test_unit.py | 5 ++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/psa_car_controller/psa/setup/apk_parser.py b/psa_car_controller/psa/setup/apk_parser.py index 83b263a..053e1bb 100644 --- a/psa_car_controller/psa/setup/apk_parser.py +++ b/psa_car_controller/psa/setup/apk_parser.py @@ -1,6 +1,6 @@ import json -import logging import os +import sys from androguard.core.apk import APK from cryptography.hazmat.backends import default_backend @@ -9,7 +9,9 @@ from cryptography.hazmat.primitives.serialization import pkcs12 from psa_car_controller.psa.constants import BRAND -logging.getLogger("androguard").setLevel(logging.ERROR) +from androguard.core.axml import logger as androguard_logger +androguard_logger.remove() +androguard_logger.add(sys.stderr, level="ERROR") class ApkParser: diff --git a/psa_car_controller/psa/setup/app_decoder.py b/psa_car_controller/psa/setup/app_decoder.py index 64b62ca..eaa495b 100755 --- a/psa_car_controller/psa/setup/app_decoder.py +++ b/psa_car_controller/psa/setup/app_decoder.py @@ -14,16 +14,16 @@ from psa_car_controller.psacc.application.charge_control import ChargeControl, C logger = logging.getLogger(__name__) -APP_VERSION = "1.48.1" -GITHUB_USER = "HansUweRempler" #"flobz" temporary replaced till flobz updated his own +APP_VERSION = "1.48.2" +GITHUB_USER = "flobz" GITHUB_REPO = "psa_apk" TIMEOUT_IN_S = 10 app = PSACarController() def get_content_from_apk(filename: str, country_code: str) -> ApkParser: + urlretrieve_from_github(GITHUB_USER, GITHUB_REPO, "", filename) apk_parser = ApkParser(filename, country_code) - urlretrieve_from_github(GITHUB_USER, GITHUB_REPO, "", apk_parser.filename) apk_parser.retrieve_content_from_apk() return apk_parser diff --git a/psa_car_controller/psa/setup/github.py b/psa_car_controller/psa/setup/github.py index 86ad39f..b4b9829 100644 --- a/psa_car_controller/psa/setup/github.py +++ b/psa_car_controller/psa/setup/github.py @@ -1,5 +1,7 @@ +import bz2 import logging from hashlib import sha1 +from os import path import requests @@ -36,9 +38,10 @@ def github_file_need_to_be_downloaded(user, repo, directory, filename): def urlretrieve_from_github(user, repo, directory, filename, branch="main"): - if github_file_need_to_be_downloaded(user, repo, directory, filename): - with open(filename, 'wb') as f: - url = "https://github.com/{}/{}/raw/{}/{}{}".format(user, repo, branch, directory, filename) + archive_name = filename + ".bz2" + if github_file_need_to_be_downloaded(user, repo, directory, archive_name) or not path.isfile(filename): + with open(archive_name, 'wb') as f: + url = "https://github.com/{}/{}/raw/{}/{}{}".format(user, repo, branch, directory, archive_name) r = requests.get(url, headers={ "Accept": "application/vnd.github.VERSION.raw" @@ -50,3 +53,5 @@ def urlretrieve_from_github(user, repo, directory, filename, branch="main"): r.raise_for_status() for chunk in r.iter_content(1024): f.write(chunk) + with bz2.BZ2File(archive_name, 'rb') as file, open(filename, 'wb') as out_file: + out_file.write(file.read()) diff --git a/tests/test_unit.py b/tests/test_unit.py index 0faa495..0d8b41b 100644 --- a/tests/test_unit.py +++ b/tests/test_unit.py @@ -5,7 +5,6 @@ import unittest from datetime import datetime, timedelta from unittest.mock import MagicMock, patch -import pytz import reverse_geocode from dateutil.tz import tzutc from greenery.lego import parse, charclass @@ -337,10 +336,10 @@ class TestUnit(unittest.TestCase): except FileNotFoundError: pass assert get_content_from_apk(filename, "FR") - assert github_file_need_to_be_downloaded(GITHUB_USER, GITHUB_REPO, "", filename) is False + assert github_file_need_to_be_downloaded(GITHUB_USER, GITHUB_REPO, "", filename + ".bz2") is False def test_file_need_to_be_updated(self): - filename = "mypeugeot.apk" + filename = "mypeugeot.apk.bz2" with open(filename, "w") as f: f.write(" ") assert github_file_need_to_be_downloaded(GITHUB_USER, GITHUB_REPO, "", filename) is True