blob: 13c969b2769f8fc694d623f1a53ba4f55c9925e9 [file] [edit]
#!/usr/bin/env python3
# Copyright (C) 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Updates the python scripts in python/perfetto/prebuilts/manifests
This script does the following, for each entry in MANIFESTS_TO_UPDATE:
- Downloads the artifact by the LUCI infrastructure, one for each arch.
- Computes the SHA-256 of each artifact.
- Generates a manifest with URL, SHA-256 and other details.
- Merges get_perfetto_prebuilt.py with the manifest and writes tools/xxx.
This script is supposed to be run by Perfetto OWNERS after every monthly release
after the LUCI jobs have completed.
"""
import argparse
import hashlib
import json
import logging
import os
import re
import subprocess
import sys
from concurrent.futures import ThreadPoolExecutor
GCS_URL = 'https://commondatastorage.googleapis.com/perfetto-luci-artifacts'
ROOT_DIR = os.path.dirname(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
MANIFESTS_DIR = os.path.join(ROOT_DIR, 'python/perfetto/prebuilts/manifests')
# The agent-skills bundle (RFC-0026) ships the rolled trace_processor wrapper,
# so its version is stamped here, in lockstep with the prebuilts. These files
# are the source the ai-agents branch is assembled from.
AI_EXTENSIONS_DIR = os.path.join(ROOT_DIR, 'ai', 'extensions')
AGENTS_INSTALL = os.path.join(ROOT_DIR, 'tools', 'agents-install')
AI_MANIFESTS = [
'claude-code/plugin.json',
'claude-code/marketplace.json',
'codex/plugin.json',
'codex/marketplace.json',
]
UNIX_ARCHS = [
'mac-amd64',
'mac-arm64',
'linux-amd64',
'linux-arm',
'linux-arm64',
'android-arm',
'android-arm64',
'android-x86',
'android-x64',
]
ALL_ARCHS = UNIX_ARCHS + ['windows-amd64']
LINUX_ARCHS = ['linux-amd64', 'linux-arm', 'linux-arm64']
MANIFESTS_TO_UPDATE = [
{
'tool': 'trace_processor_shell',
'archs': ALL_ARCHS
},
{
'tool': 'traceconv',
'archs': ALL_ARCHS
},
{
'tool': 'tracebox',
'archs': UNIX_ARCHS
},
{
'tool': 'perfetto-cpp-sdk-src',
'archs': ['src']
},
{
'tool': 'perfetto-c-sdk-src',
'archs': ['src']
},
{
# The GN target is heapprofd_glibc_preload but the output file is
# libheapprofd_glibc_preload.so (GN adds lib prefix + .so suffix).
'tool': 'heapprofd_glibc_preload',
'file_name': 'libheapprofd_glibc_preload.so',
'archs': LINUX_ARCHS,
},
]
# Maps a 'os-arch' string (were arch follows LUCI conventions) into
# corresponding tuples that match against python's platform / machine API
# (see get_perfetto_prebuilt.py for usage).
ARCH_TO_PYTHON = {
'mac-amd64': {
'platform': 'darwin',
'machine': ['x86_64'],
},
'mac-arm64': {
'platform': 'darwin',
'machine': ['arm64'],
},
'windows-amd64': {
'platform': 'win32',
'machine': ['amd64'],
},
'linux-amd64': {
'platform': 'linux',
'machine': ['x86_64'],
},
'linux-arm': {
'platform': 'linux',
'machine': ['armv6l', 'armv7l', 'armv8l'],
},
'linux-arm64': {
'platform': 'linux',
'machine': ['aarch64'],
},
}
def make_manifest(git_revision, tool, arch, file_name=None):
if arch == 'src':
file_name = file_name or (tool + '.zip')
url = '%s/%s/sdk/%s' % (GCS_URL, git_revision, file_name)
else:
ext = '.exe' if arch.startswith('windows') else ''
file_name = file_name or (tool + ext)
url = '%s/%s/%s/%s' % (GCS_URL, git_revision, arch, file_name)
logging.info('Downloading %s', url)
data = subprocess.check_output(['curl', '-fsL', '-o', '-', url])
manifest = {
'arch': arch,
'file_name': file_name,
'file_size': len(data),
'url': url,
'sha256': hashlib.sha256(data).hexdigest()
}
manifest.update(ARCH_TO_PYTHON.get(arch, {}))
return manifest
def update_manifest(git_revision, spec):
tool_name = spec['tool']
archs = spec['archs']
file_name = spec.get('file_name', None)
with ThreadPoolExecutor(max_workers=8) as executor:
manifests = list(
executor.map(
lambda arch: make_manifest(git_revision, tool_name, arch, file_name
), archs))
out_file = os.path.join(MANIFESTS_DIR, tool_name + '.py')
content = '# This file has been generated by: {script} {git_revision}\n'
content += '{tool_uppercase}_MANIFEST = {manifests}\n'
content = content.format(
script=os.path.relpath(__file__),
tool_uppercase=tool_name.upper().replace('-', '_'),
git_revision=git_revision,
manifests=str(manifests))
with open(out_file + '.tmp', 'w') as f:
f.write(content)
os.replace(out_file + '.tmp', out_file)
os.chmod(out_file, 0o755)
def update_ai_agents(version):
"""Stamps the rolled release version into the agent-skills source.
Writes `version` into the extension manifests and the installer's
DEFAULT_VERSION so the ai-agents bundle versions in lockstep with the
prebuilt roll. Only runs for release revisions (vX.Y); ad-hoc SHA rolls
leave the agent files untouched.
"""
if not re.match(r'^v[0-9]+\.[0-9]+$', version):
logging.info('Skipping ai-agents stamp for non-release revision %s',
version)
return
for rel in AI_MANIFESTS:
path = os.path.join(AI_EXTENSIONS_DIR, rel)
with open(path) as f:
data = json.load(f)
if 'version' not in data:
continue
data['version'] = version
with open(path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
with open(AGENTS_INSTALL) as f:
src = f.read()
src = re.sub(
r"^DEFAULT_VERSION = '.*'$",
f"DEFAULT_VERSION = '{version}'",
src,
flags=re.M)
with open(AGENTS_INSTALL, 'w') as f:
f.write(src)
def run_cmd(cmd):
print('Running ', cmd)
cmd_path = os.path.join(ROOT_DIR, cmd)
subprocess.check_call(cmd_path)
def main():
usage = '%s v20.0 | 0a1b2c3d\n\n' % __file__
usage += 'To list available revisions run\n'
usage += 'gsutil ls gs://perfetto-luci-artifacts/\n'
usage += 'or visit https://chrome-infra-packages.appspot.com/p/perfetto\n'
parser = argparse.ArgumentParser(usage=usage)
parser.add_argument('version')
args = parser.parse_args()
git_revision = args.version
for spec in MANIFESTS_TO_UPDATE:
logging.info('Updating %s', spec['tool'])
update_manifest(git_revision, spec)
logging.info('Updating ai-agents bundle source')
update_ai_agents(git_revision)
run_cmd('tools/gen_amalgamated_python_tools')
run_cmd('tools/format-sources')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
sys.exit(main())