1#!/usr/bin/env python3 2# 3# Copyright 2024 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17"""Update Utils.""" 18 19 20def combine_build_commands(commands): 21 """Combines build commands so that m is called once.""" 22 m_command = '' 23 result = [] 24 for cmd in commands: 25 if cmd.startswith('m '): 26 m_command += cmd[2:] + ' ' 27 else: 28 result.append(cmd) 29 if m_command: 30 result.insert(0, 'm ' + m_command.strip()) 31 return result 32 33 34def combine_update_commands(commands): 35 """Combines update tasks so that a reboot/adevice update is called only once.""" 36 commands = remove_duplicates_maintain_order(commands) 37 38 # if any command calls for a restart; just do that 39 # deduplicate and remove 40 if 'adevice update' in commands: 41 commands = remove_commands_that_starts_with(commands, 'adevice update') 42 commands = remove_commands_that_starts_with( 43 commands, 'adb shell "am force-stop' 44 ) 45 commands = ['adevice update'] + commands 46 return commands 47 48 49def remove_duplicates_maintain_order(commands): 50 """Removes duplicates while maintaining order.""" 51 seen = set() 52 result = [] 53 for item in commands: 54 if item not in seen: 55 seen.add(item) 56 result.append(item) 57 return result 58 59 60def remove_commands_that_starts_with(commands, cmd_to_remove): 61 """Removes commands that start with a command.""" 62 result = [] 63 for cmd in commands: 64 if not cmd.startswith(cmd_to_remove): 65 result.append(cmd) 66 return result 67