Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Tries to get device-specific info from various Google sites |
| 4 | # Best effort |
| 5 | # We require manual input of build id here to keep things easier |
| 6 | |
| 7 | import argparse |
| 8 | import base64 |
| 9 | from bs4 import BeautifulSoup |
| 10 | from git import cmd |
| 11 | import os |
| 12 | import urllib.request |
| 13 | |
| 14 | SCRIPT_PATH = os.path.realpath(os.path.dirname(__file__)) |
| 15 | VARS_PATH = SCRIPT_PATH + os.path.sep + os.path.pardir + os.path.sep + "vars" |
| 16 | |
| 17 | IMAGE_URL = "https://developers.google.com/android/images" |
| 18 | OTA_URL = "https://developers.google.com/android/ota" |
| 19 | COOKIE = {'Cookie': 'devsite_wall_acks=nexus-image-tos,nexus-ota-tos'} |
| 20 | |
| 21 | PLATFORM_BUILD_URL = "https://android.googlesource.com/platform/build" |
Chirayu Desai | 80c537e | 2023-05-01 23:58:23 +0530 | [diff] [blame] | 22 | BUILD_ID_URL = "https://android.googlesource.com/platform/build/+/refs/{}/core/build_id.mk?format=TEXT" |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 23 | BUILD_ID_FILTER = "BUILD_ID=" |
Chirayu Desai | 80c537e | 2023-05-01 23:58:23 +0530 | [diff] [blame] | 24 | SECURITY_PATCH_URL = "https://android.googlesource.com/platform/build/+/refs/{}/core/version_defaults.mk?format=TEXT" |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 25 | SECURITY_PATCH_FILTER = "PLATFORM_SECURITY_PATCH :=" |
| 26 | |
| 27 | def handle_image(html_id): |
| 28 | image_html = urllib.request.urlopen(urllib.request.Request(IMAGE_URL, headers=COOKIE)).read() |
| 29 | soup = BeautifulSoup(image_html, 'html.parser') |
| 30 | td = soup.find(id=html_id).find_all('td') |
| 31 | flash_url = td[1].a['href'] |
| 32 | image_url = td[2].a['href'] |
| 33 | image_sha256 = td[3].contents[0] |
Chirayu Desai | 695c3af | 2022-01-18 03:07:03 +0530 | [diff] [blame] | 34 | build_number = flash_url.split("/")[4].split("?")[0] |
| 35 | print('new_build_number="{0}"\nnew_flash_url="{1}"\nnew_image_url="{2}"\nnew_image_sha256="{3}"'.format(build_number, flash_url, image_url, image_sha256)) |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 36 | |
| 37 | def handle_ota(html_id): |
| 38 | ota_html = urllib.request.urlopen(urllib.request.Request(OTA_URL, headers=COOKIE)).read() |
| 39 | soup = BeautifulSoup(ota_html, 'html.parser') |
| 40 | td = soup.find(id=html_id).find_all('td') |
| 41 | ota_url = td[1].a['href'] |
| 42 | ota_sha256 = td[2].contents[0] |
| 43 | print('new_ota_url="{0}"\nnew_ota_sha256="{1}"'.format(ota_url, ota_sha256)) |
| 44 | |
| 45 | def get_all_aosp_tags(tag_filter): |
| 46 | all_tags = [] |
Chirayu Desai | ab60c5c | 2023-07-06 02:14:56 +0530 | [diff] [blame] | 47 | try: |
| 48 | for line in cmd.Git().ls_remote("--sort=v:refname", PLATFORM_BUILD_URL, tag_filter, tags=True, refs=True).split('\n'): |
| 49 | try: |
| 50 | (ref, tag) = line.split('\t') |
| 51 | except ValueError: |
| 52 | pass |
| 53 | all_tags.append(tag.replace("refs/tags/", "")) |
| 54 | return all_tags |
| 55 | except Exception as e: |
| 56 | return all_tags |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 57 | |
| 58 | def get_aosp_tag_for_build_id(aosp_tags, wanted_build_id): |
Chirayu Desai | ab60c5c | 2023-07-06 02:14:56 +0530 | [diff] [blame] | 59 | try: |
| 60 | for aosp_tag in aosp_tags: |
| 61 | output = base64.decodebytes(urllib.request.urlopen(BUILD_ID_URL.format("tags/" + aosp_tag)).read()).decode() |
| 62 | for line in output.split('\n'): |
| 63 | if BUILD_ID_FILTER in line: |
| 64 | found_build_id = line.split("=")[1] |
| 65 | if found_build_id == wanted_build_id: |
| 66 | print('new_aosp_tag="{0}"'.format(aosp_tag)) |
| 67 | return aosp_tag |
| 68 | print('new_aosp_tag="unknown"') |
| 69 | return 'unknown' |
| 70 | except Exception as e: |
| 71 | print('new_aosp_tag="unknown"') |
| 72 | return 'unknown' |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 73 | |
| 74 | def get_security_patch_for_aosp_tag(aosp_tag): |
Chirayu Desai | 3b06a54 | 2022-03-08 00:34:30 +0530 | [diff] [blame] | 75 | try: |
Chirayu Desai | 80c537e | 2023-05-01 23:58:23 +0530 | [diff] [blame] | 76 | output = base64.decodebytes(urllib.request.urlopen(SECURITY_PATCH_URL.format("tags/" + aosp_tag)).read()).decode() |
Chirayu Desai | 3b06a54 | 2022-03-08 00:34:30 +0530 | [diff] [blame] | 77 | except: |
| 78 | print('new_security_patch=unknown') |
| 79 | return |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 80 | for line in output.split('\n'): |
| 81 | if SECURITY_PATCH_FILTER in line: |
| 82 | security_patch = line.split(":=")[1].strip() |
| 83 | print('new_security_patch="{0}"'.format(security_patch)) |
Chirayu Desai | 3b06a54 | 2022-03-08 00:34:30 +0530 | [diff] [blame] | 84 | return |
| 85 | print('new_security_patch="unknown"') |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 86 | |
| 87 | def main(): |
| 88 | parser = argparse.ArgumentParser() |
| 89 | parser.add_argument('-b', '--build_id', help="Build ID", type=str, required=True) |
| 90 | parser.add_argument('-d', '--device', help="Device codename", type=str, required=True) |
Chirayu Desai | 80c537e | 2023-05-01 23:58:23 +0530 | [diff] [blame] | 91 | parser.add_argument('-t', '--tags_match', default="android-13.0", help='Android version tag to match', type=str) |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 92 | args = parser.parse_args() |
| 93 | html_id = "{0}{1}".format(args.device, args.build_id.lower()) |
| 94 | handle_image(html_id) |
| 95 | handle_ota(html_id) |
Chirayu Desai | 80c537e | 2023-05-01 23:58:23 +0530 | [diff] [blame] | 96 | aosp_tag = get_aosp_tag_for_build_id(get_all_aosp_tags("{0}*".format(args.tags_match)), args.build_id.upper()) |
Chirayu Desai | 6a08bc1 | 2021-11-25 23:35:09 +0530 | [diff] [blame] | 97 | get_security_patch_for_aosp_tag(aosp_tag) |
| 98 | |
| 99 | if __name__ == "__main__": |
| 100 | main() |