blob: 05f9f720d3b88eccba41dcdad20433ebadb92f28 [file] [log] [blame]
Chirayu Desai6a08bc12021-11-25 23:35:09 +05301#!/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
7import argparse
8import base64
9from bs4 import BeautifulSoup
10from git import cmd
11import os
12import urllib.request
13
14SCRIPT_PATH = os.path.realpath(os.path.dirname(__file__))
15VARS_PATH = SCRIPT_PATH + os.path.sep + os.path.pardir + os.path.sep + "vars"
16
17IMAGE_URL = "https://developers.google.com/android/images"
18OTA_URL = "https://developers.google.com/android/ota"
19COOKIE = {'Cookie': 'devsite_wall_acks=nexus-image-tos,nexus-ota-tos'}
20
21PLATFORM_BUILD_URL = "https://android.googlesource.com/platform/build"
Chirayu Desai80c537e2023-05-01 23:58:23 +053022BUILD_ID_URL = "https://android.googlesource.com/platform/build/+/refs/{}/core/build_id.mk?format=TEXT"
Chirayu Desai6a08bc12021-11-25 23:35:09 +053023BUILD_ID_FILTER = "BUILD_ID="
Chirayu Desai80c537e2023-05-01 23:58:23 +053024SECURITY_PATCH_URL = "https://android.googlesource.com/platform/build/+/refs/{}/core/version_defaults.mk?format=TEXT"
Chirayu Desai6a08bc12021-11-25 23:35:09 +053025SECURITY_PATCH_FILTER = "PLATFORM_SECURITY_PATCH :="
26
27def 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 Desai695c3af2022-01-18 03:07:03 +053034 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 Desai6a08bc12021-11-25 23:35:09 +053036
37def 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
45def get_all_aosp_tags(tag_filter):
46 all_tags = []
Chirayu Desaiab60c5c2023-07-06 02:14:56 +053047 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 Desai6a08bc12021-11-25 23:35:09 +053057
58def get_aosp_tag_for_build_id(aosp_tags, wanted_build_id):
Chirayu Desaiab60c5c2023-07-06 02:14:56 +053059 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 Desai6a08bc12021-11-25 23:35:09 +053073
74def get_security_patch_for_aosp_tag(aosp_tag):
Chirayu Desai3b06a542022-03-08 00:34:30 +053075 try:
Chirayu Desai80c537e2023-05-01 23:58:23 +053076 output = base64.decodebytes(urllib.request.urlopen(SECURITY_PATCH_URL.format("tags/" + aosp_tag)).read()).decode()
Chirayu Desai3b06a542022-03-08 00:34:30 +053077 except:
78 print('new_security_patch=unknown')
79 return
Chirayu Desai6a08bc12021-11-25 23:35:09 +053080 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 Desai3b06a542022-03-08 00:34:30 +053084 return
85 print('new_security_patch="unknown"')
Chirayu Desai6a08bc12021-11-25 23:35:09 +053086
87def 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 Desai80c537e2023-05-01 23:58:23 +053091 parser.add_argument('-t', '--tags_match', default="android-13.0", help='Android version tag to match', type=str)
Chirayu Desai6a08bc12021-11-25 23:35:09 +053092 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 Desai80c537e2023-05-01 23:58:23 +053096 aosp_tag = get_aosp_tag_for_build_id(get_all_aosp_tags("{0}*".format(args.tags_match)), args.build_id.upper())
Chirayu Desai6a08bc12021-11-25 23:35:09 +053097 get_security_patch_for_aosp_tag(aosp_tag)
98
99if __name__ == "__main__":
100 main()