From f7fdfaf8640fc0e1efccfdd5b5f8f6a42419735e Mon Sep 17 00:00:00 2001 From: Han Sol Jin Date: Mon, 27 May 2024 14:48:13 -0700 Subject: [PATCH 1/5] gen_mirror_json: fix PEP8 errors - Import `re` - No bare `except` - Space before colon in lambda Signed-off-by: Han Sol Jin --- gen_mirror_json.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/gen_mirror_json.py b/gen_mirror_json.py index 1e4621a..3d4f6bb 100644 --- a/gen_mirror_json.py +++ b/gen_mirror_json.py @@ -3,6 +3,7 @@ import hashlib import json import os +import re import sys import zipfile @@ -23,13 +24,13 @@ _, version, builddate, buildtype, device = os.path.splitext(filename)[0].split('-') print('hashing sha256 for {}'.format(filename), file=sys.stderr) sha256 = hashlib.sha256() - for buf in iter(lambda : data.read(128 * 1024), b''): + for buf in iter(lambda: data.read(128 * 1024), b''): sha256.update(buf) try: with zipfile.ZipFile('{}{}'.format(BASE_PATH, filepath), 'r') as update_zip: build_prop = update_zip.read('system/build.prop').decode('utf-8') timestamp = int(re.findall('ro.build.date.utc=([0-9]+)', build_prop)[0]) - except: + except Exception: timestamp = int(mktime(datetime.strptime(builddate, '%Y%m%d').timetuple())) builds.setdefault(device, []).append({ From 53f50e830df4a1a435ef05e49943d7db58158c02 Mon Sep 17 00:00:00 2001 From: lifehackerhansol Date: Mon, 27 May 2024 17:24:01 -0700 Subject: [PATCH 2/5] gen_mirror_json: switch to python3 Signed-off-by: Han Sol Jin --- gen_mirror_json.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gen_mirror_json.py b/gen_mirror_json.py index 3d4f6bb..a7104bc 100644 --- a/gen_mirror_json.py +++ b/gen_mirror_json.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 from __future__ import print_function import hashlib import json @@ -11,7 +11,7 @@ from time import mktime if len(sys.argv) < 2: - print("usage python {} /path/to/mirror/base/url".format(sys.argv[0])) + print("usage python3 {} /path/to/mirror/base/url".format(sys.argv[0])) sys.exit() FILE_BASE = sys.argv[1] From 49fb5892636bdc30d4dbb2d270f42603c1bb87ff Mon Sep 17 00:00:00 2001 From: Han Sol Jin Date: Mon, 27 May 2024 14:52:05 -0700 Subject: [PATCH 3/5] gen_mirror_json: fix ZipFile arguments - `filepath` and `filename` are the same thing, just use `filename` - `BASE_PATH` and `FILE_BASE` are the same thing, just use `BASE_PATH` - Account for possibility that `BASE_PATH` may not have a trailing slash Signed-off-by: Han Sol Jin --- gen_mirror_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gen_mirror_json.py b/gen_mirror_json.py index a7104bc..08261cb 100644 --- a/gen_mirror_json.py +++ b/gen_mirror_json.py @@ -27,7 +27,7 @@ for buf in iter(lambda: data.read(128 * 1024), b''): sha256.update(buf) try: - with zipfile.ZipFile('{}{}'.format(BASE_PATH, filepath), 'r') as update_zip: + with zipfile.ZipFile(f'{FILE_BASE}/{filename}', 'r') as update_zip: build_prop = update_zip.read('system/build.prop').decode('utf-8') timestamp = int(re.findall('ro.build.date.utc=([0-9]+)', build_prop)[0]) except Exception: From 023b3184159f0609c0f77df04566d85be1d2738f Mon Sep 17 00:00:00 2001 From: pourekrami Date: Wed, 19 Jan 2022 12:38:07 +0330 Subject: [PATCH 4/5] Add parsing metadata from OTA package Since Oreo, there is no `system/build.prop`, it is packaged into `system.new.dat`. So timestamp generation hasn't worked for years now. The OTA metadata on the other hand stores this timestamp (called `post-timestamp`), so let's parse that instead. --- gen_mirror_json.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/gen_mirror_json.py b/gen_mirror_json.py index 08261cb..d6d3042 100644 --- a/gen_mirror_json.py +++ b/gen_mirror_json.py @@ -26,11 +26,13 @@ sha256 = hashlib.sha256() for buf in iter(lambda: data.read(128 * 1024), b''): sha256.update(buf) + try: with zipfile.ZipFile(f'{FILE_BASE}/{filename}', 'r') as update_zip: - build_prop = update_zip.read('system/build.prop').decode('utf-8') - timestamp = int(re.findall('ro.build.date.utc=([0-9]+)', build_prop)[0]) - except Exception: + build_prop = update_zip.read('META-INF/com/android/metadata').decode('utf-8') + timestamp = (re.findall('post-timestamp=([0-9]+)', build_prop)[0]) + except Exception as e: + print(e) timestamp = int(mktime(datetime.strptime(builddate, '%Y%m%d').timetuple())) builds.setdefault(device, []).append({ From 924db5b133dfba336bfcac0d20850981e49f4618 Mon Sep 17 00:00:00 2001 From: Han Sol Jin Date: Mon, 27 May 2024 15:10:30 -0700 Subject: [PATCH 5/5] gen_mirror_json: explicitly specify we are reading in binary mode for builds Signed-off-by: Han Sol Jin --- gen_mirror_json.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gen_mirror_json.py b/gen_mirror_json.py index d6d3042..57d4f6f 100644 --- a/gen_mirror_json.py +++ b/gen_mirror_json.py @@ -18,7 +18,7 @@ builds = {} for f in [os.path.join(dp, f) for dp, dn, fn in os.walk(FILE_BASE) for f in fn]: - data = open(f) + data = open(f, 'rb') filename = f.split('/')[-1] # lineage-14.1-20171129-nightly-hiaeul-signed.zip _, version, builddate, buildtype, device = os.path.splitext(filename)[0].split('-')